win32-socket.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. /* Copyright (C) 2001, 2006 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/__scm.h"
  21. #include "libguile/modules.h"
  22. #include "libguile/numbers.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <errno.h>
  28. #include <limits.h>
  29. #ifndef PATH_MAX
  30. #define PATH_MAX 255
  31. #endif
  32. #include "win32-socket.h"
  33. /* Winsock API error description structure. The error description is
  34. necessary because there is no error list available. */
  35. typedef struct
  36. {
  37. int error; /* Error code. */
  38. char *str; /* Error description. */
  39. int replace; /* Possible error code replacement. */
  40. char *replace_str; /* Replacement symbol. */
  41. char *correct_str; /* Original symbol. */
  42. }
  43. socket_error_t;
  44. #define FILE_ETC_SERVICES "services"
  45. #define ENVIRON_ETC_SERVICES "SERVICES"
  46. #define FILE_ETC_NETWORKS "networks"
  47. #define ENVIRON_ETC_NETWORKS "NETWORKS"
  48. #define FILE_ETC_PROTOCOLS "protocol"
  49. #define ENVIRON_ETC_PROTOCOLS "PROTOCOLS"
  50. #define MAX_NAMLEN 256
  51. #define MAX_ALIASES 4
  52. /* Internal structure for a thread's M$-Windows servent interface. */
  53. typedef struct
  54. {
  55. FILE *fd; /* Current file. */
  56. char file[PATH_MAX]; /* File name. */
  57. struct servent ent; /* Return value. */
  58. char name[MAX_NAMLEN]; /* Service name. */
  59. char proto[MAX_NAMLEN]; /* Protocol name. */
  60. char alias[MAX_ALIASES][MAX_NAMLEN]; /* All aliases. */
  61. char *aliases[MAX_ALIASES]; /* Alias pointers. */
  62. int port; /* Network port. */
  63. }
  64. scm_i_servent_t;
  65. static scm_i_servent_t scm_i_servent;
  66. /* Internal structure for a thread's M$-Windows protoent interface. */
  67. typedef struct
  68. {
  69. FILE *fd; /* Current file. */
  70. char file[PATH_MAX]; /* File name. */
  71. struct protoent ent; /* Return value. */
  72. char name[MAX_NAMLEN]; /* Protocol name. */
  73. char alias[MAX_ALIASES][MAX_NAMLEN]; /* All aliases. */
  74. char *aliases[MAX_ALIASES]; /* Alias pointers. */
  75. int proto; /* Protocol number. */
  76. }
  77. scm_i_protoent_t;
  78. static scm_i_protoent_t scm_i_protoent;
  79. /* Define replacement symbols for most of the WSA* error codes. */
  80. #ifndef EWOULDBLOCK
  81. # define EWOULDBLOCK WSAEWOULDBLOCK
  82. #endif
  83. #ifndef EINPROGRESS
  84. # define EINPROGRESS WSAEINPROGRESS
  85. #endif
  86. #ifndef EALREADY
  87. # define EALREADY WSAEALREADY
  88. #endif
  89. #ifndef EDESTADDRREQ
  90. # define EDESTADDRREQ WSAEDESTADDRREQ
  91. #endif
  92. #ifndef EMSGSIZE
  93. # define EMSGSIZE WSAEMSGSIZE
  94. #endif
  95. #ifndef EPROTOTYPE
  96. # define EPROTOTYPE WSAEPROTOTYPE
  97. #endif
  98. #ifndef ENOTSOCK
  99. # define ENOTSOCK WSAENOTSOCK
  100. #endif
  101. #ifndef ENOPROTOOPT
  102. # define ENOPROTOOPT WSAENOPROTOOPT
  103. #endif
  104. #ifndef EPROTONOSUPPORT
  105. # define EPROTONOSUPPORT WSAEPROTONOSUPPORT
  106. #endif
  107. #ifndef ESOCKTNOSUPPORT
  108. # define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
  109. #endif
  110. #ifndef EOPNOTSUPP
  111. # define EOPNOTSUPP WSAEOPNOTSUPP
  112. #endif
  113. #ifndef EPFNOSUPPORT
  114. # define EPFNOSUPPORT WSAEPFNOSUPPORT
  115. #endif
  116. #ifndef EAFNOSUPPORT
  117. # define EAFNOSUPPORT WSAEAFNOSUPPORT
  118. #endif
  119. #ifndef EADDRINUSE
  120. # define EADDRINUSE WSAEADDRINUSE
  121. #endif
  122. #ifndef EADDRNOTAVAIL
  123. # define EADDRNOTAVAIL WSAEADDRNOTAVAIL
  124. #endif
  125. #ifndef ENETDOWN
  126. # define ENETDOWN WSAENETDOWN
  127. #endif
  128. #ifndef ENETUNREACH
  129. # define ENETUNREACH WSAENETUNREACH
  130. #endif
  131. #ifndef ENETRESET
  132. # define ENETRESET WSAENETRESET
  133. #endif
  134. #ifndef ECONNABORTED
  135. # define ECONNABORTED WSAECONNABORTED
  136. #endif
  137. #ifndef ECONNRESET
  138. # define ECONNRESET WSAECONNRESET
  139. #endif
  140. #ifndef ENOBUFS
  141. # define ENOBUFS WSAENOBUFS
  142. #endif
  143. #ifndef EISCONN
  144. # define EISCONN WSAEISCONN
  145. #endif
  146. #ifndef ENOTCONN
  147. # define ENOTCONN WSAENOTCONN
  148. #endif
  149. #ifndef ESHUTDOWN
  150. # define ESHUTDOWN WSAESHUTDOWN
  151. #endif
  152. #ifndef ETOOMANYREFS
  153. # define ETOOMANYREFS WSAETOOMANYREFS
  154. #endif
  155. #ifndef ETIMEDOUT
  156. # define ETIMEDOUT WSAETIMEDOUT
  157. #endif
  158. #ifndef ECONNREFUSED
  159. # define ECONNREFUSED WSAECONNREFUSED
  160. #endif
  161. #ifndef ELOOP
  162. # define ELOOP WSAELOOP
  163. #endif
  164. #ifndef EHOSTDOWN
  165. # define EHOSTDOWN WSAEHOSTDOWN
  166. #endif
  167. #ifndef EHOSTUNREACH
  168. # define EHOSTUNREACH WSAEHOSTUNREACH
  169. #endif
  170. #ifndef EPROCLIM
  171. # define EPROCLIM WSAEPROCLIM
  172. #endif
  173. #ifndef EUSERS
  174. # define EUSERS WSAEUSERS
  175. #endif
  176. #ifndef EDQUOT
  177. # define EDQUOT WSAEDQUOT
  178. #endif
  179. #ifndef ESTALE
  180. # define ESTALE WSAESTALE
  181. #endif
  182. #ifndef EREMOTE
  183. # define EREMOTE WSAEREMOTE
  184. #endif
  185. /* List of error structures. */
  186. static socket_error_t socket_errno [] = {
  187. /* 000 */ { 0, NULL, 0, NULL, NULL },
  188. /* 001 */ { 0, NULL, 0, NULL, NULL },
  189. /* 002 */ { 0, NULL, 0, NULL, NULL },
  190. /* 003 */ { 0, NULL, 0, NULL, NULL },
  191. /* 004 */ { WSAEINTR, "Interrupted function call", EINTR, NULL, "WSAEINTR" },
  192. /* 005 */ { 0, NULL, 0, NULL, NULL },
  193. /* 006 */ { 0, NULL, 0, NULL, NULL },
  194. /* 007 */ { 0, NULL, 0, NULL, NULL },
  195. /* 008 */ { 0, NULL, 0, NULL, NULL },
  196. /* 009 */ { WSAEBADF, "Bad file number", EBADF, NULL, "WSAEBADF" },
  197. /* 010 */ { 0, NULL, 0, NULL, NULL },
  198. /* 011 */ { 0, NULL, 0, NULL, NULL },
  199. /* 012 */ { 0, NULL, 0, NULL, NULL },
  200. /* 013 */ { WSAEACCES, "Permission denied", EACCES, NULL, "WSAEACCES" },
  201. /* 014 */ { WSAEFAULT, "Bad address", EFAULT, NULL, "WSAEFAULT" },
  202. /* 015 */ { 0, NULL, 0, NULL, NULL },
  203. /* 016 */ { 0, NULL, 0, NULL, NULL },
  204. /* 017 */ { 0, NULL, 0, NULL, NULL },
  205. /* 018 */ { 0, NULL, 0, NULL, NULL },
  206. /* 019 */ { 0, NULL, 0, NULL, NULL },
  207. /* 020 */ { 0, NULL, 0, NULL, NULL },
  208. /* 021 */ { 0, NULL, 0, NULL, NULL },
  209. /* 022 */ { WSAEINVAL, "Invalid argument", EINVAL, NULL, "WSAEINVAL" },
  210. /* 023 */ { 0, NULL, 0, NULL, NULL },
  211. /* 024 */ { WSAEMFILE, "Too many open files", EMFILE, NULL, "WSAEMFILE" },
  212. /* 025 */ { 0, NULL, 0, NULL, NULL },
  213. /* 026 */ { 0, NULL, 0, NULL, NULL },
  214. /* 027 */ { 0, NULL, 0, NULL, NULL },
  215. /* 028 */ { 0, NULL, 0, NULL, NULL },
  216. /* 029 */ { 0, NULL, 0, NULL, NULL },
  217. /* 030 */ { 0, NULL, 0, NULL, NULL },
  218. /* 031 */ { 0, NULL, 0, NULL, NULL },
  219. /* 032 */ { 0, NULL, 0, NULL, NULL },
  220. /* 033 */ { 0, NULL, 0, NULL, NULL },
  221. /* 034 */ { 0, NULL, 0, NULL, NULL },
  222. /* 035 */ { WSAEWOULDBLOCK, "Resource temporarily unavailable",
  223. EWOULDBLOCK, "EWOULDBLOCK", "WSAEWOULDBLOCK" },
  224. /* 036 */ { WSAEINPROGRESS, "Operation now in progress",
  225. EINPROGRESS, "EINPROGRESS", "WSAEINPROGRESS" },
  226. /* 037 */ { WSAEALREADY, "Operation already in progress",
  227. EALREADY, "EALREADY", "WSAEALREADY" },
  228. /* 038 */ { WSAENOTSOCK, "Socket operation on non-socket",
  229. ENOTSOCK, "ENOTSOCK", "WSAENOTSOCK"},
  230. /* 039 */ { WSAEDESTADDRREQ, "Destination address required",
  231. EDESTADDRREQ, "EDESTADDRREQ", "WSAEDESTADDRREQ" },
  232. /* 040 */ { WSAEMSGSIZE, "Message too long",
  233. EMSGSIZE, "EMSGSIZE", "WSAEMSGSIZE" },
  234. /* 041 */ { WSAEPROTOTYPE, "Protocol wrong type for socket",
  235. EPROTOTYPE, "EPROTOTYPE", "WSAEPROTOTYPE" },
  236. /* 042 */ { WSAENOPROTOOPT, "Bad protocol option",
  237. ENOPROTOOPT, "ENOPROTOOPT", "WSAENOPROTOOPT" },
  238. /* 043 */ { WSAEPROTONOSUPPORT, "Protocol not supported",
  239. EPROTONOSUPPORT, "EPROTONOSUPPORT", "WSAEPROTONOSUPPORT" },
  240. /* 044 */ { WSAESOCKTNOSUPPORT, "Socket type not supported",
  241. ESOCKTNOSUPPORT, "ESOCKTNOSUPPORT", "WSAESOCKTNOSUPPORT" },
  242. /* 045 */ { WSAEOPNOTSUPP, "Operation not supported",
  243. EOPNOTSUPP, "EOPNOTSUPP", "WSAEOPNOTSUPP" },
  244. /* 046 */ { WSAEPFNOSUPPORT, "Protocol family not supported",
  245. EPFNOSUPPORT, "EPFNOSUPPORT", "WSAEPFNOSUPPORT" },
  246. /* 047 */ { WSAEAFNOSUPPORT,
  247. "Address family not supported by protocol family",
  248. EAFNOSUPPORT, "EAFNOSUPPORT", "WSAEAFNOSUPPORT" },
  249. /* 048 */ { WSAEADDRINUSE, "Address already in use",
  250. EADDRINUSE, "EADDRINUSE", "WSAEADDRINUSE" },
  251. /* 049 */ { WSAEADDRNOTAVAIL, "Cannot assign requested address",
  252. EADDRNOTAVAIL, "EADDRNOTAVAIL", "WSAEADDRNOTAVAIL" },
  253. /* 050 */ { WSAENETDOWN, "Network is down",
  254. ENETDOWN, "ENETDOWN", "WSAENETDOWN" },
  255. /* 051 */ { WSAENETUNREACH, "Network is unreachable",
  256. ENETUNREACH, "ENETUNREACH", "WSAENETUNREACH" },
  257. /* 052 */ { WSAENETRESET, "Network dropped connection on reset",
  258. ENETRESET, "ENETRESET", "WSAENETRESET" },
  259. /* 053 */ { WSAECONNABORTED, "Software caused connection abort",
  260. ECONNABORTED, "ECONNABORTED", "WSAECONNABORTED" },
  261. /* 054 */ { WSAECONNRESET, "Connection reset by peer",
  262. ECONNRESET, "ECONNRESET", "WSAECONNRESET" },
  263. /* 055 */ { WSAENOBUFS, "No buffer space available",
  264. ENOBUFS, "ENOBUFS", "WSAENOBUFS" },
  265. /* 056 */ { WSAEISCONN, "Socket is already connected",
  266. EISCONN, "EISCONN", "WSAEISCONN" },
  267. /* 057 */ { WSAENOTCONN, "Socket is not connected",
  268. ENOTCONN, "ENOTCONN", "WSAENOTCONN" },
  269. /* 058 */ { WSAESHUTDOWN, "Cannot send after socket shutdown",
  270. ESHUTDOWN, "ESHUTDOWN", "WSAESHUTDOWN" },
  271. /* 059 */ { WSAETOOMANYREFS, "Too many references; can't splice",
  272. ETOOMANYREFS, "ETOOMANYREFS", "WSAETOOMANYREFS" },
  273. /* 060 */ { WSAETIMEDOUT, "Connection timed out",
  274. ETIMEDOUT, "ETIMEDOUT", "WSAETIMEDOUT" },
  275. /* 061 */ { WSAECONNREFUSED, "Connection refused",
  276. ECONNREFUSED, "ECONNREFUSED", "WSAECONNREFUSED" },
  277. /* 062 */ { WSAELOOP, "Too many levels of symbolic links",
  278. ELOOP, "ELOOP", "WSAELOOP" },
  279. /* 063 */ { WSAENAMETOOLONG, "File name too long",
  280. ENAMETOOLONG, NULL, "WSAENAMETOOLONG" },
  281. /* 064 */ { WSAEHOSTDOWN, "Host is down",
  282. EHOSTDOWN, "EHOSTDOWN", "WSAEHOSTDOWN" },
  283. /* 065 */ { WSAEHOSTUNREACH, "No route to host",
  284. EHOSTUNREACH, "EHOSTUNREACH", "WSAEHOSTUNREACH" },
  285. /* 066 */ { WSAENOTEMPTY, "Directory not empty",
  286. ENOTEMPTY, NULL, "WSAENOTEMPTY" },
  287. /* 067 */ { WSAEPROCLIM, "Too many processes",
  288. EPROCLIM, "EPROCLIM", "WSAEPROCLIM" },
  289. /* 068 */ { WSAEUSERS, "Too many users",
  290. EUSERS, "EUSERS", "WSAEUSERS" },
  291. /* 069 */ { WSAEDQUOT, "Disc quota exceeded",
  292. EDQUOT, "EDQUOT", "WSAEDQUOT" },
  293. /* 070 */ { WSAESTALE, "Stale NFS file handle",
  294. ESTALE, "ESTALE", "WSAESTALE" },
  295. /* 071 */ { WSAEREMOTE, "Too many levels of remote in path",
  296. EREMOTE, "EREMOTE", "WSAEREMOTE" },
  297. /* 072 */ { 0, NULL, 0, NULL, NULL },
  298. /* 073 */ { 0, NULL, 0, NULL, NULL },
  299. /* 074 */ { 0, NULL, 0, NULL, NULL },
  300. /* 075 */ { 0, NULL, 0, NULL, NULL },
  301. /* 076 */ { 0, NULL, 0, NULL, NULL },
  302. /* 077 */ { 0, NULL, 0, NULL, NULL },
  303. /* 078 */ { 0, NULL, 0, NULL, NULL },
  304. /* 079 */ { 0, NULL, 0, NULL, NULL },
  305. /* 080 */ { 0, NULL, 0, NULL, NULL },
  306. /* 081 */ { 0, NULL, 0, NULL, NULL },
  307. /* 082 */ { 0, NULL, 0, NULL, NULL },
  308. /* 083 */ { 0, NULL, 0, NULL, NULL },
  309. /* 084 */ { 0, NULL, 0, NULL, NULL },
  310. /* 085 */ { 0, NULL, 0, NULL, NULL },
  311. /* 086 */ { 0, NULL, 0, NULL, NULL },
  312. /* 087 */ { 0, NULL, 0, NULL, NULL },
  313. /* 088 */ { 0, NULL, 0, NULL, NULL },
  314. /* 089 */ { 0, NULL, 0, NULL, NULL },
  315. /* 090 */ { 0, NULL, 0, NULL, NULL },
  316. /* 091 */ { WSASYSNOTREADY, "Network subsystem is unavailable",
  317. 0, NULL, "WSASYSNOTREADY" },
  318. /* 092 */ { WSAVERNOTSUPPORTED, "WINSOCK.DLL version out of range",
  319. 0, NULL, "WSAVERNOTSUPPORTED" },
  320. /* 093 */ { WSANOTINITIALISED, "Successful WSAStartup not yet performed",
  321. 0, NULL, "WSANOTINITIALISED" },
  322. /* 094 */ { 0, NULL, 0, NULL, NULL },
  323. /* 095 */ { 0, NULL, 0, NULL, NULL },
  324. /* 096 */ { 0, NULL, 0, NULL, NULL },
  325. /* 097 */ { 0, NULL, 0, NULL, NULL },
  326. /* 098 */ { 0, NULL, 0, NULL, NULL },
  327. /* 099 */ { 0, NULL, 0, NULL, NULL },
  328. /* 100 */ { 0, NULL, 0, NULL, NULL },
  329. /* 101 */ { WSAEDISCON, "Graceful shutdown in progress",
  330. 0, NULL, "WSAEDISCON" },
  331. /* 102 */ { WSAENOMORE, "No more services",
  332. 0, NULL, "WSAENOMORE" },
  333. /* 103 */ { WSAECANCELLED, "Service lookup cancelled",
  334. 0, NULL, "WSAECANCELLED" },
  335. /* 104 */ { WSAEINVALIDPROCTABLE, "Invalid procedure call table",
  336. 0, NULL, "WSAEINVALIDPROCTABLE" },
  337. /* 105 */ { WSAEINVALIDPROVIDER, "Invalid service provider",
  338. 0, NULL, "WSAEINVALIDPROVIDER" },
  339. /* 106 */ { WSAEPROVIDERFAILEDINIT, "Service provider failure",
  340. 0, NULL, "WSAEPROVIDERFAILEDINIT" },
  341. /* 107 */ { WSASYSCALLFAILURE, "System call failed",
  342. 0, NULL, "WSASYSCALLFAILURE" },
  343. /* 108 */ { WSASERVICE_NOT_FOUND, "No such service",
  344. 0, NULL, "WSASERVICE_NOT_FOUND" },
  345. /* 109 */ { WSATYPE_NOT_FOUND, "Class not found",
  346. 0, NULL, "WSATYPE_NOT_FOUND" },
  347. /* 110 */ { WSA_E_NO_MORE, "No more services",
  348. 0, NULL, "WSA_E_NO_MORE" },
  349. /* 111 */ { WSA_E_CANCELLED, "Service lookup cancelled",
  350. 0, NULL, "WSA_E_CANCELLED" },
  351. /* 112 */ { WSAEREFUSED, "Database query refused",
  352. 0, NULL, "WSAEREFUSED" },
  353. /* end */ { -1, NULL, -1, NULL, NULL }
  354. };
  355. /* Extended list of error structures. */
  356. static socket_error_t socket_h_errno [] = {
  357. /* 000 */ { 0, NULL, 0, NULL, NULL },
  358. /* 001 */ { WSAHOST_NOT_FOUND, "Host not found",
  359. HOST_NOT_FOUND, "HOST_NOT_FOUND", "WSAHOST_NOT_FOUND" },
  360. /* 002 */ { WSATRY_AGAIN, "Non-authoritative host not found",
  361. TRY_AGAIN, "TRY_AGAIN", "WSATRY_AGAIN" },
  362. /* 003 */ { WSANO_RECOVERY, "This is a non-recoverable error",
  363. NO_RECOVERY, "NO_RECOVERY", "WSANO_RECOVERY" },
  364. /* 004 */ { WSANO_DATA, "Valid name, no data record of requested type",
  365. NO_DATA, "NO_DATA", "WSANO_DATA" },
  366. /* 005 */ { WSANO_ADDRESS, "No address, look for MX record",
  367. NO_ADDRESS, "NO_ADDRESS", "WSANO_ADDRESS" },
  368. /* end */ { -1, NULL, -1, NULL, NULL }
  369. };
  370. /* Returns the result of @code{WSAGetLastError()}. */
  371. int
  372. scm_i_socket_errno (void)
  373. {
  374. return WSAGetLastError ();
  375. }
  376. /* Returns a valid error message for Winsock-API error codes obtained via
  377. @code{WSAGetLastError()} or NULL otherwise. */
  378. char *
  379. scm_i_socket_strerror (int error)
  380. {
  381. if (error >= WSABASEERR && error <= (WSABASEERR + 112))
  382. return socket_errno[error - WSABASEERR].str;
  383. else if (error >= (WSABASEERR + 1000) && error <= (WSABASEERR + 1005))
  384. return socket_h_errno[error - (WSABASEERR + 1000)].str;
  385. return NULL;
  386. }
  387. /* Constructs a valid filename for the given file @var{file} in the M$-Windows
  388. directory. This is usually the default location for the network files. */
  389. char *
  390. scm_i_socket_filename (char *file)
  391. {
  392. static char dir[PATH_MAX];
  393. int len = PATH_MAX;
  394. len = GetWindowsDirectory (dir, len);
  395. if (dir[len - 1] != '\\')
  396. strcat (dir, "\\");
  397. strcat (dir, file);
  398. return dir;
  399. }
  400. /* Removes comments and white spaces at end of line and returns a pointer
  401. to the end of the line. */
  402. static char *
  403. scm_i_socket_uncomment (char *line)
  404. {
  405. char *end;
  406. if ((end = strchr (line, '#')) != NULL)
  407. *end-- = '\0';
  408. else
  409. {
  410. end = line + strlen (line) - 1;
  411. while (end > line && (*end == '\r' || *end == '\n'))
  412. *end-- = '\0';
  413. }
  414. while (end > line && isspace (*end))
  415. *end-- = '\0';
  416. return end;
  417. }
  418. /* The getservent() function reads the next line from the file `/etc/services'
  419. and returns a structure servent containing the broken out fields from the
  420. line. The `/etc/services' file is opened if necessary. */
  421. struct servent *
  422. getservent (void)
  423. {
  424. char line[MAX_NAMLEN], *end, *p;
  425. int done = 0, i, n, a;
  426. struct servent *e = NULL;
  427. /* Ensure a open file. */
  428. if (scm_i_servent.fd == NULL || feof (scm_i_servent.fd))
  429. {
  430. setservent (1);
  431. if (scm_i_servent.fd == NULL)
  432. return NULL;
  433. }
  434. while (!done)
  435. {
  436. /* Get new line. */
  437. if (fgets (line, MAX_NAMLEN, scm_i_servent.fd) != NULL)
  438. {
  439. end = scm_i_socket_uncomment (line);
  440. /* Scan the line. */
  441. if ((i = sscanf (line, "%s %d/%s%n",
  442. scm_i_servent.name,
  443. &scm_i_servent.port,
  444. scm_i_servent.proto, &n)) != 3)
  445. continue;
  446. /* Scan the remaining aliases. */
  447. p = line + n;
  448. for (a = 0; a < MAX_ALIASES && p < end && i != -1 && n > 1;
  449. a++, p += n)
  450. i = sscanf (p, "%s%n", scm_i_servent.alias[a], &n);
  451. /* Prepare the return value. */
  452. e = &scm_i_servent.ent;
  453. e->s_name = scm_i_servent.name;
  454. e->s_port = htons (scm_i_servent.port);
  455. e->s_proto = scm_i_servent.proto;
  456. e->s_aliases = scm_i_servent.aliases;
  457. scm_i_servent.aliases[a] = NULL;
  458. while (a--)
  459. scm_i_servent.aliases[a] = scm_i_servent.alias[a];
  460. done = 1;
  461. }
  462. else
  463. break;
  464. }
  465. return done ? e : NULL;
  466. }
  467. /* The setservent() function opens and rewinds the `/etc/services' file.
  468. This file can be set from outside with an environment variable specifying
  469. the file name. */
  470. void
  471. setservent (int stayopen)
  472. {
  473. char *file = NULL;
  474. endservent ();
  475. if ((file = getenv (ENVIRON_ETC_SERVICES)) != NULL)
  476. strcpy (scm_i_servent.file, file);
  477. else if ((file = scm_i_socket_filename (FILE_ETC_SERVICES)) != NULL)
  478. strcpy (scm_i_servent.file, file);
  479. scm_i_servent.fd = fopen (scm_i_servent.file, "rt");
  480. }
  481. /* The endservent() function closes the `/etc/services' file. */
  482. void
  483. endservent (void)
  484. {
  485. if (scm_i_servent.fd != NULL)
  486. {
  487. fclose (scm_i_servent.fd);
  488. scm_i_servent.fd = NULL;
  489. }
  490. }
  491. /* The getprotoent() function reads the next line from the file
  492. `/etc/protocols' and returns a structure protoent containing the broken
  493. out fields from the line. The `/etc/protocols' file is opened if
  494. necessary. */
  495. struct protoent *
  496. getprotoent (void)
  497. {
  498. char line[MAX_NAMLEN], *end, *p;
  499. int done = 0, i, n, a;
  500. struct protoent *e = NULL;
  501. /* Ensure a open file. */
  502. if (scm_i_protoent.fd == NULL || feof (scm_i_protoent.fd))
  503. {
  504. setprotoent (1);
  505. if (scm_i_protoent.fd == NULL)
  506. return NULL;
  507. }
  508. while (!done)
  509. {
  510. /* Get new line. */
  511. if (fgets (line, MAX_NAMLEN, scm_i_protoent.fd) != NULL)
  512. {
  513. end = scm_i_socket_uncomment (line);
  514. /* Scan the line. */
  515. if ((i = sscanf (line, "%s %d%n",
  516. scm_i_protoent.name,
  517. &scm_i_protoent.proto, &n)) != 2)
  518. continue;
  519. /* Scan the remaining aliases. */
  520. p = line + n;
  521. for (a = 0; a < MAX_ALIASES && p < end && i != -1 && n > 1;
  522. a++, p += n)
  523. i = sscanf (p, "%s%n", scm_i_protoent.alias[a], &n);
  524. /* Prepare the return value. */
  525. e = &scm_i_protoent.ent;
  526. e->p_name = scm_i_protoent.name;
  527. e->p_proto = scm_i_protoent.proto;
  528. e->p_aliases = scm_i_protoent.aliases;
  529. scm_i_protoent.aliases[a] = NULL;
  530. while (a--)
  531. scm_i_protoent.aliases[a] = scm_i_protoent.alias[a];
  532. done = 1;
  533. }
  534. else
  535. break;
  536. }
  537. return done ? e : NULL;
  538. }
  539. /* The setprotoent() function opens and rewinds the `/etc/protocols' file.
  540. As in setservent() the user can modify the location of the file using
  541. an environment variable. */
  542. void
  543. setprotoent (int stayopen)
  544. {
  545. char *file = NULL;
  546. endprotoent ();
  547. if ((file = getenv (ENVIRON_ETC_PROTOCOLS)) != NULL)
  548. strcpy (scm_i_protoent.file, file);
  549. else if ((file = scm_i_socket_filename (FILE_ETC_PROTOCOLS)) != NULL)
  550. strcpy (scm_i_protoent.file, file);
  551. scm_i_protoent.fd = fopen (scm_i_protoent.file, "rt");
  552. }
  553. /* The endprotoent() function closes `/etc/protocols'. */
  554. void
  555. endprotoent (void)
  556. {
  557. if (scm_i_protoent.fd != NULL)
  558. {
  559. fclose (scm_i_protoent.fd);
  560. scm_i_protoent.fd = NULL;
  561. }
  562. }
  563. /* Define both the original and replacement error symbol is possible. Thus
  564. the user is able to check symbolic errors after unsuccessful networking
  565. function calls. */
  566. static void
  567. scm_socket_symbols_Win32 (socket_error_t * e)
  568. {
  569. while (e->error != -1)
  570. {
  571. if (e->error)
  572. {
  573. if (e->correct_str)
  574. scm_c_define (e->correct_str, scm_from_int (e->error));
  575. if (e->replace && e->replace_str)
  576. scm_c_define (e->replace_str, scm_from_int (e->replace));
  577. }
  578. e++;
  579. }
  580. }
  581. /* Initialize Winsock API under M$-Windows. */
  582. void
  583. scm_i_init_socket_Win32 (void)
  584. {
  585. scm_socket_symbols_Win32 (socket_errno);
  586. scm_socket_symbols_Win32 (socket_h_errno);
  587. }