fd-io.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  1. /* Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. *
  3. * Authors: Mike Sperber, Marcus Crestani, Robert Ransom
  4. */
  5. #define NO_OLD_FFI 1
  6. #define _WIN32_WINNT 0x0400 /* for QueueUserAPC */
  7. #include <winsock2.h>
  8. #include <mswsock.h>
  9. #include <windows.h>
  10. #include <ws2tcpip.h>
  11. #include <stdio.h>
  12. #include <errno.h> /* for errno, (POSIX?/ANSI) */
  13. #include "scheme48vm.h"
  14. #include "event.h"
  15. #include "fd-io.h"
  16. #include "socket.h"
  17. #include "address.h"
  18. extern int s48_utf_8of16_to_utf_16(const unsigned char* utf_8of16,
  19. LPWSTR utf_16,
  20. int* errorp);
  21. enum stream_descriptor_type {
  22. STREAM_FILE_REGULAR, /* overlapped I/O works */
  23. STREAM_FILE_SPECIAL, /* overlapped I/O doesn't work */
  24. STREAM_SOCKET
  25. };
  26. typedef struct
  27. {
  28. HANDLE thread_handle; /* parameter */
  29. HANDLE file_handle; /* parameter */
  30. HANDLE check_semaphore; /* parameter */
  31. HANDLE abort_semaphore; /* parameter */
  32. /* in */
  33. psbool abort;
  34. char* buffer;
  35. size_t requested;
  36. PAPCFUNC callback;
  37. HANDLE callback_thread;
  38. /* out */
  39. size_t available; /* bytes written, readers only */
  40. psbool eof; /* readers only */
  41. psbool error;
  42. DWORD error_code;
  43. } file_thread_data_t;
  44. typedef struct {
  45. /* so we can pass this as LPOVERLAPPED; must be at the beginning */
  46. OVERLAPPED overlap;
  47. long fd;
  48. /* for successive calls in the main thread;
  49. only set and read from there */
  50. psbool checking;
  51. CHAR* buffer;
  52. CHAR* current;
  53. size_t current_size, max_size;
  54. } callback_data_t;
  55. typedef struct {
  56. enum stream_descriptor_type type;
  57. psbool is_free;
  58. union {
  59. struct {
  60. HANDLE handle;
  61. DWORD current_offset;
  62. DWORD current_offset_high;
  63. psbool eof; /* readers only */
  64. } file_regular_data;
  65. struct {
  66. file_thread_data_t thread_data;
  67. } file_special_data;
  68. struct {
  69. SOCKET socket;
  70. WSABUF wsabuf;
  71. SOCKET hatched_socket;
  72. /* callback to set socket hatched by connect/accept */
  73. PAPCFUNC hatch_callback;
  74. /* address returned by recvfrom */
  75. struct sockaddr_storage recv_address;
  76. socklen_t recv_address_len;
  77. } socket_data;
  78. };
  79. callback_data_t callback_data;
  80. } stream_descriptor_t;
  81. /* This is the same as DEFAULT-BUFFER-SIZE in rts/port-buffer.scm */
  82. #define FILE_CALLBACK_BUFFER_INITIAL_SIZE 4096
  83. /* If things work smoothly, this will never have to do anything. */
  84. static void
  85. maybe_grow_callback_data_buffer(callback_data_t* callback_data,
  86. size_t new_max_size)
  87. {
  88. if (new_max_size <= callback_data->max_size)
  89. return;
  90. callback_data->buffer = realloc(callback_data->buffer, new_max_size);
  91. if (callback_data->buffer == NULL)
  92. {
  93. fprintf(stderr,
  94. "failed to allocate memory for stream buffer\n");
  95. exit(1);
  96. }
  97. callback_data->max_size = new_max_size;
  98. }
  99. #define STREAM_DESCRIPTORS_INITIAL_SIZE 1024
  100. stream_descriptor_t* stream_descriptors;
  101. size_t stream_descriptors_max_size;
  102. static void
  103. setup_stream_descriptor(stream_descriptor_t* stream_descriptor, long fd)
  104. {
  105. stream_descriptor->is_free = PSTRUE;
  106. stream_descriptor->callback_data.fd = fd;
  107. stream_descriptor->callback_data.buffer = NULL;
  108. }
  109. static void
  110. initialize_stream_descriptors(void)
  111. {
  112. stream_descriptors = malloc(sizeof(stream_descriptor_t) *
  113. STREAM_DESCRIPTORS_INITIAL_SIZE);
  114. if (stream_descriptors == NULL)
  115. {
  116. fprintf(stderr,
  117. "failed to allocate memory for stream descriptors\n");
  118. exit(1);
  119. }
  120. {
  121. size_t i = 0;
  122. while (i < STREAM_DESCRIPTORS_INITIAL_SIZE)
  123. {
  124. setup_stream_descriptor(&(stream_descriptors[i]), i);
  125. ++i;
  126. }
  127. }
  128. stream_descriptors_max_size = STREAM_DESCRIPTORS_INITIAL_SIZE;
  129. }
  130. /* We might want to do some sort of freelist thing later. */
  131. static void
  132. grow_stream_descriptors(void)
  133. {
  134. size_t stream_descriptors_previous_max_size = stream_descriptors_max_size;
  135. stream_descriptors_max_size += STREAM_DESCRIPTORS_INITIAL_SIZE;
  136. stream_descriptors = realloc(stream_descriptors,
  137. stream_descriptors_max_size);
  138. if (stream_descriptors == NULL)
  139. {
  140. fprintf(stderr,
  141. "Failed to allocate memory for stream descriptor\n");
  142. exit(1);
  143. }
  144. {
  145. size_t i = stream_descriptors_previous_max_size;
  146. while (i < stream_descriptors_max_size)
  147. {
  148. setup_stream_descriptor(&(stream_descriptors[i]), i);
  149. ++i;
  150. }
  151. }
  152. }
  153. static void
  154. initialize_stream_descriptor(stream_descriptor_t* stream_descriptor,
  155. enum stream_descriptor_type type)
  156. {
  157. stream_descriptor->type = type;
  158. stream_descriptor->is_free = PSFALSE;
  159. /* #### this should probably move to the caller */
  160. switch (type)
  161. {
  162. case STREAM_FILE_REGULAR:
  163. stream_descriptor->file_regular_data.current_offset_high = 0;
  164. stream_descriptor->file_regular_data.current_offset = 0;
  165. stream_descriptor->file_regular_data.eof = PSFALSE;
  166. break;
  167. }
  168. stream_descriptor->callback_data.current_size = 0;
  169. if (stream_descriptor->callback_data.buffer == NULL)
  170. {
  171. stream_descriptor->callback_data.current
  172. = stream_descriptor->callback_data.buffer
  173. = malloc(FILE_CALLBACK_BUFFER_INITIAL_SIZE);
  174. if (stream_descriptor->callback_data.buffer == NULL)
  175. {
  176. fprintf(stderr,
  177. "Failed to allocate memory for stream buffer\n");
  178. exit(1);
  179. }
  180. stream_descriptor->callback_data.max_size
  181. = FILE_CALLBACK_BUFFER_INITIAL_SIZE;
  182. stream_descriptor->callback_data.current_size = 0;
  183. }
  184. stream_descriptor->callback_data.checking = PSFALSE;
  185. }
  186. static void
  187. deallocate_fd(long fd)
  188. {
  189. stream_descriptors[fd].is_free = PSTRUE;
  190. }
  191. static long
  192. allocate_fd(enum stream_descriptor_type type)
  193. {
  194. size_t i = 0;
  195. while ((i < stream_descriptors_max_size)
  196. && (!(stream_descriptors[i].is_free)))
  197. ++i;
  198. if (i == stream_descriptors_max_size)
  199. grow_stream_descriptors();
  200. initialize_stream_descriptor(&(stream_descriptors[i]), type);
  201. return i;
  202. }
  203. /* Windows allows doing fully asynchronous I/O on files and sockets.
  204. However, Windows still doesn't fit the Scheme 48 model completely.
  205. Here are the notable mismatches:
  206. - Scheme 48 uses longs uniformly to represent stream descriptors.
  207. Windows uses different types for files and sockets (HANDLE and SOCKET)
  208. - Scheme 48 reads first, sees that it can't complete immediately,
  209. queues up that it wants to read, then tries to read again.
  210. In Windows, you request a read, and a callback will get called
  211. *with the data*.
  212. - For writes we're better off because the model of Scheme 48
  213. actually fits Windows quite closely. */
  214. int
  215. ps_open_fd(char *filename, psbool is_input, long *status)
  216. {
  217. #define FILE_NAME_SIZE 1024
  218. #define PERMISSION 0666 /* read and write for everyone */
  219. HANDLE handle;
  220. char filename_temp[FILE_NAME_SIZE];
  221. char *expanded;
  222. WCHAR filename_utf16[FILE_NAME_SIZE];
  223. extern char *s48_expand_file_name(char *, char *, int);
  224. expanded = s48_expand_file_name(filename, filename_temp, FILE_NAME_SIZE);
  225. if (expanded == NULL)
  226. return -1;
  227. s48_utf_8of16_to_utf_16(expanded, filename_utf16, NULL);
  228. handle = CreateFileW(filename_utf16,
  229. is_input ? GENERIC_READ : GENERIC_WRITE,
  230. is_input ? FILE_SHARE_READ : FILE_SHARE_WRITE,
  231. NULL,
  232. is_input ? OPEN_EXISTING : CREATE_ALWAYS,
  233. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
  234. NULL);
  235. if (handle == INVALID_HANDLE_VALUE)
  236. {
  237. *status = (long) GetLastError();
  238. return -1;
  239. }
  240. else
  241. {
  242. long fd = allocate_fd(STREAM_FILE_REGULAR);
  243. stream_descriptor_t* stream_descriptor = &(stream_descriptors[fd]);
  244. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  245. *status = NO_ERRORS;
  246. /* here, things are always regular */
  247. stream_descriptor->file_regular_data.handle = handle;
  248. callback_data->overlap.Offset = 0;
  249. callback_data->overlap.OffsetHigh = 0;
  250. return fd;
  251. }
  252. }
  253. /*
  254. * This is for I/O streams that do not support overlapped I/O or a
  255. * select-like operation. (Stdin/out/err don't support overlapped
  256. * I/O; pipes don't support select.) The ideas are stolen from
  257. * MzScheme; the help of Matthew Flatt is gratefully acknowledged.
  258. *
  259. * Each such stream has an associated thread which carries out I/O
  260. * actions on behalf of some other thread. Upon completion, an
  261. * asynchronous procedure call to a procedure specified by the caller
  262. * is registered.
  263. */
  264. /*
  265. * This controls the text encoding used.
  266. */
  267. static BOOL is_STD_INPUT_console,
  268. is_STD_OUTPUT_console,
  269. is_STD_ERROR_console;
  270. /* defined and initialized in event.c */
  271. extern HANDLE s48_main_thread;
  272. DWORD WINAPI
  273. reader_thread_proc(LPVOID lpParameter)
  274. {
  275. DWORD n_read;
  276. stream_descriptor_t* stream_descriptor = (stream_descriptor_t*)lpParameter;
  277. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  278. long fd = callback_data->fd;
  279. file_thread_data_t* thread_data
  280. = &(stream_descriptor->file_special_data.thread_data);
  281. DWORD file_type = GetFileType(thread_data->file_handle);
  282. BOOL is_console = ((fd == STDIN_FD()) && is_STD_INPUT_console);
  283. while ((!thread_data->eof) && (!thread_data->error))
  284. {
  285. /* this comes from the main thread */
  286. WaitForSingleObject(thread_data->check_semaphore, INFINITE);
  287. /* printf("[reader_thread_proc: waited for semaphore]\n"); */
  288. if (thread_data->abort)
  289. break;
  290. /* printf("[reader_thread_proc:want %ld bytes\n", (long) thread_data->requested); */
  291. if (is_console ?
  292. ReadConsoleW(thread_data->file_handle, thread_data->buffer,
  293. thread_data->requested / 2, &n_read, NULL)
  294. : ReadFile(thread_data->file_handle, thread_data->buffer,
  295. thread_data->requested, &n_read, NULL))
  296. {
  297. thread_data->error = PSFALSE;
  298. /*
  299. * Windows reports in terms of characters, not bytes.
  300. */
  301. thread_data->available = (is_console ? (n_read * 2) : n_read);
  302. /* kludge: pressing Ctrl-C looks like EOF on stdin */
  303. if ((n_read == 0) && (file_type != FILE_TYPE_CHAR))
  304. thread_data->eof = PSTRUE;
  305. }
  306. else
  307. {
  308. thread_data->error = PSTRUE;
  309. thread_data->error_code = GetLastError();
  310. }
  311. /* printf("[reader_thread_proc:got %ld bytes]\n", (long) n_read); */
  312. /* notify */
  313. if (!QueueUserAPC(thread_data->callback,
  314. thread_data->callback_thread,
  315. (DWORD) stream_descriptor))
  316. {
  317. fprintf(stderr, "QueueUserAPC failed\n");
  318. exit(-1);
  319. }
  320. }
  321. /* clean up */
  322. if (CloseHandle(thread_data->check_semaphore))
  323. thread_data->error = PSFALSE;
  324. else
  325. {
  326. thread_data->error = PSTRUE;
  327. thread_data->error_code = GetLastError();
  328. }
  329. /* notify */
  330. ReleaseSemaphore(thread_data->abort_semaphore, 1, NULL);
  331. ExitThread(0);
  332. return 0;
  333. }
  334. /* #### most of this is the same as reader_thread_proc #### */
  335. DWORD WINAPI
  336. writer_thread_proc(LPVOID lpParameter)
  337. {
  338. DWORD n_written;
  339. stream_descriptor_t* stream_descriptor = (stream_descriptor_t*)lpParameter;
  340. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  341. long fd = callback_data->fd;
  342. file_thread_data_t* thread_data
  343. = &(stream_descriptor->file_special_data.thread_data);
  344. BOOL is_console = (((fd == STDOUT_FD()) && is_STD_OUTPUT_console)
  345. || ((fd == STDERR_FD()) && is_STD_ERROR_console));
  346. while (!thread_data->error)
  347. {
  348. /* this comes from the main thread */
  349. WaitForSingleObject(thread_data->check_semaphore, INFINITE);
  350. /* printf("[writer_thread_proc: waited for semaphore]\n"); */
  351. if (thread_data->abort)
  352. break;
  353. /* printf("[writer_thread_proc:want %ld bytes\n", (long) thread_data->requested); */
  354. if (is_console ?
  355. WriteConsoleW(thread_data->file_handle, thread_data->buffer,
  356. thread_data->requested / 2, &n_written, NULL)
  357. : WriteFile(thread_data->file_handle, thread_data->buffer,
  358. thread_data->requested, &n_written, NULL))
  359. {
  360. thread_data->error = PSFALSE;
  361. /*
  362. * Windows reports in terms of characters, not bytes.
  363. */
  364. thread_data->available = (is_console ? (n_written * 2) : n_written);
  365. }
  366. else
  367. {
  368. thread_data->error = PSTRUE;
  369. thread_data->error_code = GetLastError();
  370. }
  371. /* printf("[writer_thread_proc:got %ld bytes]\n", (long) n_written); */
  372. /* notify */
  373. if (!QueueUserAPC(thread_data->callback,
  374. thread_data->callback_thread,
  375. (DWORD) stream_descriptor))
  376. {
  377. fprintf(stderr, "QueueUserAPC failed\n");
  378. exit(-1);
  379. }
  380. }
  381. /* clean up */
  382. if (CloseHandle(thread_data->check_semaphore))
  383. thread_data->error = PSFALSE;
  384. else
  385. {
  386. thread_data->error = PSTRUE;
  387. thread_data->error_code = GetLastError();
  388. }
  389. /* notify */
  390. ReleaseSemaphore(thread_data->abort_semaphore, 1, NULL);
  391. ExitThread(0);
  392. return 0;
  393. }
  394. extern HANDLE s48_create_mutex_semaphore();
  395. static void
  396. start_reader_slash_writer_thread(HANDLE file_handle,
  397. stream_descriptor_t* stream_descriptor,
  398. LPTHREAD_START_ROUTINE thread_proc)
  399. {
  400. file_thread_data_t* thread_data = &(stream_descriptor->file_special_data.thread_data);
  401. HANDLE thread_handle;
  402. DWORD id;
  403. thread_data->abort = PSFALSE;
  404. thread_data->available = 0;
  405. thread_data->error = PSFALSE;
  406. thread_data->eof = PSFALSE;
  407. thread_data->file_handle = file_handle;
  408. thread_data->check_semaphore = s48_create_mutex_semaphore();
  409. thread_data->abort_semaphore = s48_create_mutex_semaphore();
  410. thread_handle = CreateThread(NULL, /* lpThreadAttributes */
  411. 4096, /* dwStackSize, */
  412. (LPTHREAD_START_ROUTINE)thread_proc,
  413. stream_descriptor,
  414. 0, /* dwCreationFlags, */
  415. &id);
  416. if (thread_handle == NULL)
  417. {
  418. fprintf(stderr, "CreateThread failed in start_reader_slash_writer_thread\n");
  419. free(thread_data);
  420. exit(-1);
  421. }
  422. thread_data->thread_handle = thread_handle;
  423. }
  424. static void
  425. open_special_fd(HANDLE handle, size_t fd,
  426. psbool is_input, BOOL is_redirected)
  427. {
  428. stream_descriptor_t* stream_descriptor;
  429. if (fd >= stream_descriptors_max_size)
  430. grow_stream_descriptors();
  431. if (!(stream_descriptors[fd].is_free))
  432. {
  433. fprintf(stderr, "fd %d isn't available\n", fd);
  434. exit(-1);
  435. }
  436. stream_descriptor = &(stream_descriptors[fd]);
  437. initialize_stream_descriptor(stream_descriptor, STREAM_FILE_SPECIAL);
  438. start_reader_slash_writer_thread(handle,
  439. stream_descriptor,
  440. is_input
  441. ? (LPTHREAD_START_ROUTINE)reader_thread_proc
  442. : (LPTHREAD_START_ROUTINE)writer_thread_proc);
  443. }
  444. int
  445. ps_close_fd(long fd)
  446. {
  447. stream_descriptor_t* stream_descriptor = &(stream_descriptors[fd]);
  448. int status;
  449. switch (stream_descriptor->type)
  450. {
  451. case STREAM_FILE_REGULAR:
  452. {
  453. HANDLE handle = stream_descriptor->file_regular_data.handle;
  454. if (CloseHandle(handle))
  455. status = NO_ERRORS;
  456. else
  457. status = (int) GetLastError();
  458. break;
  459. }
  460. case STREAM_FILE_SPECIAL:
  461. {
  462. file_thread_data_t* thread_data =
  463. &(stream_descriptor->file_special_data.thread_data);
  464. thread_data->abort = PSTRUE;
  465. ReleaseSemaphore(thread_data->check_semaphore, 1, NULL);
  466. /* wait for completion */
  467. WaitForSingleObject(thread_data->abort_semaphore, INFINITE);
  468. status = thread_data->error ? thread_data->error_code : NO_ERRORS;
  469. break;
  470. }
  471. case STREAM_SOCKET:
  472. {
  473. SOCKET socket = stream_descriptor->socket_data.socket;
  474. if (closesocket(socket) == 0)
  475. status = NO_ERRORS;
  476. else
  477. status = (int) WSAGetLastError();
  478. break;
  479. }
  480. }
  481. deallocate_fd(fd);
  482. return status;
  483. }
  484. extern psbool s48_is_pending(long);
  485. extern void s48_add_ready_fd(long, psbool, psbool, long);
  486. /* This is called as the result of a completed read operation; either
  487. from the overlapped I/O completion routine, or from the callback
  488. from the reader thread. */
  489. static void
  490. read_done(DWORD dwErr,
  491. size_t bytes_read,
  492. stream_descriptor_t* stream_descriptor)
  493. {
  494. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  495. long fd = callback_data->fd;
  496. /* ### need to do offset_high as well */
  497. stream_descriptor->callback_data.current_size = bytes_read;
  498. stream_descriptor->callback_data.current =
  499. stream_descriptor->callback_data.buffer;
  500. if (stream_descriptor->type == STREAM_FILE_REGULAR)
  501. {
  502. stream_descriptor->file_regular_data.current_offset += bytes_read;
  503. /* #### need to do offset_high as well */
  504. stream_descriptor->file_regular_data.eof
  505. = ((dwErr == ERROR_HANDLE_EOF) ? PSTRUE : PSFALSE);
  506. }
  507. if (callback_data->checking) /* ps_check_fd */
  508. callback_data->checking = PSFALSE;
  509. if (s48_is_pending(fd))
  510. {
  511. if ((dwErr != 0) && (dwErr != ERROR_HANDLE_EOF))
  512. s48_add_ready_fd(fd, PSTRUE, PSTRUE, dwErr);
  513. else
  514. s48_add_ready_fd(fd, PSTRUE, PSFALSE, (long)0); /* *not* bytes_read */
  515. }
  516. }
  517. /* for regular files; from overlapped I/O */
  518. static VOID WINAPI
  519. read_completed(DWORD dwErr, DWORD cbBytesRead, LPOVERLAPPED lpOverLap)
  520. {
  521. callback_data_t* callback_data = (callback_data_t*) lpOverLap;
  522. long fd = callback_data->fd;
  523. stream_descriptor_t* stream_descriptor = &(stream_descriptors[fd]);
  524. read_done(dwErr, (size_t)cbBytesRead, stream_descriptor);
  525. }
  526. /* for special files; from QueueUserAPC */
  527. static VOID CALLBACK
  528. read_callback(DWORD dwParam)
  529. {
  530. stream_descriptor_t* stream_descriptor = (stream_descriptor_t*)dwParam;
  531. file_thread_data_t* thread_data
  532. = &(stream_descriptor->file_special_data.thread_data);
  533. read_done(thread_data->error ? thread_data->error_code : NO_ERRORS,
  534. (size_t)thread_data->available,
  535. stream_descriptor);
  536. }
  537. /* from sockets */
  538. static VOID CALLBACK
  539. recv_completed(DWORD dwErr, DWORD cbBytesRead, LPOVERLAPPED lpOverLap, DWORD dwFlags)
  540. {
  541. read_completed(dwErr, cbBytesRead, lpOverLap);
  542. }
  543. psbool
  544. ps_check_fd(long fd, psbool is_read, long *status)
  545. {
  546. stream_descriptor_t* stream_descriptor = &(stream_descriptors[fd]);
  547. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  548. *status = NO_ERRORS;
  549. if (!is_read)
  550. /* conservative */
  551. return stream_descriptor->callback_data.current_size == 0;
  552. /* from now on, we know is_read is true */
  553. if (stream_descriptor->callback_data.current_size > 0)
  554. /* the buffer is non-empty */
  555. return PSTRUE;
  556. switch (stream_descriptor->type) {
  557. case STREAM_FILE_REGULAR:
  558. {
  559. /* we might consider always returning true here */
  560. HANDLE handle = stream_descriptor->file_regular_data.handle;
  561. callback_data->overlap.Offset = stream_descriptor->file_regular_data.current_offset;
  562. callback_data->overlap.OffsetHigh = stream_descriptor->file_regular_data.current_offset_high;
  563. callback_data->checking = PSTRUE;
  564. if (ReadFileEx(handle,
  565. (LPVOID)callback_data->buffer,
  566. (DWORD)1,
  567. (LPOVERLAPPED)callback_data,
  568. read_completed))
  569. return PSFALSE;
  570. else
  571. {
  572. DWORD last_error = GetLastError();
  573. if (last_error != ERROR_HANDLE_EOF)
  574. *status = (int) last_error;
  575. return PSTRUE;
  576. }
  577. }
  578. case STREAM_FILE_SPECIAL:
  579. {
  580. file_thread_data_t* thread_data = &(stream_descriptor->file_special_data.thread_data);
  581. if (thread_data->eof)
  582. return PSTRUE;
  583. if (thread_data->error)
  584. {
  585. *status = thread_data->error_code;
  586. return PSTRUE;
  587. }
  588. if (callback_data->checking)
  589. return PSFALSE;
  590. /* get the reader thread started */
  591. callback_data->checking = PSTRUE;
  592. thread_data->buffer = stream_descriptor->callback_data.buffer;
  593. thread_data->requested = 1;
  594. thread_data->callback = read_callback;
  595. thread_data->callback_thread = s48_main_thread;
  596. ReleaseSemaphore(thread_data->check_semaphore, 1, NULL);
  597. return PSFALSE;
  598. }
  599. case STREAM_SOCKET:
  600. {
  601. SOCKET socket = stream_descriptor->socket_data.socket;
  602. DWORD numberOfBytesRecvd;
  603. DWORD flags = 0;
  604. int wsa_status;
  605. int block;
  606. /* just making sure */
  607. callback_data->overlap.Offset = 0;
  608. callback_data->overlap.OffsetHigh = 0;
  609. stream_descriptor->socket_data.wsabuf.len = 1;
  610. stream_descriptor->socket_data.wsabuf.buf = (char*)callback_data->buffer;
  611. /* just making sure */
  612. callback_data->overlap.Offset = 0;
  613. callback_data->overlap.OffsetHigh = 0;
  614. callback_data->overlap.hEvent = NULL; /* this is hopefully invalid */
  615. block = 1; /* non-blocking */
  616. ioctlsocket(socket, FIONBIO, &block);
  617. wsa_status = WSARecv(socket,
  618. (LPWSABUF)&stream_descriptor->socket_data.wsabuf,
  619. 1,
  620. &numberOfBytesRecvd,
  621. &flags,
  622. (LPOVERLAPPED)callback_data,
  623. recv_completed);
  624. if ((wsa_status == 0)
  625. && (numberOfBytesRecvd == 0))
  626. return PSTRUE;
  627. if ((wsa_status == 0)
  628. || (WSAGetLastError() == WSA_IO_PENDING))
  629. return PSFALSE;
  630. else
  631. {
  632. *status = WSAGetLastError();
  633. return 0;
  634. }
  635. }
  636. }
  637. return PSFALSE; /* shouldn't happen */
  638. }
  639. /* waitp doesn't mean it should wait---it only means that the read
  640. shouldn't be registered anywhere if this is false.
  641. If EOF has been reached (i.e. no characters can be read),
  642. *eofp is set.
  643. If the read operation is queued up for asynchronous completion,
  644. *pending is set.
  645. Returns number of characters actually read. (This may be 0.)
  646. */
  647. long
  648. ps_read_fd(long fd, char *buffer, long max, psbool waitp,
  649. psbool *eofp, psbool *pending, long *status)
  650. {
  651. stream_descriptor_t* stream_descriptor = &(stream_descriptors[fd]);
  652. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  653. /* for the normal return */
  654. *eofp = PSFALSE;
  655. *pending = PSFALSE;
  656. *status = NO_ERRORS;
  657. if (callback_data->checking) /* ps_check_fd is active */
  658. return 0;
  659. /* there's still stuff in the buffer */
  660. if (callback_data->current_size > 0)
  661. {
  662. /* we want more than what's in the buffer */
  663. if (max >= (long)callback_data->current_size)
  664. {
  665. size_t size = callback_data->current_size;
  666. memcpy(buffer, callback_data->current, size);
  667. callback_data->current_size = 0;
  668. callback_data->current = stream_descriptor->callback_data.buffer;
  669. return size;
  670. }
  671. else
  672. /* less; shouldn't happen */
  673. {
  674. memcpy(buffer, callback_data->current, max);
  675. callback_data->current += max;
  676. callback_data->current_size -= max;
  677. return max;
  678. }
  679. }
  680. switch (stream_descriptor->type)
  681. {
  682. case STREAM_FILE_REGULAR:
  683. {
  684. HANDLE handle = stream_descriptor->file_regular_data.handle;
  685. if (stream_descriptor->file_regular_data.eof)
  686. {
  687. *eofp = PSTRUE;
  688. return 0;
  689. }
  690. /* There's nothing in the buffer---need to do an actual read. */
  691. maybe_grow_callback_data_buffer(callback_data, (size_t)max);
  692. callback_data->overlap.Offset = stream_descriptor->file_regular_data.current_offset;
  693. callback_data->overlap.OffsetHigh = stream_descriptor->file_regular_data.current_offset_high;
  694. if (ReadFileEx(handle,
  695. (LPVOID)callback_data->buffer,
  696. (DWORD)max,
  697. (LPOVERLAPPED)callback_data,
  698. read_completed))
  699. {
  700. /* success */
  701. if (waitp)
  702. {
  703. if (!s48_add_pending_fd(fd, PSTRUE))
  704. *status = ERROR_OUTOFMEMORY;
  705. else
  706. *pending = PSTRUE;
  707. }
  708. return 0;
  709. }
  710. else
  711. {
  712. DWORD last_error = GetLastError();
  713. if (last_error == ERROR_HANDLE_EOF)
  714. {
  715. *eofp = PSTRUE;
  716. return 0;
  717. }
  718. /* failure */
  719. *status = (int) last_error;
  720. return 0;
  721. }
  722. }
  723. case STREAM_FILE_SPECIAL:
  724. {
  725. file_thread_data_t* thread_data =
  726. &(stream_descriptor->file_special_data.thread_data);
  727. if (thread_data->eof)
  728. {
  729. *eofp = PSTRUE;
  730. return 0;
  731. }
  732. /* There's nothing in the buffer---need to do an actual read. */
  733. maybe_grow_callback_data_buffer(callback_data, (size_t)max);
  734. if (waitp)
  735. {
  736. if (!s48_add_pending_fd(fd, PSTRUE))
  737. {
  738. *status = ERROR_OUTOFMEMORY;
  739. return 0;
  740. }
  741. thread_data->buffer = callback_data->buffer;
  742. thread_data->requested = max;
  743. thread_data->callback_thread = s48_main_thread;
  744. thread_data->callback = read_callback;
  745. ReleaseSemaphore(thread_data->check_semaphore, 1, NULL);
  746. *pending = PSTRUE;
  747. }
  748. return 0;
  749. }
  750. case STREAM_SOCKET:
  751. {
  752. SOCKET socket = stream_descriptor->socket_data.socket;
  753. DWORD numberOfBytesRecvd;
  754. DWORD flags = 0;
  755. int wsa_status;
  756. int block;
  757. /* There's nothing in the buffer---need to do an actual read. */
  758. maybe_grow_callback_data_buffer(callback_data, (size_t)max);
  759. /* just making sure */
  760. callback_data->overlap.Offset = 0;
  761. callback_data->overlap.OffsetHigh = 0;
  762. stream_descriptor->socket_data.wsabuf.len = max;
  763. stream_descriptor->socket_data.wsabuf.buf = (char*)callback_data->buffer;
  764. block = 1; /* non-blocking */
  765. ioctlsocket(socket, FIONBIO, &block);
  766. wsa_status = WSARecv(socket,
  767. (LPWSABUF)&stream_descriptor->socket_data.wsabuf,
  768. 1,
  769. &numberOfBytesRecvd,
  770. &flags,
  771. (LPOVERLAPPED)callback_data,
  772. recv_completed);
  773. if ((wsa_status == 0)
  774. && (numberOfBytesRecvd == 0))
  775. *eofp = PSTRUE;
  776. if ((wsa_status == 0)
  777. || (WSAGetLastError() == WSA_IO_PENDING))
  778. {
  779. if (waitp)
  780. {
  781. if (!s48_add_pending_fd(fd, PSTRUE))
  782. *status = ERROR_OUTOFMEMORY;
  783. else
  784. *pending = PSTRUE;
  785. }
  786. return 0;
  787. }
  788. else
  789. {
  790. *status = WSAGetLastError();
  791. return 0;
  792. }
  793. }
  794. }
  795. return 0; /* shouldn't happen */
  796. }
  797. static VOID WINAPI
  798. write_done(DWORD dwErr,
  799. size_t bytes_written,
  800. stream_descriptor_t* stream_descriptor)
  801. {
  802. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  803. long fd = callback_data->fd;
  804. if ((bytes_written != 0) && (s48_is_pending(fd)))
  805. {
  806. switch (stream_descriptor->type) {
  807. case STREAM_FILE_REGULAR:
  808. {
  809. stream_descriptor->file_regular_data.current_offset += bytes_written;
  810. /* ### need to do offset_high as well */
  811. break;
  812. }
  813. }
  814. if (dwErr != 0)
  815. s48_add_ready_fd(fd, PSFALSE, PSTRUE, dwErr);
  816. else
  817. s48_add_ready_fd(fd, PSFALSE, PSFALSE, (long)bytes_written);
  818. }
  819. }
  820. /* for regular files; from overlapped I/O */
  821. static VOID WINAPI
  822. write_completed(DWORD dwErr, DWORD cbWritten, LPOVERLAPPED lpOverLap)
  823. {
  824. callback_data_t* callback_data = (callback_data_t*) lpOverLap;
  825. long fd = callback_data->fd;
  826. stream_descriptor_t* stream_descriptor = &(stream_descriptors[fd]);
  827. write_done(dwErr, (size_t)cbWritten, stream_descriptor);
  828. }
  829. /* for special files; from QueueUserAPC */
  830. static VOID CALLBACK
  831. write_callback(DWORD dwParam)
  832. {
  833. stream_descriptor_t* stream_descriptor = (stream_descriptor_t*)dwParam;
  834. file_thread_data_t* thread_data
  835. = &(stream_descriptor->file_special_data.thread_data);
  836. write_done(thread_data->error ? thread_data->error_code : 0,
  837. (size_t)thread_data->available,
  838. stream_descriptor);
  839. }
  840. /* for sockets */
  841. static VOID CALLBACK
  842. send_completed(DWORD dwErr, DWORD cbTransferred, LPOVERLAPPED lpOverLap, DWORD dwFlags)
  843. {
  844. write_completed(dwErr, cbTransferred, lpOverLap);
  845. }
  846. long
  847. ps_write_fd(long fd, char *buffer, long max, psbool *pending, long *status)
  848. {
  849. stream_descriptor_t* stream_descriptor = &(stream_descriptors[fd]);
  850. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  851. maybe_grow_callback_data_buffer(callback_data, (size_t)max);
  852. memcpy(callback_data->buffer, buffer, (size_t)max);
  853. switch (stream_descriptor->type)
  854. {
  855. case STREAM_FILE_REGULAR:
  856. {
  857. HANDLE handle = stream_descriptor->file_regular_data.handle;
  858. callback_data->overlap.Offset = stream_descriptor->file_regular_data.current_offset;
  859. callback_data->overlap.OffsetHigh = stream_descriptor->file_regular_data.current_offset_high;
  860. if (WriteFileEx(handle,
  861. (LPVOID)callback_data->buffer,
  862. (DWORD)max,
  863. (LPOVERLAPPED)callback_data,
  864. write_completed))
  865. {
  866. if (!s48_add_pending_fd(fd, PSFALSE))
  867. {
  868. *status = ERROR_OUTOFMEMORY;
  869. return 0;
  870. }
  871. *pending = PSTRUE;
  872. *status = NO_ERRORS;
  873. }
  874. else
  875. {
  876. *pending = PSFALSE;
  877. *status = (int) GetLastError();
  878. }
  879. return 0; /* we always wait for the callback */
  880. }
  881. case STREAM_FILE_SPECIAL:
  882. {
  883. file_thread_data_t* thread_data =
  884. &(stream_descriptor->file_special_data.thread_data);
  885. thread_data->buffer = callback_data->buffer;
  886. thread_data->requested = max;
  887. thread_data->callback_thread = s48_main_thread;
  888. thread_data->callback = write_callback;
  889. ReleaseSemaphore(thread_data->check_semaphore, 1, NULL);
  890. if (!s48_add_pending_fd(fd, PSFALSE))
  891. {
  892. *status = ERROR_OUTOFMEMORY;
  893. return 0;
  894. }
  895. *pending = PSTRUE;
  896. *status = NO_ERRORS;
  897. return 0; /* the thread needs to do the work */
  898. }
  899. case STREAM_SOCKET:
  900. {
  901. SOCKET socket = stream_descriptor->socket_data.socket;
  902. DWORD numberOfBytesSent;
  903. DWORD flags = 0;
  904. int wsa_status;
  905. /* just making sure */
  906. callback_data->overlap.Offset = 0;
  907. callback_data->overlap.OffsetHigh = 0;
  908. stream_descriptor->socket_data.wsabuf.len = max;
  909. stream_descriptor->socket_data.wsabuf.buf = (char*)callback_data->buffer;
  910. wsa_status = WSASend(socket,
  911. (LPWSABUF)&stream_descriptor->socket_data.wsabuf,
  912. 1,
  913. &numberOfBytesSent,
  914. flags,
  915. (LPOVERLAPPED)callback_data,
  916. send_completed);
  917. if ((wsa_status == 0)
  918. | (WSAGetLastError() == WSA_IO_PENDING))
  919. {
  920. /* success */
  921. if (!s48_add_pending_fd(fd, PSFALSE))
  922. {
  923. *status = ERROR_OUTOFMEMORY;
  924. return 0;
  925. }
  926. *pending = PSTRUE;
  927. *status = NO_ERRORS;
  928. }
  929. else
  930. {
  931. *pending = PSFALSE;
  932. *status = (int) WSAGetLastError();
  933. }
  934. return 0; /* we always wait for the callback */
  935. }
  936. }
  937. return 0; /* shouldn't happen */
  938. }
  939. long
  940. ps_io_buffer_size(void)
  941. {
  942. return 4096;
  943. }
  944. psbool
  945. ps_io_crlf_p(void)
  946. {
  947. return PSTRUE;
  948. }
  949. char *
  950. ps_console_encoding(long fd_as_long)
  951. {
  952. if (fd_as_long == STDIN_FD())
  953. return is_STD_INPUT_console ? "UTF-16LE" : "ISO8859-1";
  954. else if (fd_as_long == STDOUT_FD())
  955. return is_STD_OUTPUT_console ? "UTF-16LE" : "ISO8859-1";
  956. else if (fd_as_long == STDERR_FD())
  957. return is_STD_ERROR_console ? "UTF-16LE" : "ISO8859-1";
  958. else
  959. return NULL;
  960. }
  961. long
  962. ps_abort_fd_op(long fd_as_long)
  963. {
  964. int fd = (int)fd_as_long;
  965. stream_descriptor_t* stream_descriptor = &(stream_descriptors[fd]);
  966. psbool pending_p = s48_is_pending(fd);
  967. if (!s48_remove_fd(fd))
  968. fprintf(stderr, "Error: ps_abort_fd_op, no pending operation on fd %d\n",
  969. fd);
  970. switch (stream_descriptor->type)
  971. {
  972. case STREAM_FILE_REGULAR:
  973. {
  974. HANDLE handle = stream_descriptor->file_regular_data.handle;
  975. CancelIo(handle);
  976. /* flush any pending completion routines */
  977. if (pending_p)
  978. while (SleepEx(0, TRUE) == IO_COMPLETION_EVENT)
  979. ;
  980. break;
  981. }
  982. case STREAM_SOCKET:
  983. {
  984. SOCKET socket = stream_descriptor->socket_data.socket;
  985. CancelIo((HANDLE) socket);
  986. /* flush any pending completion routines */
  987. if (pending_p)
  988. while (SleepEx(0, TRUE) == IO_COMPLETION_EVENT)
  989. ;
  990. break;
  991. }
  992. }
  993. return 0; /* because we do not actually do any I/O in parallel the
  994. status is always zero: no characters transferred. */
  995. }
  996. /*
  997. * This checks that fd's destined for output channels are marked
  998. * as nonblocking. Stdout and stderr are a special case because
  999. * we do not want to screw up the shell, if we were invoked from
  1000. * one.
  1001. */
  1002. s48_ref_t
  1003. s48_add_channel_2(s48_call_t call, s48_ref_t mode, s48_ref_t id, long fd)
  1004. {
  1005. /* back to the VM */
  1006. return s48_make_local_ref(call, s48_really_add_channel(s48_deref(mode), s48_deref(id), fd));
  1007. }
  1008. s48_ref_t
  1009. s48_set_channel_os_index_2(s48_call_t call, s48_ref_t channel, long fd)
  1010. {
  1011. /* back to the VM */
  1012. return s48_make_local_ref(call, s48_set_channel_os_index(s48_deref(channel), fd));
  1013. }
  1014. void
  1015. s48_fd_io_init()
  1016. {
  1017. DWORD numberOfEventsRead; /* dummy, actually */
  1018. CONSOLE_CURSOR_INFO consoleCursorInfo; /* dummy, too */
  1019. HANDLE handle;
  1020. initialize_stream_descriptors();
  1021. /* these Unix-style indices are hard-wired into the VM */
  1022. handle = GetStdHandle(STD_INPUT_HANDLE);
  1023. /*
  1024. * PeekConsoleInput returns non-zero if we're indeed looking at a
  1025. * console, zero if a handle has been redirected. We need this info
  1026. * to decide whether to use the console I/O functions and Unicode.
  1027. */
  1028. is_STD_INPUT_console = PeekConsoleInput(handle, NULL, 0, &numberOfEventsRead);
  1029. open_special_fd(handle, 0, PSTRUE, is_STD_INPUT_console);
  1030. handle = GetStdHandle(STD_OUTPUT_HANDLE);
  1031. /*
  1032. * Similarly for GetConsoleCursorInfo.
  1033. */
  1034. is_STD_OUTPUT_console = GetConsoleCursorInfo(handle, &consoleCursorInfo);
  1035. open_special_fd(handle, 1, PSFALSE, is_STD_OUTPUT_console);
  1036. handle = GetStdHandle(STD_ERROR_HANDLE);
  1037. is_STD_ERROR_console = GetConsoleCursorInfo(handle, &consoleCursorInfo);
  1038. open_special_fd(handle, 2, PSFALSE, is_STD_ERROR_console);
  1039. }
  1040. /*
  1041. * An interface to Windows Sockets 2
  1042. */
  1043. /*
  1044. * Windows Sockets 2 is a mumbojumbo of Berkeley sockets, and
  1045. * Microsoft's own stuff thrown in. Of course, the two halves use
  1046. * different naming conventions, and things are generally confusing.
  1047. * Moreover, socket handles and file handles are distinct (at least
  1048. * partially), so we can't use the file operations we want on sockets.
  1049. */
  1050. socket_t
  1051. s48_extract_socket_fd(s48_call_t call, s48_ref_t sch_channel)
  1052. {
  1053. s48_check_channel_2(call, sch_channel);
  1054. {
  1055. long socket_fd
  1056. = s48_unsafe_extract_long_2(call, s48_unsafe_channel_os_index_2(call, sch_channel));
  1057. stream_descriptor_t* stream_descriptor = &(stream_descriptors[socket_fd]);
  1058. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  1059. socket_t socket = stream_descriptor->socket_data.socket;
  1060. return socket;
  1061. }
  1062. }
  1063. static s48_ref_t
  1064. s48_socket(s48_call_t call, s48_ref_t sch_af, s48_ref_t sch_type, s48_ref_t sch_protocol)
  1065. {
  1066. long fd;
  1067. int af = s48_extract_af(call, sch_af);
  1068. int socktype = s48_extract_socket_type(call, sch_type);
  1069. int protocol = s48_extract_long_2(call, sch_protocol);
  1070. s48_ref_t channel;
  1071. stream_descriptor_t* stream_descriptor;
  1072. SOCKET socket = WSASocket(af, socktype, protocol,
  1073. NULL,
  1074. 0, /* reserved */
  1075. WSA_FLAG_OVERLAPPED);
  1076. if (socket == INVALID_SOCKET)
  1077. s48_os_error_2(call, "s48_socket", WSAGetLastError(), 3,
  1078. sch_af, sch_type, sch_protocol);
  1079. fd = allocate_fd(STREAM_SOCKET);
  1080. stream_descriptor = &(stream_descriptors[fd]);
  1081. stream_descriptor->socket_data.socket = socket;
  1082. channel = s48_add_channel_2(call, s48_channel_status_special_input_2(call),
  1083. s48_enter_string_latin_1_2(call, "socket"), fd);
  1084. if (!s48_channel_p_2(call, channel))
  1085. {
  1086. ps_close_fd(fd);
  1087. s48_raise_scheme_exception_2(call, s48_extract_long_2(call, channel), 0);
  1088. }
  1089. return channel;
  1090. }
  1091. #define RETRY_OR_RAISE_NEG(C, STATUS, CALL) \
  1092. do { \
  1093. STATUS = (CALL); \
  1094. if (STATUS == SOCKET_ERROR) \
  1095. s48_os_error_2(C, NULL, WSAGetLastError(), 0); \
  1096. } while (0)
  1097. static s48_ref_t
  1098. s48_socketpair(s48_call_t call, s48_ref_t sch_af, s48_ref_t sch_type, s48_ref_t sch_protocol)
  1099. {
  1100. int status;
  1101. s48_ref_t sch_channel0, sch_channel1;
  1102. s48_ref_t sch_result;
  1103. int af = s48_extract_af(call, sch_af);
  1104. int socktype = s48_extract_socket_type(call, sch_type);
  1105. int protocol = s48_extract_long_2(call, sch_protocol);
  1106. socket_t listener, socket0, socket1;
  1107. long fd0, fd1;
  1108. stream_descriptor_t* stream_descriptor;
  1109. struct sockaddr_in addr;
  1110. socklen_t salen = sizeof(addr);
  1111. listener = WSASocket(af, socktype, protocol,
  1112. NULL,
  1113. 0, /* reserved */
  1114. WSA_FLAG_OVERLAPPED);
  1115. if (listener == INVALID_SOCKET)
  1116. s48_os_error_2(call, "s48_socketpair", WSAGetLastError(), 3,
  1117. sch_af, sch_type, sch_protocol);
  1118. addr.sin_family = AF_INET;
  1119. addr.sin_port = 0;
  1120. addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  1121. RETRY_OR_RAISE_NEG(call, status, bind(listener, (struct sockaddr*)&addr, sizeof(addr)));
  1122. RETRY_OR_RAISE_NEG(call, status, listen(listener, 1));
  1123. RETRY_OR_RAISE_NEG(call, status,
  1124. getsockname(listener, (struct sockaddr*)&addr, &salen));
  1125. socket1 = WSASocket(af, socktype, protocol,
  1126. NULL,
  1127. 0, /* reserved */
  1128. WSA_FLAG_OVERLAPPED);
  1129. if (socket1 == INVALID_SOCKET)
  1130. s48_os_error_2(call, "s48_socketpair", WSAGetLastError(), 3,
  1131. sch_af, sch_type, sch_protocol);
  1132. RETRY_OR_RAISE_NEG(call, status, connect(socket1, (struct sockaddr*) &addr, salen));
  1133. socket0 = accept(listener, (struct sockaddr*) &addr, &salen);
  1134. if (socket0 == INVALID_SOCKET)
  1135. s48_os_error_2(call, "s48_socketpair", WSAGetLastError(), 3,
  1136. sch_af, sch_type, sch_protocol);
  1137. closesocket(listener);
  1138. fd0 = allocate_fd(STREAM_SOCKET);
  1139. stream_descriptor = &(stream_descriptors[fd0]);
  1140. stream_descriptor->socket_data.socket = socket0;
  1141. fd1 = allocate_fd(STREAM_SOCKET);
  1142. stream_descriptor = &(stream_descriptors[fd1]);
  1143. stream_descriptor->socket_data.socket = socket1;
  1144. sch_channel0 = s48_add_channel_2(call, s48_channel_status_input_2(call),
  1145. s48_enter_string_latin_1_2(call, "socket"), fd0);
  1146. sch_channel1 = s48_add_channel_2(call, s48_channel_status_input_2(call),
  1147. s48_enter_string_latin_1_2(call, "socket"), fd1);
  1148. sch_result = s48_cons_2(call, sch_channel0, sch_channel1);
  1149. return sch_result;
  1150. }
  1151. /*
  1152. * dup() `socket_fd' and return an output channel holding the result.
  1153. *
  1154. * We have two versions, one to be called from C and one to be called
  1155. * from Scheme.
  1156. */
  1157. static s48_ref_t
  1158. dup_socket_channel(s48_call_t call, long socket_fd)
  1159. {
  1160. long output_fd;
  1161. SOCKET output_socket;
  1162. s48_ref_t output_channel;
  1163. stream_descriptor_t* stream_descriptor = &(stream_descriptors[socket_fd]);
  1164. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  1165. SOCKET socket = stream_descriptor->socket_data.socket;
  1166. stream_descriptor_t* output_stream_descriptor;
  1167. int buf_size;
  1168. WSAPROTOCOL_INFO protocolInfo;
  1169. if (WSADuplicateSocket(socket,
  1170. GetCurrentProcessId(),
  1171. &protocolInfo)
  1172. == SOCKET_ERROR)
  1173. s48_os_error_2(call, NULL, WSAGetLastError(), 1,
  1174. s48_enter_long_2(call, socket_fd));
  1175. output_socket = WSASocket(AF_INET,
  1176. SOCK_STREAM,
  1177. 0, /* protocol */
  1178. &protocolInfo,
  1179. 0, /* reserved */
  1180. WSA_FLAG_OVERLAPPED);
  1181. if (output_socket == INVALID_SOCKET)
  1182. s48_os_error_2(call, NULL, WSAGetLastError(), 1,
  1183. s48_enter_long_2(call, socket_fd));
  1184. buf_size = 0;
  1185. setsockopt(output_socket,
  1186. SOL_SOCKET, SO_SNDBUF,
  1187. (const char*)buf_size,
  1188. sizeof(buf_size));
  1189. output_fd = allocate_fd(STREAM_SOCKET);
  1190. output_stream_descriptor = &(stream_descriptors[output_fd]);
  1191. output_stream_descriptor->socket_data.socket = output_socket;
  1192. output_channel = s48_add_channel_2(call, s48_channel_status_output_2(call),
  1193. s48_enter_string_latin_1_2(call, "socket connection"),
  1194. output_fd);
  1195. if (!s48_channel_p_2(call, output_channel))
  1196. {
  1197. ps_close_fd(output_fd);
  1198. s48_raise_scheme_exception_2(call, s48_extract_long_2(call, output_channel), 0);
  1199. }
  1200. return output_channel;
  1201. }
  1202. static s48_ref_t
  1203. s48_dup_socket_channel(s48_call_t call, s48_ref_t channel)
  1204. {
  1205. int socket_fd;
  1206. s48_check_channel_2(call, channel);
  1207. socket_fd = s48_unsafe_extract_long_2(call, s48_unsafe_channel_os_index_2(call, channel));
  1208. return dup_socket_channel(call, socket_fd);
  1209. }
  1210. /*
  1211. * Given a bound socket, accept a connection and return a pair of the
  1212. * input channel and the raw socket address.
  1213. *
  1214. * If the accept fails because the client hasn't connected yet, then we
  1215. * return #f.
  1216. *
  1217. * If it fails for any other reason, then an exception is raised.
  1218. */
  1219. /*
  1220. * This guy finally registers the completed accept.
  1221. */
  1222. static VOID CALLBACK
  1223. accept_callback(DWORD dwParam)
  1224. {
  1225. stream_descriptor_t* stream_descriptor = (stream_descriptor_t*)dwParam;
  1226. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  1227. s48_add_ready_fd(callback_data->fd, PSTRUE, PSFALSE, (long)0);
  1228. }
  1229. /*
  1230. * This guy just waits for the event triggered by an accept,
  1231. * and notifies the VM.
  1232. */
  1233. static DWORD WINAPI
  1234. hatch_thread_proc(LPVOID lpParameter)
  1235. {
  1236. stream_descriptor_t* stream_descriptor = (stream_descriptor_t*)lpParameter;
  1237. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  1238. WaitForSingleObject(callback_data->overlap.hEvent, INFINITE);
  1239. if (!QueueUserAPC(stream_descriptor->socket_data.hatch_callback,
  1240. s48_main_thread,
  1241. (DWORD)stream_descriptor))
  1242. {
  1243. fprintf(stderr, "QueueUserAPC failed\n");
  1244. exit(-1);
  1245. }
  1246. return 0;
  1247. }
  1248. static s48_ref_t
  1249. s48_accept(s48_call_t call, s48_ref_t channel, s48_ref_t retry_p)
  1250. {
  1251. long socket_fd;
  1252. BOOL status;
  1253. s48_ref_t input_channel;
  1254. stream_descriptor_t* stream_descriptor;
  1255. callback_data_t* callback_data;
  1256. SOCKET socket;
  1257. SOCKET accept_socket;
  1258. DWORD bytecount;
  1259. s48_check_channel_2(call, channel);
  1260. socket_fd = s48_unsafe_extract_long_2(call, s48_unsafe_channel_os_index_2(call, channel));
  1261. stream_descriptor = &(stream_descriptors[socket_fd]);
  1262. callback_data = &(stream_descriptor->callback_data);
  1263. socket = stream_descriptor->socket_data.socket;
  1264. if (s48_false_p_2(call, retry_p))
  1265. /* first time */
  1266. {
  1267. callback_data->overlap.Offset = 0; /* just making sure */
  1268. callback_data->overlap.OffsetHigh = 0;
  1269. callback_data->overlap.hEvent = CreateEvent(NULL, /* no security attributes */
  1270. TRUE, /* manual reset */
  1271. FALSE, /* nonsignaled */
  1272. NULL); /* name */
  1273. accept_socket = WSASocket(AF_INET,
  1274. SOCK_STREAM,
  1275. 0, /* protocol */
  1276. NULL,
  1277. 0, /* reserved */
  1278. WSA_FLAG_OVERLAPPED);
  1279. if (accept_socket == INVALID_SOCKET)
  1280. s48_os_error_2(call, "s48_accept", WSAGetLastError(), 2,
  1281. channel, retry_p);
  1282. stream_descriptor->socket_data.hatched_socket = accept_socket;
  1283. #define SOCKADDR_BUFFER_SIZE (sizeof(struct sockaddr_storage) + 16)
  1284. maybe_grow_callback_data_buffer(callback_data, SOCKADDR_BUFFER_SIZE * 2);
  1285. status = AcceptEx(socket,
  1286. accept_socket,
  1287. callback_data->buffer,
  1288. 0,
  1289. SOCKADDR_BUFFER_SIZE,
  1290. SOCKADDR_BUFFER_SIZE,
  1291. &bytecount,
  1292. &callback_data->overlap);
  1293. }
  1294. else
  1295. /* rebound */
  1296. {
  1297. DWORD numberOfBytesTransferred;
  1298. if (GetOverlappedResult((HANDLE)socket,
  1299. &callback_data->overlap,
  1300. &numberOfBytesTransferred,
  1301. FALSE)) /* don't block */
  1302. {
  1303. accept_socket = stream_descriptor->socket_data.hatched_socket;
  1304. status = TRUE;
  1305. }
  1306. else if (GetLastError() == ERROR_IO_INCOMPLETE)
  1307. return s48_false_2(call); /* still not done */
  1308. else
  1309. s48_os_error_2(call, "s48_accept", WSAGetLastError(), 2,
  1310. channel, retry_p);
  1311. }
  1312. /*
  1313. * Check for a connection.
  1314. */
  1315. if (status)
  1316. /* success */
  1317. {
  1318. long accept_socket_fd = allocate_fd(STREAM_SOCKET);
  1319. stream_descriptor_t* accept_stream_descriptor = &(stream_descriptors[accept_socket_fd]);
  1320. accept_stream_descriptor->socket_data.socket = accept_socket;
  1321. CloseHandle(callback_data->overlap.hEvent);
  1322. /* We just need to do it, or things behave ... unexpectedly.
  1323. * Go figure.
  1324. */
  1325. if (setsockopt(accept_socket,
  1326. SOL_SOCKET,
  1327. SO_UPDATE_ACCEPT_CONTEXT,
  1328. (char *)&socket,
  1329. sizeof(socket))
  1330. != 0)
  1331. s48_os_error_2(call, "s48_accept", WSAGetLastError(), 2,
  1332. channel, retry_p);
  1333. input_channel = s48_add_channel_2(call, s48_channel_status_input_2(call),
  1334. s48_enter_string_latin_1_2(call, "socket connection"),
  1335. accept_socket_fd);
  1336. if (!s48_channel_p_2(call, input_channel))
  1337. {
  1338. ps_close_fd(accept_socket_fd);
  1339. s48_raise_scheme_exception_2(call, s48_extract_long_2(call, input_channel), 0);
  1340. }
  1341. {
  1342. LPSOCKADDR localSockaddr = NULL;
  1343. int localSockaddrLength = 0;
  1344. LPSOCKADDR remoteSockaddr = NULL;
  1345. int remoteSockaddrLength = 0;
  1346. GetAcceptExSockaddrs(callback_data->buffer,
  1347. 0,
  1348. SOCKADDR_BUFFER_SIZE,
  1349. SOCKADDR_BUFFER_SIZE,
  1350. &localSockaddr,
  1351. &localSockaddrLength,
  1352. &remoteSockaddr,
  1353. &remoteSockaddrLength);
  1354. return s48_cons_2(call,
  1355. input_channel,
  1356. s48_enter_sockaddr(call,
  1357. (const struct sockaddr*)(remoteSockaddr),
  1358. remoteSockaddrLength));
  1359. }
  1360. }
  1361. if (WSAGetLastError() == ERROR_IO_PENDING)
  1362. {
  1363. DWORD id;
  1364. HANDLE thread_handle;
  1365. if (!s48_add_pending_fd(socket_fd, PSTRUE))
  1366. s48_out_of_memory_error_2(call);
  1367. stream_descriptor->socket_data.hatch_callback = accept_callback;
  1368. thread_handle = CreateThread(NULL, /* lpThreadAttributes */
  1369. 4096, /* dwStackSize */
  1370. (LPTHREAD_START_ROUTINE)hatch_thread_proc,
  1371. stream_descriptor,
  1372. 0, /* dwCreationFlags, */
  1373. &id);
  1374. return s48_false_2(call);
  1375. }
  1376. s48_os_error_2(call, "s48_accept", WSAGetLastError(), 2,
  1377. channel, retry_p);
  1378. /* not reachable: */
  1379. return s48_false_2(call);
  1380. }
  1381. /* Connect a socket to an address.
  1382. *
  1383. * If this succeeds, it returns an output channel for the connection.
  1384. * If it fails because the connect would block, add the socket to the
  1385. * pending queue (for output) and return #f.
  1386. * If it fails for any other reason, raise an exception.
  1387. */
  1388. /*
  1389. * We need to jump through the same hoops as with s48_accept.
  1390. */
  1391. static VOID CALLBACK
  1392. connect_callback(DWORD dwParam)
  1393. {
  1394. stream_descriptor_t* stream_descriptor = (stream_descriptor_t*)dwParam;
  1395. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  1396. s48_add_ready_fd(callback_data->fd, PSFALSE, PSFALSE, (long)0);
  1397. }
  1398. static s48_ref_t
  1399. s48_connect(s48_call_t call, s48_ref_t sch_channel,
  1400. s48_ref_t sch_address,
  1401. s48_ref_t sch_retry_p)
  1402. {
  1403. long socket_fd;
  1404. stream_descriptor_t* stream_descriptor;
  1405. callback_data_t* callback_data;
  1406. SOCKET socket;
  1407. int status;
  1408. s48_check_channel_2(call, sch_channel);
  1409. socket_fd = s48_unsafe_extract_long_2(call, s48_unsafe_channel_os_index_2(call, sch_channel));
  1410. stream_descriptor = &(stream_descriptors[socket_fd]);
  1411. callback_data = &(stream_descriptor->callback_data);
  1412. socket = stream_descriptor->socket_data.socket;
  1413. /*
  1414. * Try the connection. If it works we make an output channel and return it.
  1415. * The original socket channel will be used as the input channel.
  1416. */
  1417. /*
  1418. * ConnectEx, which works like AcceptEx, only exists on the very
  1419. * latest Windows versions.
  1420. */
  1421. status = WSAConnect(socket,
  1422. (const struct sockaddr*)s48_extract_value_pointer_2(call, sch_address, struct sockaddr),
  1423. s48_value_size_2(call, sch_address),
  1424. NULL, /* lpCallerData */
  1425. NULL, /* lpCalleeData */
  1426. NULL, /* lpSQOS */
  1427. NULL); /* lpGQOS */
  1428. /*
  1429. * Check for errors. If we need to wait we set up a callback
  1430. * and return #F to tell the Scheme procedure to try again.
  1431. */
  1432. if (status == 0)
  1433. /* success */
  1434. {
  1435. s48_unsafe_stob_set_2(call, sch_channel, s48_channel_status_offset, s48_channel_status_input_2(call));
  1436. return dup_socket_channel(call, socket_fd);
  1437. }
  1438. else if (WSAGetLastError() == WSAEWOULDBLOCK)
  1439. {
  1440. DWORD id;
  1441. HANDLE thread_handle;
  1442. if (!s48_add_pending_fd(socket_fd, PSFALSE))
  1443. s48_out_of_memory_error_2(call);
  1444. stream_descriptor->socket_data.hatch_callback = connect_callback;
  1445. thread_handle = CreateThread(NULL, /* lpThreadAttributes */
  1446. 4096, /* dwStackSize */
  1447. (LPTHREAD_START_ROUTINE)hatch_thread_proc,
  1448. stream_descriptor,
  1449. 0, /* dwCreationFlags, */
  1450. &id);
  1451. callback_data->overlap.hEvent = CreateEvent(NULL, /* no security attributes */
  1452. TRUE, /* manual reset */
  1453. FALSE, /* nonsignaled */
  1454. NULL); /* name */
  1455. if (WSAEventSelect(socket, callback_data->overlap.hEvent, FD_ACCEPT)
  1456. != 0)
  1457. s48_os_error_2(call, "s48_connect", WSAGetLastError(), 3,
  1458. sch_channel, sch_address, sch_retry_p);
  1459. return s48_false_2(call);
  1460. }
  1461. else
  1462. s48_os_error_2(call, "s48_connect", WSAGetLastError(), 3,
  1463. sch_channel, sch_address, sch_retry_p);
  1464. /* not reachable */
  1465. return s48_false_2(call);
  1466. }
  1467. /*
  1468. * Receive a message. Returns pair (<byte-count> . <sender>) or just
  1469. * <byte-count> if want_sender_p is false.
  1470. */
  1471. static s48_ref_t
  1472. s48_recvfrom(s48_call_t call, s48_ref_t sch_channel,
  1473. s48_ref_t sch_buffer, s48_ref_t sch_start, s48_ref_t sch_count,
  1474. s48_ref_t sch_flags,
  1475. s48_ref_t sch_want_sender_p,
  1476. s48_ref_t sch_retry_p)
  1477. {
  1478. int retry_p = !(s48_false_p_2(call, sch_retry_p));
  1479. int want_sender_p = !(s48_false_p_2(call, sch_want_sender_p));
  1480. int flags = s48_extract_msg_flags(call, sch_flags);
  1481. size_t buffer_size = s48_byte_vector_length_2(call, sch_buffer);
  1482. size_t start = s48_extract_unsigned_long_2(call, sch_start);
  1483. size_t count = s48_extract_unsigned_long_2(call, sch_count);
  1484. long socket_fd
  1485. = s48_unsafe_extract_long_2(call, s48_channel_os_index_2(call, sch_channel));
  1486. stream_descriptor_t* stream_descriptor = &(stream_descriptors[socket_fd]);
  1487. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  1488. socket_t socket = stream_descriptor->socket_data.socket;
  1489. if ((start + count) > buffer_size)
  1490. s48_assertion_violation_2(call, "s48_sendto", "buffer start or count is wrong", 3,
  1491. sch_buffer, sch_start, sch_count);
  1492. if (!retry_p)
  1493. {
  1494. DWORD numberOfBytesRecvd;
  1495. int wsa_status;
  1496. int block;
  1497. /* There's nothing in the buffer---need to do an actual read. */
  1498. maybe_grow_callback_data_buffer(callback_data, (size_t)count);
  1499. /* just making sure */
  1500. callback_data->overlap.Offset = 0;
  1501. callback_data->overlap.OffsetHigh = 0;
  1502. stream_descriptor->socket_data.wsabuf.len = count;
  1503. stream_descriptor->socket_data.wsabuf.buf = (char*)callback_data->buffer;
  1504. stream_descriptor->socket_data.recv_address_len
  1505. = sizeof(stream_descriptor->socket_data.recv_address);
  1506. block = 1; /* non-blocking */
  1507. ioctlsocket(socket, FIONBIO, &block);
  1508. wsa_status = WSARecvFrom(socket,
  1509. (LPWSABUF)&stream_descriptor->socket_data.wsabuf,
  1510. 1,
  1511. &numberOfBytesRecvd,
  1512. &flags,
  1513. want_sender_p
  1514. ? (struct sockaddr*)&stream_descriptor->socket_data.recv_address
  1515. : NULL,
  1516. want_sender_p
  1517. ? &stream_descriptor->socket_data.recv_address_len
  1518. : NULL,
  1519. (LPOVERLAPPED)callback_data,
  1520. recv_completed);
  1521. if ((wsa_status == 0)
  1522. || (WSAGetLastError() == WSA_IO_PENDING))
  1523. {
  1524. if (! (s48_add_pending_fd(socket_fd, PSTRUE)))
  1525. s48_out_of_memory_error_2(call);
  1526. return s48_false_2(call);
  1527. }
  1528. else
  1529. s48_os_error_2(call, "s48_recvfrom", WSAGetLastError(), 7,
  1530. sch_channel, sch_buffer, sch_start, sch_count,
  1531. sch_flags, sch_want_sender_p, sch_retry_p);
  1532. }
  1533. else
  1534. {
  1535. /* rebound */
  1536. size_t size = callback_data->current_size;
  1537. memcpy(s48_extract_byte_vector_2(call, sch_buffer) + start,
  1538. callback_data->current, size);
  1539. callback_data->current_size = 0;
  1540. callback_data->current = stream_descriptor->callback_data.buffer;
  1541. if (want_sender_p)
  1542. {
  1543. s48_ref_t sch_count, sch_saddr;
  1544. s48_ref_t sch_result;
  1545. sch_count = s48_enter_unsigned_long_2(call, size);
  1546. sch_saddr
  1547. = s48_enter_sockaddr(call, (struct sockaddr *)&stream_descriptor->socket_data.recv_address,
  1548. stream_descriptor->socket_data.recv_address_len);
  1549. sch_result = s48_cons_2(call, sch_count, sch_saddr);
  1550. return sch_result;
  1551. }
  1552. else
  1553. return s48_enter_unsigned_long_2(call, size);
  1554. }
  1555. }
  1556. static s48_ref_t
  1557. s48_sendto(s48_call_t call, s48_ref_t sch_channel,
  1558. s48_ref_t sch_buffer, s48_ref_t sch_start, s48_ref_t sch_count,
  1559. s48_ref_t sch_flags,
  1560. s48_ref_t sch_saddr,
  1561. s48_ref_t sch_retry_p)
  1562. {
  1563. int retry_p = !(s48_false_p_2(call, sch_retry_p));
  1564. const struct sockaddr *sa
  1565. = s48_extract_value_pointer_2(call, sch_saddr, const struct sockaddr);
  1566. socklen_t salen = s48_value_size_2(call, sch_saddr);
  1567. int flags = s48_extract_msg_flags(call, sch_flags);
  1568. size_t buffer_size = s48_byte_vector_length_2(call, sch_buffer);
  1569. size_t start = s48_extract_unsigned_long_2(call, sch_start);
  1570. size_t count = s48_extract_unsigned_long_2(call, sch_count);
  1571. long socket_fd
  1572. = s48_unsafe_extract_long_2(call, s48_channel_os_index_2(call, sch_channel));
  1573. stream_descriptor_t* stream_descriptor = &(stream_descriptors[socket_fd]);
  1574. callback_data_t* callback_data = &(stream_descriptor->callback_data);
  1575. socket_t socket = stream_descriptor->socket_data.socket;
  1576. if ((start + count) > buffer_size)
  1577. s48_assertion_violation_2(call, "s48_sendto", "buffer start or count is wrong", 3,
  1578. sch_buffer, sch_start, sch_count);
  1579. if (!retry_p)
  1580. {
  1581. DWORD numberOfBytesSent;
  1582. DWORD flags = 0;
  1583. int wsa_status;
  1584. char* buffer = s48_extract_byte_vector_readonly_2(call, sch_buffer) + start;
  1585. maybe_grow_callback_data_buffer(callback_data, count);
  1586. memcpy(callback_data->buffer, buffer, count);
  1587. /* just making sure */
  1588. callback_data->overlap.Offset = 0;
  1589. callback_data->overlap.OffsetHigh = 0;
  1590. stream_descriptor->socket_data.wsabuf.len = count;
  1591. stream_descriptor->socket_data.wsabuf.buf = (char*)callback_data->buffer;
  1592. wsa_status = WSASendTo(socket,
  1593. (LPWSABUF)&stream_descriptor->socket_data.wsabuf,
  1594. 1,
  1595. &numberOfBytesSent,
  1596. flags,
  1597. s48_extract_value_pointer_2(call, sch_saddr, struct sockaddr),
  1598. s48_value_size_2(call, sch_saddr),
  1599. (LPOVERLAPPED)callback_data,
  1600. send_completed);
  1601. if ((wsa_status == 0)
  1602. | (WSAGetLastError() == WSA_IO_PENDING))
  1603. {
  1604. if (! (s48_add_pending_fd(socket_fd, PSFALSE)))
  1605. s48_out_of_memory_error_2(call);
  1606. return s48_false_2(call);
  1607. }
  1608. else
  1609. s48_os_error_2(call, "s48_sendto", WSAGetLastError(), 7,
  1610. sch_channel, sch_buffer, sch_start, sch_count,
  1611. sch_flags, sch_saddr, sch_retry_p);
  1612. }
  1613. else
  1614. /* rebound */
  1615. return s48_enter_long_2(call, stream_descriptor->file_regular_data.current_offset);
  1616. }
  1617. void
  1618. s48_init_os_sockets(void)
  1619. {
  1620. WORD wVersionRequested;
  1621. WSADATA wsaData;
  1622. /* This is the *highest* version we can use. Great. */
  1623. wVersionRequested = MAKEWORD(2, 0);
  1624. if (WSAStartup(wVersionRequested, &wsaData) != 0)
  1625. {
  1626. fprintf(stderr, "Windows Sockets startup failed.\n");
  1627. exit(-1);
  1628. }
  1629. S48_EXPORT_FUNCTION(s48_socket);
  1630. S48_EXPORT_FUNCTION(s48_socketpair);
  1631. S48_EXPORT_FUNCTION(s48_dup_socket_channel);
  1632. S48_EXPORT_FUNCTION(s48_accept);
  1633. S48_EXPORT_FUNCTION(s48_connect);
  1634. S48_EXPORT_FUNCTION(s48_recvfrom);
  1635. S48_EXPORT_FUNCTION(s48_sendto);
  1636. }