📄 socklib.c
字号:
/* sockLib.c - generic socket library *//* Copyright 1984 - 1999 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------04p,16mar99,spm recovered orphaned code from tor2_0_x branch (SPR #25770)04o,02dec98,n_s updated comments for sendmsg and recvmsg. spr 2355204n,10nov98,spm changed socket map for removal of closed files (SPR #21583)04n,15mar99,c_c Doc: Updated setsockopt () with SO_DEBUG flag (SPR #9636).04m,14dec97,jdi doc: cleanup.04l,10dec97,kbw fixed man page problems 04k,04aug97,kbw fixed man page problems found in beta review04j,15apr97,kbw fixed minor man page format problems04i,14arp97,vin added changes to setsockopt docs.04l,14jul97,dgp doc: change ^C to CTRL-C in setsockopt()04k,10jul97,dgp doc: fix SPRs 8470 & 8471, parameters of getsockopt()04j,31jul95,dzb check for lib init in sockLibAdd() and socket().04i,25jul95,dzb added fd-to-pSockFunc table. added sockLibAdd().04h,21jul95,dzb changed to be a BSD/STREAMS switchyard. Previous sockLib.c file moved to bsdSockLib.c.04i,26oct95,rhp doc: mention values for <flags> params in socket calls (SPR#4423)04h,14oct95,jdi doc: revised pathnames for Tornado.04g,13mar95,dzb added spl semaphore protection to socket() (SPR #4016).04f,11feb95,jdi doc format repairs.04e,18jan95,jdi doc cleanup for connectWithTimeout().04d,14not94,rhp added socket-option documentation to setsockopt man page, and added pointers from send() and sendto() (SPR#3785); specified no support for UNIX Domain sockets (SPR#3519,1432)04c,18aug94,dzb Removed the NOMANUAL from connectWithTimeout() (SPR #3550). Added m_freem() calls for mbuf interface (MSG_MBUF).04b,22apr93,caf ansification: added cast to ioctl() parameter.04a,11aug93,jmm Changed ioctl.h and socket.h to sys/ioctl.h and sys/socket.h03z,01mar93,jdi doc: addition of ioctl() functions (SPR 1922); added explanation of accept() parameters (SPR 1382).03y,03feb93,jdi documentation cleanup for 5.1.03x,19aug92,smb changed systime.h to sys/times.h.03w,26may92,rrr the tree shuffle03v,04oct91,rrr passed through the ansification filter -changed functions to ansi style -changed includes to have absolute path from h/ -changed copyright notice03u,30apr91,jdi documentation tweaks.03t,05apr91,jdi documentation -- removed header parens and x-ref numbers; doc review by dnw.03s,24mar91,jaa documentation cleanup.03r,07feb91,elh changed recvfrom to check if from is NULL before filling it.03q,31jan91,jaa documentation.03p,25oct90,dnw changed utime.h to systime.h.03o,04oct90,shl made connectWithTimeOut() NOMANUAL.03n,02oct90,hjb eliminated a race and a bug in sockClose().03m,25may90,dnw removed malloc of file name "(socket)" before calling iosFdSet. added "(socket)" as device name for iosFdSet optimization.03l,11may90,yao added missing modification history (03k) for the last checkin.03k,09may90,yao typecasted malloc to (char *).03j,07may90,hjb added sockFdVerify(), connectWithTimeout(), a hack to avoid register d2 trashing bug.03i,19apr90,hjb deleted sendit(), recvit() and modified sendmsg() and recvmsg() accordingly, de-linted.03h,18mar90,hjb optimization of send(), sendto(), sockWrite(), recv(), recvfrom(), sockRead().03g,16mar90,rdc removed polling version of select.03f,09jun89,gae cleanup of 03e.03e,07jun89,gae changed SOCKADDR back to "struct sockaddr".03d,24may89,hjb fixed a bug in bind(); when sobind() fails, we now free the allocated mbuf before returning error code.03c,14apr89,gae made select backward compatible with BSD 4.2, fixed bug, made time of 0 not actually delay, more optimizing.03b,13apr89,rdc made select detect passively closed sockets. installed marc ullman's select optimizations.03a,15oct88,dnw changed call to sleep() to ksleep().02z,13sep88,llk Fixed bug in select. Now it polls at least once at all times.02y,26aug88,gae lint.02x,18aug88,gae documentation.02w,15jul88,llk changed to malloc the name of a socket fd before calling iosFdNew.02v,22jun88,dnw changed apparent socket filename from "(network)" to "(socket)"02u,30may88,dnw changed to v4 names.02t,28may88,dnw removed setNetStatus to netLib.02s,04may88,jcf changed so_timeoSem to so_timeoSemId +ecs fixed bug in select introduced in 02r.02r,30mar88,gae made mainly iosLib independent. Cleaned up select(), made it use BSD4.3 fd_set's and caught small bug.02q,27jan88,jcf made kernel independent.02p,05jan88,rdc added include of sysm.h02o,14dec87,gae added include of vxWorks.h!!!02n,18nov87,ecs documentation.02m,17nov87,rdc fixed documentation for setsockopt.02l,04nov87,dnw fixed warning from missing coercion in sendto.02k,03nov87,rdc added shutdown, cleaned up documentation.02j,13oct87,rdc added select, setSelectDelay. make setNetStatus not be LOCAL.02i,30sep87,gae removed use of fdTable by replacing with calls to iosFdSet().02h,02may87,dnw removed unnecessary includes.02g,01apr87,ecs fixed bugs in getsockname & getpeername (found by lint!) changed references to "struct sockaddr" to "SOCKADDR".02f,26mar87,rdc made sendit and recvit call splnet first thing.02e,23mar87,jlf documentation.02d,30jan87,rdc added getsockname and getpeername. sendit and recvit weren't always calling setNetStatus.02c,24dec86,gae changed stsLib.h to vwModNum.h.02b,08nov86,dnw fixed bug in closeSock of not freeing fd.02a,15oct86,rdc completely rewritten for new 4.3 network code.01i,04sep86,jlf minor documentation. made listen return OK always.01h,01jul86,jlf minor documentation01g,18jun86,rdc delinted.01f,29apr86,rdc changed memAllocates to mallocs and de-linted. Added getsockopt and setsockopt to implement KEEP_ALIVE.01e,09apr86,rdc all linked list code now uses lstLib. added sockCloseAll and sockNumActive.01d,28jan86,jlf documentation01c,20nov85,rdc fixed accept to be 4.2 like01b,08oct85,rdc de-linted01a,03oct85,rdc written*//*This library provides UNIX BSD 4.4 compatible socket calls. Use these calls to open, close, read, and write sockets. These sockets can join processes on the same CPU or on different CPUs between which there is anetwork connection. The calling sequences of these routines are identical to their equivalents under UNIX BSD 4.4.ADDRESS FAMILYVxWorks sockets support only the Internet Domain address family. UseAF_INET for the <domain> argument in subroutines that require it.There is no support for the UNIX Domain address family.IOCTL FUNCTIONSSockets respond to the following ioctl() functions. These functions aredefined in the header files ioLib.h and ioctl.h..IP 'FIONBIO' 15 Turns on/off non-blocking I/O..CS on = TRUE; status = ioctl (sFd, FIONBIO, &on);.CE.IP 'FIONREAD'Reports the number of read-ready bytes available on the socket. On the return of ioctl(), <bytesAvailable> has the number of bytes available to read from the socket..CS status = ioctl (sFd, FIONREAD, &bytesAvailable);.CE.IP 'SIOCATMARK'Reports whether there is out-of-band data to be read from the socket. Onthe return of ioctl(), <atMark> is TRUE (1) if there is out-of-banddata. Otherwise, it is FALSE (0)..CS status = ioctl (sFd, SIOCATMARK, &atMark);.CEINCLUDE FILES: types.h, mbuf.h, socket.h, socketvar.hSEE ALSO: netLib,.pG "Network"*//* includes */#include "vxWorks.h"#include "stdlib.h"#include "iosLib.h"#include "netLib.h"#include "errnoLib.h"#include "sockFunc.h"#include "sockLib.h"/* typedefs */typedef struct sockLibMap { int domainMap; /* mapping address family */ int domainReal; /* real address family */ SOCK_FUNC * pSockFunc; /* socket function table */ struct sockLibMap * pNext; /* next socket lib mapping */ } SOCK_LIB_MAP;/* globals */SOCK_FUNC ** pSockFdMap = NULL;/* locals */LOCAL SOCK_LIB_MAP * pSockLibMapH = NULL;/********************************************************************************* sockLibInit - initialize a socket library back-end library.** RETURNS: OK, or ERROR if the back-end could not be initialized.** NOMANUAL*/STATUS sockLibInit ( int fdMax /* maximum file descriptors */ ) { if (pSockFdMap != NULL) /* already initialized ? */ return (OK); /* allocate fd-to-pSockFunc mapping array */ if ((pSockFdMap = (SOCK_FUNC **) calloc (1, fdMax * sizeof (SOCK_FUNC **))) == NULL) return (ERROR); return (OK); }/********************************************************************************* sockLibAdd - add a socket library back-end** RETURNS: OK, or ERROR if the socket back-end could not be added.** NOMANUAL*/STATUS sockLibAdd ( FUNCPTR sockLibInitRtn, /* back-end init routine */ int domainMap, /* address family */ int domainReal /* address family */ ) { SOCK_LIB_MAP * pSockLibMap; /* back-end mapping entry ptr */ if (pSockFdMap == NULL) /* initialized yet ? */ return (ERROR); if ((pSockLibMap = (SOCK_LIB_MAP *) malloc (sizeof (SOCK_LIB_MAP))) == NULL) return (ERROR); /* init socket back-end */ if ((sockLibInitRtn == NULL) || ((pSockLibMap->pSockFunc = (SOCK_FUNC *) (sockLibInitRtn) ()) == NULL)) { free (pSockLibMap); /* back-end error */ return (ERROR); } pSockLibMap->domainMap = domainMap; /* stuff mapping address family */ pSockLibMap->domainReal = domainReal; /* stuff real address family */ pSockLibMap->pNext = pSockLibMapH; /* hook into lib list */ pSockLibMapH = pSockLibMap; /* update lib list head */ return (OK); }/********************************************************************************* socket - open a socket** This routine opens a socket and returns a socket descriptor.* The socket descriptor is passed to the other socket routines to identify the* socket. The socket descriptor is a standard I/O system file descriptor* (fd) and can be used with the close(), read(), write(), and ioctl() routines.** Available socket types include:* .IP SOCK_STREAM 18* Specifies a connection-based (stream) socket.* .IP SOCK_DGRAM* Specifies a datagram (UDP) socket.* .IP SOCK_RAW* Specifies a raw socket.* .LP** RETURNS: A socket descriptor, or ERROR.*/int socket ( int domain, /* address family (for example, AF_INET) */ int type, /* SOCK_STREAM, SOCK_DGRAM, or SOCK_RAW */ int protocol /* socket protocol (usually 0) */ ) { SOCK_LIB_MAP * pSockLibMap; int newFd; if (pSockFdMap == NULL) /* initialized yet ? */ return (ERROR); /* search for a suitable back-end library entry */ for (pSockLibMap = pSockLibMapH; (pSockLibMap != NULL) && (pSockLibMap->domainMap != domain); pSockLibMap = pSockLibMap->pNext) ; /* found library map entry ? */ if ((pSockLibMap == NULL) || (pSockLibMap->pSockFunc->socketRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } /* call back-end to open a socket */ if ((newFd = (pSockLibMap->pSockFunc->socketRtn) (pSockLibMap->domainReal, type, protocol)) == ERROR) return (ERROR); pSockFdMap[newFd] = pSockLibMap->pSockFunc; /* stuff fd map array */ return (newFd); }/********************************************************************************* bind - bind a name to a socket** This routine associates a network address (also referred to as its "name")* with a specified socket so that other processes can connect or send to it.* When a socket is created with socket(), it belongs to an address family* but has no assigned name.** RETURNS:* OK, or ERROR if there is an invalid socket, the address is either* unavailable or in use, or the socket is already bound.*/STATUS bind ( int s, /* socket descriptor */ struct sockaddr *name, /* name to be bound */ int namelen /* length of name */ ) { SOCK_FUNC * pSockFunc = pSockFdMap[s]; if ((pSockFunc == NULL) || (pSockFunc->bindRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } return ((pSockFunc->bindRtn) (s, name, namelen)); }/********************************************************************************* listen - enable connections to a socket** This routine enables connections to a socket. It also specifies the* maximum number of unaccepted connections that can be pending at one time* (<backlog>). After enabling connections with listen(), connections are* actually accepted by accept().** RETURNS: OK, or ERROR if the socket is invalid or unable to listen.*/STATUS listen ( int s, /* socket descriptor */ int backlog /* number of connections to queue */ ) { SOCK_FUNC * pSockFunc = pSockFdMap[s]; if ((pSockFunc == NULL) || (pSockFunc->listenRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } return ((pSockFunc->listenRtn) (s, backlog)); }/********************************************************************************* accept - accept a connection from a socket** This routine accepts a connection on a socket, and returns a new socket* created for the connection. The socket must be bound to an address with* bind(), and enabled for connections by a call to listen(). The accept()* routine dequeues the first connection and creates a new socket with the* same properties as <s>. It blocks the caller until a connection is* present, unless the socket is marked as non-blocking.** The parameter <addrlen> should be initialized to the size of the available* buffer pointed to by <addr>. Upon return, <addrlen> contains the size in* bytes of the peer's address stored in <addr>.** RETURNS* A socket descriptor, or ERROR if the call fails.*/int accept ( int s, /* socket descriptor */ struct sockaddr *addr, /* peer address */ int *addrlen /* peer address length */ ) { SOCK_FUNC * pSockFunc = pSockFdMap[s]; int newFd; if ((pSockFunc == NULL) || (pSockFunc->acceptRtn == NULL)) { netErrnoSet (ENOTSUP); return (ERROR); } if ((newFd = (pSockFunc->acceptRtn) (s, addr, addrlen)) == ERROR)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -