2 * For license terms, see the file COPYING in this directory.
5 /***********************************************************************
8 programmer: Carl Harris, ceharris@mal.com
9 description: socket library functions
11 ***********************************************************************/
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
22 #include <sys/ioctl.h>
23 #if defined(STDC_HEADERS)
26 #if defined(HAVE_UNISTD_H)
29 #if defined(HAVE_STDARG_H)
38 /* Size of buffer for internal buffering read function
39 don't increase beyond the maximum atomic read/write size for
40 your sockets, or you'll take a potentially huge performance hit */
42 #define INTERNAL_BUFSIZE 2048
44 int Socket(host, clientPort)
50 struct sockaddr_in ad;
53 memset(&ad, 0, sizeof(ad));
54 ad.sin_family = AF_INET;
56 inaddr = inet_addr(host);
57 if (inaddr != INADDR_NONE)
58 memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
61 hp = gethostbyname(host);
64 memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
66 ad.sin_port = htons(clientPort);
68 sock = socket(AF_INET, SOCK_STREAM, 0);
71 if (connect(sock, (struct sockaddr *) &ad, sizeof(ad)) < 0)
76 int SockGets(socket,buf,len)
82 RETSIGTYPE (*sigsave)();
86 if (SockInternalRead(socket, buf, 1) != 1)
92 if (*buf != '\r') /* remove all CRs */
99 int SockPuts(socket,buf)
105 if (rc = SockWrite(socket, buf, strlen(buf)))
107 return SockWrite(socket, "\r\n", 2);
110 int SockWrite(socket,buf,len)
119 n = write(socket, buf, len);
128 int SockRead(socket,buf,len)
138 n = SockInternalRead(socket, buf, len);
147 static int sbuflen = 0;
149 int SockInternalRead (socket,buf,len)
154 static char sbuf [INTERNAL_BUFSIZE];
158 /* buffer is empty; refresh. */
159 if ((sbuflen = read(socket,sbuf,INTERNAL_BUFSIZE)) < 0) {
162 perror("SockInternalRead: read");
169 ; /* already some data in the buffer */
171 /* can't get more than we have right now. */
172 /* XXX -- should probably try to load any unused part of sbuf
173 so that as much of 'len' as possible can be satisfied */
177 ; /* wants no more than we already have */
179 /* transfer to caller's buffer */
181 /* special case: if caller only wants one character, it probably
182 costs a lot more to call bcopy than to do it ourselves. */
194 /* SockClearHeader: call this procedure in order to kill off any
195 forthcoming Header info from the socket that we no longer want.
197 int SockClearHeader(socket)
201 static char sbuf[INTERNAL_BUFSIZE];
205 if ((res = SockDataWaiting(socket)) <= 0)
210 if (SockGets(socket,sbuf,INTERNAL_BUFSIZE) < 0)
224 /* SockDataWaiting: Return a non-zero value if this socket is waiting
226 int SockDataWaiting(int socket)
229 char sbuf[INTERNAL_BUFSIZE];
232 flags = fcntl(socket,F_GETFL,0);
234 /* set it to non-block */
235 if (fcntl(socket,F_SETFL,flags | O_NONBLOCK) == -1)
238 if ((n = recv(socket,sbuf,INTERNAL_BUFSIZE,MSG_PEEK)) == -1)
240 /* No data to read. */
241 if (errno == EWOULDBLOCK)
247 /* reset it to block (or, whatever it was). */
248 fcntl(socket,F_SETFL,flags);
251 #if defined(HAVE_STDARG_H)
252 int SockPrintf(int socket, char* format, ...)
255 int SockPrintf(socket,format,va_alist)
264 #if defined(HAVE_STDARG_H)
265 va_start(ap, format) ;
269 vsprintf(buf, format, ap);
271 return SockWrite(socket, buf, strlen(buf));