1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
|
#include "include.h"
#include "winsock2.h"
#include "windows.h"
#include "errno.h"
#include "signal.h"
#include "stdlib.h"
#ifdef DODEBUG
#define dprintf(s,arg) emsg(s,arg)
#else
#define dprintf(s,arg)
#endif
#ifndef EWOULDBLOCK
#define EWOULDBLOCK EAGAIN
#endif
#include "errno.h"
#include <tchar.h>
#include <time.h>
#include <winsock2.h>
#include <winsock.h>
#define Tcl_GetErrno() errno
#define Tcl_SetErrno(n) errno=n
/*
* The following structure contains pointers to all of the WinSock API entry
* points used by Tcl. It is initialized by InitSockets. Since we
* dynamically load Winsock.dll on demand, we must use this function table
* to refer to functions in the socket API.
*/
static struct {
HINSTANCE hInstance; /* Handle to WinSock library. */
SOCKET (PASCAL FAR *accept)(SOCKET s, struct sockaddr FAR *addr,
int FAR *addrlen);
int (PASCAL FAR *bind)(SOCKET s, const struct sockaddr FAR *addr,
int namelen);
int (PASCAL FAR *closesocket)(SOCKET s);
int (PASCAL FAR *connect)(SOCKET s, const struct sockaddr FAR *name,
int namelen);
int (PASCAL FAR *ioctlsocket)(SOCKET s, long cmd, u_long FAR *argp);
int (PASCAL FAR *getsockopt)(SOCKET s, int level, int optname,
char FAR * optval, int FAR *optlen);
u_short (PASCAL FAR *htons)(u_short hostshort);
unsigned long (PASCAL FAR *inet_addr)(const char FAR * cp);
char FAR * (PASCAL FAR *inet_ntoa)(struct in_addr in);
int (PASCAL FAR *listen)(SOCKET s, int backlog);
u_short (PASCAL FAR *ntohs)(u_short netshort);
int (PASCAL FAR *recv)(SOCKET s, char FAR * buf, int len, int flags);
int (PASCAL FAR *select)(int nfds, fd_set FAR * readfds,
fd_set FAR * writefds, fd_set FAR * exceptfds,
const struct timeval FAR * tiemout);
int (PASCAL FAR *send)(SOCKET s, const char FAR * buf, int len, int flags);
int (PASCAL FAR *setsockopt)(SOCKET s, int level, int optname,
const char FAR * optval, int optlen);
int (PASCAL FAR *shutdown)(SOCKET s, int how);
SOCKET (PASCAL FAR *socket)(int af, int type, int protocol);
struct hostent FAR * (PASCAL FAR *gethostbyname)(const char FAR * name);
struct hostent FAR * (PASCAL FAR *gethostbyaddr)(const char FAR *addr,
int addrlen, int addrtype);
int (PASCAL FAR *gethostname)(char FAR * name, int namelen);
int (PASCAL FAR *getpeername)(SOCKET sock, struct sockaddr FAR *name,
int FAR *namelen);
struct servent FAR * (PASCAL FAR *getservbyname)(const char FAR * name,
const char FAR * proto);
int (PASCAL FAR *getsockname)(SOCKET sock, struct sockaddr FAR *name,
int FAR *namelen);
int (PASCAL FAR *WSAStartup)(WORD wVersionRequired, LPWSADATA lpWSAData);
int (PASCAL FAR *WSACleanup)(void);
int (PASCAL FAR *WSAGetLastError)(void);
int (PASCAL FAR *WSAAsyncSelect)(SOCKET s, HWND hWnd, u_int wMsg,
long lEvent);
} winSock;
static int SocketsEnabled();
static void close_winsock();
extern void doReverse ( char *s, int n );
/*
*----------------------------------------------------------------------
*
* InitSockets --
*
* Initialize the socket module. Attempts to load the wsock32.dll
* library and set up the winSock function table. If successful,
* registers the event window for the socket notifier code.
*
* Assumes Mutex is held.
*
* Results:
* None.
*
* Side effects:
* Dynamically loads wsock32.dll, and registers a new window
* class and creates a window for use in asynchronous socket
* notification.
*
*----------------------------------------------------------------------
*/
static void
InitSockets()
{
WSADATA wsaData;
static int initialized;
if (! initialized) {
initialized = 1;
winSock.hInstance = LoadLibraryA("wsock32.dll");
/*
* Initialize the function table.
*/
if (!SocketsEnabled()) {
return;
}
winSock.accept = (SOCKET (PASCAL FAR *)(SOCKET s,
struct sockaddr FAR *addr, int FAR *addrlen))
GetProcAddress(winSock.hInstance, "accept");
winSock.bind = (int (PASCAL FAR *)(SOCKET s,
const struct sockaddr FAR *addr, int namelen))
GetProcAddress(winSock.hInstance, "bind");
winSock.closesocket = (int (PASCAL FAR *)(SOCKET s))
GetProcAddress(winSock.hInstance, "closesocket");
winSock.connect = (int (PASCAL FAR *)(SOCKET s,
const struct sockaddr FAR *name, int namelen))
GetProcAddress(winSock.hInstance, "connect");
winSock.ioctlsocket = (int (PASCAL FAR *)(SOCKET s, long cmd,
u_long FAR *argp))
GetProcAddress(winSock.hInstance, "ioctlsocket");
winSock.getsockopt = (int (PASCAL FAR *)(SOCKET s,
int level, int optname, char FAR * optval, int FAR *optlen))
GetProcAddress(winSock.hInstance, "getsockopt");
winSock.htons = (u_short (PASCAL FAR *)(u_short hostshort))
GetProcAddress(winSock.hInstance, "htons");
winSock.inet_addr = (unsigned long (PASCAL FAR *)(const char FAR *cp))
GetProcAddress(winSock.hInstance, "inet_addr");
winSock.inet_ntoa = (char FAR * (PASCAL FAR *)(struct in_addr in))
GetProcAddress(winSock.hInstance, "inet_ntoa");
winSock.listen = (int (PASCAL FAR *)(SOCKET s, int backlog))
GetProcAddress(winSock.hInstance, "listen");
winSock.ntohs = (u_short (PASCAL FAR *)(u_short netshort))
GetProcAddress(winSock.hInstance, "ntohs");
winSock.recv = (int (PASCAL FAR *)(SOCKET s, char FAR * buf,
int len, int flags)) GetProcAddress(winSock.hInstance, "recv");
winSock.select = (int (PASCAL FAR *)(int nfds, fd_set FAR * readfds,
fd_set FAR * writefds, fd_set FAR * exceptfds,
const struct timeval FAR * tiemout))
GetProcAddress(winSock.hInstance, "select");
winSock.send = (int (PASCAL FAR *)(SOCKET s, const char FAR * buf,
int len, int flags)) GetProcAddress(winSock.hInstance, "send");
winSock.setsockopt = (int (PASCAL FAR *)(SOCKET s, int level,
int optname, const char FAR * optval, int optlen))
GetProcAddress(winSock.hInstance, "setsockopt");
winSock.shutdown = (int (PASCAL FAR *)(SOCKET s, int how))
GetProcAddress(winSock.hInstance, "shutdown");
winSock.socket = (SOCKET (PASCAL FAR *)(int af, int type,
int protocol)) GetProcAddress(winSock.hInstance, "socket");
winSock.gethostbyaddr = (struct hostent FAR * (PASCAL FAR *)
(const char FAR *addr, int addrlen, int addrtype))
GetProcAddress(winSock.hInstance, "gethostbyaddr");
winSock.gethostbyname = (struct hostent FAR * (PASCAL FAR *)
(const char FAR *name))
GetProcAddress(winSock.hInstance, "gethostbyname");
winSock.gethostname = (int (PASCAL FAR *)(char FAR * name,
int namelen)) GetProcAddress(winSock.hInstance, "gethostname");
winSock.getpeername = (int (PASCAL FAR *)(SOCKET sock,
struct sockaddr FAR *name, int FAR *namelen))
GetProcAddress(winSock.hInstance, "getpeername");
winSock.getservbyname = (struct servent FAR * (PASCAL FAR *)
(const char FAR * name, const char FAR * proto))
GetProcAddress(winSock.hInstance, "getservbyname");
winSock.getsockname = (int (PASCAL FAR *)(SOCKET sock,
struct sockaddr FAR *name, int FAR *namelen))
GetProcAddress(winSock.hInstance, "getsockname");
winSock.WSAStartup = (int (PASCAL FAR *)(WORD wVersionRequired,
LPWSADATA lpWSAData)) GetProcAddress(winSock.hInstance, "WSAStartup");
winSock.WSACleanup = (int (PASCAL FAR *)(void))
GetProcAddress(winSock.hInstance, "WSACleanup");
winSock.WSAGetLastError = (int (PASCAL FAR *)(void))
GetProcAddress(winSock.hInstance, "WSAGetLastError");
winSock.WSAAsyncSelect = (int (PASCAL FAR *)(SOCKET s, HWND hWnd,
u_int wMsg, long lEvent))
GetProcAddress(winSock.hInstance, "WSAAsyncSelect");
/*
* Now check that all fields are properly initialized. If not, return
* zero to indicate that we failed to initialize properly.
*/
if ((winSock.hInstance == NULL) ||
(winSock.accept == NULL) ||
(winSock.bind == NULL) ||
(winSock.closesocket == NULL) ||
(winSock.connect == NULL) ||
(winSock.ioctlsocket == NULL) ||
(winSock.getsockopt == NULL) ||
(winSock.htons == NULL) ||
(winSock.inet_addr == NULL) ||
(winSock.inet_ntoa == NULL) ||
(winSock.listen == NULL) ||
(winSock.ntohs == NULL) ||
(winSock.recv == NULL) ||
(winSock.select == NULL) ||
(winSock.send == NULL) ||
(winSock.setsockopt == NULL) ||
(winSock.socket == NULL) ||
(winSock.gethostbyname == NULL) ||
(winSock.gethostbyaddr == NULL) ||
(winSock.gethostname == NULL) ||
(winSock.getpeername == NULL) ||
(winSock.getservbyname == NULL) ||
(winSock.getsockname == NULL) ||
(winSock.WSAStartup == NULL) ||
(winSock.WSACleanup == NULL) ||
(winSock.WSAGetLastError == NULL) ||
(winSock.WSAAsyncSelect == NULL)) {
goto unloadLibrary;
}
/*
* Initialize the winsock library and check the version number.
*/
if ((*winSock.WSAStartup)(MAKEWORD(2,2), &wsaData) != 0) {
emsg("unloading");
goto unloadLibrary;
}
#ifdef WSA_VERSION_REQD
if (wsaData.wVersion != WSA_VERSION_REQD) {
(*winSock.WSACleanup)();
goto unloadLibrary;
}
#endif
}
atexit(close_winsock);
return;
/*
* Check for per-thread initialization.
*/
unloadLibrary:
FreeLibrary(winSock.hInstance);
winSock.hInstance = NULL;
return;
}
/*
*----------------------------------------------------------------------
*
* SocketsEnabled --
*
* Check that the WinSock DLL is loaded and ready.
*
* Results:
* 1 if it is.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
/* ARGSUSED */
static int
SocketsEnabled()
{
int enabled;
enabled = (winSock.hInstance != NULL);
if (!enabled) {
InitSockets();
enabled = (winSock.hInstance != NULL);
}
return enabled;
}
static void close_winsock()
{
if (winSock.hInstance != NULL)
(*winSock.WSACleanup)();
}
/*
*----------------------------------------------------------------------
*
* CreateSocketAddress --
*
* This function initializes a sockaddr structure for a host and port.
*
* Results:
* 1 if the host was valid, 0 if the host could not be converted to
* an IP address.
*
* Side effects:
* Fills in the *sockaddrPtr structure.
*
*----------------------------------------------------------------------
*/
static int
CreateSocketAddress(sockaddrPtr, host, port)
struct sockaddr_in *sockaddrPtr; /* Socket address */
char *host; /* Host. NULL implies INADDR_ANY */
int port; /* Port number */
{
struct hostent *hostent; /* Host database entry */
struct in_addr addr; /* For 64/32 bit madness */
/*
* Check that WinSock is initialized; do not call it if not, to
* prevent system crashes. This can happen at exit time if the exit
* handler for WinSock ran before other exit handlers that want to
* use sockets.
*/
if (!SocketsEnabled()) {
Tcl_SetErrno(EFAULT);
return 0;
}
(void) memset((char *) sockaddrPtr, '\0', sizeof(struct sockaddr_in));
sockaddrPtr->sin_family = AF_INET;
sockaddrPtr->sin_port = (*winSock.htons)((short) (port & 0xFFFF));
if (host == NULL) {
addr.s_addr = INADDR_ANY;
} else {
addr.s_addr = (*winSock.inet_addr)(host);
if (addr.s_addr == INADDR_NONE) {
hostent = (*winSock.gethostbyname)(host);
if (hostent != NULL) {
memcpy((char *) &addr,
(char *) hostent->h_addr_list[0],
(size_t) hostent->h_length);
} else {
#ifdef EHOSTUNREACH
Tcl_SetErrno(EHOSTUNREACH);
#else
#ifdef ENXIO
Tcl_SetErrno(ENXIO);
#endif
#endif
return 0; /* Error. */
}
}
}
/*
* NOTE: On 64 bit machines the assignment below is rumored to not
* do the right thing. Please report errors related to this if you
* observe incorrect behavior on 64 bit machines such as DEC Alphas.
* Should we modify this code to do an explicit memcpy?
*/
sockaddrPtr->sin_addr.s_addr = addr.s_addr;
return 1; /* Success. */
}
#ifdef DEBUG
static void myerr(char *s,int d)
{
if (0)
emsg(s,d);
}
#else
#define myerr(a,b)
#endif
/*
*----------------------------------------------------------------------
*
* CreateSocket --
*
* This function opens a new socket and initializes the
* return -1 on failure, or else an fd
*
*----------------------------------------------------------------------
*/
static int myerror;
int
CreateSocket(port, host, server, myaddr, myport, async)
int port; /* Port number to open. */
char *host; /* Name of host on which to open port. */
int server; /* 1 if socket should be a server socket,
* else 0 for a client socket. */
char *myaddr; /* Optional client-side address */
int myport; /* Optional client-side port */
int async; /* If nonzero, connect client socket
* asynchronously. */
{
u_long flag = 1; /* Indicates nonblocking mode. */
int asyncConnect = 0; /* Will be 1 if async connect is
* in progress. */
struct sockaddr_in sockaddr; /* Socket address */
struct sockaddr_in mysockaddr; /* Socket address for client */
SOCKET sock = 0;
/*
* Check that WinSock is initialized; do not call it if not, to
* prevent system crashes. This can happen at exit time if the exit
* handler for WinSock ran before other exit handlers that want to
* use sockets.
*/
if (!SocketsEnabled()) {
return -1;
}
if (! CreateSocketAddress(&sockaddr, host, port)) {
goto error;
}
if ((myaddr != NULL || myport != 0) &&
! CreateSocketAddress(&mysockaddr, myaddr, myport)) {
goto error;
}
sock = (*winSock.socket)(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
goto error;
}
/*
* Win-NT has a misfeature that sockets are inherited in child
* processes by default. Turn off the inherit bit.
*/
SetHandleInformation( (HANDLE) sock, HANDLE_FLAG_INHERIT, 0 );
/*
* Set kernel space buffering
*/
/* TclSockMinimumBuffers(sock, TCP_BUFFER_SIZE); */
if (server) {
/*
* Bind to the specified port. Note that we must not call setsockopt
* with SO_REUSEADDR because Microsoft allows addresses to be reused
* even if they are still in use.
*
* Bind should not be affected by the socket having already been
* set into nonblocking mode. If there is trouble, this is one place
* to look for bugs.
*/
if ((*winSock.bind)(sock, (struct sockaddr *) &sockaddr,
sizeof(sockaddr)) == SOCKET_ERROR) {
goto error;
}
/*
* Set the maximum number of pending connect requests to the
* max value allowed on each platform (Win32 and Win32s may be
* different, and there may be differences between TCP/IP stacks).
*/
if ((*winSock.listen)(sock, SOMAXCONN) == SOCKET_ERROR) {
goto error;
}
} else {
/*
* Try to bind to a local port, if specified.
*/
if (myaddr != NULL || myport != 0) {
if ((*winSock.bind)(sock, (struct sockaddr *) &mysockaddr,
sizeof(struct sockaddr)) == SOCKET_ERROR) {
goto error;
}
}
/*
* Set the socket into nonblocking mode if the connect should be
* done in the background.
*/
if (async) {
if ((*winSock.ioctlsocket)(sock, FIONBIO, &flag) == SOCKET_ERROR) {
goto error;
}
}
/*
* Attempt to connect to the remote socket.
*/
if ((*winSock.connect)(sock, (struct sockaddr *) &sockaddr,
sizeof(sockaddr)) == SOCKET_ERROR) {
myerror = (*winSock.WSAGetLastError)();
if (myerror != WSAEWOULDBLOCK) {
goto error;
}
}
/*
* The connection is progressing in the background.
*/
asyncConnect = 1;
}
/*
* Set up the select mask for read/write events. If the connect
* attempt has not completed, include connect events.
*/
/*
* Register for interest in events in the select mask. Note that this
* automatically places the socket into non-blocking mode.
*/
(*winSock.ioctlsocket)(sock, FIONBIO, &flag);
/* SendMessage(tsdPtr->hwnd, SOCKET_SELECT,
(WPARAM) SELECT, (LPARAM) infoPtr);
*/
return sock;
error:
/* TclWinConvertWSAError((*winSock.WSAGetLastError)());
if (interp != NULL) {
Tcl_AppendResult(interp, "couldn't open socket: ",
Tcl_PosixError(interp), (char *) NULL);
}
*/
if (sock != INVALID_SOCKET) {
(*winSock.closesocket)(sock);
}
return -1;
}
/*
*----------------------------------------------------------------------
*
* TcpOutputProc --
*
* This procedure is called by the generic IO level to write data
* to a socket based channel.
*
* Results:
* The number of bytes written or -1 on failure.
*
* Side effects:
* Produces output on the socket.
*
*----------------------------------------------------------------------
*/
int
TcpOutputProc ( int fd, char *buf, int toWrite, int *errorCodePtr, int block )
{
int bytesWritten=0;
int error;
int count=1000*30;
*errorCodePtr = 0;
/*
* Check that WinSock is initialized; do not call it if not, to
* prevent system crashes. This can happen at exit time if the exit
* handler for WinSock ran before other exit handlers that want to
* use sockets.
*/
if (!SocketsEnabled()) {
*errorCodePtr = EFAULT;
return -1;
}
while (block) {
AGAIN:
/*
* In the blocking case, wait until the file becomes writable
* or closed and try again.
*/
{ fd_set writefds;
int res;
struct timeval timeout;
FD_ZERO(&writefds);
FD_SET(fd,&writefds);
timeout.tv_sec = (block == 0 ? 0 : 60*60*24*30);
timeout.tv_usec = 0;
if (!(res=(*winSock.select)(fd+1,NULL,&writefds,NULL,&timeout)) )
{ bytesWritten = -1;
break;
}
}
bytesWritten = (*winSock.send)(fd, buf, toWrite, 0);
if (bytesWritten != SOCKET_ERROR) {
/*
* Since Windows won't generate a new write event until we hit
* an overflow condition, we need to force the event loop to
* poll until the condition changes.
*/
break;
}
/*
* Check for error condition or overflow. In the event of overflow, we
* need to clear the FD_WRITE flag so we can detect the next writable
* event. Note that Windows only sends a new writable event after a
* send fails with WSAEWOULDBLOCK.
*/
error = (*winSock.WSAGetLastError)();
if (error == WSAEWOULDBLOCK) {
*errorCodePtr = EWOULDBLOCK;
CHECK_INTERRUPT;
Sleep(30);
bytesWritten = -1;
if (--count < 0)
break;
else goto AGAIN;
} else {
/* TclWinConvertWSAError(error); */
*errorCodePtr = EINVAL;
bytesWritten = -1;
break;
}
}
return bytesWritten;
}
/*
getCharGclSocket(strm,block) -- get one character from a socket
stream.
Results: a character or EOF if at end of file
Side Effects: The buffer may be filled, and the fill pointer
of the buffer may be changed.
*/
int getCharGclSocket(strm,block)
object strm;
object block;
{
object bufp = SOCKET_STREAM_BUFFER(strm);
if (!SocketsEnabled()) {
return -1;
}
if (bufp->ust.ust_fillp > 0) {
dprintf("getchar returns (%c)\n",bufp->ust.ust_self[-1+(bufp->ust.ust_fillp)]);
return bufp->ust.ust_self[--(bufp->ust.ust_fillp)];
}
else {
fd_set readfds;
struct timeval timeout;
int fd = SOCKET_STREAM_FD(strm);
if (1)
{ int high;
AGAIN:
/* under cygwin a too large timout like (1<<30) does not work */
timeout.tv_sec = (block != Ct ? 0 : 0);
timeout.tv_usec = (block != Ct ? 0 : 10000);
FD_ZERO(&readfds);
FD_SET(fd,&readfds);
high = (*winSock.select)(fd+1,&readfds,NULL,NULL,&timeout);
if (high > 0)
{ object bufp = SOCKET_STREAM_BUFFER(strm);
int n;
n = (*winSock.recv)(fd,bufp->st.st_self ,bufp->ust.ust_dim,0);
doReverse(bufp->st.st_self,n);
bufp->ust.ust_fillp=n;
if (n > 0)
{
return bufp->ust.ust_self[--(bufp->ust.ust_fillp)];
}
else
{ return EOF;
FEerror("select said there was stuff there but there was not",0);
}
}
CHECK_INTERRUPT;
/* probably a signal interrupted us.. */
if (block == Ct)
goto AGAIN;
return EOF;
}
}
}
void tcpCloseSocket(SOCKET fd)
{
(*winSock.closesocket)(fd);
}
void ungetCharGclSocket ( int c, object strm)
{ object bufp = SOCKET_STREAM_BUFFER(strm);
if (c == EOF) return;
dprintf("pushing back %c\n",c);
if (bufp->ust.ust_fillp < bufp->ust.ust_dim) {
bufp->ust.ust_self[(bufp->ust.ust_fillp)++]=c;
} else {
FEerror("Tried to unget too many chars",0);
}
}
void doReverse ( char *s, int n )
{
char *p=&s[n-1];
int m = n/2;
while (--m>=0) {
int tem = *s;
*s = *p;
*p = tem;
s++; p--;
}
}
/*
void
sigint()
{
install_default_signals();
terminal_interrupt(1);
}
*/
#if 0
BOOL WINAPI inthandler(DWORD i)
{
emsg("in handler %d",i);
terminal_interrupt(1);
return TRUE;
}
#endif
void
alarm(int n) {
return;
}
/* to do:
in the lisp:
start a shared named file based on the pid.
Then others will be able to send us messages, eg:interrupt!
and we will check this value in the CHECK_INTERRUPT
places..
Then a little program like test4 or test3 can change
the memory.
*/
static struct {
HANDLE handle;
LPVOID address;
DWORD length ;
char name[20] ;
} sharedMemory = {0,0,0x10000} ;
void sigterm()
{
exit(0);
}
#ifdef SIGABRT
void sigabrt()
{
do_gcl_abort();
}
#endif
void sigkill()
{
do_gcl_abort();
}
static void
init_signals_pendingPtr() {
static unsigned int where;
if (sharedMemory.address) {
signalsPendingPtr = sharedMemory.address;
} else {
signalsPendingPtr = (void *)&where;
}
gcl_signal(SIGKILL,sigkill);
gcl_signal(SIGTERM,sigterm);
#ifdef SIGABRT
gcl_signal(SIGABRT,sigabrt);
#endif
}
void
close_shared_memory() {
if (sharedMemory.handle)
CloseHandle(sharedMemory.handle);
sharedMemory.handle = NULL;
if (sharedMemory.address)
UnmapViewOfFile(sharedMemory.address);
sharedMemory.address = NULL;
init_signals_pendingPtr();
}
void
init_shared_memory (void) {
static int n;
if (n) return;
n=1;
sharedMemory.address=0;
init_signals_pendingPtr();
return;
sprintf(sharedMemory.name,"gcl-%d",getpid());
sharedMemory.handle =
CreateFileMapping((HANDLE)-1,NULL,PAGE_READWRITE,0,sharedMemory.length ,TEXT(sharedMemory.name));
if (sharedMemory.handle == NULL)
error("CreateFileMapping failed");
sharedMemory.address =
MapViewOfFile(sharedMemory.handle, /* Handle to mapping object. */
FILE_MAP_WRITE, /* Read/write permission */
0, /* Max. object size. */
0, /* Size of hFile. */
0); /* Map entire file. */
if (sharedMemory.address == NULL)
error("MapViewOfFile failed");
init_signals_pendingPtr();
atexit(close_shared_memory);
}
/* The only signal REALLY handled somewhat under mingw is the
SIGINT, and we need to make the following allow blocking of this.
by for example taking the signal and then recording we got it,
but delivering it later in the unblock code time ... ie in the
*/
static sigset_t _current_set=0;
void
sigemptyset( sigset_t *set)
{
*set = 0;
}
void
sigaddset( sigset_t *set, int n)
{
*set |= (1 << n);
}
int
sigismember ( sigset_t *set, int n)
{
return ((*set & (1 << n)) != 0);
}
int
sigprocmask (int how , const sigset_t *set,sigset_t *oldset)
{
if (oldset) *oldset = _current_set;
if (set) {
switch (how)
{
case SIG_BLOCK:
_current_set |= *set;
break;
case SIG_UNBLOCK:
_current_set &= ~(*set);
break;
case SIG_SETMASK:
_current_set = *set;
break;
}
}
return 0;
}
char *GCLExeName ( void )
{
static char module_name_buf[128];
char *rv = NULL;
module_name_buf[0] = 0;
DWORD result = GetModuleFileName ( (HMODULE) NULL, (LPTSTR) &module_name_buf, 128 );
if ( result != 0 ) {
rv = module_name_buf;
}
return ( (char *) rv );
}
int
vsystem(const char *command) {
STARTUPINFO s={0};
PROCESS_INFORMATION p={0};
long unsigned int e;
massert(CreateProcess(NULL,(void *)command,NULL,NULL,FALSE,0,NULL,NULL,&s,&p));
massert(!WaitForSingleObject(p.hProcess,INFINITE));
massert(GetExitCodeProcess(p.hProcess,&e));
massert(CloseHandle(p.hProcess));
massert(CloseHandle(p.hThread));
return e;
}
|