winnet.c 53 KB

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