c5e438ec |
1 | /* |
2 | * Unix networking abstraction. |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <assert.h> |
8 | #include <errno.h> |
9 | #include <fcntl.h> |
10 | #include <unistd.h> |
11 | #include <sys/types.h> |
12 | #include <sys/socket.h> |
13 | #include <sys/ioctl.h> |
14 | #include <arpa/inet.h> |
15 | #include <netinet/in.h> |
16 | #include <netinet/tcp.h> |
17 | #include <netdb.h> |
fc0f17db |
18 | #include <sys/un.h> |
c5e438ec |
19 | |
20 | #define DEFINE_PLUG_METHOD_MACROS |
21 | #include "putty.h" |
22 | #include "network.h" |
23 | #include "tree234.h" |
24 | |
dbf58cfa |
25 | /* Solaris needs <sys/sockio.h> for SIOCATMARK. */ |
5f86b177 |
26 | #ifndef SIOCATMARK |
dbf58cfa |
27 | #include <sys/sockio.h> |
28 | #endif |
29 | |
fc0f17db |
30 | #ifndef X11_UNIX_PATH |
31 | # define X11_UNIX_PATH "/tmp/.X11-unix/X" |
32 | #endif |
33 | |
c5e438ec |
34 | struct Socket_tag { |
35 | struct socket_function_table *fn; |
36 | /* the above variable absolutely *must* be the first in this structure */ |
cbe2d68f |
37 | const char *error; |
c5e438ec |
38 | int s; |
39 | Plug plug; |
40 | void *private_ptr; |
41 | bufchain output_data; |
1cfc764f |
42 | int connected; /* irrelevant for listening sockets */ |
c5e438ec |
43 | int writable; |
44 | int frozen; /* this causes readability notifications to be ignored */ |
45 | int frozen_readable; /* this means we missed at least one readability |
46 | * notification while we were frozen */ |
47 | int localhost_only; /* for listening sockets */ |
48 | char oobdata[1]; |
49 | int sending_oob; |
50 | int oobpending; /* is there OOB data available to read? */ |
51 | int oobinline; |
52 | int pending_error; /* in case send() returns error */ |
53 | int listener; |
7555d6a5 |
54 | int nodelay, keepalive; /* for connect()-type sockets */ |
55 | int privport, port; /* and again */ |
56 | SockAddr addr; |
c5e438ec |
57 | }; |
58 | |
59 | /* |
60 | * We used to typedef struct Socket_tag *Socket. |
61 | * |
62 | * Since we have made the networking abstraction slightly more |
63 | * abstract, Socket no longer means a tcp socket (it could mean |
64 | * an ssl socket). So now we must use Actual_Socket when we know |
65 | * we are talking about a tcp socket. |
66 | */ |
67 | typedef struct Socket_tag *Actual_Socket; |
68 | |
69 | struct SockAddr_tag { |
cbe2d68f |
70 | const char *error; |
b7a189f3 |
71 | /* |
72 | * Which address family this address belongs to. AF_INET for |
73 | * IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name |
74 | * resolution has not been done and a simple host name is held |
75 | * in this SockAddr structure. |
76 | */ |
c5e438ec |
77 | int family; |
05581745 |
78 | #ifndef NO_IPV6 |
7555d6a5 |
79 | struct addrinfo *ais; /* Addresses IPv6 style. */ |
80 | struct addrinfo *ai; /* steps along the linked list */ |
792c5eb5 |
81 | #else |
7555d6a5 |
82 | unsigned long *addresses; /* Addresses IPv4 style. */ |
83 | int naddresses, curraddr; |
c5e438ec |
84 | #endif |
b7a189f3 |
85 | char hostname[512]; /* Store an unresolved host name. */ |
c5e438ec |
86 | }; |
87 | |
88 | static tree234 *sktree; |
89 | |
0ff9ea38 |
90 | static void uxsel_tell(Actual_Socket s); |
91 | |
c5e438ec |
92 | static int cmpfortree(void *av, void *bv) |
93 | { |
94 | Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv; |
95 | int as = a->s, bs = b->s; |
96 | if (as < bs) |
97 | return -1; |
98 | if (as > bs) |
99 | return +1; |
100 | return 0; |
101 | } |
102 | |
103 | static int cmpforsearch(void *av, void *bv) |
104 | { |
105 | Actual_Socket b = (Actual_Socket) bv; |
f7f9fb5c |
106 | int as = *(int *)av, bs = b->s; |
c5e438ec |
107 | if (as < bs) |
108 | return -1; |
109 | if (as > bs) |
110 | return +1; |
111 | return 0; |
112 | } |
113 | |
114 | void sk_init(void) |
115 | { |
116 | sktree = newtree234(cmpfortree); |
117 | } |
118 | |
119 | void sk_cleanup(void) |
120 | { |
121 | Actual_Socket s; |
122 | int i; |
123 | |
124 | if (sktree) { |
125 | for (i = 0; (s = index234(sktree, i)) != NULL; i++) { |
126 | close(s->s); |
127 | } |
128 | } |
129 | } |
130 | |
05581745 |
131 | SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family) |
c5e438ec |
132 | { |
3d88e64d |
133 | SockAddr ret = snew(struct SockAddr_tag); |
05581745 |
134 | #ifndef NO_IPV6 |
792c5eb5 |
135 | struct addrinfo hints; |
136 | int err; |
137 | #else |
c5e438ec |
138 | unsigned long a; |
139 | struct hostent *h = NULL; |
7555d6a5 |
140 | int n; |
792c5eb5 |
141 | #endif |
c5e438ec |
142 | char realhost[8192]; |
143 | |
144 | /* Clear the structure and default to IPv4. */ |
145 | memset(ret, 0, sizeof(struct SockAddr_tag)); |
146 | ret->family = 0; /* We set this one when we have resolved the host. */ |
147 | *realhost = '\0'; |
148 | ret->error = NULL; |
149 | |
05581745 |
150 | #ifndef NO_IPV6 |
792c5eb5 |
151 | hints.ai_flags = AI_CANONNAME; |
81b34354 |
152 | hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET : |
153 | address_family == ADDRTYPE_IPV6 ? AF_INET6 : |
154 | AF_UNSPEC); |
155 | hints.ai_socktype = SOCK_STREAM; |
792c5eb5 |
156 | hints.ai_protocol = 0; |
157 | hints.ai_addrlen = 0; |
158 | hints.ai_addr = NULL; |
159 | hints.ai_canonname = NULL; |
160 | hints.ai_next = NULL; |
7555d6a5 |
161 | err = getaddrinfo(host, NULL, &hints, &ret->ais); |
162 | ret->ai = ret->ais; |
792c5eb5 |
163 | if (err != 0) { |
164 | ret->error = gai_strerror(err); |
165 | return ret; |
166 | } |
167 | ret->family = ret->ai->ai_family; |
168 | *realhost = '\0'; |
169 | if (ret->ai->ai_canonname != NULL) |
170 | strncat(realhost, ret->ai->ai_canonname, sizeof(realhost) - 1); |
171 | else |
172 | strncat(realhost, host, sizeof(realhost) - 1); |
173 | #else |
72e22079 |
174 | if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) { |
792c5eb5 |
175 | /* |
176 | * Otherwise use the IPv4-only gethostbyname... (NOTE: |
177 | * we don't use gethostbyname as a fallback!) |
178 | */ |
179 | if (ret->family == 0) { |
7555d6a5 |
180 | /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */ |
181 | if ( (h = gethostbyname(host)) ) |
182 | ret->family = AF_INET; |
792c5eb5 |
183 | } |
184 | if (ret->family == 0) { |
7555d6a5 |
185 | ret->error = (h_errno == HOST_NOT_FOUND || |
186 | h_errno == NO_DATA || |
187 | h_errno == NO_ADDRESS ? "Host does not exist" : |
188 | h_errno == TRY_AGAIN ? |
189 | "Temporary name service failure" : |
190 | "gethostbyname: unknown error"); |
191 | return ret; |
c5e438ec |
192 | } |
792c5eb5 |
193 | /* This way we are always sure the h->h_name is valid :) */ |
194 | strncpy(realhost, h->h_name, sizeof(realhost)); |
7555d6a5 |
195 | for (n = 0; h->h_addr_list[n]; n++); |
196 | ret->addresses = snewn(n, unsigned long); |
197 | ret->naddresses = n; |
198 | for (n = 0; n < ret->naddresses; n++) { |
199 | memcpy(&a, h->h_addr_list[n], sizeof(a)); |
200 | ret->addresses[n] = ntohl(a); |
201 | } |
c5e438ec |
202 | } else { |
203 | /* |
204 | * This must be a numeric IPv4 address because it caused a |
205 | * success return from inet_addr. |
206 | */ |
207 | ret->family = AF_INET; |
208 | strncpy(realhost, host, sizeof(realhost)); |
7555d6a5 |
209 | ret->addresses = snew(unsigned long); |
210 | ret->naddresses = 1; |
211 | ret->addresses[0] = ntohl(a); |
212 | ret->curraddr = 0; |
c5e438ec |
213 | } |
792c5eb5 |
214 | #endif |
c5e438ec |
215 | realhost[lenof(realhost)-1] = '\0'; |
3d88e64d |
216 | *canonicalname = snewn(1+strlen(realhost), char); |
c5e438ec |
217 | strcpy(*canonicalname, realhost); |
218 | return ret; |
219 | } |
220 | |
e8fa8f62 |
221 | SockAddr sk_nonamelookup(const char *host) |
b7a189f3 |
222 | { |
3d88e64d |
223 | SockAddr ret = snew(struct SockAddr_tag); |
ab0873ab |
224 | ret->error = NULL; |
b7a189f3 |
225 | ret->family = AF_UNSPEC; |
226 | strncpy(ret->hostname, host, lenof(ret->hostname)); |
227 | ret->hostname[lenof(ret->hostname)-1] = '\0'; |
e14d27a1 |
228 | #ifndef NO_IPV6 |
7555d6a5 |
229 | ret->ais = NULL; |
230 | #else |
231 | ret->addresses = NULL; |
e14d27a1 |
232 | #endif |
b7a189f3 |
233 | return ret; |
234 | } |
235 | |
7555d6a5 |
236 | static int sk_nextaddr(SockAddr addr) |
237 | { |
238 | #ifndef NO_IPV6 |
239 | if (addr->ai->ai_next) { |
240 | addr->ai = addr->ai->ai_next; |
241 | addr->family = addr->ai->ai_family; |
242 | return TRUE; |
243 | } else |
244 | return FALSE; |
245 | #else |
246 | if (addr->curraddr+1 < addr->naddresses) { |
247 | addr->curraddr++; |
248 | return TRUE; |
249 | } else { |
250 | return FALSE; |
251 | } |
252 | #endif |
253 | } |
254 | |
c5e438ec |
255 | void sk_getaddr(SockAddr addr, char *buf, int buflen) |
256 | { |
792c5eb5 |
257 | |
258 | if (addr->family == AF_UNSPEC) { |
259 | strncpy(buf, addr->hostname, buflen); |
260 | buf[buflen-1] = '\0'; |
261 | } else { |
05581745 |
262 | #ifndef NO_IPV6 |
792c5eb5 |
263 | if (getnameinfo(addr->ai->ai_addr, addr->ai->ai_addrlen, buf, buflen, |
264 | NULL, 0, NI_NUMERICHOST) != 0) { |
265 | buf[0] = '\0'; |
266 | strncat(buf, "<unknown>", buflen - 1); |
267 | } |
268 | #else |
c5e438ec |
269 | struct in_addr a; |
792c5eb5 |
270 | assert(addr->family == AF_INET); |
7555d6a5 |
271 | a.s_addr = htonl(addr->addresses[addr->curraddr]); |
c5e438ec |
272 | strncpy(buf, inet_ntoa(a), buflen); |
b7a189f3 |
273 | buf[buflen-1] = '\0'; |
792c5eb5 |
274 | #endif |
c5e438ec |
275 | } |
c5e438ec |
276 | } |
277 | |
b804e1e5 |
278 | int sk_hostname_is_local(char *name) |
279 | { |
280 | return !strcmp(name, "localhost"); |
281 | } |
282 | |
147d3370 |
283 | #define ipv4_is_loopback(addr) \ |
284 | (((addr).s_addr & htonl(0xff000000)) == htonl(0x7f000000)) |
285 | |
286 | static int sockaddr_is_loopback(struct sockaddr *sa) |
287 | { |
288 | struct sockaddr_in *sin; |
289 | #ifndef NO_IPV6 |
290 | struct sockaddr_in6 *sin6; |
291 | #endif |
292 | |
293 | switch (sa->sa_family) { |
294 | case AF_INET: |
295 | sin = (struct sockaddr_in *)sa; |
296 | return ipv4_is_loopback(sin->sin_addr); |
297 | #ifndef NO_IPV6 |
298 | case AF_INET6: |
299 | sin6 = (struct sockaddr_in6 *)sa; |
300 | return IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr); |
301 | #endif |
ff677091 |
302 | case AF_UNIX: |
147d3370 |
303 | return TRUE; |
304 | default: |
305 | return FALSE; |
306 | } |
307 | } |
308 | |
b804e1e5 |
309 | int sk_address_is_local(SockAddr addr) |
310 | { |
792c5eb5 |
311 | |
312 | if (addr->family == AF_UNSPEC) |
313 | return 0; /* we don't know; assume not */ |
314 | else { |
05581745 |
315 | #ifndef NO_IPV6 |
147d3370 |
316 | return sockaddr_is_loopback(addr->ai->ai_addr); |
792c5eb5 |
317 | #else |
b804e1e5 |
318 | struct in_addr a; |
792c5eb5 |
319 | assert(addr->family == AF_INET); |
7555d6a5 |
320 | a.s_addr = htonl(addr->addresses[addr->curraddr]); |
b804e1e5 |
321 | return ipv4_is_loopback(a); |
792c5eb5 |
322 | #endif |
b804e1e5 |
323 | } |
b804e1e5 |
324 | } |
325 | |
c5e438ec |
326 | int sk_addrtype(SockAddr addr) |
327 | { |
b7a189f3 |
328 | return (addr->family == AF_INET ? ADDRTYPE_IPV4 : |
05581745 |
329 | #ifndef NO_IPV6 |
b7a189f3 |
330 | addr->family == AF_INET6 ? ADDRTYPE_IPV6 : |
331 | #endif |
332 | ADDRTYPE_NAME); |
c5e438ec |
333 | } |
334 | |
335 | void sk_addrcopy(SockAddr addr, char *buf) |
336 | { |
792c5eb5 |
337 | |
05581745 |
338 | #ifndef NO_IPV6 |
792c5eb5 |
339 | if (addr->family == AF_INET) |
340 | memcpy(buf, &((struct sockaddr_in *)addr->ai->ai_addr)->sin_addr, |
341 | sizeof(struct in_addr)); |
342 | else if (addr->family == AF_INET6) |
343 | memcpy(buf, &((struct sockaddr_in6 *)addr->ai->ai_addr)->sin6_addr, |
344 | sizeof(struct in6_addr)); |
345 | else |
346 | assert(FALSE); |
347 | #else |
348 | struct in_addr a; |
349 | |
350 | assert(addr->family == AF_INET); |
7555d6a5 |
351 | a.s_addr = htonl(addr->addresses[addr->curraddr]); |
792c5eb5 |
352 | memcpy(buf, (char*) &a.s_addr, 4); |
c5e438ec |
353 | #endif |
c5e438ec |
354 | } |
355 | |
356 | void sk_addr_free(SockAddr addr) |
357 | { |
792c5eb5 |
358 | |
05581745 |
359 | #ifndef NO_IPV6 |
7555d6a5 |
360 | if (addr->ais != NULL) |
361 | freeaddrinfo(addr->ais); |
362 | #else |
363 | sfree(addr->addresses); |
792c5eb5 |
364 | #endif |
c5e438ec |
365 | sfree(addr); |
366 | } |
367 | |
368 | static Plug sk_tcp_plug(Socket sock, Plug p) |
369 | { |
370 | Actual_Socket s = (Actual_Socket) sock; |
371 | Plug ret = s->plug; |
372 | if (p) |
373 | s->plug = p; |
374 | return ret; |
375 | } |
376 | |
377 | static void sk_tcp_flush(Socket s) |
378 | { |
379 | /* |
380 | * We send data to the socket as soon as we can anyway, |
381 | * so we don't need to do anything here. :-) |
382 | */ |
383 | } |
384 | |
385 | static void sk_tcp_close(Socket s); |
e0e7dff8 |
386 | static int sk_tcp_write(Socket s, const char *data, int len); |
387 | static int sk_tcp_write_oob(Socket s, const char *data, int len); |
c5e438ec |
388 | static void sk_tcp_set_private_ptr(Socket s, void *ptr); |
389 | static void *sk_tcp_get_private_ptr(Socket s); |
390 | static void sk_tcp_set_frozen(Socket s, int is_frozen); |
cbe2d68f |
391 | static const char *sk_tcp_socket_error(Socket s); |
c5e438ec |
392 | |
2f92b717 |
393 | static struct socket_function_table tcp_fn_table = { |
394 | sk_tcp_plug, |
395 | sk_tcp_close, |
396 | sk_tcp_write, |
397 | sk_tcp_write_oob, |
398 | sk_tcp_flush, |
399 | sk_tcp_set_private_ptr, |
400 | sk_tcp_get_private_ptr, |
401 | sk_tcp_set_frozen, |
402 | sk_tcp_socket_error |
403 | }; |
404 | |
f7f9fb5c |
405 | Socket sk_register(OSSocket sockfd, Plug plug) |
c5e438ec |
406 | { |
c5e438ec |
407 | Actual_Socket ret; |
408 | |
409 | /* |
410 | * Create Socket structure. |
411 | */ |
3d88e64d |
412 | ret = snew(struct Socket_tag); |
2f92b717 |
413 | ret->fn = &tcp_fn_table; |
c5e438ec |
414 | ret->error = NULL; |
415 | ret->plug = plug; |
416 | bufchain_init(&ret->output_data); |
417 | ret->writable = 1; /* to start with */ |
418 | ret->sending_oob = 0; |
419 | ret->frozen = 1; |
420 | ret->frozen_readable = 0; |
421 | ret->localhost_only = 0; /* unused, but best init anyway */ |
422 | ret->pending_error = 0; |
423 | ret->oobpending = FALSE; |
424 | ret->listener = 0; |
7555d6a5 |
425 | ret->addr = NULL; |
a8cb2b6e |
426 | ret->connected = 1; |
c5e438ec |
427 | |
f7f9fb5c |
428 | ret->s = sockfd; |
c5e438ec |
429 | |
430 | if (ret->s < 0) { |
7555d6a5 |
431 | ret->error = strerror(errno); |
c5e438ec |
432 | return (Socket) ret; |
433 | } |
434 | |
435 | ret->oobinline = 0; |
436 | |
0ff9ea38 |
437 | uxsel_tell(ret); |
c5e438ec |
438 | add234(sktree, ret); |
439 | |
440 | return (Socket) ret; |
441 | } |
442 | |
7555d6a5 |
443 | static int try_connect(Actual_Socket sock) |
c5e438ec |
444 | { |
c5e438ec |
445 | int s; |
05581745 |
446 | #ifndef NO_IPV6 |
c5e438ec |
447 | struct sockaddr_in6 a6; |
448 | #endif |
449 | struct sockaddr_in a; |
fc0f17db |
450 | struct sockaddr_un au; |
451 | const struct sockaddr *sa; |
7555d6a5 |
452 | int err = 0; |
c5e438ec |
453 | short localport; |
fc0f17db |
454 | int fl, salen; |
c5e438ec |
455 | |
7555d6a5 |
456 | if (sock->s >= 0) |
457 | close(sock->s); |
458 | |
459 | plug_log(sock->plug, 0, sock->addr, sock->port, NULL, 0); |
c5e438ec |
460 | |
461 | /* |
462 | * Open socket. |
463 | */ |
7555d6a5 |
464 | assert(sock->addr->family != AF_UNSPEC); |
465 | s = socket(sock->addr->family, SOCK_STREAM, 0); |
466 | sock->s = s; |
c5e438ec |
467 | |
468 | if (s < 0) { |
7555d6a5 |
469 | err = errno; |
470 | goto ret; |
c5e438ec |
471 | } |
472 | |
7555d6a5 |
473 | if (sock->oobinline) { |
c5e438ec |
474 | int b = TRUE; |
475 | setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b)); |
476 | } |
477 | |
7555d6a5 |
478 | if (sock->nodelay) { |
c5e438ec |
479 | int b = TRUE; |
480 | setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b)); |
481 | } |
482 | |
7555d6a5 |
483 | if (sock->keepalive) { |
79bf227b |
484 | int b = TRUE; |
485 | setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b)); |
486 | } |
487 | |
c5e438ec |
488 | /* |
489 | * Bind to local address. |
490 | */ |
7555d6a5 |
491 | if (sock->privport) |
c5e438ec |
492 | localport = 1023; /* count from 1023 downwards */ |
493 | else |
494 | localport = 0; /* just use port 0 (ie kernel picks) */ |
495 | |
d2c29274 |
496 | /* BSD IP stacks need sockaddr_in zeroed before filling in */ |
497 | memset(&a,'\0',sizeof(struct sockaddr_in)); |
05581745 |
498 | #ifndef NO_IPV6 |
d2c29274 |
499 | memset(&a6,'\0',sizeof(struct sockaddr_in6)); |
500 | #endif |
fc0f17db |
501 | |
502 | /* We don't try to bind to a local address for UNIX domain sockets. (Why |
503 | * do we bother doing the bind when localport == 0 anyway?) */ |
7555d6a5 |
504 | if(sock->addr->family != AF_UNIX) { |
fc0f17db |
505 | /* Loop round trying to bind */ |
506 | while (1) { |
507 | int retcode; |
c5e438ec |
508 | |
05581745 |
509 | #ifndef NO_IPV6 |
7555d6a5 |
510 | if (sock->addr->family == AF_INET6) { |
fc0f17db |
511 | /* XXX use getaddrinfo to get a local address? */ |
512 | a6.sin6_family = AF_INET6; |
513 | a6.sin6_addr = in6addr_any; |
514 | a6.sin6_port = htons(localport); |
515 | retcode = bind(s, (struct sockaddr *) &a6, sizeof(a6)); |
516 | } else |
c5e438ec |
517 | #endif |
fc0f17db |
518 | { |
7555d6a5 |
519 | assert(sock->addr->family == AF_INET); |
fc0f17db |
520 | a.sin_family = AF_INET; |
521 | a.sin_addr.s_addr = htonl(INADDR_ANY); |
522 | a.sin_port = htons(localport); |
523 | retcode = bind(s, (struct sockaddr *) &a, sizeof(a)); |
524 | } |
525 | if (retcode >= 0) { |
526 | err = 0; |
527 | break; /* done */ |
528 | } else { |
529 | err = errno; |
530 | if (err != EADDRINUSE) /* failed, for a bad reason */ |
531 | break; |
532 | } |
533 | |
534 | if (localport == 0) |
535 | break; /* we're only looping once */ |
536 | localport--; |
537 | if (localport == 0) |
538 | break; /* we might have got to the end */ |
c5e438ec |
539 | } |
fc0f17db |
540 | |
7555d6a5 |
541 | if (err) |
542 | goto ret; |
c5e438ec |
543 | } |
544 | |
545 | /* |
546 | * Connect to remote address. |
547 | */ |
7555d6a5 |
548 | switch(sock->addr->family) { |
05581745 |
549 | #ifndef NO_IPV6 |
fc0f17db |
550 | case AF_INET: |
551 | /* XXX would be better to have got getaddrinfo() to fill in the port. */ |
7555d6a5 |
552 | ((struct sockaddr_in *)sock->addr->ai->ai_addr)->sin_port = |
553 | htons(sock->port); |
554 | sa = (const struct sockaddr *)sock->addr->ai->ai_addr; |
555 | salen = sock->addr->ai->ai_addrlen; |
fc0f17db |
556 | break; |
557 | case AF_INET6: |
7555d6a5 |
558 | ((struct sockaddr_in *)sock->addr->ai->ai_addr)->sin_port = |
559 | htons(sock->port); |
560 | sa = (const struct sockaddr *)sock->addr->ai->ai_addr; |
561 | salen = sock->addr->ai->ai_addrlen; |
fc0f17db |
562 | break; |
792c5eb5 |
563 | #else |
fc0f17db |
564 | case AF_INET: |
565 | a.sin_family = AF_INET; |
7555d6a5 |
566 | a.sin_addr.s_addr = htonl(sock->addr->addresses[sock->addr->curraddr]); |
567 | a.sin_port = htons((short) sock->port); |
fc0f17db |
568 | sa = (const struct sockaddr *)&a; |
569 | salen = sizeof a; |
570 | break; |
792c5eb5 |
571 | #endif |
fc0f17db |
572 | case AF_UNIX: |
7555d6a5 |
573 | assert(sock->port == 0); /* to catch confused people */ |
574 | assert(strlen(sock->addr->hostname) < sizeof au.sun_path); |
fc0f17db |
575 | memset(&au, 0, sizeof au); |
576 | au.sun_family = AF_UNIX; |
7555d6a5 |
577 | strcpy(au.sun_path, sock->addr->hostname); |
fc0f17db |
578 | sa = (const struct sockaddr *)&au; |
579 | salen = sizeof au; |
580 | break; |
581 | |
582 | default: |
583 | assert(0 && "unknown address family"); |
8815b68b |
584 | exit(1); /* XXX: GCC doesn't understand assert() on some systems. */ |
fc0f17db |
585 | } |
4b70e70e |
586 | |
587 | fl = fcntl(s, F_GETFL); |
588 | if (fl != -1) |
589 | fcntl(s, F_SETFL, fl | O_NONBLOCK); |
051dd789 |
590 | |
fc0f17db |
591 | if ((connect(s, sa, salen)) < 0) { |
051dd789 |
592 | if ( errno != EINPROGRESS ) { |
7555d6a5 |
593 | err = errno; |
594 | goto ret; |
c5e438ec |
595 | } |
596 | } else { |
597 | /* |
598 | * If we _don't_ get EWOULDBLOCK, the connect has completed |
599 | * and we should set the socket as connected and writable. |
600 | */ |
7555d6a5 |
601 | sock->connected = 1; |
602 | sock->writable = 1; |
c5e438ec |
603 | } |
604 | |
7555d6a5 |
605 | uxsel_tell(sock); |
606 | add234(sktree, sock); |
607 | |
608 | ret: |
609 | if (err) |
610 | plug_log(sock->plug, 1, sock->addr, sock->port, strerror(err), err); |
611 | return err; |
612 | } |
613 | |
614 | Socket sk_new(SockAddr addr, int port, int privport, int oobinline, |
615 | int nodelay, int keepalive, Plug plug) |
616 | { |
617 | Actual_Socket ret; |
618 | int err; |
619 | |
620 | /* |
621 | * Create Socket structure. |
622 | */ |
623 | ret = snew(struct Socket_tag); |
624 | ret->fn = &tcp_fn_table; |
625 | ret->error = NULL; |
626 | ret->plug = plug; |
627 | bufchain_init(&ret->output_data); |
628 | ret->connected = 0; /* to start with */ |
629 | ret->writable = 0; /* to start with */ |
630 | ret->sending_oob = 0; |
631 | ret->frozen = 0; |
632 | ret->frozen_readable = 0; |
633 | ret->localhost_only = 0; /* unused, but best init anyway */ |
634 | ret->pending_error = 0; |
635 | ret->oobpending = FALSE; |
636 | ret->listener = 0; |
637 | ret->addr = addr; |
638 | ret->s = -1; |
639 | ret->oobinline = oobinline; |
640 | ret->nodelay = nodelay; |
641 | ret->keepalive = keepalive; |
642 | ret->privport = privport; |
643 | ret->port = port; |
644 | |
645 | err = 0; |
646 | do { |
647 | err = try_connect(ret); |
648 | } while (err && sk_nextaddr(ret->addr)); |
c5e438ec |
649 | |
7555d6a5 |
650 | if (err) |
651 | ret->error = strerror(err); |
f85e6f6e |
652 | |
c5e438ec |
653 | return (Socket) ret; |
654 | } |
655 | |
05581745 |
656 | Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int address_family) |
c5e438ec |
657 | { |
c5e438ec |
658 | int s; |
05581745 |
659 | #ifndef NO_IPV6 |
792c5eb5 |
660 | struct addrinfo hints, *ai; |
661 | char portstr[6]; |
adb18b94 |
662 | struct sockaddr_in6 a6; |
792c5eb5 |
663 | #endif |
adb18b94 |
664 | struct sockaddr *addr; |
665 | int addrlen; |
c5e438ec |
666 | struct sockaddr_in a; |
c5e438ec |
667 | Actual_Socket ret; |
668 | int retcode; |
669 | int on = 1; |
670 | |
671 | /* |
672 | * Create Socket structure. |
673 | */ |
3d88e64d |
674 | ret = snew(struct Socket_tag); |
2f92b717 |
675 | ret->fn = &tcp_fn_table; |
c5e438ec |
676 | ret->error = NULL; |
677 | ret->plug = plug; |
678 | bufchain_init(&ret->output_data); |
679 | ret->writable = 0; /* to start with */ |
680 | ret->sending_oob = 0; |
681 | ret->frozen = 0; |
682 | ret->frozen_readable = 0; |
683 | ret->localhost_only = local_host_only; |
684 | ret->pending_error = 0; |
685 | ret->oobpending = FALSE; |
686 | ret->listener = 1; |
7555d6a5 |
687 | ret->addr = NULL; |
c5e438ec |
688 | |
689 | /* |
05581745 |
690 | * Translate address_family from platform-independent constants |
691 | * into local reality. |
692 | */ |
693 | address_family = (address_family == ADDRTYPE_IPV4 ? AF_INET : |
40040af1 |
694 | #ifndef NO_IPV6 |
695 | address_family == ADDRTYPE_IPV6 ? AF_INET6 : |
696 | #endif |
697 | AF_UNSPEC); |
05581745 |
698 | |
699 | #ifndef NO_IPV6 |
700 | /* Let's default to IPv6. |
701 | * If the stack doesn't support IPv6, we will fall back to IPv4. */ |
702 | if (address_family == AF_UNSPEC) address_family = AF_INET6; |
703 | #else |
704 | /* No other choice, default to IPv4 */ |
705 | if (address_family == AF_UNSPEC) address_family = AF_INET; |
706 | #endif |
707 | |
708 | /* |
c5e438ec |
709 | * Open socket. |
710 | */ |
05581745 |
711 | s = socket(address_family, SOCK_STREAM, 0); |
712 | |
40040af1 |
713 | #ifndef NO_IPV6 |
05581745 |
714 | /* If the host doesn't support IPv6 try fallback to IPv4. */ |
715 | if (s < 0 && address_family == AF_INET6) { |
716 | address_family = AF_INET; |
717 | s = socket(address_family, SOCK_STREAM, 0); |
718 | } |
40040af1 |
719 | #endif |
c5e438ec |
720 | |
721 | if (s < 0) { |
7555d6a5 |
722 | ret->error = strerror(errno); |
c5e438ec |
723 | return (Socket) ret; |
724 | } |
725 | |
726 | ret->oobinline = 0; |
727 | |
728 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)); |
729 | |
adb18b94 |
730 | retcode = -1; |
731 | addr = NULL; addrlen = -1; /* placate optimiser */ |
732 | |
733 | if (srcaddr != NULL) { |
05581745 |
734 | #ifndef NO_IPV6 |
adb18b94 |
735 | hints.ai_flags = AI_NUMERICHOST; |
736 | hints.ai_family = address_family; |
d5474756 |
737 | hints.ai_socktype = SOCK_STREAM; |
adb18b94 |
738 | hints.ai_protocol = 0; |
739 | hints.ai_addrlen = 0; |
740 | hints.ai_addr = NULL; |
741 | hints.ai_canonname = NULL; |
742 | hints.ai_next = NULL; |
062af27b |
743 | assert(port >= 0 && port <= 99999); |
adb18b94 |
744 | sprintf(portstr, "%d", port); |
745 | retcode = getaddrinfo(srcaddr, portstr, &hints, &ai); |
7f8c817c |
746 | if (retcode == 0) { |
ca9892ae |
747 | addr = ai->ai_addr; |
748 | addrlen = ai->ai_addrlen; |
749 | } |
adb18b94 |
750 | #else |
751 | memset(&a,'\0',sizeof(struct sockaddr_in)); |
752 | a.sin_family = AF_INET; |
753 | a.sin_port = htons(port); |
754 | a.sin_addr.s_addr = inet_addr(srcaddr); |
755 | if (a.sin_addr.s_addr != (in_addr_t)(-1)) { |
756 | /* Override localhost_only with specified listen addr. */ |
757 | ret->localhost_only = ipv4_is_loopback(a.sin_addr); |
adb18b94 |
758 | } |
dffa03fd |
759 | addr = (struct sockaddr *)&a; |
adb18b94 |
760 | addrlen = sizeof(a); |
761 | retcode = 0; |
792c5eb5 |
762 | #endif |
adb18b94 |
763 | } |
6ee9b735 |
764 | |
adb18b94 |
765 | if (retcode != 0) { |
766 | #ifndef NO_IPV6 |
767 | if (address_family == AF_INET6) { |
768 | memset(&a6,'\0',sizeof(struct sockaddr_in6)); |
769 | a6.sin6_family = AF_INET6; |
770 | a6.sin6_port = htons(port); |
771 | if (local_host_only) |
772 | a6.sin6_addr = in6addr_loopback; |
773 | else |
774 | a6.sin6_addr = in6addr_any; |
775 | addr = (struct sockaddr *)&a6; |
776 | addrlen = sizeof(a6); |
777 | } else |
778 | #endif |
779 | { |
780 | memset(&a,'\0',sizeof(struct sockaddr_in)); |
781 | a.sin_family = AF_INET; |
782 | a.sin_port = htons(port); |
6ee9b735 |
783 | if (local_host_only) |
784 | a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
785 | else |
786 | a.sin_addr.s_addr = htonl(INADDR_ANY); |
adb18b94 |
787 | addr = (struct sockaddr *)&a; |
788 | addrlen = sizeof(a); |
789 | } |
c5e438ec |
790 | } |
791 | |
adb18b94 |
792 | retcode = bind(s, addr, addrlen); |
793 | if (retcode < 0) { |
794 | close(s); |
7555d6a5 |
795 | ret->error = strerror(errno); |
c5e438ec |
796 | return (Socket) ret; |
797 | } |
798 | |
c5e438ec |
799 | if (listen(s, SOMAXCONN) < 0) { |
800 | close(s); |
7555d6a5 |
801 | ret->error = strerror(errno); |
c5e438ec |
802 | return (Socket) ret; |
803 | } |
804 | |
adb18b94 |
805 | ret->s = s; |
806 | |
0ff9ea38 |
807 | uxsel_tell(ret); |
c5e438ec |
808 | add234(sktree, ret); |
809 | |
810 | return (Socket) ret; |
811 | } |
812 | |
813 | static void sk_tcp_close(Socket sock) |
814 | { |
815 | Actual_Socket s = (Actual_Socket) sock; |
816 | |
0ff9ea38 |
817 | uxsel_del(s->s); |
c5e438ec |
818 | del234(sktree, s); |
819 | close(s->s); |
7555d6a5 |
820 | if (s->addr) |
821 | sk_addr_free(s->addr); |
c5e438ec |
822 | sfree(s); |
823 | } |
824 | |
0a4022a1 |
825 | void *sk_getxdmdata(void *sock, int *lenp) |
2f92b717 |
826 | { |
827 | Actual_Socket s = (Actual_Socket) sock; |
0a4022a1 |
828 | #ifdef NO_IPV6 |
2f92b717 |
829 | struct sockaddr_in addr; |
0a4022a1 |
830 | #else |
831 | struct sockaddr_storage addr; |
832 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr; |
833 | #endif |
834 | struct sockaddr *sa = (struct sockaddr *)&addr; |
835 | struct sockaddr_in *sin = (struct sockaddr_in *)&addr; |
2f92b717 |
836 | socklen_t addrlen; |
0a4022a1 |
837 | char *buf; |
838 | static unsigned int unix_addr = 0xFFFFFFFF; |
2f92b717 |
839 | |
840 | /* |
841 | * We must check that this socket really _is_ an Actual_Socket. |
842 | */ |
843 | if (s->fn != &tcp_fn_table) |
0a4022a1 |
844 | return NULL; /* failure */ |
2f92b717 |
845 | |
2f92b717 |
846 | addrlen = sizeof(addr); |
0a4022a1 |
847 | if (getsockname(s->s, sa, &addrlen) < 0) |
848 | return NULL; |
849 | switch(sa->sa_family) { |
fc0f17db |
850 | case AF_INET: |
0a4022a1 |
851 | *lenp = 6; |
852 | buf = snewn(*lenp, char); |
853 | PUT_32BIT_MSB_FIRST(buf, ntohl(sin->sin_addr.s_addr)); |
854 | PUT_16BIT_MSB_FIRST(buf+4, ntohs(sin->sin_port)); |
fc0f17db |
855 | break; |
0a4022a1 |
856 | #ifndef NO_IPV6 |
857 | case AF_INET6: |
858 | *lenp = 6; |
859 | buf = snewn(*lenp, char); |
860 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { |
861 | memcpy(buf, sin6->sin6_addr.s6_addr + 12, 4); |
862 | PUT_16BIT_MSB_FIRST(buf+4, ntohs(sin6->sin6_port)); |
863 | } else |
864 | /* This is stupid, but it's what XLib does. */ |
865 | memset(buf, 0, 6); |
866 | break; |
867 | #endif |
fc0f17db |
868 | case AF_UNIX: |
0a4022a1 |
869 | *lenp = 6; |
870 | buf = snewn(*lenp, char); |
871 | PUT_32BIT_MSB_FIRST(buf, unix_addr--); |
872 | PUT_16BIT_MSB_FIRST(buf+4, getpid()); |
fc0f17db |
873 | break; |
2f92b717 |
874 | |
fc0f17db |
875 | /* XXX IPV6 */ |
876 | |
877 | default: |
0a4022a1 |
878 | return NULL; |
fc0f17db |
879 | } |
2f92b717 |
880 | |
0a4022a1 |
881 | return buf; |
2f92b717 |
882 | } |
883 | |
c5e438ec |
884 | /* |
885 | * The function which tries to send on a socket once it's deemed |
886 | * writable. |
887 | */ |
888 | void try_send(Actual_Socket s) |
889 | { |
890 | while (s->sending_oob || bufchain_size(&s->output_data) > 0) { |
891 | int nsent; |
892 | int err; |
893 | void *data; |
894 | int len, urgentflag; |
895 | |
896 | if (s->sending_oob) { |
897 | urgentflag = MSG_OOB; |
898 | len = s->sending_oob; |
899 | data = &s->oobdata; |
900 | } else { |
901 | urgentflag = 0; |
902 | bufchain_prefix(&s->output_data, &data, &len); |
903 | } |
904 | nsent = send(s->s, data, len, urgentflag); |
905 | noise_ultralight(nsent); |
906 | if (nsent <= 0) { |
907 | err = (nsent < 0 ? errno : 0); |
908 | if (err == EWOULDBLOCK) { |
909 | /* |
910 | * Perfectly normal: we've sent all we can for the moment. |
911 | */ |
912 | s->writable = FALSE; |
913 | return; |
f3e0acda |
914 | } else { |
c5e438ec |
915 | /* |
f3e0acda |
916 | * We unfortunately can't just call plug_closing(), |
c5e438ec |
917 | * because it's quite likely that we're currently |
918 | * _in_ a call from the code we'd be calling back |
919 | * to, so we'd have to make half the SSH code |
920 | * reentrant. Instead we flag a pending error on |
921 | * the socket, to be dealt with (by calling |
922 | * plug_closing()) at some suitable future moment. |
923 | */ |
924 | s->pending_error = err; |
925 | return; |
c5e438ec |
926 | } |
927 | } else { |
928 | if (s->sending_oob) { |
929 | if (nsent < len) { |
930 | memmove(s->oobdata, s->oobdata+nsent, len-nsent); |
931 | s->sending_oob = len - nsent; |
932 | } else { |
933 | s->sending_oob = 0; |
934 | } |
935 | } else { |
936 | bufchain_consume(&s->output_data, nsent); |
937 | } |
938 | } |
939 | } |
0ff9ea38 |
940 | uxsel_tell(s); |
c5e438ec |
941 | } |
942 | |
e0e7dff8 |
943 | static int sk_tcp_write(Socket sock, const char *buf, int len) |
c5e438ec |
944 | { |
945 | Actual_Socket s = (Actual_Socket) sock; |
946 | |
947 | /* |
948 | * Add the data to the buffer list on the socket. |
949 | */ |
950 | bufchain_add(&s->output_data, buf, len); |
951 | |
952 | /* |
953 | * Now try sending from the start of the buffer list. |
954 | */ |
955 | if (s->writable) |
956 | try_send(s); |
957 | |
c2a71b0d |
958 | /* |
959 | * Update the select() status to correctly reflect whether or |
960 | * not we should be selecting for write. |
961 | */ |
962 | uxsel_tell(s); |
963 | |
c5e438ec |
964 | return bufchain_size(&s->output_data); |
965 | } |
966 | |
e0e7dff8 |
967 | static int sk_tcp_write_oob(Socket sock, const char *buf, int len) |
c5e438ec |
968 | { |
969 | Actual_Socket s = (Actual_Socket) sock; |
970 | |
971 | /* |
972 | * Replace the buffer list on the socket with the data. |
973 | */ |
974 | bufchain_clear(&s->output_data); |
975 | assert(len <= sizeof(s->oobdata)); |
976 | memcpy(s->oobdata, buf, len); |
977 | s->sending_oob = len; |
978 | |
979 | /* |
980 | * Now try sending from the start of the buffer list. |
981 | */ |
982 | if (s->writable) |
983 | try_send(s); |
984 | |
c2a71b0d |
985 | /* |
986 | * Update the select() status to correctly reflect whether or |
987 | * not we should be selecting for write. |
988 | */ |
989 | uxsel_tell(s); |
990 | |
c5e438ec |
991 | return s->sending_oob; |
992 | } |
993 | |
0ff9ea38 |
994 | static int net_select_result(int fd, int event) |
c5e438ec |
995 | { |
996 | int ret; |
c5e438ec |
997 | char buf[20480]; /* nice big buffer for plenty of speed */ |
998 | Actual_Socket s; |
999 | u_long atmark; |
1000 | |
1001 | /* Find the Socket structure */ |
f7f9fb5c |
1002 | s = find234(sktree, &fd, cmpforsearch); |
c5e438ec |
1003 | if (!s) |
1004 | return 1; /* boggle */ |
1005 | |
1006 | noise_ultralight(event); |
1007 | |
1008 | switch (event) { |
c5e438ec |
1009 | case 4: /* exceptional */ |
1010 | if (!s->oobinline) { |
1011 | /* |
1012 | * On a non-oobinline socket, this indicates that we |
1013 | * can immediately perform an OOB read and get back OOB |
1014 | * data, which we will send to the back end with |
1015 | * type==2 (urgent data). |
1016 | */ |
1017 | ret = recv(s->s, buf, sizeof(buf), MSG_OOB); |
1018 | noise_ultralight(ret); |
1019 | if (ret <= 0) { |
f3e0acda |
1020 | return plug_closing(s->plug, |
1021 | ret == 0 ? "Internal networking trouble" : |
1022 | strerror(errno), errno, 0); |
c5e438ec |
1023 | } else { |
7555d6a5 |
1024 | /* |
1025 | * Receiving actual data on a socket means we can |
1026 | * stop falling back through the candidate |
1027 | * addresses to connect to. |
1028 | */ |
1029 | if (s->addr) { |
1030 | sk_addr_free(s->addr); |
1031 | s->addr = NULL; |
1032 | } |
c5e438ec |
1033 | return plug_receive(s->plug, 2, buf, ret); |
1034 | } |
1035 | break; |
1036 | } |
1037 | |
1038 | /* |
1039 | * If we reach here, this is an oobinline socket, which |
56e5b2db |
1040 | * means we should set s->oobpending and then deal with it |
1041 | * when we get called for the readability event (which |
1042 | * should also occur). |
c5e438ec |
1043 | */ |
1044 | s->oobpending = TRUE; |
56e5b2db |
1045 | break; |
c5e438ec |
1046 | case 1: /* readable; also acceptance */ |
1047 | if (s->listener) { |
1048 | /* |
1049 | * On a listening socket, the readability event means a |
1050 | * connection is ready to be accepted. |
1051 | */ |
19a4130c |
1052 | #ifdef NO_IPV6 |
1053 | struct sockaddr_in ss; |
1054 | #else |
1055 | struct sockaddr_storage ss; |
1056 | #endif |
1057 | socklen_t addrlen = sizeof(ss); |
c5e438ec |
1058 | int t; /* socket of connection */ |
1059 | |
19a4130c |
1060 | memset(&ss, 0, addrlen); |
1061 | t = accept(s->s, (struct sockaddr *)&ss, &addrlen); |
c5e438ec |
1062 | if (t < 0) { |
1063 | break; |
1064 | } |
1065 | |
19a4130c |
1066 | if (s->localhost_only && |
1067 | !sockaddr_is_loopback((struct sockaddr *)&ss)) { |
c5e438ec |
1068 | close(t); /* someone let nonlocal through?! */ |
f7f9fb5c |
1069 | } else if (plug_accepting(s->plug, t)) { |
c5e438ec |
1070 | close(t); /* denied or error */ |
1071 | } |
1072 | break; |
1073 | } |
1074 | |
1075 | /* |
1076 | * If we reach here, this is not a listening socket, so |
1077 | * readability really means readability. |
1078 | */ |
1079 | |
1080 | /* In the case the socket is still frozen, we don't even bother */ |
1081 | if (s->frozen) { |
1082 | s->frozen_readable = 1; |
1083 | break; |
1084 | } |
1085 | |
1086 | /* |
1087 | * We have received data on the socket. For an oobinline |
1088 | * socket, this might be data _before_ an urgent pointer, |
1089 | * in which case we send it to the back end with type==1 |
1090 | * (data prior to urgent). |
1091 | */ |
1092 | if (s->oobinline && s->oobpending) { |
1093 | atmark = 1; |
1094 | if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark) |
1095 | s->oobpending = FALSE; /* clear this indicator */ |
1096 | } else |
1097 | atmark = 1; |
1098 | |
56e5b2db |
1099 | ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0); |
c5e438ec |
1100 | noise_ultralight(ret); |
1101 | if (ret < 0) { |
1102 | if (errno == EWOULDBLOCK) { |
1103 | break; |
1104 | } |
1105 | } |
1106 | if (ret < 0) { |
7555d6a5 |
1107 | /* |
1108 | * An error at this point _might_ be an error reported |
1109 | * by a non-blocking connect(). So before we return a |
1110 | * panic status to the user, let's just see whether |
1111 | * that's the case. |
1112 | */ |
1113 | int err = errno; |
1114 | if (s->addr) { |
1115 | plug_log(s->plug, 1, s->addr, s->port, strerror(err), err); |
1116 | while (s->addr && sk_nextaddr(s->addr)) { |
1117 | err = try_connect(s); |
1118 | } |
1119 | } |
1120 | if (err != 0) |
1121 | return plug_closing(s->plug, strerror(err), err, 0); |
c5e438ec |
1122 | } else if (0 == ret) { |
1123 | return plug_closing(s->plug, NULL, 0, 0); |
1124 | } else { |
7555d6a5 |
1125 | /* |
1126 | * Receiving actual data on a socket means we can |
1127 | * stop falling back through the candidate |
1128 | * addresses to connect to. |
1129 | */ |
1130 | if (s->addr) { |
1131 | sk_addr_free(s->addr); |
1132 | s->addr = NULL; |
1133 | } |
c5e438ec |
1134 | return plug_receive(s->plug, atmark ? 0 : 1, buf, ret); |
1135 | } |
1136 | break; |
1137 | case 2: /* writable */ |
051dd789 |
1138 | if (!s->connected) { |
1139 | /* |
1140 | * select() reports a socket as _writable_ when an |
1141 | * asynchronous connection is completed. |
1142 | */ |
1143 | s->connected = s->writable = 1; |
0ff9ea38 |
1144 | uxsel_tell(s); |
051dd789 |
1145 | break; |
1146 | } else { |
c5e438ec |
1147 | int bufsize_before, bufsize_after; |
1148 | s->writable = 1; |
1149 | bufsize_before = s->sending_oob + bufchain_size(&s->output_data); |
1150 | try_send(s); |
1151 | bufsize_after = s->sending_oob + bufchain_size(&s->output_data); |
1152 | if (bufsize_after < bufsize_before) |
1153 | plug_sent(s->plug, bufsize_after); |
1154 | } |
1155 | break; |
1156 | } |
1157 | |
1158 | return 1; |
1159 | } |
1160 | |
1161 | /* |
1162 | * Deal with socket errors detected in try_send(). |
1163 | */ |
1164 | void net_pending_errors(void) |
1165 | { |
1166 | int i; |
1167 | Actual_Socket s; |
1168 | |
1169 | /* |
1170 | * This might be a fiddly business, because it's just possible |
1171 | * that handling a pending error on one socket might cause |
1172 | * others to be closed. (I can't think of any reason this might |
1173 | * happen in current SSH implementation, but to maintain |
1174 | * generality of this network layer I'll assume the worst.) |
1175 | * |
1176 | * So what we'll do is search the socket list for _one_ socket |
1177 | * with a pending error, and then handle it, and then search |
1178 | * the list again _from the beginning_. Repeat until we make a |
1179 | * pass with no socket errors present. That way we are |
1180 | * protected against the socket list changing under our feet. |
1181 | */ |
1182 | |
1183 | do { |
1184 | for (i = 0; (s = index234(sktree, i)) != NULL; i++) { |
1185 | if (s->pending_error) { |
1186 | /* |
1187 | * An error has occurred on this socket. Pass it to the |
1188 | * plug. |
1189 | */ |
7555d6a5 |
1190 | plug_closing(s->plug, strerror(s->pending_error), |
c5e438ec |
1191 | s->pending_error, 0); |
1192 | break; |
1193 | } |
1194 | } |
1195 | } while (s); |
1196 | } |
1197 | |
1198 | /* |
1199 | * Each socket abstraction contains a `void *' private field in |
1200 | * which the client can keep state. |
1201 | */ |
1202 | static void sk_tcp_set_private_ptr(Socket sock, void *ptr) |
1203 | { |
1204 | Actual_Socket s = (Actual_Socket) sock; |
1205 | s->private_ptr = ptr; |
1206 | } |
1207 | |
1208 | static void *sk_tcp_get_private_ptr(Socket sock) |
1209 | { |
1210 | Actual_Socket s = (Actual_Socket) sock; |
1211 | return s->private_ptr; |
1212 | } |
1213 | |
1214 | /* |
1215 | * Special error values are returned from sk_namelookup and sk_new |
1216 | * if there's a problem. These functions extract an error message, |
1217 | * or return NULL if there's no problem. |
1218 | */ |
cbe2d68f |
1219 | const char *sk_addr_error(SockAddr addr) |
c5e438ec |
1220 | { |
1221 | return addr->error; |
1222 | } |
cbe2d68f |
1223 | static const char *sk_tcp_socket_error(Socket sock) |
c5e438ec |
1224 | { |
1225 | Actual_Socket s = (Actual_Socket) sock; |
1226 | return s->error; |
1227 | } |
1228 | |
1229 | static void sk_tcp_set_frozen(Socket sock, int is_frozen) |
1230 | { |
1231 | Actual_Socket s = (Actual_Socket) sock; |
1232 | if (s->frozen == is_frozen) |
1233 | return; |
1234 | s->frozen = is_frozen; |
1235 | if (!is_frozen && s->frozen_readable) { |
1236 | char c; |
1237 | recv(s->s, &c, 1, MSG_PEEK); |
1238 | } |
1239 | s->frozen_readable = 0; |
0ff9ea38 |
1240 | uxsel_tell(s); |
c5e438ec |
1241 | } |
1242 | |
0ff9ea38 |
1243 | static void uxsel_tell(Actual_Socket s) |
c5e438ec |
1244 | { |
0ff9ea38 |
1245 | int rwx = 0; |
1cfc764f |
1246 | if (s->listener) { |
1247 | rwx |= 1; /* read == accept */ |
1248 | } else { |
1249 | if (!s->connected) |
1250 | rwx |= 2; /* write == connect */ |
1251 | if (s->connected && !s->frozen) |
1252 | rwx |= 1 | 4; /* read, except */ |
1253 | if (bufchain_size(&s->output_data)) |
1254 | rwx |= 2; /* write */ |
1255 | } |
0ff9ea38 |
1256 | uxsel_set(s->s, rwx, net_select_result); |
c5e438ec |
1257 | } |
1258 | |
1259 | int net_service_lookup(char *service) |
1260 | { |
1261 | struct servent *se; |
1262 | se = getservbyname(service, NULL); |
1263 | if (se != NULL) |
1264 | return ntohs(se->s_port); |
1265 | else |
1266 | return 0; |
1267 | } |
fc0f17db |
1268 | |
1269 | SockAddr platform_get_x11_unix_address(int displaynum, char **canonicalname) |
1270 | { |
1271 | SockAddr ret = snew(struct SockAddr_tag); |
1272 | int n; |
1273 | |
1274 | memset(ret, 0, sizeof *ret); |
1275 | ret->family = AF_UNIX; |
1276 | n = snprintf(ret->hostname, sizeof ret->hostname, |
1277 | "%s%d", X11_UNIX_PATH, displaynum); |
1278 | if(n < 0) |
1279 | ret->error = "snprintf failed"; |
1280 | else if(n >= sizeof ret->hostname) |
1281 | ret->error = "X11 UNIX name too long"; |
1282 | else |
1283 | *canonicalname = dupstr(ret->hostname); |
7555d6a5 |
1284 | #ifndef NO_IPV6 |
1285 | ret->ais = NULL; |
1286 | #else |
1287 | ret->addresses = NULL; |
1288 | #endif |
fc0f17db |
1289 | return ret; |
1290 | } |