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> |
18 | |
19 | #define DEFINE_PLUG_METHOD_MACROS |
20 | #include "putty.h" |
21 | #include "network.h" |
22 | #include "tree234.h" |
23 | |
6ee9b735 |
24 | #define ipv4_is_loopback(addr) (inet_netof(addr) == IN_LOOPBACKNET) |
25 | |
c5e438ec |
26 | struct Socket_tag { |
27 | struct socket_function_table *fn; |
28 | /* the above variable absolutely *must* be the first in this structure */ |
29 | char *error; |
30 | int s; |
31 | Plug plug; |
32 | void *private_ptr; |
33 | bufchain output_data; |
34 | int connected; |
35 | int writable; |
36 | int frozen; /* this causes readability notifications to be ignored */ |
37 | int frozen_readable; /* this means we missed at least one readability |
38 | * notification while we were frozen */ |
39 | int localhost_only; /* for listening sockets */ |
40 | char oobdata[1]; |
41 | int sending_oob; |
42 | int oobpending; /* is there OOB data available to read? */ |
43 | int oobinline; |
44 | int pending_error; /* in case send() returns error */ |
45 | int listener; |
46 | }; |
47 | |
48 | /* |
49 | * We used to typedef struct Socket_tag *Socket. |
50 | * |
51 | * Since we have made the networking abstraction slightly more |
52 | * abstract, Socket no longer means a tcp socket (it could mean |
53 | * an ssl socket). So now we must use Actual_Socket when we know |
54 | * we are talking about a tcp socket. |
55 | */ |
56 | typedef struct Socket_tag *Actual_Socket; |
57 | |
58 | struct SockAddr_tag { |
59 | char *error; |
60 | /* address family this belongs to, AF_INET for IPv4, AF_INET6 for IPv6. */ |
61 | int family; |
62 | unsigned long address; /* Address IPv4 style. */ |
63 | #ifdef IPV6 |
64 | struct addrinfo *ai; /* Address IPv6 style. */ |
65 | #endif |
66 | }; |
67 | |
68 | static tree234 *sktree; |
69 | |
70 | static int cmpfortree(void *av, void *bv) |
71 | { |
72 | Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv; |
73 | int as = a->s, bs = b->s; |
74 | if (as < bs) |
75 | return -1; |
76 | if (as > bs) |
77 | return +1; |
78 | return 0; |
79 | } |
80 | |
81 | static int cmpforsearch(void *av, void *bv) |
82 | { |
83 | Actual_Socket b = (Actual_Socket) bv; |
84 | int as = (int) av, bs = b->s; |
85 | if (as < bs) |
86 | return -1; |
87 | if (as > bs) |
88 | return +1; |
89 | return 0; |
90 | } |
91 | |
92 | void sk_init(void) |
93 | { |
94 | sktree = newtree234(cmpfortree); |
95 | } |
96 | |
97 | void sk_cleanup(void) |
98 | { |
99 | Actual_Socket s; |
100 | int i; |
101 | |
102 | if (sktree) { |
103 | for (i = 0; (s = index234(sktree, i)) != NULL; i++) { |
104 | close(s->s); |
105 | } |
106 | } |
107 | } |
108 | |
109 | char *error_string(int error) |
110 | { |
e266735f |
111 | return strerror(error); |
c5e438ec |
112 | } |
113 | |
114 | SockAddr sk_namelookup(char *host, char **canonicalname) |
115 | { |
116 | SockAddr ret = smalloc(sizeof(struct SockAddr_tag)); |
117 | unsigned long a; |
118 | struct hostent *h = NULL; |
119 | char realhost[8192]; |
120 | |
121 | /* Clear the structure and default to IPv4. */ |
122 | memset(ret, 0, sizeof(struct SockAddr_tag)); |
123 | ret->family = 0; /* We set this one when we have resolved the host. */ |
124 | *realhost = '\0'; |
125 | ret->error = NULL; |
126 | |
127 | if ((a = inet_addr(host)) == (unsigned long) INADDR_NONE) { |
128 | #ifdef IPV6 |
129 | if (getaddrinfo(host, NULL, NULL, &ret->ai) == 0) { |
130 | ret->family = ret->ai->ai_family; |
131 | } else |
132 | #endif |
133 | { |
134 | /* |
135 | * Otherwise use the IPv4-only gethostbyname... (NOTE: |
136 | * we don't use gethostbyname as a fallback!) |
137 | */ |
138 | if (ret->family == 0) { |
139 | /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */ |
140 | if ( (h = gethostbyname(host)) ) |
141 | ret->family = AF_INET; |
142 | } |
8c43874c |
143 | if (ret->family == 0) { |
c5e438ec |
144 | ret->error = (h_errno == HOST_NOT_FOUND || |
145 | h_errno == NO_DATA || |
146 | h_errno == NO_ADDRESS ? "Host does not exist" : |
147 | h_errno == TRY_AGAIN ? |
148 | "Temporary name service failure" : |
149 | "gethostbyname: unknown error"); |
8c43874c |
150 | return ret; |
151 | } |
c5e438ec |
152 | } |
153 | |
154 | #ifdef IPV6 |
155 | /* If we got an address info use that... */ |
156 | if (ret->ai) { |
157 | |
158 | /* Are we in IPv4 fallback mode? */ |
159 | /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */ |
160 | if (ret->family == AF_INET) |
161 | memcpy(&a, |
162 | (char *) &((struct sockaddr_in *) ret->ai-> |
163 | ai_addr)->sin_addr, sizeof(a)); |
164 | |
165 | /* Now let's find that canonicalname... */ |
166 | if (getnameinfo((struct sockaddr *) ret->ai->ai_addr, |
167 | ret->family == |
168 | AF_INET ? sizeof(struct sockaddr_in) : |
169 | sizeof(struct sockaddr_in6), realhost, |
170 | sizeof(realhost), NULL, 0, 0) != 0) { |
171 | strncpy(realhost, host, sizeof(realhost)); |
172 | } |
173 | } |
174 | /* We used the IPv4-only gethostbyname()... */ |
175 | else |
176 | #endif |
177 | { |
178 | memcpy(&a, h->h_addr, sizeof(a)); |
179 | /* This way we are always sure the h->h_name is valid :) */ |
180 | strncpy(realhost, h->h_name, sizeof(realhost)); |
181 | } |
182 | } else { |
183 | /* |
184 | * This must be a numeric IPv4 address because it caused a |
185 | * success return from inet_addr. |
186 | */ |
187 | ret->family = AF_INET; |
188 | strncpy(realhost, host, sizeof(realhost)); |
189 | } |
190 | ret->address = ntohl(a); |
191 | realhost[lenof(realhost)-1] = '\0'; |
192 | *canonicalname = smalloc(1+strlen(realhost)); |
193 | strcpy(*canonicalname, realhost); |
194 | return ret; |
195 | } |
196 | |
197 | void sk_getaddr(SockAddr addr, char *buf, int buflen) |
198 | { |
199 | #ifdef IPV6 |
200 | if (addr->family == AF_INET) { |
201 | #endif |
202 | struct in_addr a; |
203 | a.s_addr = htonl(addr->address); |
204 | strncpy(buf, inet_ntoa(a), buflen); |
205 | #ifdef IPV6 |
206 | } else { |
207 | FIXME; /* I don't know how to get a text form of an IPv6 address. */ |
208 | } |
209 | #endif |
210 | } |
211 | |
b804e1e5 |
212 | int sk_hostname_is_local(char *name) |
213 | { |
214 | return !strcmp(name, "localhost"); |
215 | } |
216 | |
217 | int sk_address_is_local(SockAddr addr) |
218 | { |
219 | #ifdef IPV6 |
220 | if (addr->family == AF_INET) { |
221 | #endif |
222 | struct in_addr a; |
223 | a.s_addr = htonl(addr->address); |
224 | return ipv4_is_loopback(a); |
225 | #ifdef IPV6 |
226 | } else { |
227 | FIXME; /* someone who can compile for IPV6 had better do this bit */ |
228 | } |
229 | #endif |
230 | } |
231 | |
c5e438ec |
232 | int sk_addrtype(SockAddr addr) |
233 | { |
234 | return (addr->family == AF_INET ? ADDRTYPE_IPV4 : ADDRTYPE_IPV6); |
235 | } |
236 | |
237 | void sk_addrcopy(SockAddr addr, char *buf) |
238 | { |
239 | #ifdef IPV6 |
240 | if (addr->family == AF_INET) { |
241 | #endif |
242 | struct in_addr a; |
243 | a.s_addr = htonl(addr->address); |
244 | memcpy(buf, (char*) &a.s_addr, 4); |
245 | #ifdef IPV6 |
246 | } else { |
247 | memcpy(buf, (char*) addr->ai, 16); |
248 | } |
249 | #endif |
250 | } |
251 | |
252 | void sk_addr_free(SockAddr addr) |
253 | { |
254 | sfree(addr); |
255 | } |
256 | |
257 | static Plug sk_tcp_plug(Socket sock, Plug p) |
258 | { |
259 | Actual_Socket s = (Actual_Socket) sock; |
260 | Plug ret = s->plug; |
261 | if (p) |
262 | s->plug = p; |
263 | return ret; |
264 | } |
265 | |
266 | static void sk_tcp_flush(Socket s) |
267 | { |
268 | /* |
269 | * We send data to the socket as soon as we can anyway, |
270 | * so we don't need to do anything here. :-) |
271 | */ |
272 | } |
273 | |
274 | static void sk_tcp_close(Socket s); |
275 | static int sk_tcp_write(Socket s, char *data, int len); |
276 | static int sk_tcp_write_oob(Socket s, char *data, int len); |
277 | static void sk_tcp_set_private_ptr(Socket s, void *ptr); |
278 | static void *sk_tcp_get_private_ptr(Socket s); |
279 | static void sk_tcp_set_frozen(Socket s, int is_frozen); |
280 | static char *sk_tcp_socket_error(Socket s); |
281 | |
282 | Socket sk_register(void *sock, Plug plug) |
283 | { |
284 | static struct socket_function_table fn_table = { |
285 | sk_tcp_plug, |
286 | sk_tcp_close, |
287 | sk_tcp_write, |
288 | sk_tcp_write_oob, |
289 | sk_tcp_flush, |
290 | sk_tcp_set_private_ptr, |
291 | sk_tcp_get_private_ptr, |
292 | sk_tcp_set_frozen, |
293 | sk_tcp_socket_error |
294 | }; |
295 | |
296 | Actual_Socket ret; |
297 | |
298 | /* |
299 | * Create Socket structure. |
300 | */ |
301 | ret = smalloc(sizeof(struct Socket_tag)); |
302 | ret->fn = &fn_table; |
303 | ret->error = NULL; |
304 | ret->plug = plug; |
305 | bufchain_init(&ret->output_data); |
306 | ret->writable = 1; /* to start with */ |
307 | ret->sending_oob = 0; |
308 | ret->frozen = 1; |
309 | ret->frozen_readable = 0; |
310 | ret->localhost_only = 0; /* unused, but best init anyway */ |
311 | ret->pending_error = 0; |
312 | ret->oobpending = FALSE; |
313 | ret->listener = 0; |
314 | |
315 | ret->s = (int)sock; |
316 | |
317 | if (ret->s < 0) { |
318 | ret->error = error_string(errno); |
319 | return (Socket) ret; |
320 | } |
321 | |
322 | ret->oobinline = 0; |
323 | |
324 | add234(sktree, ret); |
325 | |
326 | return (Socket) ret; |
327 | } |
328 | |
329 | Socket sk_new(SockAddr addr, int port, int privport, int oobinline, |
330 | int nodelay, Plug plug) |
331 | { |
332 | static struct socket_function_table fn_table = { |
333 | sk_tcp_plug, |
334 | sk_tcp_close, |
335 | sk_tcp_write, |
336 | sk_tcp_write_oob, |
337 | sk_tcp_flush, |
338 | sk_tcp_set_private_ptr, |
339 | sk_tcp_get_private_ptr, |
340 | sk_tcp_set_frozen, |
341 | sk_tcp_socket_error |
342 | }; |
343 | |
344 | int s; |
345 | #ifdef IPV6 |
346 | struct sockaddr_in6 a6; |
347 | #endif |
348 | struct sockaddr_in a; |
349 | int err; |
350 | Actual_Socket ret; |
351 | short localport; |
352 | |
353 | /* |
354 | * Create Socket structure. |
355 | */ |
356 | ret = smalloc(sizeof(struct Socket_tag)); |
357 | ret->fn = &fn_table; |
358 | ret->error = NULL; |
359 | ret->plug = plug; |
360 | bufchain_init(&ret->output_data); |
361 | ret->connected = 0; /* to start with */ |
362 | ret->writable = 0; /* to start with */ |
363 | ret->sending_oob = 0; |
364 | ret->frozen = 0; |
365 | ret->frozen_readable = 0; |
366 | ret->localhost_only = 0; /* unused, but best init anyway */ |
367 | ret->pending_error = 0; |
368 | ret->oobpending = FALSE; |
369 | ret->listener = 0; |
370 | |
371 | /* |
372 | * Open socket. |
373 | */ |
374 | s = socket(addr->family, SOCK_STREAM, 0); |
375 | ret->s = s; |
376 | |
377 | if (s < 0) { |
378 | ret->error = error_string(errno); |
379 | return (Socket) ret; |
380 | } |
381 | |
382 | ret->oobinline = oobinline; |
383 | if (oobinline) { |
384 | int b = TRUE; |
385 | setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b)); |
386 | } |
387 | |
388 | if (nodelay) { |
389 | int b = TRUE; |
390 | setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b)); |
391 | } |
392 | |
393 | /* |
394 | * Bind to local address. |
395 | */ |
396 | if (privport) |
397 | localport = 1023; /* count from 1023 downwards */ |
398 | else |
399 | localport = 0; /* just use port 0 (ie kernel picks) */ |
400 | |
401 | /* Loop round trying to bind */ |
402 | while (1) { |
403 | int retcode; |
404 | |
405 | #ifdef IPV6 |
406 | if (addr->family == AF_INET6) { |
407 | memset(&a6, 0, sizeof(a6)); |
408 | a6.sin6_family = AF_INET6; |
409 | /*a6.sin6_addr = in6addr_any; *//* == 0 */ |
410 | a6.sin6_port = htons(localport); |
411 | } else |
412 | #endif |
413 | { |
414 | a.sin_family = AF_INET; |
415 | a.sin_addr.s_addr = htonl(INADDR_ANY); |
416 | a.sin_port = htons(localport); |
417 | } |
418 | #ifdef IPV6 |
419 | retcode = bind(s, (addr->family == AF_INET6 ? |
420 | (struct sockaddr *) &a6 : |
421 | (struct sockaddr *) &a), |
422 | (addr->family == |
423 | AF_INET6 ? sizeof(a6) : sizeof(a))); |
424 | #else |
425 | retcode = bind(s, (struct sockaddr *) &a, sizeof(a)); |
426 | #endif |
427 | if (retcode >= 0) { |
428 | err = 0; |
429 | break; /* done */ |
430 | } else { |
431 | err = errno; |
432 | if (err != EADDRINUSE) /* failed, for a bad reason */ |
433 | break; |
434 | } |
435 | |
436 | if (localport == 0) |
437 | break; /* we're only looping once */ |
438 | localport--; |
439 | if (localport == 0) |
440 | break; /* we might have got to the end */ |
441 | } |
442 | |
443 | if (err) { |
444 | ret->error = error_string(err); |
445 | return (Socket) ret; |
446 | } |
447 | |
448 | /* |
449 | * Connect to remote address. |
450 | */ |
451 | #ifdef IPV6 |
452 | if (addr->family == AF_INET6) { |
453 | memset(&a, 0, sizeof(a)); |
454 | a6.sin6_family = AF_INET6; |
455 | a6.sin6_port = htons((short) port); |
456 | a6.sin6_addr = |
457 | ((struct sockaddr_in6 *) addr->ai->ai_addr)->sin6_addr; |
458 | } else |
459 | #endif |
460 | { |
461 | a.sin_family = AF_INET; |
462 | a.sin_addr.s_addr = htonl(addr->address); |
463 | a.sin_port = htons((short) port); |
464 | } |
465 | |
466 | if (( |
467 | #ifdef IPV6 |
468 | connect(s, ((addr->family == AF_INET6) ? |
469 | (struct sockaddr *) &a6 : (struct sockaddr *) &a), |
470 | (addr->family == AF_INET6) ? sizeof(a6) : sizeof(a)) |
471 | #else |
472 | connect(s, (struct sockaddr *) &a, sizeof(a)) |
473 | #endif |
474 | ) < 0) { |
475 | /* |
476 | * FIXME: We are prepared to receive EWOULDBLOCK here, |
477 | * because we might want the connection to be made |
478 | * asynchronously; but how do we actually arrange this in |
479 | * Unix? I forget. |
480 | */ |
481 | if ( errno != EWOULDBLOCK ) { |
482 | ret->error = error_string(errno); |
483 | return (Socket) ret; |
484 | } |
485 | } else { |
486 | /* |
487 | * If we _don't_ get EWOULDBLOCK, the connect has completed |
488 | * and we should set the socket as connected and writable. |
489 | */ |
490 | ret->connected = 1; |
491 | ret->writable = 1; |
492 | } |
493 | |
494 | add234(sktree, ret); |
495 | |
496 | return (Socket) ret; |
497 | } |
498 | |
6ee9b735 |
499 | Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only) |
c5e438ec |
500 | { |
501 | static struct socket_function_table fn_table = { |
502 | sk_tcp_plug, |
503 | sk_tcp_close, |
504 | sk_tcp_write, |
505 | sk_tcp_write_oob, |
506 | sk_tcp_flush, |
507 | sk_tcp_set_private_ptr, |
508 | sk_tcp_get_private_ptr, |
509 | sk_tcp_set_frozen, |
510 | sk_tcp_socket_error |
511 | }; |
512 | |
513 | int s; |
514 | #ifdef IPV6 |
515 | struct sockaddr_in6 a6; |
516 | #endif |
517 | struct sockaddr_in a; |
518 | int err; |
519 | Actual_Socket ret; |
520 | int retcode; |
521 | int on = 1; |
522 | |
523 | /* |
524 | * Create Socket structure. |
525 | */ |
526 | ret = smalloc(sizeof(struct Socket_tag)); |
527 | ret->fn = &fn_table; |
528 | ret->error = NULL; |
529 | ret->plug = plug; |
530 | bufchain_init(&ret->output_data); |
531 | ret->writable = 0; /* to start with */ |
532 | ret->sending_oob = 0; |
533 | ret->frozen = 0; |
534 | ret->frozen_readable = 0; |
535 | ret->localhost_only = local_host_only; |
536 | ret->pending_error = 0; |
537 | ret->oobpending = FALSE; |
538 | ret->listener = 1; |
539 | |
540 | /* |
541 | * Open socket. |
542 | */ |
543 | s = socket(AF_INET, SOCK_STREAM, 0); |
544 | ret->s = s; |
545 | |
546 | if (s < 0) { |
547 | ret->error = error_string(errno); |
548 | return (Socket) ret; |
549 | } |
550 | |
551 | ret->oobinline = 0; |
552 | |
553 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)); |
554 | |
555 | #ifdef IPV6 |
556 | if (addr->family == AF_INET6) { |
557 | memset(&a6, 0, sizeof(a6)); |
558 | a6.sin6_family = AF_INET6; |
6ee9b735 |
559 | /* FIXME: srcaddr is ignored for IPv6, because I (SGT) don't |
560 | * know how to do it. :-) */ |
c5e438ec |
561 | if (local_host_only) |
562 | a6.sin6_addr = in6addr_loopback; |
563 | else |
564 | a6.sin6_addr = in6addr_any; |
565 | a6.sin6_port = htons(port); |
566 | } else |
567 | #endif |
568 | { |
6ee9b735 |
569 | int got_addr = 0; |
c5e438ec |
570 | a.sin_family = AF_INET; |
6ee9b735 |
571 | |
572 | /* |
573 | * Bind to source address. First try an explicitly |
574 | * specified one... |
575 | */ |
576 | if (srcaddr) { |
577 | a.sin_addr.s_addr = inet_addr(srcaddr); |
578 | if (a.sin_addr.s_addr != INADDR_NONE) { |
579 | /* Override localhost_only with specified listen addr. */ |
580 | ret->localhost_only = ipv4_is_loopback(a.sin_addr); |
581 | got_addr = 1; |
582 | } |
583 | } |
584 | |
585 | /* |
586 | * ... and failing that, go with one of the standard ones. |
587 | */ |
588 | if (!got_addr) { |
589 | if (local_host_only) |
590 | a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
591 | else |
592 | a.sin_addr.s_addr = htonl(INADDR_ANY); |
593 | } |
594 | |
c5e438ec |
595 | a.sin_port = htons((short)port); |
596 | } |
597 | #ifdef IPV6 |
598 | retcode = bind(s, (addr->family == AF_INET6 ? |
599 | (struct sockaddr *) &a6 : |
600 | (struct sockaddr *) &a), |
601 | (addr->family == |
602 | AF_INET6 ? sizeof(a6) : sizeof(a))); |
603 | #else |
604 | retcode = bind(s, (struct sockaddr *) &a, sizeof(a)); |
605 | #endif |
606 | if (retcode >= 0) { |
607 | err = 0; |
608 | } else { |
609 | err = errno; |
610 | } |
611 | |
612 | if (err) { |
613 | ret->error = error_string(err); |
614 | return (Socket) ret; |
615 | } |
616 | |
617 | |
618 | if (listen(s, SOMAXCONN) < 0) { |
619 | close(s); |
620 | ret->error = error_string(errno); |
621 | return (Socket) ret; |
622 | } |
623 | |
624 | add234(sktree, ret); |
625 | |
626 | return (Socket) ret; |
627 | } |
628 | |
629 | static void sk_tcp_close(Socket sock) |
630 | { |
631 | Actual_Socket s = (Actual_Socket) sock; |
632 | |
633 | del234(sktree, s); |
634 | close(s->s); |
635 | sfree(s); |
636 | } |
637 | |
638 | /* |
639 | * The function which tries to send on a socket once it's deemed |
640 | * writable. |
641 | */ |
642 | void try_send(Actual_Socket s) |
643 | { |
644 | while (s->sending_oob || bufchain_size(&s->output_data) > 0) { |
645 | int nsent; |
646 | int err; |
647 | void *data; |
648 | int len, urgentflag; |
649 | |
650 | if (s->sending_oob) { |
651 | urgentflag = MSG_OOB; |
652 | len = s->sending_oob; |
653 | data = &s->oobdata; |
654 | } else { |
655 | urgentflag = 0; |
656 | bufchain_prefix(&s->output_data, &data, &len); |
657 | } |
658 | nsent = send(s->s, data, len, urgentflag); |
659 | noise_ultralight(nsent); |
660 | if (nsent <= 0) { |
661 | err = (nsent < 0 ? errno : 0); |
662 | if (err == EWOULDBLOCK) { |
663 | /* |
664 | * Perfectly normal: we've sent all we can for the moment. |
665 | */ |
666 | s->writable = FALSE; |
667 | return; |
668 | } else if (nsent == 0 || |
669 | err == ECONNABORTED || err == ECONNRESET) { |
670 | /* |
671 | * If send() returns CONNABORTED or CONNRESET, we |
672 | * unfortunately can't just call plug_closing(), |
673 | * because it's quite likely that we're currently |
674 | * _in_ a call from the code we'd be calling back |
675 | * to, so we'd have to make half the SSH code |
676 | * reentrant. Instead we flag a pending error on |
677 | * the socket, to be dealt with (by calling |
678 | * plug_closing()) at some suitable future moment. |
679 | */ |
680 | s->pending_error = err; |
681 | return; |
682 | } else { |
683 | /* We're inside the Unix frontend here, so we know |
684 | * that the frontend handle is unnecessary. */ |
685 | logevent(NULL, error_string(err)); |
686 | fatalbox("%s", error_string(err)); |
687 | } |
688 | } else { |
689 | if (s->sending_oob) { |
690 | if (nsent < len) { |
691 | memmove(s->oobdata, s->oobdata+nsent, len-nsent); |
692 | s->sending_oob = len - nsent; |
693 | } else { |
694 | s->sending_oob = 0; |
695 | } |
696 | } else { |
697 | bufchain_consume(&s->output_data, nsent); |
698 | } |
699 | } |
700 | } |
701 | } |
702 | |
703 | static int sk_tcp_write(Socket sock, char *buf, int len) |
704 | { |
705 | Actual_Socket s = (Actual_Socket) sock; |
706 | |
707 | /* |
708 | * Add the data to the buffer list on the socket. |
709 | */ |
710 | bufchain_add(&s->output_data, buf, len); |
711 | |
712 | /* |
713 | * Now try sending from the start of the buffer list. |
714 | */ |
715 | if (s->writable) |
716 | try_send(s); |
717 | |
718 | return bufchain_size(&s->output_data); |
719 | } |
720 | |
721 | static int sk_tcp_write_oob(Socket sock, char *buf, int len) |
722 | { |
723 | Actual_Socket s = (Actual_Socket) sock; |
724 | |
725 | /* |
726 | * Replace the buffer list on the socket with the data. |
727 | */ |
728 | bufchain_clear(&s->output_data); |
729 | assert(len <= sizeof(s->oobdata)); |
730 | memcpy(s->oobdata, buf, len); |
731 | s->sending_oob = len; |
732 | |
733 | /* |
734 | * Now try sending from the start of the buffer list. |
735 | */ |
736 | if (s->writable) |
737 | try_send(s); |
738 | |
739 | return s->sending_oob; |
740 | } |
741 | |
742 | int select_result(int fd, int event) |
743 | { |
744 | int ret; |
745 | int err; |
746 | char buf[20480]; /* nice big buffer for plenty of speed */ |
747 | Actual_Socket s; |
748 | u_long atmark; |
749 | |
750 | /* Find the Socket structure */ |
751 | s = find234(sktree, (void *) fd, cmpforsearch); |
752 | if (!s) |
753 | return 1; /* boggle */ |
754 | |
755 | noise_ultralight(event); |
756 | |
757 | switch (event) { |
758 | #ifdef FIXME_NONBLOCKING_CONNECTIONS |
759 | case FIXME: /* connected */ |
760 | s->connected = s->writable = 1; |
761 | break; |
762 | #endif |
763 | case 4: /* exceptional */ |
764 | if (!s->oobinline) { |
765 | /* |
766 | * On a non-oobinline socket, this indicates that we |
767 | * can immediately perform an OOB read and get back OOB |
768 | * data, which we will send to the back end with |
769 | * type==2 (urgent data). |
770 | */ |
771 | ret = recv(s->s, buf, sizeof(buf), MSG_OOB); |
772 | noise_ultralight(ret); |
773 | if (ret <= 0) { |
774 | char *str = (ret == 0 ? "Internal networking trouble" : |
775 | error_string(errno)); |
776 | /* We're inside the Unix frontend here, so we know |
777 | * that the frontend handle is unnecessary. */ |
778 | logevent(NULL, str); |
779 | fatalbox("%s", str); |
780 | } else { |
781 | return plug_receive(s->plug, 2, buf, ret); |
782 | } |
783 | break; |
784 | } |
785 | |
786 | /* |
787 | * If we reach here, this is an oobinline socket, which |
56e5b2db |
788 | * means we should set s->oobpending and then deal with it |
789 | * when we get called for the readability event (which |
790 | * should also occur). |
c5e438ec |
791 | */ |
792 | s->oobpending = TRUE; |
56e5b2db |
793 | break; |
c5e438ec |
794 | case 1: /* readable; also acceptance */ |
795 | if (s->listener) { |
796 | /* |
797 | * On a listening socket, the readability event means a |
798 | * connection is ready to be accepted. |
799 | */ |
800 | struct sockaddr_in isa; |
801 | int addrlen = sizeof(struct sockaddr_in); |
802 | int t; /* socket of connection */ |
803 | |
804 | memset(&isa, 0, sizeof(struct sockaddr_in)); |
805 | err = 0; |
806 | t = accept(s->s,(struct sockaddr *)&isa,&addrlen); |
807 | if (t < 0) { |
808 | break; |
809 | } |
810 | |
6ee9b735 |
811 | if (s->localhost_only && !ipv4_is_loopback(isa.sin_addr)) { |
c5e438ec |
812 | close(t); /* someone let nonlocal through?! */ |
813 | } else if (plug_accepting(s->plug, (void*)t)) { |
814 | close(t); /* denied or error */ |
815 | } |
816 | break; |
817 | } |
818 | |
819 | /* |
820 | * If we reach here, this is not a listening socket, so |
821 | * readability really means readability. |
822 | */ |
823 | |
824 | /* In the case the socket is still frozen, we don't even bother */ |
825 | if (s->frozen) { |
826 | s->frozen_readable = 1; |
827 | break; |
828 | } |
829 | |
830 | /* |
831 | * We have received data on the socket. For an oobinline |
832 | * socket, this might be data _before_ an urgent pointer, |
833 | * in which case we send it to the back end with type==1 |
834 | * (data prior to urgent). |
835 | */ |
836 | if (s->oobinline && s->oobpending) { |
837 | atmark = 1; |
838 | if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark) |
839 | s->oobpending = FALSE; /* clear this indicator */ |
840 | } else |
841 | atmark = 1; |
842 | |
56e5b2db |
843 | ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0); |
c5e438ec |
844 | noise_ultralight(ret); |
845 | if (ret < 0) { |
846 | if (errno == EWOULDBLOCK) { |
847 | break; |
848 | } |
849 | } |
850 | if (ret < 0) { |
851 | return plug_closing(s->plug, error_string(errno), errno, 0); |
852 | } else if (0 == ret) { |
853 | return plug_closing(s->plug, NULL, 0, 0); |
854 | } else { |
855 | return plug_receive(s->plug, atmark ? 0 : 1, buf, ret); |
856 | } |
857 | break; |
858 | case 2: /* writable */ |
859 | { |
860 | int bufsize_before, bufsize_after; |
861 | s->writable = 1; |
862 | bufsize_before = s->sending_oob + bufchain_size(&s->output_data); |
863 | try_send(s); |
864 | bufsize_after = s->sending_oob + bufchain_size(&s->output_data); |
865 | if (bufsize_after < bufsize_before) |
866 | plug_sent(s->plug, bufsize_after); |
867 | } |
868 | break; |
869 | } |
870 | |
871 | return 1; |
872 | } |
873 | |
874 | /* |
875 | * Deal with socket errors detected in try_send(). |
876 | */ |
877 | void net_pending_errors(void) |
878 | { |
879 | int i; |
880 | Actual_Socket s; |
881 | |
882 | /* |
883 | * This might be a fiddly business, because it's just possible |
884 | * that handling a pending error on one socket might cause |
885 | * others to be closed. (I can't think of any reason this might |
886 | * happen in current SSH implementation, but to maintain |
887 | * generality of this network layer I'll assume the worst.) |
888 | * |
889 | * So what we'll do is search the socket list for _one_ socket |
890 | * with a pending error, and then handle it, and then search |
891 | * the list again _from the beginning_. Repeat until we make a |
892 | * pass with no socket errors present. That way we are |
893 | * protected against the socket list changing under our feet. |
894 | */ |
895 | |
896 | do { |
897 | for (i = 0; (s = index234(sktree, i)) != NULL; i++) { |
898 | if (s->pending_error) { |
899 | /* |
900 | * An error has occurred on this socket. Pass it to the |
901 | * plug. |
902 | */ |
903 | plug_closing(s->plug, error_string(s->pending_error), |
904 | s->pending_error, 0); |
905 | break; |
906 | } |
907 | } |
908 | } while (s); |
909 | } |
910 | |
911 | /* |
912 | * Each socket abstraction contains a `void *' private field in |
913 | * which the client can keep state. |
914 | */ |
915 | static void sk_tcp_set_private_ptr(Socket sock, void *ptr) |
916 | { |
917 | Actual_Socket s = (Actual_Socket) sock; |
918 | s->private_ptr = ptr; |
919 | } |
920 | |
921 | static void *sk_tcp_get_private_ptr(Socket sock) |
922 | { |
923 | Actual_Socket s = (Actual_Socket) sock; |
924 | return s->private_ptr; |
925 | } |
926 | |
927 | /* |
928 | * Special error values are returned from sk_namelookup and sk_new |
929 | * if there's a problem. These functions extract an error message, |
930 | * or return NULL if there's no problem. |
931 | */ |
932 | char *sk_addr_error(SockAddr addr) |
933 | { |
934 | return addr->error; |
935 | } |
936 | static char *sk_tcp_socket_error(Socket sock) |
937 | { |
938 | Actual_Socket s = (Actual_Socket) sock; |
939 | return s->error; |
940 | } |
941 | |
942 | static void sk_tcp_set_frozen(Socket sock, int is_frozen) |
943 | { |
944 | Actual_Socket s = (Actual_Socket) sock; |
945 | if (s->frozen == is_frozen) |
946 | return; |
947 | s->frozen = is_frozen; |
948 | if (!is_frozen && s->frozen_readable) { |
949 | char c; |
950 | recv(s->s, &c, 1, MSG_PEEK); |
951 | } |
952 | s->frozen_readable = 0; |
953 | } |
954 | |
955 | /* |
956 | * For Unix select()-based frontends: enumerate all sockets |
957 | * currently active, and state whether we currently wish to receive |
958 | * select events on them for reading, writing and exceptional |
959 | * status. |
960 | */ |
961 | static void set_rwx(Actual_Socket s, int *rwx) |
962 | { |
963 | int val = 0; |
964 | if (s->connected && !s->frozen) |
965 | val |= 1 | 4; /* read, except */ |
966 | if (bufchain_size(&s->output_data)) |
967 | val |= 2; /* write */ |
968 | if (s->listener) |
969 | val |= 1; /* read == accept */ |
970 | *rwx = val; |
971 | } |
972 | |
973 | int first_socket(int *state, int *rwx) |
974 | { |
975 | Actual_Socket s; |
976 | *state = 0; |
977 | s = index234(sktree, (*state)++); |
978 | if (s) |
979 | set_rwx(s, rwx); |
980 | return s ? s->s : -1; |
981 | } |
982 | |
983 | int next_socket(int *state, int *rwx) |
984 | { |
985 | Actual_Socket s = index234(sktree, (*state)++); |
986 | if (s) |
987 | set_rwx(s, rwx); |
988 | return s ? s->s : -1; |
989 | } |
990 | |
991 | int net_service_lookup(char *service) |
992 | { |
993 | struct servent *se; |
994 | se = getservbyname(service, NULL); |
995 | if (se != NULL) |
996 | return ntohs(se->s_port); |
997 | else |
998 | return 0; |
999 | } |