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
|
/*
* Copyright (C) 1990-2000 by CERN/IT/PDP/DM
* All rights reserved
*/
#if !defined(lint)
static char sccsid[] = "@(#)socket.c,v 1.7 2000/05/31 10:33:54 CERN/IT/PDP/DM Olof Barring";
#endif /* lint */
/* socket.c Generalized network interface */
#undef DEBUG
/* Define DUMP to print buffers contents - heavy debug mode */
#define READ(x,y,z) recv(x,y,z,0) /* Actual read system call */
#define WRITE(x,y,z) send(x,y,z,0) /* Actual write system call */
#if defined(_WIN32)
#define CLOSE(x) closesocket(x) /* Actual close system call */
#define IOCTL(x,y,z) ioctlsocket(x,y,&(z)) /* Actual ioctl system call*/
#else /* _WIN32 */
#define CLOSE(x) close(x) /* Actual close system call */
#define IOCTL(x,y,z) ioctl(x,y,z) /* Actual ioctl system call */
#endif /* _WIN32 */
#ifndef READTIMEOUTVALUE
#define READTIMEOUTVALUE 60 /* Default read time out */
#endif /* READTIMEOUTVALUE */
static int rtimeout=READTIMEOUTVALUE;
static int timeout_set=0;
/*
* Define BLOCKSIZE if read/write calls have a length upper limit
* E.g. VMS QIO calls are limited to transfer 65536 bytes.
*/
#ifdef BLOCKSIZE
#undef BLOCKSIZE /* be safe */
#endif /* BLOCKSIZE */
#include <stdio.h>
#include <sys/types.h>
#if defined(_WIN32)
#include <winsock2.h>
#include <ws_errmsg.h>
#else
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#endif
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#if defined(_AIX) && defined(_IBMR2)
#include <sys/select.h>
#endif /* _AIX */
#include <net.h> /* networking specifics */
#if defined(DEBUG) || defined(DUMP)
#include <log.h> /* logging functions */
#endif /* DEBUG || DUMP */
#include <serrno.h> /* special errors */
#ifdef READTIMEOUT
static jmp_buf alarmbuf;
static void (* defsigalrm) ();
#endif
#ifndef min
#define min(x, y) (((x-y) > 0) ? y : x)
#endif /* min */
#ifdef DUMP
#include <ctype.h>
static int
Dump(buf, nbytes)
char *buf;
int nbytes;
{
register int i, j;
register char c;
log(LOG_DEBUG ," *** Hexadecimal dump *** \n");
for (i=0;i<nbytes/20;i++) {
for (j=0;j<20;j++) {
log(LOG_DEBUG ,"<%2.2X>", (char) buf[i*20+j]);
}
log(LOG_DEBUG ,"\n");
}
for (i=0;i<nbytes%20;i++) {
log(LOG_DEBUG ,"<%2.2X>",buf[(nbytes/20)*20+i]);
}
log(LOG_DEBUG ,"\n *** Interpreted dump *** \n");
for (i=0;i<nbytes/80;i++) {
for (j=0;j<80;j++) {
c = (char) buf[i*80+j];
if (isprint(c)) log(LOG_DEBUG ,"%c", c);
else log(LOG_DEBUG ,".");
}
log(LOG_DEBUG ,"\n");
}
for (i=0;i<nbytes%80;i++) {
c = (char) buf[(nbytes/80)*80+i];
if (isprint(c)) log(LOG_DEBUG ,"%c", c);
else log(LOG_DEBUG ,".");
}
log(LOG_DEBUG ,"\n");
}
#endif /* DUMP */
#ifdef READTIMEOUT
void catch()
{
longjmp(alarmbuf, 1);
}
#endif /* READTIMEOUT */
static int /* non atomic receive with time out */
t_recv (s, buf, nbytes)
SOCKET s;
char *buf;
int nbytes;
{
fd_set fds;
struct timeval timeout;
FD_ZERO (&fds);
FD_SET (s, &fds);
timeout.tv_sec = rtimeout;
timeout.tv_usec = 0;
#if defined(DEBUG)
fprintf(stdout,"select(%d, %x, %x, %x, %d.%d)\n",
FD_SETSIZE,&fds,(fd_set *)0,(fd_set *)0,timeout.tv_sec, timeout.tv_usec);
#endif /* DEBUG */
switch(select(FD_SETSIZE,&fds,(fd_set *)0,(fd_set *)0,&timeout)) {
case -1:
#if defined(DEBUG)
fprintf(stdout,"select returned -1\n");
#endif /* DEBUG */
return (-1); /* an error has occured */
case 0:
#if defined(DEBUG)
fprintf(stdout,"select timed out\n");
syslog(LOG_ALERT, "[%d]: socket: network recv timed out", getpid());
#endif /* DEBUG */
serrno = SETIMEDOUT; return(-1);
default: break;
}
#if defined(DEBUG)
fprintf(stdout,"select returned data\n");
#endif /* DEBUG */
#ifdef BLOCKSIZE
return( READ(s, buf, min(BLOCKSIZE, nbytes)));
#else
return( READ(s, buf, nbytes));
#endif
}
int DLL_DECL
s_recv (s, buf, nbytes)
SOCKET s;
char *buf;
int nbytes;
{
register int n, nb;
if (nbytes < 0) {
serrno = EINVAL;
return(-1);
}
#if defined(DEBUG)
log(LOG_DEBUG ,"dorecv(%x, %x, %d)\n", s, buf, nbytes);
#endif /* DEBUG */
#ifdef READTIMEOUT
if (setjmp(alarmbuf) == 1) {
signal(SIGALRM, defsigalrm); /* restore alarm handler*/
errno = ETIMEDOUT;
return(-1);
}
defsigalrm = signal (SIGALRM, (void (*)()) catch);
#endif /* READTIMEOUT */
nb = nbytes;
for (; nb >0;) {
#ifdef READTIMEOUT
alarm(rtimeout);/* successive calls reset the alarm */
#endif /* READTIMEOUT */
#ifdef BLOCKSIZE
if (timeout_set)
n = t_recv(s, buf, min(BLOCKSIZE, nb));
else
n = READ(s, buf, min(BLOCKSIZE, nb));
#else
if (timeout_set)
n = t_recv(s, buf, nb);
else
n = READ(s, buf, nb);
#endif
nb -= n;
#ifdef READTIMEOUT
alarm(0);
signal(SIGALRM, defsigalrm);
#endif /* READTIMEOUT */
if (n <= 0) {
if (n == 0) {
serrno=SECONNDROP;
return(0);
}
#if defined(DEBUG)
log(LOG_DEBUG ,"ERROR: %d while n=%d,nb-n=%d,buf=%x\n",
errno, n, nb, buf);
#endif /* DEBUG */
return (n);
}
#if defined(DEBUG)
log(LOG_DEBUG ,"dorecv: %d bytes received\n",n);
#if defined(DUMP)
log(LOG_DEBUG ,"dorecv: dump follows\n");
Dump(buf,n);
#endif /* DUMP */
#endif /* DEBUG */
buf += n;
}
return (nbytes);
}
int DLL_DECL
s_send (s, buf, nbytes)
SOCKET s;
char *buf;
int nbytes;
{
register int n, nb;
if (nbytes < 0) {
serrno = EINVAL;
return(-1);
}
#if defined(DEBUG)
log(LOG_DEBUG, "dosend(%x, %x, %d)\n", s, buf, nbytes);
#endif
nb = nbytes;
for (; nb >0;) {
#ifdef BLOCKSIZE
n = WRITE(s, buf, min(BLOCKSIZE, nb));
#else
n = WRITE(s, buf, nb);
#endif
nb -= n;
if (n <= 0) {
if (n == 0) {
serrno=SECONNDROP;
return(0);
}
#if defined(DEBUG)
log(LOG_DEBUG ,"ERROR: %d while n=%d,nb-n=%d,buf=%x\n",
errno, n, nb, buf);
#endif /* DEBUG */
return (n);
}
#if defined(DEBUG)
#if defined(DUMP)
log(LOG_DEBUG ,"dosend: dump follows\n");
Dump(buf,n);
#endif /* DUMP */
log(LOG_DEBUG ,"dosend: %d bytes sent\n",n);
#endif /* DEBUG */
buf += n;
}
#if defined(DEBUG)
log(LOG_DEBUG, "dosend(%x) returns %d\n", s, nbytes);
#endif /* DEBUG */
return (nbytes);
}
int DLL_DECL
s_close(s)
SOCKET s;
{
return(CLOSE(s));
}
char DLL_DECL *
s_errmsg() /* return last error message */
{
#if !defined(_WIN32)
if ( serrno != 0 ) return((char *)sstrerror(serrno));
else return((char *)sstrerror(errno));
#else /* _WIN32 */
if ( serrno != 0 ) return((char *)sstrerror(serrno));
else return(geterr());
#endif /* _WIN32 */
}
/*
* Solaris 2.x defines but does not document a s_ioctl routine
* in libsocket.a, therefore conflicting with ours. Hence this
* workaround.
*/
#if defined(SOLARIS) && (SOLARIS == 1)
#define s_ioctl sol_s_ioctl
#endif /* SOLARIS */
int DLL_DECL
s_ioctl(s, request, arg) /* issue an ioctl(2) call */
SOCKET s;
int request;
int arg;
{
return(IOCTL(s, request, arg));
}
int DLL_DECL s_nrecv(s, buf, nbytes) /* Non blocking read */
SOCKET s;
char *buf;
int nbytes;
{
#if defined(BLOCKSIZE)
return(READ(s, buf, min(BLOCKSIZE,nbytes)));
#else
return(READ(s, buf, nbytes));
#endif /* BLOCKSIZE */
}
int DLL_DECL setrtimo(val)
int val;
{
register int otimeout;
otimeout = rtimeout;
rtimeout=val;
#if defined(DEBUG)
fprintf(stdout,"setrtimo: switching to time'dout recv\n");
#endif /* DEBUG */
timeout_set = 1;
return(otimeout);
}
int (*recvfunc)()=s_recv; /* recv function to use */
int (*sendfunc)()=s_send; /* send function to use */
int (*closefunc)()=s_close; /* close function to use */
int (*ioctlfunc)()=s_ioctl; /* ioctl function to use */
char *(*errfunc)()=s_errmsg; /* strerror function to use */
|