aboutsummaryrefslogtreecommitdiffstats
path: root/missing_d/getaddrinfo.c
blob: 5233cf5632f274c517896bffe7bb4291bd6b242f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef HAVE_SOCKETS
#error getaddrinfo.c included by mistake! no socket support!
#else
#include <sys/types.h>
#include <sys/socket.h>
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include <errno.h>
#include <string.h>	/* strerror */

#include "getaddrinfo.h"

void
freeaddrinfo(struct addrinfo *res)
{
	if (res->ai_addr != NULL)
		free(res->ai_addr);
	free(res);
}

int
getaddrinfo(const char *hostname, const char *portname,
	struct addrinfo *hints, struct addrinfo **res)
{
	struct addrinfo *out;
	if (res == NULL)
		return EINVAL;

	out = (struct addrinfo *) calloc(1, sizeof(*out));
	if (out == NULL) {
		*res = NULL;
		return ENOMEM;
	}

	out->ai_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr_in));
	if (out->ai_addr == NULL) {
		free(out);
		*res = NULL;
		return ENOMEM;
	}

	out->ai_socktype = SOCK_STREAM;
	if (hints != NULL) {
		if (hints->ai_socktype)
			out->ai_socktype = hints->ai_socktype;
		if (hints->ai_protocol)
			out->ai_protocol = hints->ai_protocol;
	}

	if (out->ai_protocol == 0) {
		switch (out->ai_socktype) {
		case SOCK_STREAM:
			out->ai_protocol = IPPROTO_TCP;
			break;
		case SOCK_DGRAM:
			out->ai_protocol = IPPROTO_UDP;
			break;
		case SOCK_RAW:
			out->ai_protocol = IPPROTO_RAW;
			break;
		}
	}

	out->ai_addrlen = sizeof(struct sockaddr_in);
	memset(out->ai_addr, '\0', sizeof(struct sockaddr_in));

	if (hostname != NULL) {
		struct hostent *he;
		he = gethostbyname(hostname);
		if (he != NULL && he->h_addr_list != NULL) {
			((struct sockaddr_in *)out->ai_addr)->sin_addr.s_addr
				= ((struct in_addr *)he->h_addr_list[0])->s_addr;
		} else {
			freeaddrinfo(out);
			return EADDRNOTAVAIL;
		}
	} else {
		if (!(out->ai_flags & AI_PASSIVE))
			((struct sockaddr_in *)out->ai_addr)->sin_addr.s_addr
							= htonl(INADDR_ANY);
	}
	((struct sockaddr_in *)out->ai_addr)->sin_family = AF_INET;
	out->ai_family = AF_INET;

	if (portname != NULL && *portname) {
		long portnum;
		char *end;
		portnum = strtol(portname, &end, 10);
		if (*end == '\0' && portnum > 0 && portnum < 65536) {
			((struct sockaddr_in *)out->ai_addr)->sin_port
							= htons(portnum);
		} else {
			struct servent *se;
			se = getservbyname(portname, NULL);
			if (se != NULL) {
				((struct sockaddr_in *)out->ai_addr)->sin_port
							= se->s_port;
			}
		}
	}

	*res = out;

	return 0;
}

const char *
gai_strerror(int errcode)
{
	return strerror(errcode);
}
#endif