network.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. /*
  2. * Windows networking abstraction.
  3. *
  4. * For the IPv6 code in here I am indebted to Jeroen Massar and
  5. * unfix.org.
  6. */
  7. #include <winsock2.h> /* need to put this first, for winelib builds */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <assert.h>
  11. #define NEED_DECLARATION_OF_SELECT /* in order to initialise it */
  12. #include "putty.h"
  13. #include "network.h"
  14. #include "tree234.h"
  15. #include "ssh.h"
  16. #include <ws2tcpip.h>
  17. #if HAVE_AFUNIX_H
  18. #include <afunix.h>
  19. #endif
  20. #ifndef NO_IPV6
  21. #ifdef __clang__
  22. #pragma clang diagnostic push
  23. #pragma clang diagnostic ignored "-Wmissing-braces"
  24. #endif
  25. const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
  26. const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
  27. #ifdef __clang__
  28. #pragma clang diagnostic pop
  29. #endif
  30. #endif
  31. #define ipv4_is_loopback(addr) \
  32. ((p_ntohl(addr.s_addr) & 0xFF000000L) == 0x7F000000L)
  33. /*
  34. * Mutable state that goes with a SockAddr: stores information
  35. * about where in the list of candidate IP(v*) addresses we've
  36. * currently got to.
  37. */
  38. typedef struct SockAddrStep_tag SockAddrStep;
  39. struct SockAddrStep_tag {
  40. #ifndef NO_IPV6
  41. struct addrinfo *ai; /* steps along addr->ais */
  42. #endif
  43. int curraddr;
  44. };
  45. typedef struct NetSocket NetSocket;
  46. struct NetSocket {
  47. const char *error;
  48. SOCKET s;
  49. Plug *plug;
  50. bufchain output_data;
  51. bool connected;
  52. bool writable;
  53. bool frozen; /* this causes readability notifications to be ignored */
  54. bool frozen_readable; /* this means we missed at least one readability
  55. * notification while we were frozen */
  56. bool localhost_only; /* for listening sockets */
  57. char oobdata[1];
  58. size_t sending_oob;
  59. bool oobinline, nodelay, keepalive, privport;
  60. enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof;
  61. SockAddr *addr;
  62. SockAddrStep step;
  63. int port;
  64. int pending_error; /* in case send() returns error */
  65. /*
  66. * We sometimes need pairs of Socket structures to be linked:
  67. * if we are listening on the same IPv6 and v4 port, for
  68. * example. So here we define `parent' and `child' pointers to
  69. * track this link.
  70. */
  71. NetSocket *parent, *child;
  72. Socket sock;
  73. };
  74. /*
  75. * Top-level discriminator for SockAddr.
  76. *
  77. * UNRESOLVED means a host name not yet put through DNS; IP means a
  78. * resolved IP address (or list of them); UNIX indicates the AF_UNIX
  79. * network family (which Windows also has); NAMEDPIPE indicates that
  80. * this SockAddr is phony, holding a Windows named pipe pathname
  81. * instead of any address WinSock can understand.
  82. */
  83. typedef enum SuperFamily {
  84. UNRESOLVED,
  85. IP,
  86. #if HAVE_AFUNIX_H
  87. UNIX,
  88. #endif
  89. NAMEDPIPE
  90. } SuperFamily;
  91. struct SockAddr {
  92. int refcount;
  93. const char *error;
  94. SuperFamily superfamily;
  95. #ifndef NO_IPV6
  96. struct addrinfo *ais; /* Addresses IPv6 style. */
  97. #endif
  98. unsigned long *addresses; /* Addresses IPv4 style. */
  99. int naddresses;
  100. char hostname[512]; /* Store an unresolved host name. */
  101. };
  102. /*
  103. * Which address family this address belongs to. AF_INET for IPv4;
  104. * AF_INET6 for IPv6; AF_UNIX for Unix-domain sockets; AF_UNSPEC
  105. * indicates that name resolution has not been done and a simple host
  106. * name is held in this SockAddr structure.
  107. */
  108. static inline int sockaddr_family(SockAddr *addr, SockAddrStep step)
  109. {
  110. switch (addr->superfamily) {
  111. case IP:
  112. #ifndef NO_IPV6
  113. if (step.ai)
  114. return step.ai->ai_family;
  115. #endif
  116. return AF_INET;
  117. #if HAVE_AFUNIX_H
  118. case UNIX:
  119. return AF_UNIX;
  120. #endif
  121. default:
  122. return AF_UNSPEC;
  123. }
  124. }
  125. /*
  126. * Start a SockAddrStep structure to step through multiple
  127. * addresses.
  128. */
  129. #ifndef NO_IPV6
  130. #define START_STEP(addr, step) \
  131. ((step).ai = (addr)->ais, (step).curraddr = 0)
  132. #else
  133. #define START_STEP(addr, step) \
  134. ((step).curraddr = 0)
  135. #endif
  136. static tree234 *sktree;
  137. static int cmpfortree(void *av, void *bv)
  138. {
  139. NetSocket *a = (NetSocket *)av, *b = (NetSocket *)bv;
  140. uintptr_t as = (uintptr_t) a->s, bs = (uintptr_t) b->s;
  141. if (as < bs)
  142. return -1;
  143. if (as > bs)
  144. return +1;
  145. if (a < b)
  146. return -1;
  147. if (a > b)
  148. return +1;
  149. return 0;
  150. }
  151. static int cmpforsearch(void *av, void *bv)
  152. {
  153. NetSocket *b = (NetSocket *)bv;
  154. uintptr_t as = (uintptr_t) av, bs = (uintptr_t) b->s;
  155. if (as < bs)
  156. return -1;
  157. if (as > bs)
  158. return +1;
  159. return 0;
  160. }
  161. DECL_WINDOWS_FUNCTION(static, int, WSAStartup, (WORD, LPWSADATA));
  162. DECL_WINDOWS_FUNCTION(static, int, WSACleanup, (void));
  163. DECL_WINDOWS_FUNCTION(static, int, closesocket, (SOCKET));
  164. DECL_WINDOWS_FUNCTION(static, ULONG, ntohl, (ULONG));
  165. DECL_WINDOWS_FUNCTION(static, ULONG, htonl, (ULONG));
  166. DECL_WINDOWS_FUNCTION(static, USHORT, htons, (USHORT));
  167. DECL_WINDOWS_FUNCTION(static, USHORT, ntohs, (USHORT));
  168. DECL_WINDOWS_FUNCTION(static, int, gethostname, (char *, int));
  169. DECL_WINDOWS_FUNCTION(static, struct hostent FAR *, gethostbyname,
  170. (const char FAR *));
  171. DECL_WINDOWS_FUNCTION(static, struct servent FAR *, getservbyname,
  172. (const char FAR *, const char FAR *));
  173. DECL_WINDOWS_FUNCTION(static, ULONG, inet_addr, (const char FAR *));
  174. DECL_WINDOWS_FUNCTION(static, char FAR *, inet_ntoa, (struct in_addr));
  175. DECL_WINDOWS_FUNCTION(static, const char FAR *, inet_ntop,
  176. (int, void FAR *, char *, size_t));
  177. DECL_WINDOWS_FUNCTION(static, int, connect,
  178. (SOCKET, const struct sockaddr FAR *, int));
  179. DECL_WINDOWS_FUNCTION(static, int, bind,
  180. (SOCKET, const struct sockaddr FAR *, int));
  181. DECL_WINDOWS_FUNCTION(static, int, setsockopt,
  182. (SOCKET, int, int, const char FAR *, int));
  183. DECL_WINDOWS_FUNCTION(static, SOCKET, socket, (int, int, int));
  184. DECL_WINDOWS_FUNCTION(static, int, listen, (SOCKET, int));
  185. DECL_WINDOWS_FUNCTION(static, int, send, (SOCKET, const char FAR *, int, int));
  186. DECL_WINDOWS_FUNCTION(static, int, shutdown, (SOCKET, int));
  187. DECL_WINDOWS_FUNCTION(static, int, ioctlsocket,
  188. (SOCKET, LONG, ULONG FAR *));
  189. DECL_WINDOWS_FUNCTION(static, SOCKET, accept,
  190. (SOCKET, struct sockaddr FAR *, int FAR *));
  191. DECL_WINDOWS_FUNCTION(static, int, getpeername,
  192. (SOCKET, struct sockaddr FAR *, int FAR *));
  193. DECL_WINDOWS_FUNCTION(static, int, getsockname,
  194. (SOCKET, struct sockaddr FAR *, int FAR *));
  195. DECL_WINDOWS_FUNCTION(static, int, recv, (SOCKET, char FAR *, int, int));
  196. DECL_WINDOWS_FUNCTION(static, int, WSAIoctl,
  197. (SOCKET, DWORD, LPVOID, DWORD, LPVOID, DWORD,
  198. LPDWORD, LPWSAOVERLAPPED,
  199. LPWSAOVERLAPPED_COMPLETION_ROUTINE));
  200. #ifndef NO_IPV6
  201. DECL_WINDOWS_FUNCTION(static, int, getaddrinfo,
  202. (const char *nodename, const char *servname,
  203. const struct addrinfo *hints, struct addrinfo **res));
  204. DECL_WINDOWS_FUNCTION(static, void, freeaddrinfo, (struct addrinfo *res));
  205. DECL_WINDOWS_FUNCTION(static, int, getnameinfo,
  206. (const struct sockaddr FAR *sa, socklen_t salen,
  207. char FAR *host, DWORD hostlen, char FAR *serv,
  208. DWORD servlen, int flags));
  209. DECL_WINDOWS_FUNCTION(static, int, WSAAddressToStringA,
  210. (LPSOCKADDR, DWORD, LPWSAPROTOCOL_INFO,
  211. LPSTR, LPDWORD));
  212. #endif
  213. static HMODULE winsock_module = NULL;
  214. static WSADATA wsadata;
  215. #ifndef NO_IPV6
  216. static HMODULE winsock2_module = NULL;
  217. static HMODULE wship6_module = NULL;
  218. #endif
  219. static bool sk_startup(int hi, int lo)
  220. {
  221. WORD winsock_ver;
  222. winsock_ver = MAKEWORD(hi, lo);
  223. if (p_WSAStartup(winsock_ver, &wsadata)) {
  224. return false;
  225. }
  226. if (LOBYTE(wsadata.wVersion) != LOBYTE(winsock_ver)) {
  227. return false;
  228. }
  229. return true;
  230. }
  231. DEF_WINDOWS_FUNCTION(WSAAsyncSelect);
  232. DEF_WINDOWS_FUNCTION(WSAEventSelect);
  233. DEF_WINDOWS_FUNCTION(WSAGetLastError);
  234. DEF_WINDOWS_FUNCTION(WSAEnumNetworkEvents);
  235. DEF_WINDOWS_FUNCTION(select);
  236. void sk_init(void)
  237. {
  238. #ifndef NO_IPV6
  239. winsock2_module =
  240. #endif
  241. winsock_module = load_system32_dll("ws2_32.dll");
  242. if (!winsock_module) {
  243. winsock_module = load_system32_dll("wsock32.dll");
  244. }
  245. if (!winsock_module)
  246. modalfatalbox("Unable to load any WinSock library");
  247. #ifndef NO_IPV6
  248. /* Check if we have getaddrinfo in Winsock */
  249. if (GetProcAddress(winsock_module, "getaddrinfo") != NULL) {
  250. GET_WINDOWS_FUNCTION(winsock_module, getaddrinfo);
  251. GET_WINDOWS_FUNCTION(winsock_module, freeaddrinfo);
  252. GET_WINDOWS_FUNCTION_NO_TYPECHECK(winsock_module, getnameinfo);
  253. /* This function would fail its type-check if we did one,
  254. * because the VS header file provides an inline definition
  255. * which is __cdecl instead of WINAPI. */
  256. } else {
  257. /* Fall back to wship6.dll for Windows 2000 */
  258. wship6_module = load_system32_dll("wship6.dll");
  259. if (wship6_module) {
  260. GET_WINDOWS_FUNCTION(wship6_module, getaddrinfo);
  261. GET_WINDOWS_FUNCTION(wship6_module, freeaddrinfo);
  262. /* See comment above about type check */
  263. GET_WINDOWS_FUNCTION_NO_TYPECHECK(wship6_module, getnameinfo);
  264. } else {
  265. }
  266. }
  267. GET_WINDOWS_FUNCTION(winsock2_module, WSAAddressToStringA);
  268. #endif
  269. GET_WINDOWS_FUNCTION(winsock_module, WSAAsyncSelect);
  270. GET_WINDOWS_FUNCTION(winsock_module, WSAEventSelect);
  271. /* We don't type-check select because at least some MinGW versions
  272. * of the Windows API headers seem to disagree with the
  273. * documentation on whether the 'struct timeval *' pointer is
  274. * const or not. */
  275. GET_WINDOWS_FUNCTION_NO_TYPECHECK(winsock_module, select);
  276. GET_WINDOWS_FUNCTION(winsock_module, WSAGetLastError);
  277. GET_WINDOWS_FUNCTION(winsock_module, WSAEnumNetworkEvents);
  278. GET_WINDOWS_FUNCTION(winsock_module, WSAStartup);
  279. GET_WINDOWS_FUNCTION(winsock_module, WSACleanup);
  280. GET_WINDOWS_FUNCTION(winsock_module, closesocket);
  281. /* Winelib maps ntohl and friends to things like
  282. * __wine_ulong_swap, which fail these type checks hopelessly */
  283. GET_WINDOWS_FUNCTION_NO_TYPECHECK(winsock_module, ntohl);
  284. GET_WINDOWS_FUNCTION_NO_TYPECHECK(winsock_module, htonl);
  285. GET_WINDOWS_FUNCTION_NO_TYPECHECK(winsock_module, htons);
  286. GET_WINDOWS_FUNCTION_NO_TYPECHECK(winsock_module, ntohs);
  287. GET_WINDOWS_FUNCTION_NO_TYPECHECK(winsock_module, gethostname);
  288. GET_WINDOWS_FUNCTION(winsock_module, gethostbyname);
  289. GET_WINDOWS_FUNCTION(winsock_module, getservbyname);
  290. GET_WINDOWS_FUNCTION(winsock_module, inet_addr);
  291. GET_WINDOWS_FUNCTION(winsock_module, inet_ntoa);
  292. /* Older Visual Studio, and MinGW as of Ubuntu 16.04, don't know
  293. * about this function at all, so can't type-check it. Also there
  294. * seems to be some disagreement in the VS headers about whether
  295. * the second argument is void * or const void *, so I omit the
  296. * type check. */
  297. GET_WINDOWS_FUNCTION_NO_TYPECHECK(winsock_module, inet_ntop);
  298. GET_WINDOWS_FUNCTION(winsock_module, connect);
  299. GET_WINDOWS_FUNCTION(winsock_module, bind);
  300. GET_WINDOWS_FUNCTION(winsock_module, setsockopt);
  301. GET_WINDOWS_FUNCTION(winsock_module, socket);
  302. GET_WINDOWS_FUNCTION(winsock_module, listen);
  303. GET_WINDOWS_FUNCTION(winsock_module, send);
  304. GET_WINDOWS_FUNCTION(winsock_module, shutdown);
  305. GET_WINDOWS_FUNCTION(winsock_module, ioctlsocket);
  306. GET_WINDOWS_FUNCTION(winsock_module, accept);
  307. GET_WINDOWS_FUNCTION(winsock_module, getpeername);
  308. GET_WINDOWS_FUNCTION(winsock_module, getsockname);
  309. GET_WINDOWS_FUNCTION(winsock_module, recv);
  310. GET_WINDOWS_FUNCTION(winsock_module, WSAIoctl);
  311. /* Try to get the best WinSock version we can get */
  312. if (!sk_startup(2,2) &&
  313. !sk_startup(2,0) &&
  314. !sk_startup(1,1)) {
  315. modalfatalbox("Unable to initialise WinSock");
  316. }
  317. sktree = newtree234(cmpfortree);
  318. }
  319. void sk_cleanup(void)
  320. {
  321. NetSocket *s;
  322. int i;
  323. if (sktree) {
  324. for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
  325. p_closesocket(s->s);
  326. }
  327. freetree234(sktree);
  328. sktree = NULL;
  329. }
  330. if (p_WSACleanup)
  331. p_WSACleanup();
  332. if (winsock_module)
  333. FreeLibrary(winsock_module);
  334. #ifndef NO_IPV6
  335. if (wship6_module)
  336. FreeLibrary(wship6_module);
  337. #endif
  338. }
  339. const char *winsock_error_string(int error)
  340. {
  341. /*
  342. * Error codes we know about and have historically had reasonably
  343. * sensible error messages for.
  344. */
  345. switch (error) {
  346. case WSAEACCES:
  347. return "Network error: Permission denied";
  348. case WSAEADDRINUSE:
  349. return "Network error: Address already in use";
  350. case WSAEADDRNOTAVAIL:
  351. return "Network error: Cannot assign requested address";
  352. case WSAEAFNOSUPPORT:
  353. return
  354. "Network error: Address family not supported by protocol family";
  355. case WSAEALREADY:
  356. return "Network error: Operation already in progress";
  357. case WSAECONNABORTED:
  358. return "Network error: Software caused connection abort";
  359. case WSAECONNREFUSED:
  360. return "Network error: Connection refused";
  361. case WSAECONNRESET:
  362. return "Network error: Connection reset by peer";
  363. case WSAEDESTADDRREQ:
  364. return "Network error: Destination address required";
  365. case WSAEFAULT:
  366. return "Network error: Bad address";
  367. case WSAEHOSTDOWN:
  368. return "Network error: Host is down";
  369. case WSAEHOSTUNREACH:
  370. return "Network error: No route to host";
  371. case WSAEINPROGRESS:
  372. return "Network error: Operation now in progress";
  373. case WSAEINTR:
  374. return "Network error: Interrupted function call";
  375. case WSAEINVAL:
  376. return "Network error: Invalid argument";
  377. case WSAEISCONN:
  378. return "Network error: Socket is already connected";
  379. case WSAEMFILE:
  380. return "Network error: Too many open files";
  381. case WSAEMSGSIZE:
  382. return "Network error: Message too long";
  383. case WSAENETDOWN:
  384. return "Network error: Network is down";
  385. case WSAENETRESET:
  386. return "Network error: Network dropped connection on reset";
  387. case WSAENETUNREACH:
  388. return "Network error: Network is unreachable";
  389. case WSAENOBUFS:
  390. return "Network error: No buffer space available";
  391. case WSAENOPROTOOPT:
  392. return "Network error: Bad protocol option";
  393. case WSAENOTCONN:
  394. return "Network error: Socket is not connected";
  395. case WSAENOTSOCK:
  396. return "Network error: Socket operation on non-socket";
  397. case WSAEOPNOTSUPP:
  398. return "Network error: Operation not supported";
  399. case WSAEPFNOSUPPORT:
  400. return "Network error: Protocol family not supported";
  401. case WSAEPROCLIM:
  402. return "Network error: Too many processes";
  403. case WSAEPROTONOSUPPORT:
  404. return "Network error: Protocol not supported";
  405. case WSAEPROTOTYPE:
  406. return "Network error: Protocol wrong type for socket";
  407. case WSAESHUTDOWN:
  408. return "Network error: Cannot send after socket shutdown";
  409. case WSAESOCKTNOSUPPORT:
  410. return "Network error: Socket type not supported";
  411. case WSAETIMEDOUT:
  412. return "Network error: Connection timed out";
  413. case WSAEWOULDBLOCK:
  414. return "Network error: Resource temporarily unavailable";
  415. case WSAEDISCON:
  416. return "Network error: Graceful shutdown in progress";
  417. }
  418. /*
  419. * Handle any other error code by delegating to win_strerror.
  420. */
  421. return win_strerror(error);
  422. }
  423. static inline const char *namelookup_strerror(DWORD err)
  424. {
  425. /* PuTTY has traditionally translated a few of the likely error
  426. * messages into more concise strings than the standard Windows ones */
  427. return (err == WSAENETDOWN ? "Network is down" :
  428. err == WSAHOST_NOT_FOUND ? "Host does not exist" :
  429. err == WSATRY_AGAIN ? "Host not found" :
  430. win_strerror(err));
  431. }
  432. SockAddr *sk_namelookup(const char *host, char **canonicalname,
  433. int address_family)
  434. {
  435. *canonicalname = NULL;
  436. SockAddr *addr = snew(SockAddr);
  437. memset(addr, 0, sizeof(SockAddr));
  438. addr->superfamily = UNRESOLVED;
  439. addr->refcount = 1;
  440. #ifndef NO_IPV6
  441. /*
  442. * Use getaddrinfo, as long as it's available. This should handle
  443. * both IPv4 and IPv6 address literals, and hostnames, in one
  444. * unified API.
  445. */
  446. if (p_getaddrinfo) {
  447. struct addrinfo hints;
  448. memset(&hints, 0, sizeof(hints));
  449. hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
  450. address_family == ADDRTYPE_IPV6 ? AF_INET6 :
  451. AF_UNSPEC);
  452. hints.ai_flags = AI_CANONNAME;
  453. hints.ai_socktype = SOCK_STREAM;
  454. /* strip [] on IPv6 address literals */
  455. char *trimmed_host = host_strduptrim(host);
  456. int err = p_getaddrinfo(trimmed_host, NULL, &hints, &addr->ais);
  457. sfree(trimmed_host);
  458. if (addr->ais) {
  459. addr->superfamily = IP;
  460. if (addr->ais->ai_canonname)
  461. *canonicalname = dupstr(addr->ais->ai_canonname);
  462. else
  463. *canonicalname = dupstr(host);
  464. } else {
  465. addr->error = namelookup_strerror(err);
  466. }
  467. return addr;
  468. }
  469. #endif
  470. /*
  471. * Failing that (if IPv6 support was not compiled in, or if
  472. * getaddrinfo turned out to be unavailable at run time), try the
  473. * old-fashioned approach, which is to start by manually checking
  474. * for an IPv4 literal and then use gethostbyname.
  475. */
  476. unsigned long a = p_inet_addr(host);
  477. if (a != (unsigned long) INADDR_NONE) {
  478. addr->addresses = snew(unsigned long);
  479. addr->naddresses = 1;
  480. addr->addresses[0] = p_ntohl(a);
  481. addr->superfamily = IP;
  482. *canonicalname = dupstr(host);
  483. return addr;
  484. }
  485. struct hostent *h = p_gethostbyname(host);
  486. if (h) {
  487. addr->superfamily = IP;
  488. size_t n;
  489. for (n = 0; h->h_addr_list[n]; n++);
  490. addr->addresses = snewn(n, unsigned long);
  491. addr->naddresses = n;
  492. for (n = 0; n < addr->naddresses; n++) {
  493. uint32_t a;
  494. memcpy(&a, h->h_addr_list[n], sizeof(a));
  495. addr->addresses[n] = p_ntohl(a);
  496. }
  497. *canonicalname = dupstr(h->h_name);
  498. } else {
  499. DWORD err = p_WSAGetLastError();
  500. addr->error = namelookup_strerror(err);
  501. }
  502. return addr;
  503. }
  504. static SockAddr *sk_special_addr(SuperFamily superfamily, const char *name)
  505. {
  506. SockAddr *addr = snew(SockAddr);
  507. addr->error = NULL;
  508. addr->superfamily = superfamily;
  509. #ifndef NO_IPV6
  510. addr->ais = NULL;
  511. #endif
  512. addr->addresses = NULL;
  513. addr->naddresses = 0;
  514. addr->refcount = 1;
  515. strncpy(addr->hostname, name, lenof(addr->hostname));
  516. addr->hostname[lenof(addr->hostname)-1] = '\0';
  517. return addr;
  518. }
  519. SockAddr *sk_nonamelookup(const char *host)
  520. {
  521. return sk_special_addr(UNRESOLVED, host);
  522. }
  523. SockAddr *sk_namedpipe_addr(const char *pipename)
  524. {
  525. return sk_special_addr(NAMEDPIPE, pipename);
  526. }
  527. #if HAVE_AFUNIX_H
  528. SockAddr *sk_unix_addr(const char *sockpath)
  529. {
  530. return sk_special_addr(UNIX, sockpath);
  531. }
  532. #endif
  533. static bool sk_nextaddr(SockAddr *addr, SockAddrStep *step)
  534. {
  535. #ifndef NO_IPV6
  536. if (step->ai) {
  537. if (step->ai->ai_next) {
  538. step->ai = step->ai->ai_next;
  539. return true;
  540. } else
  541. return false;
  542. }
  543. #endif
  544. if (step->curraddr+1 < addr->naddresses) {
  545. step->curraddr++;
  546. return true;
  547. } else {
  548. return false;
  549. }
  550. }
  551. void sk_getaddr(SockAddr *addr, char *buf, int buflen)
  552. {
  553. SockAddrStep step;
  554. START_STEP(addr, step);
  555. #ifndef NO_IPV6
  556. if (step.ai) {
  557. int err = 0;
  558. if (p_WSAAddressToStringA) {
  559. DWORD dwbuflen = buflen;
  560. err = p_WSAAddressToStringA(step.ai->ai_addr, step.ai->ai_addrlen,
  561. NULL, buf, &dwbuflen);
  562. } else
  563. err = -1;
  564. if (err) {
  565. strncpy(buf, addr->hostname, buflen);
  566. if (!buf[0])
  567. strncpy(buf, "<unknown>", buflen);
  568. buf[buflen-1] = '\0';
  569. }
  570. } else
  571. #endif
  572. if (sockaddr_family(addr, step) == AF_INET) {
  573. struct in_addr a;
  574. assert(addr->addresses && step.curraddr < addr->naddresses);
  575. a.s_addr = p_htonl(addr->addresses[step.curraddr]);
  576. strncpy(buf, p_inet_ntoa(a), buflen);
  577. buf[buflen-1] = '\0';
  578. } else {
  579. strncpy(buf, addr->hostname, buflen);
  580. buf[buflen-1] = '\0';
  581. }
  582. }
  583. /*
  584. * This constructs a SockAddr that points at one specific sub-address
  585. * of a parent SockAddr. The returned SockAddr does not own all its
  586. * own memory: it points into the old one's data structures, so it
  587. * MUST NOT be used after the old one is freed, and it MUST NOT be
  588. * passed to sk_addr_free. (The latter is why it's returned by value
  589. * rather than dynamically allocated - that should clue in anyone
  590. * writing a call to it that something is weird about it.)
  591. */
  592. static SockAddr sk_extractaddr_tmp(
  593. SockAddr *addr, const SockAddrStep *step)
  594. {
  595. SockAddr toret;
  596. toret = *addr; /* structure copy */
  597. toret.refcount = 1;
  598. #ifndef NO_IPV6
  599. toret.ais = step->ai;
  600. #endif
  601. if (sockaddr_family(addr, *step) == AF_INET
  602. #ifndef NO_IPV6
  603. && !toret.ais
  604. #endif
  605. )
  606. toret.addresses += step->curraddr;
  607. return toret;
  608. }
  609. bool sk_addr_needs_port(SockAddr *addr)
  610. {
  611. return addr->superfamily != NAMEDPIPE
  612. #if HAVE_AFUNIX_H
  613. && addr->superfamily != UNIX
  614. #endif
  615. ;
  616. }
  617. bool sk_hostname_is_local(const char *name)
  618. {
  619. return !strcmp(name, "localhost") ||
  620. !strcmp(name, "::1") ||
  621. !strncmp(name, "127.", 4);
  622. }
  623. static INTERFACE_INFO local_interfaces[16];
  624. static int n_local_interfaces; /* 0=not yet, -1=failed, >0=number */
  625. static bool ipv4_is_local_addr(struct in_addr addr)
  626. {
  627. if (ipv4_is_loopback(addr))
  628. return true; /* loopback addresses are local */
  629. if (!n_local_interfaces) {
  630. SOCKET s = p_socket(AF_INET, SOCK_DGRAM, 0);
  631. DWORD retbytes;
  632. SetHandleInformation((HANDLE)s, HANDLE_FLAG_INHERIT, 0);
  633. if (p_WSAIoctl &&
  634. p_WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0,
  635. local_interfaces, sizeof(local_interfaces),
  636. &retbytes, NULL, NULL) == 0)
  637. n_local_interfaces = retbytes / sizeof(INTERFACE_INFO);
  638. else
  639. n_local_interfaces = -1;
  640. }
  641. if (n_local_interfaces > 0) {
  642. int i;
  643. for (i = 0; i < n_local_interfaces; i++) {
  644. SOCKADDR_IN *address =
  645. (SOCKADDR_IN *)&local_interfaces[i].iiAddress;
  646. if (address->sin_addr.s_addr == addr.s_addr)
  647. return true; /* this address is local */
  648. }
  649. }
  650. return false; /* this address is not local */
  651. }
  652. bool sk_address_is_local(SockAddr *addr)
  653. {
  654. SockAddrStep step;
  655. int family;
  656. START_STEP(addr, step);
  657. family = sockaddr_family(addr, step);
  658. #ifndef NO_IPV6
  659. if (family == AF_INET6) {
  660. return IN6_IS_ADDR_LOOPBACK(&((const struct sockaddr_in6 *)step.ai->ai_addr)->sin6_addr);
  661. } else
  662. #endif
  663. if (family == AF_INET) {
  664. #ifndef NO_IPV6
  665. if (step.ai) {
  666. return ipv4_is_local_addr(((struct sockaddr_in *)step.ai->ai_addr)
  667. ->sin_addr);
  668. } else
  669. #endif
  670. {
  671. struct in_addr a;
  672. assert(addr->addresses && step.curraddr < addr->naddresses);
  673. a.s_addr = p_htonl(addr->addresses[step.curraddr]);
  674. return ipv4_is_local_addr(a);
  675. }
  676. } else {
  677. assert(family == AF_UNSPEC);
  678. return false; /* we don't know; assume not */
  679. }
  680. }
  681. bool sk_address_is_special_local(SockAddr *addr)
  682. {
  683. return false; /* no Unix-domain socket analogue here */
  684. }
  685. int sk_addrtype(SockAddr *addr)
  686. {
  687. SockAddrStep step;
  688. int family;
  689. START_STEP(addr, step);
  690. family = sockaddr_family(addr, step);
  691. return (family == AF_INET ? ADDRTYPE_IPV4 :
  692. #ifndef NO_IPV6
  693. family == AF_INET6 ? ADDRTYPE_IPV6 :
  694. #endif
  695. ADDRTYPE_NAME);
  696. }
  697. void sk_addrcopy(SockAddr *addr, char *buf)
  698. {
  699. SockAddrStep step;
  700. int family;
  701. START_STEP(addr, step);
  702. family = sockaddr_family(addr, step);
  703. assert(family != AF_UNSPEC);
  704. #ifndef NO_IPV6
  705. if (step.ai) {
  706. if (family == AF_INET)
  707. memcpy(buf, &((struct sockaddr_in *)step.ai->ai_addr)->sin_addr,
  708. sizeof(struct in_addr));
  709. else if (family == AF_INET6)
  710. memcpy(buf, &((struct sockaddr_in6 *)step.ai->ai_addr)->sin6_addr,
  711. sizeof(struct in6_addr));
  712. else
  713. unreachable("bad address family in sk_addrcopy");
  714. } else
  715. #endif
  716. if (family == AF_INET) {
  717. struct in_addr a;
  718. assert(addr->addresses && step.curraddr < addr->naddresses);
  719. a.s_addr = p_htonl(addr->addresses[step.curraddr]);
  720. memcpy(buf, (char*) &a.s_addr, 4);
  721. }
  722. }
  723. void sk_addr_free(SockAddr *addr)
  724. {
  725. if (--addr->refcount > 0)
  726. return;
  727. #ifndef NO_IPV6
  728. if (addr->ais && p_freeaddrinfo)
  729. p_freeaddrinfo(addr->ais);
  730. #endif
  731. if (addr->addresses)
  732. sfree(addr->addresses);
  733. sfree(addr);
  734. }
  735. SockAddr *sk_addr_dup(SockAddr *addr)
  736. {
  737. addr->refcount++;
  738. return addr;
  739. }
  740. static Plug *sk_net_plug(Socket *sock, Plug *p)
  741. {
  742. NetSocket *s = container_of(sock, NetSocket, sock);
  743. Plug *ret = s->plug;
  744. if (p)
  745. s->plug = p;
  746. return ret;
  747. }
  748. static void sk_net_close(Socket *s);
  749. static size_t sk_net_write(Socket *s, const void *data, size_t len);
  750. static size_t sk_net_write_oob(Socket *s, const void *data, size_t len);
  751. static void sk_net_write_eof(Socket *s);
  752. static void sk_net_set_frozen(Socket *s, bool is_frozen);
  753. static const char *sk_net_socket_error(Socket *s);
  754. static SocketEndpointInfo *sk_net_endpoint_info(Socket *s, bool peer);
  755. static const SocketVtable NetSocket_sockvt = {
  756. .plug = sk_net_plug,
  757. .close = sk_net_close,
  758. .write = sk_net_write,
  759. .write_oob = sk_net_write_oob,
  760. .write_eof = sk_net_write_eof,
  761. .set_frozen = sk_net_set_frozen,
  762. .socket_error = sk_net_socket_error,
  763. .endpoint_info = sk_net_endpoint_info,
  764. };
  765. static Socket *sk_net_accept(accept_ctx_t ctx, Plug *plug)
  766. {
  767. DWORD err;
  768. const char *errstr;
  769. NetSocket *s;
  770. /*
  771. * Create NetSocket structure.
  772. */
  773. s = snew(NetSocket);
  774. s->sock.vt = &NetSocket_sockvt;
  775. s->error = NULL;
  776. s->plug = plug;
  777. bufchain_init(&s->output_data);
  778. s->writable = true; /* to start with */
  779. s->sending_oob = 0;
  780. s->outgoingeof = EOF_NO;
  781. s->frozen = true;
  782. s->frozen_readable = false;
  783. s->localhost_only = false; /* unused, but best init anyway */
  784. s->pending_error = 0;
  785. s->parent = s->child = NULL;
  786. s->addr = NULL;
  787. s->s = (SOCKET)ctx.p;
  788. if (s->s == INVALID_SOCKET) {
  789. err = p_WSAGetLastError();
  790. s->error = winsock_error_string(err);
  791. return &s->sock;
  792. }
  793. s->oobinline = false;
  794. /* Set up a select mechanism. This could be an AsyncSelect on a
  795. * window, or an EventSelect on an event object. */
  796. errstr = do_select(s->s, true);
  797. if (errstr) {
  798. s->error = errstr;
  799. return &s->sock;
  800. }
  801. add234(sktree, s);
  802. return &s->sock;
  803. }
  804. static DWORD try_connect(NetSocket *sock)
  805. {
  806. SOCKET s;
  807. #ifndef NO_IPV6
  808. SOCKADDR_IN6 a6;
  809. #endif
  810. SOCKADDR_IN a;
  811. DWORD err;
  812. const char *errstr;
  813. short localport;
  814. int family;
  815. if (sock->s != INVALID_SOCKET) {
  816. do_select(sock->s, false);
  817. p_closesocket(sock->s);
  818. }
  819. {
  820. SockAddr thisaddr = sk_extractaddr_tmp(
  821. sock->addr, &sock->step);
  822. plug_log(sock->plug, &sock->sock, PLUGLOG_CONNECT_TRYING,
  823. &thisaddr, sock->port, NULL, 0);
  824. }
  825. /*
  826. * Open socket.
  827. */
  828. family = sockaddr_family(sock->addr, sock->step);
  829. /*
  830. * Remove the socket from the tree before we overwrite its
  831. * internal socket id, because that forms part of the tree's
  832. * sorting criterion. We'll add it back before exiting this
  833. * function, whether we changed anything or not.
  834. */
  835. del234(sktree, sock);
  836. s = p_socket(family, SOCK_STREAM, 0);
  837. sock->s = s;
  838. if (s == INVALID_SOCKET) {
  839. err = p_WSAGetLastError();
  840. sock->error = winsock_error_string(err);
  841. goto ret;
  842. }
  843. SetHandleInformation((HANDLE)s, HANDLE_FLAG_INHERIT, 0);
  844. if (sock->oobinline) {
  845. BOOL b = true;
  846. p_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
  847. }
  848. if (sock->nodelay) {
  849. BOOL b = true;
  850. p_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
  851. }
  852. if (sock->keepalive) {
  853. BOOL b = true;
  854. p_setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));
  855. }
  856. /*
  857. * Bind to local address.
  858. */
  859. if (sock->privport)
  860. localport = 1023; /* count from 1023 downwards */
  861. else
  862. localport = 0; /* just use port 0 (ie winsock picks) */
  863. /* Loop round trying to bind */
  864. while (1) {
  865. int sockcode;
  866. #ifndef NO_IPV6
  867. if (family == AF_INET6) {
  868. memset(&a6, 0, sizeof(a6));
  869. a6.sin6_family = AF_INET6;
  870. /*a6.sin6_addr = in6addr_any; */ /* == 0 done by memset() */
  871. a6.sin6_port = p_htons(localport);
  872. } else
  873. #endif
  874. {
  875. a.sin_family = AF_INET;
  876. a.sin_addr.s_addr = p_htonl(INADDR_ANY);
  877. a.sin_port = p_htons(localport);
  878. }
  879. #ifndef NO_IPV6
  880. sockcode = p_bind(s, (family == AF_INET6 ?
  881. (struct sockaddr *) &a6 :
  882. (struct sockaddr *) &a),
  883. (family == AF_INET6 ? sizeof(a6) : sizeof(a)));
  884. #else
  885. sockcode = p_bind(s, (struct sockaddr *) &a, sizeof(a));
  886. #endif
  887. if (sockcode != SOCKET_ERROR) {
  888. err = 0;
  889. break; /* done */
  890. } else {
  891. err = p_WSAGetLastError();
  892. if (err != WSAEADDRINUSE) /* failed, for a bad reason */
  893. break;
  894. }
  895. if (localport == 0)
  896. break; /* we're only looping once */
  897. localport--;
  898. if (localport == 0)
  899. break; /* we might have got to the end */
  900. }
  901. if (err) {
  902. sock->error = winsock_error_string(err);
  903. goto ret;
  904. }
  905. /*
  906. * Connect to remote address.
  907. */
  908. #ifndef NO_IPV6
  909. if (sock->step.ai) {
  910. if (family == AF_INET6) {
  911. a6.sin6_family = AF_INET6;
  912. a6.sin6_port = p_htons((short) sock->port);
  913. a6.sin6_addr =
  914. ((struct sockaddr_in6 *) sock->step.ai->ai_addr)->sin6_addr;
  915. a6.sin6_flowinfo = ((struct sockaddr_in6 *) sock->step.ai->ai_addr)->sin6_flowinfo;
  916. a6.sin6_scope_id = ((struct sockaddr_in6 *) sock->step.ai->ai_addr)->sin6_scope_id;
  917. } else {
  918. a.sin_family = AF_INET;
  919. a.sin_addr =
  920. ((struct sockaddr_in *) sock->step.ai->ai_addr)->sin_addr;
  921. a.sin_port = p_htons((short) sock->port);
  922. }
  923. } else
  924. #endif
  925. {
  926. assert(sock->addr->addresses && sock->step.curraddr < sock->addr->naddresses);
  927. a.sin_family = AF_INET;
  928. a.sin_addr.s_addr = p_htonl(sock->addr->addresses[sock->step.curraddr]);
  929. a.sin_port = p_htons((short) sock->port);
  930. }
  931. /* Set up a select mechanism. This could be an AsyncSelect on a
  932. * window, or an EventSelect on an event object. */
  933. errstr = do_select(s, true);
  934. if (errstr) {
  935. sock->error = errstr;
  936. err = 1;
  937. goto ret;
  938. }
  939. if ((
  940. #ifndef NO_IPV6
  941. p_connect(s,
  942. ((family == AF_INET6) ? (struct sockaddr *) &a6 :
  943. (struct sockaddr *) &a),
  944. (family == AF_INET6) ? sizeof(a6) : sizeof(a))
  945. #else
  946. p_connect(s, (struct sockaddr *) &a, sizeof(a))
  947. #endif
  948. ) == SOCKET_ERROR) {
  949. err = p_WSAGetLastError();
  950. /*
  951. * We expect a potential EWOULDBLOCK here, because the
  952. * chances are the front end has done a select for
  953. * FD_CONNECT, so that connect() will complete
  954. * asynchronously.
  955. */
  956. if ( err != WSAEWOULDBLOCK ) {
  957. sock->error = winsock_error_string(err);
  958. goto ret;
  959. }
  960. } else {
  961. /*
  962. * If we _don't_ get EWOULDBLOCK, the connect has completed
  963. * and we should set the socket as writable.
  964. */
  965. sock->writable = true;
  966. SockAddr thisaddr = sk_extractaddr_tmp(sock->addr, &sock->step);
  967. plug_log(sock->plug, &sock->sock, PLUGLOG_CONNECT_SUCCESS,
  968. &thisaddr, sock->port, NULL, 0);
  969. }
  970. err = 0;
  971. ret:
  972. /*
  973. * No matter what happened, put the socket back in the tree.
  974. */
  975. add234(sktree, sock);
  976. if (err) {
  977. SockAddr thisaddr = sk_extractaddr_tmp(
  978. sock->addr, &sock->step);
  979. plug_log(sock->plug, &sock->sock, PLUGLOG_CONNECT_FAILED,
  980. &thisaddr, sock->port, sock->error, err);
  981. }
  982. return err;
  983. }
  984. Socket *sk_new(SockAddr *addr, int port, bool privport, bool oobinline,
  985. bool nodelay, bool keepalive, Plug *plug)
  986. {
  987. NetSocket *s;
  988. DWORD err;
  989. /*
  990. * Create NetSocket structure.
  991. */
  992. s = snew(NetSocket);
  993. s->sock.vt = &NetSocket_sockvt;
  994. s->error = NULL;
  995. s->plug = plug;
  996. bufchain_init(&s->output_data);
  997. s->connected = false; /* to start with */
  998. s->writable = false; /* to start with */
  999. s->sending_oob = 0;
  1000. s->outgoingeof = EOF_NO;
  1001. s->frozen = false;
  1002. s->frozen_readable = false;
  1003. s->localhost_only = false; /* unused, but best init anyway */
  1004. s->pending_error = 0;
  1005. s->parent = s->child = NULL;
  1006. s->oobinline = oobinline;
  1007. s->nodelay = nodelay;
  1008. s->keepalive = keepalive;
  1009. s->privport = privport;
  1010. s->port = port;
  1011. s->addr = addr;
  1012. START_STEP(s->addr, s->step);
  1013. s->s = INVALID_SOCKET;
  1014. err = 0;
  1015. do {
  1016. err = try_connect(s);
  1017. } while (err && sk_nextaddr(s->addr, &s->step));
  1018. return &s->sock;
  1019. }
  1020. static Socket *sk_newlistener_internal(
  1021. const char *srcaddr, int port, Plug *plug,
  1022. bool local_host_only, int orig_address_family)
  1023. {
  1024. SOCKET sk;
  1025. SOCKADDR_IN a;
  1026. #ifndef NO_IPV6
  1027. SOCKADDR_IN6 a6;
  1028. #endif
  1029. #if HAVE_AFUNIX_H
  1030. SOCKADDR_UN au;
  1031. #endif
  1032. struct sockaddr *bindaddr;
  1033. unsigned bindsize;
  1034. DWORD err;
  1035. const char *errstr;
  1036. NetSocket *s;
  1037. int retcode;
  1038. int address_family = orig_address_family;
  1039. /*
  1040. * Create NetSocket structure.
  1041. */
  1042. s = snew(NetSocket);
  1043. s->sock.vt = &NetSocket_sockvt;
  1044. s->error = NULL;
  1045. s->plug = plug;
  1046. bufchain_init(&s->output_data);
  1047. s->writable = false; /* to start with */
  1048. s->sending_oob = 0;
  1049. s->outgoingeof = EOF_NO;
  1050. s->frozen = false;
  1051. s->frozen_readable = false;
  1052. s->localhost_only = local_host_only;
  1053. s->pending_error = 0;
  1054. s->parent = s->child = NULL;
  1055. s->addr = NULL;
  1056. /*
  1057. * Our default, if passed the `don't care' value
  1058. * ADDRTYPE_UNSPEC, is to listen on IPv4. If IPv6 is supported,
  1059. * we will also set up a second socket listening on IPv6, but
  1060. * the v4 one is primary since that ought to work even on
  1061. * non-v6-supporting systems.
  1062. */
  1063. if (address_family == AF_UNSPEC) address_family = AF_INET;
  1064. /*
  1065. * Open socket.
  1066. */
  1067. sk = p_socket(address_family, SOCK_STREAM, 0);
  1068. s->s = sk;
  1069. if (sk == INVALID_SOCKET) {
  1070. err = p_WSAGetLastError();
  1071. s->error = winsock_error_string(err);
  1072. return &s->sock;
  1073. }
  1074. SetHandleInformation((HANDLE)sk, HANDLE_FLAG_INHERIT, 0);
  1075. s->oobinline = false;
  1076. #if HAVE_AFUNIX_H
  1077. if (address_family != AF_UNIX)
  1078. #endif
  1079. {
  1080. BOOL on = true;
  1081. p_setsockopt(sk, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
  1082. (const char *)&on, sizeof(on));
  1083. }
  1084. switch (address_family) {
  1085. #ifndef NO_IPV6
  1086. case AF_INET6: {
  1087. memset(&a6, 0, sizeof(a6));
  1088. a6.sin6_family = AF_INET6;
  1089. if (local_host_only)
  1090. a6.sin6_addr = in6addr_loopback;
  1091. else
  1092. a6.sin6_addr = in6addr_any;
  1093. if (srcaddr != NULL && p_getaddrinfo) {
  1094. struct addrinfo hints;
  1095. struct addrinfo *ai;
  1096. int err;
  1097. memset(&hints, 0, sizeof(hints));
  1098. hints.ai_family = AF_INET6;
  1099. hints.ai_flags = 0;
  1100. {
  1101. /* strip [] on IPv6 address literals */
  1102. char *trimmed_addr = host_strduptrim(srcaddr);
  1103. err = p_getaddrinfo(trimmed_addr, NULL, &hints, &ai);
  1104. sfree(trimmed_addr);
  1105. }
  1106. if (err == 0 && ai->ai_family == AF_INET6) {
  1107. a6.sin6_addr =
  1108. ((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
  1109. }
  1110. }
  1111. a6.sin6_port = p_htons(port);
  1112. bindaddr = (struct sockaddr *)&a6;
  1113. bindsize = sizeof(a6);
  1114. break;
  1115. }
  1116. #endif
  1117. case AF_INET: {
  1118. bool got_addr = false;
  1119. a.sin_family = AF_INET;
  1120. /*
  1121. * Bind to source address. First try an explicitly
  1122. * specified one...
  1123. */
  1124. if (srcaddr) {
  1125. a.sin_addr.s_addr = p_inet_addr(srcaddr);
  1126. if (a.sin_addr.s_addr != INADDR_NONE) {
  1127. /* Override localhost_only with specified listen addr. */
  1128. s->localhost_only = ipv4_is_loopback(a.sin_addr);
  1129. got_addr = true;
  1130. }
  1131. }
  1132. /*
  1133. * ... and failing that, go with one of the standard ones.
  1134. */
  1135. if (!got_addr) {
  1136. if (local_host_only)
  1137. a.sin_addr.s_addr = p_htonl(INADDR_LOOPBACK);
  1138. else
  1139. a.sin_addr.s_addr = p_htonl(INADDR_ANY);
  1140. }
  1141. a.sin_port = p_htons((short)port);
  1142. bindaddr = (struct sockaddr *)&a;
  1143. bindsize = sizeof(a);
  1144. break;
  1145. }
  1146. #if HAVE_AFUNIX_H
  1147. case AF_UNIX: {
  1148. au.sun_family = AF_UNIX;
  1149. strncpy(au.sun_path, srcaddr, sizeof(au.sun_path));
  1150. bindaddr = (struct sockaddr *)&au;
  1151. bindsize = sizeof(au);
  1152. break;
  1153. }
  1154. #endif
  1155. default:
  1156. unreachable("bad address family in sk_newlistener_internal");
  1157. }
  1158. retcode = p_bind(sk, bindaddr, bindsize);
  1159. if (retcode != SOCKET_ERROR) {
  1160. err = 0;
  1161. } else {
  1162. err = p_WSAGetLastError();
  1163. }
  1164. if (err) {
  1165. p_closesocket(sk);
  1166. s->error = winsock_error_string(err);
  1167. return &s->sock;
  1168. }
  1169. if (p_listen(sk, SOMAXCONN) == SOCKET_ERROR) {
  1170. p_closesocket(sk);
  1171. s->error = winsock_error_string(p_WSAGetLastError());
  1172. return &s->sock;
  1173. }
  1174. /* Set up a select mechanism. This could be an AsyncSelect on a
  1175. * window, or an EventSelect on an event object. */
  1176. errstr = do_select(sk, true);
  1177. if (errstr) {
  1178. p_closesocket(sk);
  1179. s->error = errstr;
  1180. return &s->sock;
  1181. }
  1182. add234(sktree, s);
  1183. #ifndef NO_IPV6
  1184. /*
  1185. * If we were given ADDRTYPE_UNSPEC, we must also create an
  1186. * IPv6 listening socket and link it to this one.
  1187. */
  1188. if (address_family == AF_INET && orig_address_family == AF_UNSPEC) {
  1189. Socket *other = sk_newlistener_internal(srcaddr, port, plug,
  1190. local_host_only, AF_INET6);
  1191. if (other) {
  1192. NetSocket *ns = container_of(other, NetSocket, sock);
  1193. if (!ns->error) {
  1194. ns->parent = s;
  1195. s->child = ns;
  1196. } else {
  1197. sfree(ns);
  1198. }
  1199. }
  1200. }
  1201. #endif
  1202. return &s->sock;
  1203. }
  1204. Socket *sk_newlistener(const char *srcaddr, int port, Plug *plug,
  1205. bool local_host_only, int orig_address_family)
  1206. {
  1207. /*
  1208. * Translate address_family from platform-independent constants
  1209. * into local reality.
  1210. */
  1211. int address_family = (orig_address_family == ADDRTYPE_IPV4 ? AF_INET :
  1212. #ifndef NO_IPV6
  1213. orig_address_family == ADDRTYPE_IPV6 ? AF_INET6 :
  1214. #endif
  1215. AF_UNSPEC);
  1216. return sk_newlistener_internal(srcaddr, port, plug, local_host_only,
  1217. address_family);
  1218. }
  1219. Socket *sk_newlistener_unix(const char *path, Plug *plug)
  1220. {
  1221. #if HAVE_AFUNIX_H
  1222. return sk_newlistener_internal(path, 0, plug, false, AF_UNIX);
  1223. #else
  1224. return new_error_socket_fmt(
  1225. plug, "AF_UNIX support not compiled into this program");
  1226. #endif
  1227. }
  1228. static void sk_net_close(Socket *sock)
  1229. {
  1230. NetSocket *s = container_of(sock, NetSocket, sock);
  1231. if (s->child)
  1232. sk_net_close(&s->child->sock);
  1233. bufchain_clear(&s->output_data);
  1234. del234(sktree, s);
  1235. do_select(s->s, false);
  1236. p_closesocket(s->s);
  1237. if (s->addr)
  1238. sk_addr_free(s->addr);
  1239. delete_callbacks_for_context(s);
  1240. sfree(s);
  1241. }
  1242. void plug_closing_system_error(Plug *plug, DWORD error)
  1243. {
  1244. PlugCloseType type = PLUGCLOSE_ERROR;
  1245. if (error == ERROR_BROKEN_PIPE)
  1246. type = PLUGCLOSE_BROKEN_PIPE;
  1247. plug_closing(plug, type, win_strerror(error));
  1248. }
  1249. void plug_closing_winsock_error(Plug *plug, DWORD error)
  1250. {
  1251. plug_closing(plug, PLUGCLOSE_ERROR, winsock_error_string(error));
  1252. }
  1253. /*
  1254. * Deal with socket errors detected in try_send().
  1255. */
  1256. static void socket_error_callback(void *vs)
  1257. {
  1258. NetSocket *s = (NetSocket *)vs;
  1259. /*
  1260. * Just in case other socket work has caused this socket to vanish
  1261. * or become somehow non-erroneous before this callback arrived...
  1262. */
  1263. if (!find234(sktree, s, NULL) || !s->pending_error)
  1264. return;
  1265. /*
  1266. * An error has occurred on this socket. Pass it to the plug.
  1267. */
  1268. plug_closing_winsock_error(s->plug, s->pending_error);
  1269. }
  1270. /*
  1271. * The function which tries to send on a socket once it's deemed
  1272. * writable.
  1273. */
  1274. static void try_send(NetSocket *s)
  1275. {
  1276. while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
  1277. int nsent;
  1278. DWORD err;
  1279. const void *data;
  1280. size_t len;
  1281. int urgentflag;
  1282. if (s->sending_oob) {
  1283. urgentflag = MSG_OOB;
  1284. len = s->sending_oob;
  1285. data = &s->oobdata;
  1286. } else {
  1287. urgentflag = 0;
  1288. ptrlen bufdata = bufchain_prefix(&s->output_data);
  1289. data = bufdata.ptr;
  1290. len = bufdata.len;
  1291. }
  1292. len = min(len, INT_MAX); /* WinSock send() takes an int */
  1293. nsent = p_send(s->s, data, len, urgentflag);
  1294. noise_ultralight(NOISE_SOURCE_IOLEN, nsent);
  1295. if (nsent <= 0) {
  1296. err = (nsent < 0 ? p_WSAGetLastError() : 0);
  1297. if ((err < WSABASEERR && nsent < 0) || err == WSAEWOULDBLOCK) {
  1298. /*
  1299. * Perfectly normal: we've sent all we can for the moment.
  1300. *
  1301. * (Some WinSock send() implementations can return
  1302. * <0 but leave no sensible error indication -
  1303. * WSAGetLastError() is called but returns zero or
  1304. * a small number - so we check that case and treat
  1305. * it just like WSAEWOULDBLOCK.)
  1306. */
  1307. s->writable = false;
  1308. return;
  1309. } else {
  1310. /*
  1311. * If send() returns a socket error, we unfortunately
  1312. * can't just call plug_closing(), because it's quite
  1313. * likely that we're currently _in_ a call from the
  1314. * code we'd be calling back to, so we'd have to make
  1315. * half the SSH code reentrant. Instead we flag a
  1316. * pending error on the socket, to be dealt with (by
  1317. * calling plug_closing()) at some suitable future
  1318. * moment.
  1319. */
  1320. s->pending_error = err;
  1321. queue_toplevel_callback(socket_error_callback, s);
  1322. return;
  1323. }
  1324. } else {
  1325. if (s->sending_oob) {
  1326. if (nsent < len) {
  1327. memmove(s->oobdata, s->oobdata+nsent, len-nsent);
  1328. s->sending_oob = len - nsent;
  1329. } else {
  1330. s->sending_oob = 0;
  1331. }
  1332. } else {
  1333. bufchain_consume(&s->output_data, nsent);
  1334. }
  1335. }
  1336. }
  1337. /*
  1338. * If we reach here, we've finished sending everything we might
  1339. * have needed to send. Send EOF, if we need to.
  1340. */
  1341. if (s->outgoingeof == EOF_PENDING) {
  1342. p_shutdown(s->s, SD_SEND);
  1343. s->outgoingeof = EOF_SENT;
  1344. }
  1345. }
  1346. static size_t sk_net_write(Socket *sock, const void *buf, size_t len)
  1347. {
  1348. NetSocket *s = container_of(sock, NetSocket, sock);
  1349. assert(s->outgoingeof == EOF_NO);
  1350. /*
  1351. * Add the data to the buffer list on the socket.
  1352. */
  1353. bufchain_add(&s->output_data, buf, len);
  1354. /*
  1355. * Now try sending from the start of the buffer list.
  1356. */
  1357. if (s->writable)
  1358. try_send(s);
  1359. return bufchain_size(&s->output_data);
  1360. }
  1361. static size_t sk_net_write_oob(Socket *sock, const void *buf, size_t len)
  1362. {
  1363. NetSocket *s = container_of(sock, NetSocket, sock);
  1364. assert(s->outgoingeof == EOF_NO);
  1365. /*
  1366. * Replace the buffer list on the socket with the data.
  1367. */
  1368. bufchain_clear(&s->output_data);
  1369. assert(len <= sizeof(s->oobdata));
  1370. memcpy(s->oobdata, buf, len);
  1371. s->sending_oob = len;
  1372. /*
  1373. * Now try sending from the start of the buffer list.
  1374. */
  1375. if (s->writable)
  1376. try_send(s);
  1377. return s->sending_oob;
  1378. }
  1379. static void sk_net_write_eof(Socket *sock)
  1380. {
  1381. NetSocket *s = container_of(sock, NetSocket, sock);
  1382. assert(s->outgoingeof == EOF_NO);
  1383. /*
  1384. * Mark the socket as pending outgoing EOF.
  1385. */
  1386. s->outgoingeof = EOF_PENDING;
  1387. /*
  1388. * Now try sending from the start of the buffer list.
  1389. */
  1390. if (s->writable)
  1391. try_send(s);
  1392. }
  1393. void select_result(WPARAM wParam, LPARAM lParam)
  1394. {
  1395. int ret;
  1396. DWORD err;
  1397. char buf[20480]; /* nice big buffer for plenty of speed */
  1398. NetSocket *s;
  1399. bool atmark;
  1400. /* wParam is the socket itself */
  1401. if (wParam == 0)
  1402. return; /* boggle */
  1403. s = find234(sktree, (void *) wParam, cmpforsearch);
  1404. if (!s)
  1405. return; /* boggle */
  1406. if ((err = WSAGETSELECTERROR(lParam)) != 0) {
  1407. /*
  1408. * An error has occurred on this socket. Pass it to the
  1409. * plug.
  1410. */
  1411. if (s->addr) {
  1412. SockAddr thisaddr = sk_extractaddr_tmp(
  1413. s->addr, &s->step);
  1414. plug_log(s->plug, &s->sock, PLUGLOG_CONNECT_FAILED, &thisaddr,
  1415. s->port, winsock_error_string(err), err);
  1416. while (err && s->addr && sk_nextaddr(s->addr, &s->step)) {
  1417. err = try_connect(s);
  1418. }
  1419. }
  1420. if (err != 0)
  1421. plug_closing_winsock_error(s->plug, err);
  1422. return;
  1423. }
  1424. noise_ultralight(NOISE_SOURCE_IOID, wParam);
  1425. switch (WSAGETSELECTEVENT(lParam)) {
  1426. case FD_CONNECT:
  1427. s->connected = true;
  1428. s->writable = true;
  1429. /*
  1430. * Once a socket is connected, we can stop falling back
  1431. * through the candidate addresses to connect to. But first,
  1432. * let the plug know we were successful.
  1433. */
  1434. if (s->addr) {
  1435. SockAddr thisaddr = sk_extractaddr_tmp(
  1436. s->addr, &s->step);
  1437. plug_log(s->plug, &s->sock, PLUGLOG_CONNECT_SUCCESS,
  1438. &thisaddr, s->port, NULL, 0);
  1439. sk_addr_free(s->addr);
  1440. s->addr = NULL;
  1441. }
  1442. break;
  1443. case FD_READ:
  1444. /* In the case the socket is still frozen, we don't even bother */
  1445. if (s->frozen) {
  1446. s->frozen_readable = true;
  1447. break;
  1448. }
  1449. /*
  1450. * We have received data on the socket. For an oobinline
  1451. * socket, this might be data _before_ an urgent pointer,
  1452. * in which case we send it to the back end with type==1
  1453. * (data prior to urgent).
  1454. */
  1455. if (s->oobinline) {
  1456. u_long atmark_from_ioctl = 1;
  1457. p_ioctlsocket(s->s, SIOCATMARK, &atmark_from_ioctl);
  1458. /*
  1459. * Avoid checking the return value from ioctlsocket(),
  1460. * on the grounds that some WinSock wrappers don't
  1461. * support it. If it does nothing, we get atmark==1,
  1462. * which is equivalent to `no OOB pending', so the
  1463. * effect will be to non-OOB-ify any OOB data.
  1464. */
  1465. atmark = atmark_from_ioctl;
  1466. } else
  1467. atmark = true;
  1468. ret = p_recv(s->s, buf, sizeof(buf), 0);
  1469. noise_ultralight(NOISE_SOURCE_IOLEN, ret);
  1470. if (ret < 0) {
  1471. err = p_WSAGetLastError();
  1472. if (err == WSAEWOULDBLOCK) {
  1473. break;
  1474. }
  1475. }
  1476. if (ret < 0) {
  1477. plug_closing_winsock_error(s->plug, err);
  1478. } else if (0 == ret) {
  1479. plug_closing_normal(s->plug);
  1480. } else {
  1481. plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
  1482. }
  1483. break;
  1484. case FD_OOB:
  1485. /*
  1486. * This will only happen on a non-oobinline socket. It
  1487. * indicates that we can immediately perform an OOB read
  1488. * and get back OOB data, which we will send to the back
  1489. * end with type==2 (urgent data).
  1490. */
  1491. ret = p_recv(s->s, buf, sizeof(buf), MSG_OOB);
  1492. noise_ultralight(NOISE_SOURCE_IOLEN, ret);
  1493. if (ret <= 0) {
  1494. int err = p_WSAGetLastError();
  1495. plug_closing_winsock_error(s->plug, err);
  1496. } else {
  1497. plug_receive(s->plug, 2, buf, ret);
  1498. }
  1499. break;
  1500. case FD_WRITE: {
  1501. int bufsize_before, bufsize_after;
  1502. s->writable = true;
  1503. bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
  1504. try_send(s);
  1505. bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
  1506. if (bufsize_after < bufsize_before)
  1507. plug_sent(s->plug, bufsize_after);
  1508. break;
  1509. }
  1510. case FD_CLOSE:
  1511. /* Signal a close on the socket. First read any outstanding data. */
  1512. do {
  1513. ret = p_recv(s->s, buf, sizeof(buf), 0);
  1514. if (ret < 0) {
  1515. err = p_WSAGetLastError();
  1516. if (err == WSAEWOULDBLOCK)
  1517. break;
  1518. plug_closing_winsock_error(s->plug, err);
  1519. } else {
  1520. if (ret)
  1521. plug_receive(s->plug, 0, buf, ret);
  1522. else
  1523. plug_closing_normal(s->plug);
  1524. }
  1525. } while (ret > 0);
  1526. return;
  1527. case FD_ACCEPT: {
  1528. #ifdef NO_IPV6
  1529. struct sockaddr_in isa;
  1530. #else
  1531. struct sockaddr_storage isa; // FIXME: also if Unix and no IPv6
  1532. #endif
  1533. int addrlen = sizeof(isa);
  1534. SOCKET t; /* socket of connection */
  1535. accept_ctx_t actx;
  1536. memset(&isa, 0, sizeof(isa));
  1537. err = 0;
  1538. t = p_accept(s->s,(struct sockaddr *)&isa,&addrlen);
  1539. if (t == INVALID_SOCKET) {
  1540. err = p_WSAGetLastError();
  1541. if (err == WSATRY_AGAIN)
  1542. break;
  1543. }
  1544. actx.p = (void *)t;
  1545. #ifndef NO_IPV6
  1546. if (isa.ss_family == AF_INET &&
  1547. s->localhost_only &&
  1548. !ipv4_is_local_addr(((struct sockaddr_in *)&isa)->sin_addr))
  1549. #else
  1550. if (s->localhost_only && !ipv4_is_local_addr(isa.sin_addr))
  1551. #endif
  1552. {
  1553. p_closesocket(t); /* dodgy WinSock let nonlocal through */
  1554. } else if (plug_accepting(s->plug, sk_net_accept, actx)) {
  1555. p_closesocket(t); /* denied or error */
  1556. }
  1557. break;
  1558. }
  1559. }
  1560. }
  1561. /*
  1562. * Special error values are returned from sk_namelookup and sk_new
  1563. * if there's a problem. These functions extract an error message,
  1564. * or return NULL if there's no problem.
  1565. */
  1566. const char *sk_addr_error(SockAddr *addr)
  1567. {
  1568. return addr->error;
  1569. }
  1570. static const char *sk_net_socket_error(Socket *sock)
  1571. {
  1572. NetSocket *s = container_of(sock, NetSocket, sock);
  1573. return s->error;
  1574. }
  1575. static SocketEndpointInfo *sk_net_endpoint_info(Socket *sock, bool peer)
  1576. {
  1577. NetSocket *s = container_of(sock, NetSocket, sock);
  1578. #ifdef NO_IPV6
  1579. struct sockaddr_in addr;
  1580. #else
  1581. struct sockaddr_storage addr; // FIXME: also if Unix and no IPv6
  1582. char buf[INET6_ADDRSTRLEN];
  1583. #endif
  1584. int addrlen = sizeof(addr);
  1585. SocketEndpointInfo *pi;
  1586. {
  1587. int retd = (peer ?
  1588. p_getpeername(s->s, (struct sockaddr *)&addr, &addrlen) :
  1589. p_getsockname(s->s, (struct sockaddr *)&addr, &addrlen));
  1590. if (retd < 0)
  1591. return NULL;
  1592. }
  1593. pi = snew(SocketEndpointInfo);
  1594. pi->addressfamily = ADDRTYPE_UNSPEC;
  1595. pi->addr_text = NULL;
  1596. pi->port = -1;
  1597. pi->log_text = NULL;
  1598. if (((struct sockaddr *)&addr)->sa_family == AF_INET) {
  1599. pi->addressfamily = ADDRTYPE_IPV4;
  1600. memcpy(pi->addr_bin.ipv4, &((struct sockaddr_in *)&addr)->sin_addr, 4);
  1601. pi->port = p_ntohs(((struct sockaddr_in *)&addr)->sin_port);
  1602. pi->addr_text = dupstr(
  1603. p_inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr));
  1604. pi->log_text = dupprintf("%s:%d", pi->addr_text, pi->port);
  1605. #ifndef NO_IPV6
  1606. } else if (((struct sockaddr *)&addr)->sa_family == AF_INET6) {
  1607. pi->addressfamily = ADDRTYPE_IPV6;
  1608. memcpy(pi->addr_bin.ipv6,
  1609. &((struct sockaddr_in6 *)&addr)->sin6_addr, 16);
  1610. pi->port = p_ntohs(((struct sockaddr_in6 *)&addr)->sin6_port);
  1611. pi->addr_text = dupstr(
  1612. p_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&addr)->sin6_addr,
  1613. buf, sizeof(buf)));
  1614. pi->log_text = dupprintf("[%s]:%d", pi->addr_text, pi->port);
  1615. #endif
  1616. } else {
  1617. sfree(pi);
  1618. return NULL;
  1619. }
  1620. return pi;
  1621. }
  1622. static void sk_net_set_frozen(Socket *sock, bool is_frozen)
  1623. {
  1624. NetSocket *s = container_of(sock, NetSocket, sock);
  1625. if (s->frozen == is_frozen)
  1626. return;
  1627. s->frozen = is_frozen;
  1628. if (!is_frozen) {
  1629. do_select(s->s, true);
  1630. if (s->frozen_readable) {
  1631. char c;
  1632. p_recv(s->s, &c, 1, MSG_PEEK);
  1633. }
  1634. }
  1635. s->frozen_readable = false;
  1636. }
  1637. void socket_reselect_all(void)
  1638. {
  1639. NetSocket *s;
  1640. int i;
  1641. for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
  1642. if (!s->frozen)
  1643. do_select(s->s, true);
  1644. }
  1645. }
  1646. /*
  1647. * For Plink: enumerate all sockets currently active.
  1648. */
  1649. SOCKET first_socket(int *state)
  1650. {
  1651. NetSocket *s;
  1652. *state = 0;
  1653. s = index234(sktree, (*state)++);
  1654. return s ? s->s : INVALID_SOCKET;
  1655. }
  1656. SOCKET next_socket(int *state)
  1657. {
  1658. NetSocket *s = index234(sktree, (*state)++);
  1659. return s ? s->s : INVALID_SOCKET;
  1660. }
  1661. bool socket_writable(SOCKET skt)
  1662. {
  1663. NetSocket *s = find234(sktree, (void *)skt, cmpforsearch);
  1664. if (s)
  1665. return bufchain_size(&s->output_data) > 0;
  1666. else
  1667. return false;
  1668. }
  1669. int net_service_lookup(const char *service)
  1670. {
  1671. struct servent *se;
  1672. se = p_getservbyname(service, NULL);
  1673. if (se != NULL)
  1674. return p_ntohs(se->s_port);
  1675. else
  1676. return 0;
  1677. }
  1678. char *get_hostname(void)
  1679. {
  1680. char hostbuf[256]; /* MSDN docs for gethostname() promise this is enough */
  1681. if (p_gethostname(hostbuf, sizeof(hostbuf)) < 0)
  1682. return NULL;
  1683. return dupstr(hostbuf);
  1684. }
  1685. SockAddr *platform_get_x11_unix_address(const char *display, int displaynum)
  1686. {
  1687. SockAddr *addr = snew(SockAddr);
  1688. memset(addr, 0, sizeof(SockAddr));
  1689. addr->error = "unix sockets for X11 not supported on this platform";
  1690. addr->refcount = 1;
  1691. return addr;
  1692. }