2
#error getaddrinfo.c included by mistake! no socket support!
5
#include <sys/socket.h>
9
#ifdef HAVE_NETINET_IN_H
10
#include <netinet/in.h>
12
#ifdef HAVE_ARPA_INET_H
13
#include <arpa/inet.h>
16
#include "getaddrinfo.h"
19
freeaddrinfo(struct addrinfo *res)
21
if (res->ai_addr != NULL)
27
getaddrinfo(const char *hostname, const char *portname,
28
struct addrinfo *hints, struct addrinfo **res)
34
out = (struct addrinfo *) malloc(sizeof(*out));
39
memset(out, '\0', sizeof(*out));
41
out->ai_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr_in));
42
if (out->ai_addr == NULL) {
48
out->ai_socktype = SOCK_STREAM;
50
if (hints->ai_socktype)
51
out->ai_socktype = hints->ai_socktype;
52
if (hints->ai_protocol)
53
out->ai_protocol = hints->ai_protocol;
56
if (out->ai_protocol == 0) {
57
switch (out->ai_socktype) {
59
out->ai_protocol = IPPROTO_TCP;
62
out->ai_protocol = IPPROTO_UDP;
65
out->ai_protocol = IPPROTO_RAW;
70
out->ai_addrlen = sizeof(struct sockaddr_in);
71
memset(out->ai_addr, '\0', sizeof(struct sockaddr_in));
73
if (hostname != NULL) {
75
he = gethostbyname(hostname);
76
if (he != NULL && he->h_addr_list != NULL) {
77
((struct sockaddr_in *)out->ai_addr)->sin_addr.s_addr
78
= ((struct in_addr *)he->h_addr_list[0])->s_addr;
84
if (!(out->ai_flags & AI_PASSIVE))
85
((struct sockaddr_in *)out->ai_addr)->sin_addr.s_addr
88
((struct sockaddr_in *)out->ai_addr)->sin_family = AF_INET;
89
out->ai_family = AF_INET;
91
if (portname != NULL && *portname) {
94
portnum = strtol(portname, &end, 10);
95
if (*end == '\0' && portnum > 0 && portnum < 65536) {
96
((struct sockaddr_in *)out->ai_addr)->sin_port
100
se = getservbyname(portname, NULL);
102
((struct sockaddr_in *)out->ai_addr)->sin_port