winnet.c 52 KB

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