network.c 57 KB

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