📄 socklib.c
字号:
/* sockLib.c - generic socket library *//* Copyright 1984 - 2002 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------05g,27jun02,rae removed excessive accept() argument validation05f,21may02,vvv updated connectWithTimeout doc (SPR #27684)05e,10may02,kbw making man page edits05d,28apr02,rae Corrected getsockname() doc (SPR #70327)05c,26apr02,vvv updated setsockopt doc (SPR #21992/28868/30335)05b,07dec01,rae merge from synth ver 05c (spr #28181 correctly)05a,15oct01,rae merge from truestack ver 05b base 04q (SPR #28181 etc.)04z,12sep01,cyr doc: correct typo per SPR 2906404y,16jan01,cco Add 'See Also' reference to UNIX network Programming04x,10jan01,dgr correcting comment header block for shutdown() as per SPR#3552904w,14nov00,rae removed unused argument from sockLibAdd()04v,10nov00,ham doc: corrected setsockopt(SO_DEBUG) example (SPR #62267).04u,24oct00,dgr adding to setsockopt() comment header block (re: SPR #20562)04t,18oct00,zhu change return value for sockFdtosockFunc04s,17oct00,zhu calloc doesn't need zero out04r,16oct00,zhu zero out pSockFdMap and move file descriptor check funtion here04q,17aug99,pai changed send() prototype to use const on the second param (SPR 21829)04t,08nov99,pul T2 cumulative patch 204s,29apr99,pul Upgraded NPT phase3 code to tor2.0.004r,16mar99,spm recovered orphaned code from tor2_0_x branch (SPR #25770)04q,-2feb99,sgv Added bitFlags to the sockLibAdd routine, added routine sockfdtosockFunc04p,02dec98,n_s updated comments for sendmsg and recvmsg. spr 2355204o,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,14apr97,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.However, although the socket interface is compatible with VxWorks, the VxWorks environment does affect how you use sockets. Specifically, the globally accessible file descriptors available in the single address space world of VxWorks require that you take extra precautions when closing a file descriptor.You must make sure that you do not close the file descriptor on which a task is pending during an accept(). Although the accept() on the closed file descriptor sometimes returns with an error, the accept() can also fail to return at all. Thus, if you need to be able to close a socket connections file descriptor asynchronously, you may need to set up a semaphore-based locking mechanism that prevents the close while an accept() is pending on the file descriptor.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);.CETo use this feature, include the following component:INCLUDE_BSD_SOCKETINCLUDE FILES: types.h, mbuf.h, socket.h, socketvar.hSEE ALSO: netLib,.I "UNIX Network Programming", by W. Richard Stevens*//* includes */#include "vxWorks.h"#include "stdlib.h"#include "iosLib.h"#include "netLib.h"#include "errnoLib.h"#include "sockFunc.h"#include "sockLib.h"#include "memPartLib.h"#include "netBufLib.h"#include "string.h"#ifdef VIRTUAL_STACK#include "netinet/vsLib.h"#endif /* VIRTUAL_STACK *//* globals */SOCK_FUNC ** pSockFdMap = NULL;/* locals */LOCAL SOCK_LIB_MAP * pSockLibMapH = NULL;LOCAL UINT sockFdMax = 0; /* for parameter verification *//********************************************************************************* 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 **) KHEAP_ALLOC(fdMax * sizeof (SOCK_FUNC **))) == NULL) return (ERROR); bzero ((char *)pSockFdMap, fdMax * sizeof(SOCK_FUNC **)); sockFdMax = fdMax; 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 *) KHEAP_ALLOC(sizeof(SOCK_LIB_MAP))) == NULL) return (ERROR); /* init socket back-end */ if ((sockLibInitRtn == NULL) || ((pSockLibMap->pSockFunc = (SOCK_FUNC *) (sockLibInitRtn) ()) == NULL)) { KHEAP_FREE((char *)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 = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -