winnet.c 52 KB

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