uxnet.c 27 KB

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