1
/* the following file compiles under win95 using cygwinb19 */
7
#define dprintf(s,arg) \
8
do {fprintf(stderr,s,arg); \
12
#define dprintf(s,arg)
20
#include <sys/types.h>
29
/************* for the sockets ******************/
30
#include <sys/socket.h> /* struct sockaddr, SOCK_STREAM, ... */
32
# include <sys/utsname.h> /* uname system call. */
34
#include <netinet/in.h> /* struct in_addr, struct sockaddr_in */
35
#include <arpa/inet.h> /* inet_ntoa() */
36
#include <netdb.h> /* gethostbyname() */
38
/****************end for sockets *******************/
43
* These bits may be ORed together into the "flags" field of a TcpState
48
#define TCP_ASYNC_SOCKET (1<<0) /* Asynchronous socket. */
49
#define TCP_ASYNC_CONNECT (1<<1) /* Async connect in progress. */
52
* The following defines the maximum length of the listen queue. This is
53
* the number of outstanding yet-to-be-serviced requests for a connection
54
* on a server socket, more than this number of outstanding requests and
55
* the connection request will fail.
68
#define ERROR_MESSAGE(msg) do{ fprintf(stderr,msg); exit(1) ; } while(0)
83
struct timeval timeout;
87
fd = doConnect(argv[1],atoi(argv[2]));
89
perror("cant connect");
99
high = select(fd+1,&readfds,NULL,NULL,&timeout);
103
n = read(fd,buf,sizeof(buf));
104
if (3 == sscanf(buf,"%d %s %d",&x,op,&y)) {
107
case '+': sprintf(out,"%d\n",x+y);
109
case '*': sprintf(out,"%d\n",x*y);
112
sprintf(out,"bad operation\n");
114
write(fd,out,strlen(out));
124
*----------------------------------------------------------------------
126
* CreateSocketAddress --
128
* This function initializes a sockaddr structure for a host and port.
131
* 1 if the host was valid, 0 if the host could not be converted to
135
* Fills in the *sockaddrPtr structure.
137
*----------------------------------------------------------------------
141
CreateSocketAddress(struct sockaddr_in *sockaddrPtr, char *host, int port)
143
/* Host. NULL implies INADDR_ANY */
146
struct hostent *hostent; /* Host database entry */
147
struct in_addr addr; /* For 64/32 bit madness */
149
(void) memset((VOID *) sockaddrPtr, '\0', sizeof(struct sockaddr_in));
150
sockaddrPtr->sin_family = AF_INET;
151
sockaddrPtr->sin_port = htons((unsigned short) (port & 0xFFFF));
153
addr.s_addr = INADDR_ANY;
155
addr.s_addr = inet_addr(host);
156
if (addr.s_addr == -1) {
157
hostent = gethostbyname(host);
158
if (hostent != NULL) {
159
memcpy((VOID *) &addr,
160
(VOID *) hostent->h_addr_list[0],
161
(size_t) hostent->h_length);
164
errno = EHOSTUNREACH;
170
return 0; /* error */
176
* NOTE: On 64 bit machines the assignment below is rumored to not
177
* do the right thing. Please report errors related to this if you
178
* observe incorrect behavior on 64 bit machines such as DEC Alphas.
179
* Should we modify this code to do an explicit memcpy?
182
sockaddrPtr->sin_addr.s_addr = addr.s_addr;
183
return 1; /* Success. */
188
/* return -1 on failure, or else an fd */
190
CreateSocket(int port, char *host, int server, char *myaddr, int myport, int async)
191
/* Port number to open. */
192
/* Name of host on which to open port.
193
* NULL implies INADDR_ANY */
194
/* 1 if socket should be a server socket,
195
* else 0 for a client socket. */
196
/* Optional client-side address */
197
/* Optional client-side port */
198
/* If nonzero and creating a client socket,
199
* attempt to do an async connect. Otherwise
200
* do a synchronous connect or bind. */
202
int status, sock, asyncConnect, curState, origState;
203
struct sockaddr_in sockaddr; /* socket address */
204
struct sockaddr_in mysockaddr; /* Socket address for client */
208
if (! CreateSocketAddress(&sockaddr, host, port)) {
211
if ((myaddr != NULL || myport != 0) &&
212
! CreateSocketAddress(&mysockaddr, myaddr, myport)) {
216
sock = socket(AF_INET, SOCK_STREAM, 0);
222
* Set the close-on-exec flag so that the socket will not get
223
* inherited by child processes.
226
fcntl(sock, F_SETFD, FD_CLOEXEC);
233
* Set up to reuse server addresses automatically and bind to the
238
(void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &status,
240
status = bind(sock, (struct sockaddr *) &sockaddr,
241
sizeof(struct sockaddr));
243
status = listen(sock, SOMAXCONN);
246
if (myaddr != NULL || myport != 0) {
248
(void) setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
249
(char *) &curState, sizeof(curState));
250
status = bind(sock, (struct sockaddr *) &mysockaddr,
251
sizeof(struct sockaddr));
258
* Attempt to connect. The connect may fail at present with an
259
* EINPROGRESS but at a later time it will complete. The caller
260
* will set up a file handler on the socket if she is interested in
261
* being informed when the connect completes.
266
origState = fcntl(sock, F_GETFL);
267
curState = origState | O_NONBLOCK;
268
status = fcntl(sock, F_SETFL, curState);
273
status = ioctl(sock, FIONBIO, &curState);
279
status = connect(sock, (struct sockaddr *) &sockaddr,
282
if (errno == EINPROGRESS) {
293
ERROR_MESSAGE("couldn't open socket:");
308
ERROR_MESSAGE("couldn't open socket:");
317
char *host; /*name of host we are trying to connect to */
318
int port; /* port number to use */
320
return CreateSocket(port, host, 0 , NULL , 0 , 0);
326
#define SOCKET_FD(strm) ((strm)->sm.sm_fp ? fileno((strm)->sm.sm_fp) : -1)
328
DEFUN_NEW("GETPEERNAME",object,fSgetpeername,SI,1,1,NONE,OO,OO,OO,OO,(object sock),
329
"Return a list of three elements: the address, the hostname and the port for the other end of the socket. If hostname is not available it will be equal to the address. Invalid on server sockets. Return NIL on failure.")
331
struct sockaddr_in peername;
332
int size = sizeof(struct sockaddr_in);
333
struct hostent *hostEntPtr;
336
if (getpeername(SOCKET_FD(sock), (struct sockaddr *) &peername, &size)
338
address=make_simple_string(inet_ntoa(peername.sin_addr));
339
hostEntPtr = gethostbyaddr((char *) &(peername.sin_addr),
340
sizeof(peername.sin_addr), AF_INET);
341
if (hostEntPtr != (struct hostent *) NULL)
342
host = make_simple_string(hostEntPtr->h_name);
344
return list(3,address,host,make_fixnum(ntohs(peername.sin_port)));
351
DEFUN_NEW("GETSOCKNAME",object,fSgetsockname,SI,1,1,NONE,OO,OO,OO,OO,(object sock),
352
"Return a list of three elements: the address, the hostname and the port for the socket. If hostname is not available it will be equal to the address. Return NIL on failure. ")
353
{ struct sockaddr_in sockname;
354
int size = sizeof(struct sockaddr_in);
355
struct hostent *hostEntPtr;
359
if (getsockname(SOCKET_FD(sock), (struct sockaddr *) &sockname, &size)
361
address= make_simple_string(inet_ntoa(sockname.sin_addr));
362
hostEntPtr = gethostbyaddr((char *) &(sockname.sin_addr),
363
sizeof(sockname.sin_addr), AF_INET);
364
if (hostEntPtr != (struct hostent *) NULL)
365
host = make_simple_string(hostEntPtr->h_name);
367
return list(3,address,host,make_fixnum(ntohs(sockname.sin_port)));
375
Use on a tcp socket to alter the blocking or non blocking.
376
Results 0 if succeeds and errno if fails.
379
the channel is setto blocking or nonblocking mode.
382
DEFUN_NEW("SET-BLOCKING",object,fSset_blocking,SI,2,2,NONE,OO,OO,OO,OO,(object sock,object setBlocking),
383
"Set blocking on if MODE is T otherwise off. Return 0 if succeeds. Otherwise the error number.")
389
/* set our idea of whether blocking on or off
390
setBlocking==Cnil <==> blocking turned off. */
391
SET_STREAM_FLAG(sock,gcl_sm_tcp_async,setBlocking==Cnil);
392
if (sock->sm.sm_mode == smm_two_way) {
393
/* check for case they are sock streams and so
395
if (STREAM_INPUT_STREAM(sock)->sm.sm_fp != NULL
396
&&STREAM_OUTPUT_STREAM(sock)->sm.sm_fp != NULL
397
&& (SOCKET_FD(STREAM_INPUT_STREAM(sock))==
398
SOCKET_FD(STREAM_OUTPUT_STREAM(sock))))
400
SET_STREAM_FLAG(STREAM_OUTPUT_STREAM(sock),
401
gcl_sm_tcp_async,setBlocking==Cnil);
402
sock = STREAM_INPUT_STREAM(sock);
403
/* they share an 'fd' and so only do one. */
408
int x1 = fix(FFN(fSset_blocking)(STREAM_INPUT_STREAM(sock),setBlocking));
409
int x2 = fix(FFN(fSset_blocking)(STREAM_OUTPUT_STREAM(sock),setBlocking));
410
/* if either is negative result return negative. (ie fail)
411
If either is positive return positive (ie fail)
412
Zero result means both ok. (ie succeed)
415
return make_fixnum((x1 < 0 || x2 < 0 ? -2 : x1 > 0 ? x1 : x2));
419
if (sock->sm.sm_fp == NULL)
420
return make_fixnum(-2);
421
fd = SOCKET_FD(sock);
425
setting = fcntl(fd, F_GETFL);
426
if (setBlocking != Cnil) {
427
setting &= (~(O_NONBLOCK));
429
setting |= O_NONBLOCK;
431
if (fcntl(fd, F_SETFL, setting) < 0) {
432
return make_fixnum(errno);
437
if (setBlocking != Cnil) {
439
if (ioctl(fd, (int) FIONBIO, &setting) == -1) {
440
return make_fixnum(errno);
444
if (ioctl(fd, (int) FIONBIO, &setting) == -1) {
445
return make_fixnum(errno);
449
return make_fixnum(0);
452
/* with 2 args return the function if any.
455
/*setHandler(stream,readable,function)
456
object stream; stream to watch
457
object readable; keyword readable,writable
458
object function; the handler function to be invoked with arg stream
463
/* goes through the streams does a select with 0 timeout, and invokes
473
joe(int x) { return x; }
476
get a character from FP but block, if it would return
477
the EOF, but the stream is not closed.
483
struct timeval timeout;
486
/* fprintf(stderr,"<socket 0x%x>",fp);
488
fprintf(stderr,"in getOneChar, fd=%d,fp=%p",fd,fp);
497
timeout.tv_usec = 200000;
501
high = select(fd+1,&readfds,NULL,NULL,&timeout);
505
fprintf(stderr,"in getOneChar, fd=%d,fp=%p",fd,fp);
508
if ( ch != EOF || feof(fp) ) {
509
/* fprintf(stderr,"< 0x%x returning %d,%c>\n",fp,ch,ch);
513
fprintf(stderr,"in getOneChar, ch= %c,%d\n",ch,ch);
516
if (ch != EOF) return ch;
517
if (feof(fp)) return EOF;
524
#define dprintf(s,arg) \
525
do {fprintf(stderr,s,arg); \
529
#define dprintf(s,arg)
533
ungetCharGclSocket(int c, object strm)
534
/* the character to unget */
536
{ object bufp = SOCKET_STREAM_BUFFER(strm);
537
if (c == EOF) return;
538
dprintf("pushing back %c\n",c);
539
if (bufp->ust.ust_fillp < bufp->ust.ust_dim) {
540
bufp->ust.ust_self[(bufp->ust.ust_fillp)++]=c;
542
FEerror("Tried to unget too many chars",0);
548
*----------------------------------------------------------------------
552
* This procedure is invoked by the generic IO level to write output
553
* to a TCP socket based channel.
555
* NOTE: We cannot share code with FilePipeOutputProc because here
556
* we must use send, not write, to get reliable error reporting.
559
* The number of bytes written is returned. An output argument is
560
* set to a POSIX error code if an error occurred, or zero.
563
* Writes output on the output device of the channel.
565
*----------------------------------------------------------------------
569
TcpOutputProc(int fd, char *buf, int toWrite, int *errorCodePtr)
571
/* The data buffer. */
572
/* How many bytes to write? */
573
/* Where to store error code. */
578
written = send(fd, buf, (size_t) toWrite, 0);
582
*errorCodePtr = errno;
587
tcpCloseSocket(int fd)
594
doReverse(char *s, int n)
608
getCharGclSocket(strm,block) -- get one character from a socket
610
Results: a character or EOF if at end of file
611
Side Effects: The buffer may be filled, and the fill pointer
612
of the buffer may be changed.
615
getCharGclSocket(object strm, object block)
617
object bufp = SOCKET_STREAM_BUFFER(strm);
618
if (bufp->ust.ust_fillp > 0) {
619
dprintf("getchar returns (%c)\n",bufp->ust.ust_self[-1+(bufp->ust.ust_fillp)]);
620
return bufp->ust.ust_self[--(bufp->ust.ust_fillp)];
624
struct timeval timeout;
625
int fd = SOCKET_STREAM_FD(strm);
629
/* under cygwin a too large timout like (1<<30) does not work */
630
timeout.tv_sec = (block != Ct ? 0 : 0);
631
timeout.tv_usec = 10000;
634
high = select(fd+1,&readfds,NULL,NULL,&timeout);
636
{ object bufp = SOCKET_STREAM_BUFFER(strm);
638
n = SAFE_READ(fd,bufp->ust.ust_self ,bufp->ust.ust_dim);
639
doReverse(bufp->ust.ust_self,n);
640
bufp->ust.ust_fillp=n;
643
dprintf("getchar returns (%c)\n",bufp->ust.ust_self[-1+(bufp->ust.ust_fillp)]);
644
return bufp->ust.ust_self[--(bufp->ust.ust_fillp)];
649
FEerror("select said there was stuff there but there was not",0);
652
/* probably a signal interrupted us.. */