summaryrefslogtreecommitdiff
path: root/sys/src/ape/lib/bsd/getaddrinfo.c
blob: 158f22f6215e7f9bd255323274e8216675f16c68 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* posix */
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

/* bsd extensions */
#include <sys/uio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include "priv.h"

/* for malloc/free */
#include <stdlib.h>

void
freeaddrinfo(struct addrinfo *res)
{
	struct addrinfo *info;

	while((info = res) != 0){
		res = res->ai_next;
		free(info->ai_canonname);
		free(info->ai_addr);
		free(info);
	}
}

static int
sockfamily(char *addr)
{
	if(strchr(addr, ':') != 0)
		return AF_INET6;
	else
		return AF_INET;
}

static int
sockproto(char *proto)
{
	if(strcmp(proto, "tcp") == 0)
		return SOCK_STREAM;
	if(strcmp(proto, "udp") == 0)
		return SOCK_DGRAM;
	if(strcmp(proto, "il") == 0)
		return SOCK_RDM;
	return 0;
}

static int
filladdrinfo(char *s, struct addrinfo *a, struct addrinfo *h)
{
	struct sockaddr sa;
	char *p, *q;

	if(*s != '/')
		return 1;
	if((q = strchr(s, ' ')) == 0)
		return 1;
	while(*q == ' ')
		*q++ = 0;
	if((p = strrchr(s+1, '/')) == 0)
		return 1;
	*p = 0;
	if((p = strrchr(s+1, '/')) == 0)
		return 1;
	*p++ = 0;
	if(*p == 0)
		return 1;

	if((a->ai_socktype = sockproto(p)) == 0)
		return 1;
	if((p = strchr(q, '!')) != 0){
		*p++ = 0;
		a->ai_family = sockfamily(q);
	} else{
		p = q;
		q = 0;
		if((a->ai_family = h->ai_family) == 0)
			a->ai_family = AF_INET;
	}
	if((a->ai_socktype == SOCK_RDM || h->ai_socktype != 0)
	&& (a->ai_socktype != h->ai_socktype))
		return 1;
	if(h->ai_family != 0 && a->ai_family != h->ai_family)
		return 1;
	if(_sock_inaddr(a->ai_family, q, p, &sa, &a->ai_addrlen) <= 0)
		return 1;
	if((a->ai_addr = malloc(a->ai_addrlen)) == 0)
		return EAI_MEMORY;
	memmove(a->ai_addr, &sa, a->ai_addrlen);
	return 0;
}

int
getaddrinfo(char *node, char *serv, struct addrinfo *hints, struct addrinfo **res)
{
	static struct addrinfo nohints;
	struct addrinfo *a, *head, **tail;
	char buf[1024], *proto;
	int n, fd, err;

	if(res != 0)
		*res = 0;

	if(hints == 0)
		hints = &nohints;

	proto = "net";
	switch(hints->ai_family){
	default:
		return EAI_FAMILY;
	case AF_INET:
	case AF_INET6:
		switch(hints->ai_socktype){
		default:
			return EAI_SOCKTYPE;
		case SOCK_STREAM:
			proto = "tcp";
			break;
		case SOCK_DGRAM:
			proto = "udp";
			break;
		case SOCK_RDM:
			proto = "il";
			break;
		case 0:
			break;
		}
		break;
	case AF_UNSPEC:
		break;
	}
	if(serv == 0){
		if(node == 0)
			return EAI_NONAME;
		serv = "0";
	}
	if(node == 0){
		if(hints->ai_flags & AI_PASSIVE)
			node = "*";
		else if(hints->ai_family == AF_INET6)
			node = "::1";
		else
			node = "127.0.0.1";
	}

	if((fd = open("/net/cs", O_RDWR)) < 0)
		return EAI_SYSTEM;

	snprintf(buf, sizeof(buf), "%s!%s!%s", proto, node, serv);
	n = strlen(buf);
	if(write(fd, buf, n) != n){
		close(fd);
		return EAI_AGAIN;
	}
	lseek(fd, 0, 0);

	head = 0;
	tail = &head;
	for(;;){
		if((n = read(fd, buf, sizeof(buf)-1)) <= 0)
			break;
		buf[n] = '\0';
		if((a = malloc(sizeof(*a))) == 0){
			freeaddrinfo(head);
			close(fd);
			return EAI_MEMORY;
		}
		memset(a, 0, sizeof(*a));
		if((err = filladdrinfo(buf, a, hints)) != 0){
			freeaddrinfo(a);
			if(err < 0){
				freeaddrinfo(head);
				close(fd);
				return err;
			}
		} else {
			*tail = a;
			tail = &a->ai_next;
		}
	}
	close(fd);

	if(head == 0)
		return EAI_NODATA;

	if((hints->ai_flags & AI_CANONNAME) != 0 && (hints->ai_flags & AI_NUMERICHOST) == 0){
		n = _sock_ipattr(node);
		if(n != Tsys && n != Tdom){
			if(getnameinfo(head->ai_addr, head->ai_addrlen, buf, sizeof(buf), 0, 0, NI_NAMEREQD) == 0)
				node = buf;
			else
				node = 0;
		}
		if(node != 0){
			n = strlen(node)+1;
			if((head->ai_canonname = malloc(n)) == 0){
				freeaddrinfo(head);
				return EAI_MEMORY;
			}
			memmove(head->ai_canonname, node, n);
		}
	}

	if(res != 0)
		*res = head;
	else
		freeaddrinfo(head);

	return 0;
}