00001
00025
00026 #include "netapi_t.h"
00027 #include "netapi.h"
00028
00029 #include <stdio.h>
00030
00031 /* errorInfo struct initialise */
00032 static struct errorInfo errInf;
00033 errorInfoPtr errInfp = &errInf;
00034
00035 /* clientInfo struct initialise */
00036 static struct clientInfo cInf;
00037 clientInfoPtr cInfp = &cInf;
00038 static char IPAddr[17];
00039
00040 static char* errorCodes[266];
00041 static char* specialCode = "socket call failed";
00042 static int errorCodesInit = 0;
00043
00044 static void initErrorCodes()
00045 {
00046 errorCodes[0] = "No error";
00047 errorCodes[222] = "Invalid argument";
00048 errorCodes[235] = "Operation would block";
00049 errorCodes[236] = "Operation now in progress";
00050 errorCodes[237] = "Operation already in progress";
00051 errorCodes[238] = "Socket operation on non-socket";
00052 errorCodes[239] = "Destination address required";
00053 errorCodes[240] = "Message too long";
00054 errorCodes[241] = "Protocol wrong type for socket";
00055 errorCodes[242] = "Protocol not available";
00056 errorCodes[243] = "Protocol not supported";
00057 errorCodes[244] = "Socket type not supported";
00058 errorCodes[245] = "Operation not supported";
00059 errorCodes[246] = "Protocol family not supported";
00060 errorCodes[247] = "Address family not supported by protocol family";
00061 errorCodes[248] = "Address already in use";
00062 errorCodes[249] = "Can't assign requested address";
00063 errorCodes[250] = "Network is down";
00064 errorCodes[251] = "Network is unreachable";
00065 errorCodes[252] = "Network dropped connection on reset";
00066 errorCodes[253] = "Software caused connection abort";
00067 errorCodes[254] = "Connection reset by peer";
00068 errorCodes[255] = "No buffer space available";
00069 errorCodes[256] = "Socket is already connected";
00070 errorCodes[257] = "Socket is not connected";
00071 errorCodes[258] = "Can't send after socket shutdown";
00072 errorCodes[259] = "Too many references: can't splice";
00073 errorCodes[260] = "Operation timed out";
00074 errorCodes[261] = "Connection refused";
00075 errorCodes[264] = "Host is down";
00076 errorCodes[265] = "No route to host";
00077 }
00078
00079
00080 int openSocket(int type)
00081 {
00082 union REGS inregs;
00083 union REGS outregs;
00084
00085 inregs.h.ah = API_OPENSOCKET;
00086 inregs.h.al = type;
00087 int86(TCPIPVECT,&inregs,&outregs);
00088
00089 if ( evalError(&outregs) ) { /* error occurs */
00090 return 0;
00091 }
00092 return (int)outregs.x.ax;
00093 }
00094
00095 bool closeSocket(int sockDescr)
00096 {
00097 union REGS inregs;
00098 union REGS outregs;
00099
00100 inregs.h.ah = API_CLOSESOCKET;
00101 inregs.x.bx = sockDescr;
00102 int86(TCPIPVECT,&inregs,&outregs);
00103
00104 if ( evalError(&outregs) ) { /* error occurs */
00105 return 0;
00106 }
00107
00108 return 1;
00109 }
00110
00111 bool bind(int sockDescr, unsigned int localPort)
00112 {
00113 union REGS inregs;
00114 union REGS outregs;
00115 struct sockaddr_in addr;
00116
00117 addr.sin_family = PF_INET;
00118 /* convert your port to correct byte order */
00119 inregs.h.ah = API_HTONS;
00120 inregs.x.bx = localPort;
00121 int86(TCPIPVECT,&inregs,&outregs);
00122
00123 addr.sin_port = outregs.x.ax;
00124 addr.sin_addr.s_addr = 0;
00125
00126 inregs.h.ah = API_BIND;
00127 inregs.x.bx = sockDescr;
00128 inregs.x.dx = FP_SEG(&addr);
00129 inregs.x.si = FP_OFF(&addr);
00130 int86(TCPIPVECT,&inregs,&outregs);
00131
00132 if ( evalError(&outregs) ) { /* error occurs */
00133 return 0;
00134 }
00135
00136 return 1;
00137 }
00138
00139 bool connect(int sockDescr, char* IPAddress, unsigned int remotePort)
00140 {
00141 union REGS inregs;
00142 union REGS outregs;
00143 struct sockaddr_in addr;
00144
00145 unsigned long IP;
00146
00147 addr.sin_family = PF_INET;
00148
00149 /* convert server port to correct byte order */
00150 inregs.h.ah = API_HTONS;
00151 inregs.x.bx = remotePort;
00152 int86(TCPIPVECT,&inregs,&outregs);
00153
00154 addr.sin_port = outregs.x.ax;
00155
00156 /* convert IPAddress */
00157 if ( !(ascii2inet(IPAddress, &IP)) ) {
00158 return 0;
00159 }
00160 /* the ip address is in IP */
00161 addr.sin_addr.s_addr = IP;
00162
00163 inregs.h.ah = API_CONNECT;
00164 inregs.x.bx = sockDescr;
00165 inregs.x.dx = FP_SEG(&addr);
00166 inregs.x.si = FP_OFF(&addr);
00167 int86(TCPIPVECT,&inregs,&outregs);
00168
00169 if ( evalError(&outregs) ) { /* error occurs */
00170 return 0;
00171 }
00172
00173 return 1;
00174 }
00175
00176 int TCPSend(int sockDescr,
00177 byte* dataBuffer,
00178 int length,
00179 int flags)
00180 {
00181 union REGS inregs;
00182 union REGS outregs;
00183 struct send_params sendParams;
00184
00185 sendParams.bufferPtr = (char*) dataBuffer;
00186 sendParams.bufferLength = length;
00187 sendParams.flags = flags;
00188 /* we don't need this for TCP, because we assume that connection is established */
00189
00190 sendParams.toPtr = NULL;
00191 sendParams.tolengthPtr = NULL;
00192
00193 /* send */
00194 inregs.h.ah = API_SEND;
00195 inregs.x.bx = sockDescr;
00196 inregs.x.dx = FP_SEG(&sendParams);
00197 inregs.x.si = FP_OFF(&sendParams);
00198 int86(TCPIPVECT,&inregs,&outregs);
00199
00200 if ( evalError(&outregs) ) { /* error occurs */
00201 return 0;
00202 }
00203
00204 return outregs.x.ax;
00205 }
00206
00207 int TCPReceive(int sockDescr,
00208 byte* dataBuffer,
00209 int length,
00210 int flags,
00211 unsigned long timeout)
00212 {
00213 union REGS inregs;
00214 union REGS outregs;
00215 struct recv_params recParams;
00216
00217 recParams.bufferPtr = (char*) dataBuffer;
00218 recParams.bufferLength = length;
00219 recParams.flags = flags;
00220 if ( flags != MSG_TIMEOUT ) { /* no timeout mode while msg sending */
00221 recParams.timeout = 0;
00222 } else {
00223 recParams.timeout = timeout; /* timeout for timeout mode while msg sending */
00224 }
00225 /* we don't need this for TCP, because we assume the connection is established */
00226 recParams.fromPtr = NULL;
00227 recParams.fromlengthPtr = NULL;
00228
00229 /* receiving */
00230 inregs.h.ah = API_RECV;
00231 inregs.x.bx = sockDescr;
00232 inregs.x.dx = FP_SEG(&recParams);
00233 inregs.x.si = FP_OFF(&recParams);
00234 int86(TCPIPVECT,&inregs,&outregs);
00235
00236 if ( evalError(&outregs) ) { /* error occurs */
00237 return 0;
00238 }
00239
00240 return outregs.x.ax;
00241 }
00242
00243 bool TCPAbortConnection(int sockDescr)
00244 {
00245 union REGS inregs;
00246 union REGS outregs;
00247
00248 inregs.h.ah = API_RESETCONNECTION;
00249 inregs.x.bx = sockDescr;
00250 int86(TCPIPVECT,&inregs,&outregs);
00251
00252 if ( evalError(&outregs) ) { /* error occurs */
00253 return 0;
00254 }
00255
00256 return 1;
00257 }
00258
00259 bool TCPCloseLinger(int sockDescr, unsigned int seconds)
00260 {
00261 union REGS inregs;
00262 union REGS outregs;
00263
00264 inregs.h.ah = 0x13;
00265 inregs.x.bx = sockDescr;
00266 inregs.x.cx = seconds;
00267
00268 int86(TCPIPVECT,&inregs,&outregs);
00269
00270 if ( evalError(&outregs) ) { /* error occurs */
00271 return 0;
00272 }
00273
00274 return 1;
00275
00276 }
00277
00278 bool TCPSocketReuse(int sockDescr)
00279 {
00280 union REGS inregs;
00281 union REGS outregs;
00282
00283 inregs.h.ah = 0x14;
00284 inregs.x.bx = sockDescr;
00285 int86(TCPIPVECT,&inregs,&outregs);
00286
00287 if ( evalError(&outregs) ) { /* error occurs */
00288 return 0;
00289 }
00290
00291 return 1;
00292 }
00293
00294 int UDPSend(int sockDescr,
00295 char* IPAddress,
00296 unsigned int remotePort,
00297 byte* dataBuffer,
00298 int length,
00299 int flags,
00300 bool convertPort)
00301 {
00302 union REGS inregs;
00303 union REGS outregs;
00304 struct send_params sendParams;
00305 struct sockaddr_in addr;
00306 unsigned long IP;
00307 int toLength;
00308
00309 /* convert dotted decimal string to ulong */
00310 ascii2inet(IPAddress, &IP);
00311 addr.sin_addr.s_addr = IP;
00312
00313 /* set remotePort in correct Byte order */
00314 if (convertPort) {
00315 inregs.h.ah = API_HTONS;
00316 inregs.x.bx = remotePort;
00317 int86(TCPIPVECT,&inregs,&outregs);
00318
00319 addr.sin_port = outregs.x.ax;
00320 } else {
00321 addr.sin_port = remotePort;
00322 }
00323
00324 toLength = sizeof(struct sockaddr_in);
00325 sendParams.bufferPtr = (char*) dataBuffer;
00326 sendParams.bufferLength = length;
00327 sendParams.flags = flags;
00328 sendParams.toPtr = (struct sockaddr *)&addr;
00329 sendParams.tolengthPtr = &toLength;
00330
00331 /* sendto */
00332 inregs.h.ah = API_SENDTO;
00333 inregs.x.bx = sockDescr;
00334 inregs.x.dx = FP_SEG(&sendParams);
00335 inregs.x.si = FP_OFF(&sendParams);
00336 int86(TCPIPVECT,&inregs,&outregs);
00337
00338 if ( evalError(&outregs) ) { /* error occurs */
00339 return 0;
00340 }
00341
00342 return outregs.x.ax;
00343 }
00344
00345 int UDPReceive(int sockDescr,
00346 byte* dataBuffer,
00347 int length,
00348 int flags,
00349 unsigned long timeout)
00350 {
00351 union REGS inregs;
00352 union REGS outregs;
00353 struct recv_params recParams;
00354 struct sockaddr_in addr;
00355 int fromLength;
00356 clientInfoPtr cip;
00357
00358 fromLength = sizeof(struct sockaddr_in);
00359 recParams.bufferPtr = (char*) dataBuffer;
00360 recParams.bufferLength = length;
00361 recParams.flags = flags;
00362 recParams.fromPtr = (struct sockaddr *)&addr;
00363 recParams.fromlengthPtr = (&fromLength);
00364 if ( flags != MSG_TIMEOUT ) { /* no timeout mode while msg sending */
00365 recParams.timeout = 0;
00366 } else {
00367 recParams.timeout = timeout; /* timeout for timeout mode while msg sending */
00368 }
00369 inregs.h.ah = API_RECVFROM;
00370 inregs.x.bx = sockDescr;
00371 inregs.x.dx = FP_SEG(&recParams);
00372 inregs.x.si = FP_OFF(&recParams);
00373 int86(TCPIPVECT,&inregs,&outregs);
00374
00375 if ( evalError(&outregs) ) { /* error occurs */
00376 return 0;
00377 }
00378
00379 /* save the client info */
00380 inet2ascii(&addr.sin_addr.s_addr, IPAddr);
00381 cip = getClientInfo();
00382 cip->IPAddress = IPAddr;
00383 cip->port = addr.sin_port;
00384
00385 return outregs.x.ax;
00386 }
00387
00388 bool listen(int sockDescr, int connections)
00389 {
00390 union REGS inregs;
00391 union REGS outregs;
00392 struct sockaddr_in addr;
00393
00394 inregs.h.ah = API_LISTEN;
00395 inregs.x.cx = (unsigned int) connections;
00396 inregs.x.bx = sockDescr;
00397
00398 int86(TCPIPVECT,&inregs,&outregs);
00399
00400 if ( evalError(&outregs) ) { /* error occurs */
00401 return 0;
00402 }
00403
00404 return 1;
00405 }
00406
00407 int accept(int sockDescr)
00408 {
00409 union REGS inregs;
00410 union REGS outregs;
00411 struct sockaddr_in addr;
00412 clientInfoPtr cip;
00413
00414 addr.sin_family = PF_INET;
00415 addr.sin_port = 0;
00416 addr.sin_addr.s_addr = 0L;
00417
00418 inregs.h.ah = API_ACCEPT;
00419 inregs.x.bx = sockDescr;
00420 inregs.x.dx = FP_SEG(&addr);
00421 inregs.x.si = FP_OFF(&addr);
00422 int86(TCPIPVECT,&inregs,&outregs);
00423
00424 if ( evalError(&outregs) ) { /* error occurs */
00425 return 0;
00426 }
00427
00428 /* save the client info */
00429 inet2ascii(&addr.sin_addr.s_addr, IPAddr);
00430 cip = getClientInfo();
00431 cip->IPAddress = IPAddr;
00432 cip->port = addr.sin_port;
00433
00434 return outregs.x.ax; /* returns new socket descriptor */
00435 }
00436
00437 int waitingBytes(int sockDescr)
00438 {
00439 union REGS inregs;
00440 union REGS outregs;
00441
00442 inregs.h.ah = API_GETRCV_BYTES;
00443 inregs.x.bx = sockDescr;
00444 int86(TCPIPVECT,&inregs,&outregs);
00445
00446 if ( evalError(&outregs) ) { /* error occurs */
00447 return 0;
00448 }
00449
00450 return outregs.x.ax;
00451 }
00452
00453 clientInfoPtr getClientInfo()
00454 {
00455 return cInfp;
00456 }
00457
00458 errorInfoPtr getErrorInfo()
00459 {
00460 return errInfp;
00461 }
00462
00463 int getErrorCode()
00464 {
00465 return getErrorInfo()->errorCode;
00466 }
00467
00468 void inet2ascii(unsigned long far* IP, char far* IPAddress)
00469 {
00470 union REGS inregs;
00471 union REGS outregs;
00472 struct SREGS sregs;
00473
00474 inregs.h.ah = API_INETTOASCII;
00475 inregs.x.bx = FP_SEG(IP);
00476 inregs.x.si = FP_OFF(IP);
00477 sregs.es = FP_SEG(IPAddress);
00478 inregs.x.di = FP_OFF(IPAddress);
00479
00480 int86x(TCPIPVECT,&inregs,&outregs,&sregs);
00481 }
00482
00483 bool ascii2inet(char far* IPAddress, unsigned long far* IP)
00484 {
00485 union REGS inregs;
00486 union REGS outregs;
00487 struct SREGS sregs;
00488
00489 inregs.h.ah = API_INETADDR;
00490 inregs.x.bx = FP_SEG(IPAddress);
00491 inregs.x.si = FP_OFF(IPAddress);
00492 sregs.es = FP_SEG(IP);
00493 inregs.x.di = FP_OFF(IP);
00494
00495 int86x(TCPIPVECT,&inregs,&outregs,&sregs);
00496
00497 if( outregs.x.dx == (unsigned int)API_ERROR ) { /* syntax error */
00498 return 0;
00499 }
00500
00501 return 1;
00502 }
00503
00504 void sleep(unsigned int mseconds)
00505 {
00506 union REGS inregs;
00507 union REGS outregs;
00508
00509 inregs.h.ah = API_SLEEP;
00510 inregs.x.bx = mseconds; /* milliseconds */
00511 int86(TCPIPVECT,&inregs,&outregs);
00512 }
00513
00514 bool evalError(union REGS* outregs)
00515 {
00516 if (!errorCodesInit) { /* errorCodes were not initialize */
00517 initErrorCodes();
00518 errorCodesInit = 1;
00519 }
00520
00521 getErrorInfo()->errorCode = (*outregs).x.dx;
00522
00523 if ( getErrorInfo()->errorCode == -1 ) {
00524 getErrorInfo()->errorDescr = specialCode;
00525 } else {
00526 getErrorInfo()->errorDescr = errorCodes[(*outregs).x.dx];
00527 }
00528
00529 if ( getErrorInfo()->errorCode == 0 ) { /* no error */
00530 return 0;
00531 }
00532 /* there was an error */
00533 return 1;
00534 }
00535