sys/socket.h

http://fxr.watson.org/fxr/source/sys/socket.h


这个是不是内核里头的啊?

  1 /*-
    2  * Copyright (c) 1982, 1985, 1986, 1988, 1993, 1994
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   27  * SUCH DAMAGE.
   28  *
   29  *      @(#)socket.h    8.4 (Berkeley) 2/21/94
   30  * $FreeBSD: head/sys/sys/socket.h 254925 2013-08-26 18:16:05Z jhb $
   31  */
   32 
   33 #ifndef _SYS_SOCKET_H_
   34 #define _SYS_SOCKET_H_
   35 
   36 #include 
   37 #include 
   38 #include 
   39 #include 
   40 
   41 /*
   42  * Definitions related to sockets: types, address families, options.
   43  */
   44 
   45 /*
   46  * Data types.
   47  */
   48 #if __BSD_VISIBLE
   49 #ifndef _GID_T_DECLARED
   50 typedef __gid_t         gid_t;
   51 #define _GID_T_DECLARED
   52 #endif
   53 
   54 #ifndef _OFF_T_DECLARED
   55 typedef __off_t         off_t;
   56 #define _OFF_T_DECLARED
   57 #endif
   58 
   59 #ifndef _PID_T_DECLARED
   60 typedef __pid_t         pid_t;
   61 #define _PID_T_DECLARED
   62 #endif
   63 #endif
   64 
   65 #ifndef _SA_FAMILY_T_DECLARED
   66 typedef __sa_family_t   sa_family_t;
   67 #define _SA_FAMILY_T_DECLARED
   68 #endif
   69 
   70 #ifndef _SOCKLEN_T_DECLARED
   71 typedef __socklen_t     socklen_t;
   72 #define _SOCKLEN_T_DECLARED
   73 #endif
   74  
   75 #ifndef _SSIZE_T_DECLARED
   76 typedef __ssize_t       ssize_t;
   77 #define _SSIZE_T_DECLARED
   78 #endif
   79 
   80 #if __BSD_VISIBLE 
   81 #ifndef _UID_T_DECLARED
   82 typedef __uid_t         uid_t;
   83 #define _UID_T_DECLARED
   84 #endif
   85 #endif
   86 
   87 /*
   88  * Types
   89  */
   90 #define SOCK_STREAM     1               /* stream socket 流*/
   91 #define SOCK_DGRAM      2               /* datagram socket 数据报*/
   92 #define SOCK_RAW        3               /* raw-protocol interface  不懂*/
   93 #if __BSD_VISIBLE
   94 #define SOCK_RDM        4               /* reliably-delivered message */
   95 #endif
   96 #define SOCK_SEQPACKET  5               /* sequenced packet stream */
   97 
   98 #if __BSD_VISIBLE
   99 /*
  100  * Creation flags, OR'ed into socket() and socketpair() type argument.
  101  */
  102 #define SOCK_CLOEXEC    0x10000000
  103 #define SOCK_NONBLOCK   0x20000000
  104 #endif
  105 
  106 /*
  107  * Option flags per-socket.  每个socket的选项标识
  108  */
  109 #define SO_DEBUG        0x0001          /* turn on debugging info recording 打开调试信息*/
  110 #define SO_ACCEPTCONN   0x0002          /* socket has had listen() 监听?*/
  111 #define SO_REUSEADDR    0x0004          /* allow local address reuse  允许本地地址重用?*/
  112 #define SO_KEEPALIVE    0x0008          /* keep connections alive 保持连接在线*/
  113 #define SO_DONTROUTE    0x0010          /* just use interface addresses 仅仅使用接口地址?不使用路由?*/
  114 #define SO_BROADCAST    0x0020          /* permit sending of broadcast msgs 允许发送广播消息*/
  115 #if __BSD_VISIBLE
  116 #define SO_USELOOPBACK  0x0040          /* bypass hardware when possible */
  117 #endif
  118 #define SO_LINGER       0x0080          /* linger on close if data present */
  119 #define SO_OOBINLINE    0x0100          /* leave received OOB data in line */
  120 #if __BSD_VISIBLE
  121 #define SO_REUSEPORT    0x0200          /* allow local address & port reuse */
  122 #define SO_TIMESTAMP    0x0400          /* timestamp received dgram traffic */
  123 #define SO_NOSIGPIPE    0x0800          /* no SIGPIPE from EPIPE */
  124 #define SO_ACCEPTFILTER 0x1000          /* there is an accept filter */
  125 #define SO_BINTIME      0x2000          /* timestamp received dgram traffic */
  126 #endif
  127 #define SO_NO_OFFLOAD   0x4000          /* socket cannot be offloaded */
  128 #define SO_NO_DDP       0x8000          /* disable direct data placement */
  129 
  130 /*
  131  * Additional options, not kept in so_options. 啥意思?
  132  */
  133 #define SO_SNDBUF       0x1001          /* send buffer size 发送缓冲的大小*/
  134 #define SO_RCVBUF       0x1002          /* receive buffer size 接受缓冲的大小*/
  135 #define SO_SNDLOWAT     0x1003          /* send low-water mark 发送低水位的mark????*/
  136 #define SO_RCVLOWAT     0x1004          /* receive low-water mark */
  137 #define SO_SNDTIMEO     0x1005          /* send timeout */
  138 #define SO_RCVTIMEO     0x1006          /* receive timeout */
  139 #define SO_ERROR        0x1007          /* get error status and clear */
  140 #define SO_TYPE         0x1008          /* get socket type */
  141 #if __BSD_VISIBLE
  142 #define SO_LABEL        0x1009          /* socket's MAC label */
  143 #define SO_PEERLABEL    0x1010          /* socket's peer's MAC label */
  144 #define SO_LISTENQLIMIT 0x1011          /* socket's backlog limit */
  145 #define SO_LISTENQLEN   0x1012          /* socket's complete queue length */
  146 #define SO_LISTENINCQLEN        0x1013  /* socket's incomplete queue length */
  147 #define SO_SETFIB       0x1014          /* use this FIB to route */
  148 #define SO_USER_COOKIE  0x1015          /* user cookie (dummynet etc.) */
  149 #define SO_PROTOCOL     0x1016          /* get socket protocol (Linux name) */
  150 #define SO_PROTOTYPE    SO_PROTOCOL     /* alias for SO_PROTOCOL (SunOS name) */
  151 #endif
  152 
  153 /*
  154  * Space reserved for new socket options added by third-party vendors.
  155  * This range applies to all socket option levels.  New socket options
  156  * in FreeBSD should always use an option value less than SO_VENDOR.
  157  */
  158 #if __BSD_VISIBLE
  159 #define SO_VENDOR       0x80000000
  160 #endif
  161 
  162 /*
  163  * Structure used for manipulating linger option.
  164  */
  165 struct linger {
  166         int     l_onoff;                /* option on/off */
  167         int     l_linger;               /* linger time */
  168 };
  169 
  170 #if __BSD_VISIBLE
  171 struct accept_filter_arg {
  172         char    af_name[16];
  173         char    af_arg[256-16];
  174 };
  175 #endif
  176 
  177 /*
  178  * Level number for (get/set)sockopt() to apply to socket itself.
  179  */
  180 #define SOL_SOCKET      0xffff          /* options for socket level */
  181 
  182 /*
  183  * Address families. 地址家族
  184  */
  185 #define AF_UNSPEC       0               /* unspecified */
  186 #if __BSD_VISIBLE
  187 #define AF_LOCAL        AF_UNIX         /* local to host (pipes, portals) */ 本地?如pipe ?
  188 #endif
  189 #define AF_UNIX         1               /* standardized name for AF_LOCAL */
  190 #define AF_INET         2               /* internetwork: UDP, TCP, etc. */ internet?
  191 #if __BSD_VISIBLE
  192 #define AF_IMPLINK      3               /* arpanet imp addresses */
  193 #define AF_PUP          4               /* pup protocols: e.g. BSP */
  194 #define AF_CHAOS        5               /* mit CHAOS protocols */
  195 #define AF_NETBIOS      6               /* SMB protocols */
  196 #define AF_ISO          7               /* ISO protocols */
  197 #define AF_OSI          AF_ISO
  198 #define AF_ECMA         8               /* European computer manufacturers */
  199 #define AF_DATAKIT      9               /* datakit protocols */
  200 #define AF_CCITT        10              /* CCITT protocols, X.25 etc */
  201 #define AF_SNA          11              /* IBM SNA */
  202 #define AF_DECnet       12              /* DECnet */
  203 #define AF_DLI          13              /* DEC Direct data link interface */
  204 #define AF_LAT          14              /* LAT */
  205 #define AF_HYLINK       15              /* NSC Hyperchannel */
  206 #define AF_APPLETALK    16              /* Apple Talk */
  207 #define AF_ROUTE        17              /* Internal Routing Protocol */
  208 #define AF_LINK         18              /* Link layer interface */
  209 #define pseudo_AF_XTP   19              /* eXpress Transfer Protocol (no AF) */
  210 #define AF_COIP         20              /* connection-oriented IP, aka ST II */
  211 #define AF_CNT          21              /* Computer Network Technology */
  212 #define pseudo_AF_RTIP  22              /* Help Identify RTIP packets */
  213 #define AF_IPX          23              /* Novell Internet Protocol */
  214 #define AF_SIP          24              /* Simple Internet Protocol */
  215 #define pseudo_AF_PIP   25              /* Help Identify PIP packets */
  216 #define AF_ISDN         26              /* Integrated Services Digital Network*/
  217 #define AF_E164         AF_ISDN         /* CCITT E.164 recommendation */
  218 #define pseudo_AF_KEY   27              /* Internal key-management function */
  219 #endif
  220 #define AF_INET6        28              /* IPv6 */
  221 #if __BSD_VISIBLE
  222 #define AF_NATM         29              /* native ATM access */
  223 #define AF_ATM          30              /* ATM */
  224 #define pseudo_AF_HDRCMPLT 31           /* Used by BPF to not rewrite headers
  225                                          * in interface output routine
  226                                          */
  227 #define AF_NETGRAPH     32              /* Netgraph sockets */
  228 #define AF_SLOW         33              /* 802.3ad slow protocol */
  229 #define AF_SCLUSTER     34              /* Sitara cluster protocol */
  230 #define AF_ARP          35
  231 #define AF_BLUETOOTH    36              /* Bluetooth sockets */
  232 #define AF_IEEE80211    37              /* IEEE 802.11 protocol */
  233 #define AF_INET_SDP     40              /* OFED Socket Direct Protocol ipv4 */
  234 #define AF_INET6_SDP    42              /* OFED Socket Direct Protocol ipv6 */
  235 #define AF_MAX          42
  236 /*
  237  * When allocating a new AF_ constant, please only allocate
  238  * even numbered constants for FreeBSD until 134 as odd numbered AF_
  239  * constants 39-133 are now reserved for vendors.
  240  */
  241 #define AF_VENDOR00 39
  242 #define AF_VENDOR01 41
  243 #define AF_VENDOR02 43
  244 #define AF_VENDOR03 45
  245 #define AF_VENDOR04 47
  246 #define AF_VENDOR05 49
  247 #define AF_VENDOR06 51
  248 #define AF_VENDOR07 53
  249 #define AF_VENDOR08 55
  250 #define AF_VENDOR09 57
  251 #define AF_VENDOR10 59
  252 #define AF_VENDOR11 61
  253 #define AF_VENDOR12 63
  254 #define AF_VENDOR13 65
  255 #define AF_VENDOR14 67
  256 #define AF_VENDOR15 69
  257 #define AF_VENDOR16 71
  258 #define AF_VENDOR17 73
  259 #define AF_VENDOR18 75
  260 #define AF_VENDOR19 77
  261 #define AF_VENDOR20 79
  262 #define AF_VENDOR21 81
  263 #define AF_VENDOR22 83
  264 #define AF_VENDOR23 85
  265 #define AF_VENDOR24 87
  266 #define AF_VENDOR25 89
  267 #define AF_VENDOR26 91
  268 #define AF_VENDOR27 93
  269 #define AF_VENDOR28 95
  270 #define AF_VENDOR29 97
  271 #define AF_VENDOR30 99
  272 #define AF_VENDOR31 101
  273 #define AF_VENDOR32 103
  274 #define AF_VENDOR33 105
  275 #define AF_VENDOR34 107
  276 #define AF_VENDOR35 109
  277 #define AF_VENDOR36 111
  278 #define AF_VENDOR37 113
  279 #define AF_VENDOR38 115
  280 #define AF_VENDOR39 117
  281 #define AF_VENDOR40 119
  282 #define AF_VENDOR41 121
  283 #define AF_VENDOR42 123
  284 #define AF_VENDOR43 125
  285 #define AF_VENDOR44 127
  286 #define AF_VENDOR45 129
  287 #define AF_VENDOR46 131
  288 #define AF_VENDOR47 133
  289 #endif
  290 
  291 /*
  292  * Structure used by kernel to store most       这居然是内核用来存储大多数地址用的
  293  * addresses.
  294  */
  295 struct sockaddr {
  296         unsigned char   sa_len;         /* total length */  “socket地址”的全部长度?
  297         sa_family_t     sa_family;      /* address family */  地址家族
  298         char            sa_data[14];    /* actually longer; address value */  实际的长度?地址值
  299 };
  300 #if __BSD_VISIBLE
  301 #define SOCK_MAXADDRLEN 255             /* longest possible addresses */
  302 
  303 /*
  304  * Structure used by kernel to pass protocol   内科以raw socket 来传递协议信息的。
  305  * information in raw sockets.
  306  */
  307 struct sockproto {
  308         unsigned short  sp_family;              /* address family */ 地址家族
  309         unsigned short  sp_protocol;            /* protocol */  协议
  310 };
  311 #endif
  312 
  313 #include 
  314 
  315 #if __BSD_VISIBLE
  316 /*
  317  * Protocol families, same as address families for now.
  318  */
  319 #define PF_UNSPEC       AF_UNSPEC
  320 #define PF_LOCAL        AF_LOCAL
  321 #define PF_UNIX         PF_LOCAL        /* backward compatibility */
  322 #define PF_INET         AF_INET
  323 #define PF_IMPLINK      AF_IMPLINK
  324 #define PF_PUP          AF_PUP
  325 #define PF_CHAOS        AF_CHAOS
  326 #define PF_NETBIOS      AF_NETBIOS
  327 #define PF_ISO          AF_ISO
  328 #define PF_OSI          AF_ISO
  329 #define PF_ECMA         AF_ECMA
  330 #define PF_DATAKIT      AF_DATAKIT
  331 #define PF_CCITT        AF_CCITT
  332 #define PF_SNA          AF_SNA
  333 #define PF_DECnet       AF_DECnet
  334 #define PF_DLI          AF_DLI
  335 #define PF_LAT          AF_LAT
  336 #define PF_HYLINK       AF_HYLINK
  337 #define PF_APPLETALK    AF_APPLETALK
  338 #define PF_ROUTE        AF_ROUTE
  339 #define PF_LINK         AF_LINK
  340 #define PF_XTP          pseudo_AF_XTP   /* really just proto family, no AF */
  341 #define PF_COIP         AF_COIP
  342 #define PF_CNT          AF_CNT
  343 #define PF_SIP          AF_SIP
  344 #define PF_IPX          AF_IPX
  345 #define PF_RTIP         pseudo_AF_RTIP  /* same format as AF_INET */
  346 #define PF_PIP          pseudo_AF_PIP
  347 #define PF_ISDN         AF_ISDN
  348 #define PF_KEY          pseudo_AF_KEY
  349 #define PF_INET6        AF_INET6
  350 #define PF_NATM         AF_NATM
  351 #define PF_ATM          AF_ATM
  352 #define PF_NETGRAPH     AF_NETGRAPH
  353 #define PF_SLOW         AF_SLOW
  354 #define PF_SCLUSTER     AF_SCLUSTER
  355 #define PF_ARP          AF_ARP
  356 #define PF_BLUETOOTH    AF_BLUETOOTH
  357 #define PF_IEEE80211    AF_IEEE80211
  358 #define PF_INET_SDP     AF_INET_SDP
  359 #define PF_INET6_SDP    AF_INET6_SDP
  360 
  361 #define PF_MAX          AF_MAX
  362 
  363 /*
  364  * Definitions for network related sysctl, CTL_NET.
  365  *
  366  * Second level is protocol family.
  367  * Third level is protocol number.
  368  *
  369  * Further levels are defined by the individual families below.
  370  */
  371 #define NET_MAXID       AF_MAX
  372 
  373 /*
  374  * PF_ROUTE - Routing table  路由表
  375  *
  376  * Three additional levels are defined:
  377  *      Fourth: address family, 0 is wildcard
  378  *      Fifth: type of info, defined below
  379  *      Sixth: flag(s) to mask with for NET_RT_FLAGS
  380  */
  381 #define NET_RT_DUMP     1               /* dump; may limit to a.f. */
  382 #define NET_RT_FLAGS    2               /* by flags, e.g. RESOLVING */
  383 #define NET_RT_IFLIST   3               /* survey interface list */
  384 #define NET_RT_IFMALIST 4               /* return multicast address list */
  385 #define NET_RT_IFLISTL  5               /* Survey interface list, using 'l'en
  386                                          * versions of msghdr structs. */
  387 #define NET_RT_MAXID    6
  388 
  389 #endif /* __BSD_VISIBLE */
  390 
  391 /*
  392  * Maximum queue length specifiable by listen.     监听的最大的队列的长度
  393  */
  394 #define SOMAXCONN       128
  395 
  396 /*
  397  * Message header for recvmsg and sendmsg calls.       recvmsg和sendmsg调用的消息头
  398  * Used value-result for recvmsg, value only for sendmsg.
  399  */
  400 struct msghdr {
  401         void            *msg_name;              /* optional address */
  402         socklen_t        msg_namelen;           /* size of address */
  403         struct iovec    *msg_iov;               /* scatter/gather array */
  404         int              msg_iovlen;            /* # elements in msg_iov */
  405         void            *msg_control;           /* ancillary data, see below */
  406         socklen_t        msg_controllen;        /* ancillary data buffer len */
  407         int              msg_flags;             /* flags on received message */
  408 };
  409 
  410 #define MSG_OOB         0x1             /* process out-of-band data */
  411 #define MSG_PEEK        0x2             /* peek at incoming message */
  412 #define MSG_DONTROUTE   0x4             /* send without using routing tables */
  413 #define MSG_EOR         0x8             /* data completes record */
  414 #define MSG_TRUNC       0x10            /* data discarded before delivery */
  415 #define MSG_CTRUNC      0x20            /* control data lost before delivery */
  416 #define MSG_WAITALL     0x40            /* wait for full request or error */
  417 #if __POSIX_VISIBLE >= 200809
  418 #define MSG_NOSIGNAL    0x20000         /* do not generate SIGPIPE on EOF */
  419 #endif
  420 #if __BSD_VISIBLE
  421 #define MSG_DONTWAIT    0x80            /* this message should be nonblocking */
  422 #define MSG_EOF         0x100           /* data completes connection */
  423 #define MSG_NOTIFICATION 0x2000         /* SCTP notification */
  424 #define MSG_NBIO        0x4000          /* FIONBIO mode, used by fifofs */
  425 #define MSG_COMPAT      0x8000          /* used in sendit() */
  426 #define MSG_CMSG_CLOEXEC 0x40000        /* make received fds close-on-exec */
  427 #endif
  428 #ifdef _KERNEL
  429 #define MSG_SOCALLBCK   0x10000         /* for use by socket callbacks - soreceive (TCP) */
  430 #endif
  431 
  432 /*
  433  * Header for ancillary data objects in msg_control buffer.
  434  * Used for additional information with/about a datagram
  435  * not expressible by flags.  The format is a sequence
  436  * of message elements headed by cmsghdr structures.
  437  */
  438 struct cmsghdr {
  439         socklen_t       cmsg_len;               /* data byte count, including hdr */
  440         int             cmsg_level;             /* originating protocol */
  441         int             cmsg_type;              /* protocol-specific type */
  442 /* followed by  u_char  cmsg_data[]; */
  443 };
  444 
  445 #if __BSD_VISIBLE
  446 /*
  447  * While we may have more groups than this, the cmsgcred struct must
  448  * be able to fit in an mbuf and we have historically supported a
  449  * maximum of 16 groups.
  450 */
  451 #define CMGROUP_MAX 16
  452 
  453 /*
  454  * Credentials structure, used to verify the identity of a peer
  455  * process that has sent us a message. This is allocated by the
  456  * peer process but filled in by the kernel. This prevents the
  457  * peer from lying about its identity. (Note that cmcred_groups[0]
  458  * is the effective GID.)
  459  */
  460 struct cmsgcred {
  461         pid_t   cmcred_pid;             /* PID of sending process */
  462         uid_t   cmcred_uid;             /* real UID of sending process */
  463         uid_t   cmcred_euid;            /* effective UID of sending process */
  464         gid_t   cmcred_gid;             /* real GID of sending process */
  465         short   cmcred_ngroups;         /* number or groups */
  466         gid_t   cmcred_groups[CMGROUP_MAX];     /* groups */
  467 };
  468 
  469 /*
  470  * Socket credentials证书.
  471  */
  472 struct sockcred {
  473         uid_t   sc_uid;                 /* real user id */
  474         uid_t   sc_euid;                /* effective user id */
  475         gid_t   sc_gid;                 /* real group id */
  476         gid_t   sc_egid;                /* effective group id */
  477         int     sc_ngroups;             /* number of supplemental groups */
  478         gid_t   sc_groups[1];           /* variable length */
  479 };
  480 
  481 /*
  482  * Compute size of a sockcred structure with groups.
  483  */
  484 #define SOCKCREDSIZE(ngrps) \
  485         (sizeof(struct sockcred) + (sizeof(gid_t) * ((ngrps) - 1)))
  486 
  487 #endif /* __BSD_VISIBLE */
  488 
  489 /* given pointer to struct cmsghdr, return pointer to data */
  490 #define CMSG_DATA(cmsg)         ((unsigned char *)(cmsg) + \
  491                                  _ALIGN(sizeof(struct cmsghdr)))
  492 
  493 /* given pointer to struct cmsghdr, return pointer to next cmsghdr */
  494 #define CMSG_NXTHDR(mhdr, cmsg) \
  495         ((char *)(cmsg) == NULL ? CMSG_FIRSTHDR(mhdr) : \
  496             ((char *)(cmsg) + _ALIGN(((struct cmsghdr *)(cmsg))->cmsg_len) + \
  497           _ALIGN(sizeof(struct cmsghdr)) > \
  498             (char *)(mhdr)->msg_control + (mhdr)->msg_controllen) ? \
  499             (struct cmsghdr *)0 : \
  500             (struct cmsghdr *)(void *)((char *)(cmsg) + \
  501             _ALIGN(((struct cmsghdr *)(cmsg))->cmsg_len)))
  502 
  503 /*
  504  * RFC 2292 requires to check msg_controllen, in case that the kernel returns
  505  * an empty list for some reasons.
  506  */
  507 #define CMSG_FIRSTHDR(mhdr) \
  508         ((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
  509          (struct cmsghdr *)(mhdr)->msg_control : \
  510          (struct cmsghdr *)NULL)
  511 
  512 #if __BSD_VISIBLE
  513 /* RFC 2292 additions */
  514 #define CMSG_SPACE(l)           (_ALIGN(sizeof(struct cmsghdr)) + _ALIGN(l))
  515 #define CMSG_LEN(l)             (_ALIGN(sizeof(struct cmsghdr)) + (l))
  516 #endif
  517 
  518 #ifdef _KERNEL
  519 #define CMSG_ALIGN(n)   _ALIGN(n)
  520 #endif
  521 
  522 /* "Socket"-level control message types: */
  523 #define SCM_RIGHTS      0x01            /* access rights (array of int) */
  524 #if __BSD_VISIBLE
  525 #define SCM_TIMESTAMP   0x02            /* timestamp (struct timeval) */
  526 #define SCM_CREDS       0x03            /* process creds (struct cmsgcred) */
  527 #define SCM_BINTIME     0x04            /* timestamp (struct bintime) */
  528 #endif
  529 
  530 #if __BSD_VISIBLE
  531 /*
  532  * 4.3 compat sockaddr, move to compat file later
  533  */
  534 struct osockaddr {
  535         unsigned short sa_family;       /* address family */
  536         char    sa_data[14];            /* up to 14 bytes of direct address */
  537 };
  538 
  539 /*
  540  * 4.3-compat message header (move to compat file later).
  541  */
  542 struct omsghdr {
  543         char    *msg_name;              /* optional address */
  544         int     msg_namelen;            /* size of address */
  545         struct  iovec *msg_iov;         /* scatter/gather array */
  546         int     msg_iovlen;             /* # elements in msg_iov */
  547         char    *msg_accrights;         /* access rights sent/received */
  548         int     msg_accrightslen;
  549 };
  550 #endif
  551 
  552 /*
  553  * howto arguments for shutdown(2), specified by Posix.1g.
  554  */
  555 #define SHUT_RD         0               /* shut down the reading side */
  556 #define SHUT_WR         1               /* shut down the writing side */
  557 #define SHUT_RDWR       2               /* shut down both sides */
  558 
  559 #if __BSD_VISIBLE
  560 /* for SCTP */
  561 /* we cheat and use the SHUT_XX defines for these */
  562 #define PRU_FLUSH_RD     SHUT_RD
  563 #define PRU_FLUSH_WR     SHUT_WR
  564 #define PRU_FLUSH_RDWR   SHUT_RDWR
  565 #endif
  566 
  567 
  568 #if __BSD_VISIBLE
  569 /*
  570  * sendfile(2) header/trailer struct
  571  */
  572 struct sf_hdtr {
  573         struct iovec *headers;  /* pointer to an array of header struct iovec's */
  574         int hdr_cnt;            /* number of header iovec's */
  575         struct iovec *trailers; /* pointer to an array of trailer struct iovec's */
  576         int trl_cnt;            /* number of trailer iovec's */
  577 };
  578 
  579 /*
  580  * Sendfile-specific flag(s)
  581  */
  582 #define SF_NODISKIO     0x00000001
  583 #define SF_MNOWAIT      0x00000002
  584 #define SF_SYNC         0x00000004
  585 
  586 #ifdef _KERNEL
  587 #define SFK_COMPAT      0x00000001
  588 #endif /* _KERNEL */
  589 #endif /* __BSD_VISIBLE */
  590 
  591 #ifndef _KERNEL
  592 
  593 #include 
  594 
  595 __BEGIN_DECLS
  596 int     accept(int, struct sockaddr * __restrict, socklen_t * __restrict);
  597 int     bind(int, const struct sockaddr *, socklen_t);
  598 int     connect(int, const struct sockaddr *, socklen_t);
  599 #if __BSD_VISIBLE
  600 int     accept4(int, struct sockaddr * __restrict, socklen_t * __restrict, int);
  601 int     bindat(int, int, const struct sockaddr *, socklen_t);
  602 int     connectat(int, int, const struct sockaddr *, socklen_t);
  603 #endif
  604 int     getpeername(int, struct sockaddr * __restrict, socklen_t * __restrict);
  605 int     getsockname(int, struct sockaddr * __restrict, socklen_t * __restrict);
  606 int     getsockopt(int, int, int, void * __restrict, socklen_t * __restrict);
  607 int     listen(int, int);
  608 ssize_t recv(int, void *, size_t, int);
  609 ssize_t recvfrom(int, void *, size_t, int, struct sockaddr * __restrict, socklen_t * __restrict);
  610 ssize_t recvmsg(int, struct msghdr *, int);
  611 ssize_t send(int, const void *, size_t, int);
  612 ssize_t sendto(int, const void *,
  613             size_t, int, const struct sockaddr *, socklen_t);
  614 ssize_t sendmsg(int, const struct msghdr *, int);
  615 #if __BSD_VISIBLE
  616 int     sendfile(int, int, off_t, size_t, struct sf_hdtr *, off_t *, int);
  617 int     setfib(int);
  618 #endif
  619 int     setsockopt(int, int, int, const void *, socklen_t);
  620 int     shutdown(int, int);
  621 int     sockatmark(int);
  622 int     socket(int, int, int);
  623 int     socketpair(int, int, int, int *);
  624 __END_DECLS
  625 
  626 #endif /* !_KERNEL */
  627 
  628 #ifdef _KERNEL
  629 struct socket;
  630 
  631 struct tcpcb *so_sototcpcb(struct socket *so);
  632 struct inpcb *so_sotoinpcb(struct socket *so);
  633 struct sockbuf *so_sockbuf_snd(struct socket *);
  634 struct sockbuf *so_sockbuf_rcv(struct socket *);
  635 
  636 int so_state_get(const struct socket *);
  637 void so_state_set(struct socket *, int);
  638 
  639 int so_options_get(const struct socket *);
  640 void so_options_set(struct socket *, int);
  641 
  642 int so_error_get(const struct socket *);
  643 void so_error_set(struct socket *, int);
  644 
  645 int so_linger_get(const struct socket *);
  646 void so_linger_set(struct socket *, int);
  647 
  648 struct protosw *so_protosw_get(const struct socket *);
  649 void so_protosw_set(struct socket *, struct protosw *);
  650 
  651 void so_sorwakeup_locked(struct socket *so);
  652 void so_sowwakeup_locked(struct socket *so);
  653 
  654 void so_sorwakeup(struct socket *so);
  655 void so_sowwakeup(struct socket *so);
  656 
  657 void so_lock(struct socket *so);
  658 void so_unlock(struct socket *so);
  659 
  660 void so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), void *arg);
  661 
  662 #endif
  663 
  664 
  665 #endif /* !_SYS_SOCKET_H_ */






http://man7.org/linux/man-pages/man2/socket.2.html



SOCKET(2)                 Linux Programmer's Manual                SOCKET(2)

NAME         top

       socket - create an endpoint for communication

SYNOPSIS         top

       #include           /* See NOTES */
       #include 

       int socket(int domain, int type, int protocol);

DESCRIPTION         top

       socket() creates an endpoint for communication and returns a
       descriptor.

       The domain argument specifies a communication domain; this selects
       the protocol family which will be used for communication.  These
       families are defined in .  The currently understood
       formats include:

       Name                Purpose                          Man page
       AF_UNIX, AF_LOCAL   Local communication              unix(7)
       AF_INET             IPv4 Internet protocols          ip(7)
       AF_INET6            IPv6 Internet protocols          ipv6(7)
       AF_IPX              IPX - Novell protocols
       AF_NETLINK          Kernel user interface device     netlink(7)
       AF_X25              ITU-T X.25 / ISO-8208 protocol   x25(7)
       AF_AX25             Amateur radio AX.25 protocol
       AF_ATMPVC           Access to raw ATM PVCs
       AF_APPLETALK        Appletalk                        ddp(7)
       AF_PACKET           Low level packet interface       packet(7)

       The socket has the indicated type, which specifies the communication
       semantics语义的.  Currently defined types are:

       SOCK_STREAM     Provides sequenced, reliable, two-way, connection-
                       based byte streams.  An out-of-band data transmission
                       mechanism may be supported.

       SOCK_DGRAM      Supports datagrams (connectionless, unreliable
                       messages of a fixed maximum length).

       SOCK_SEQPACKET  Provides a sequenced, reliable, two-way connection-
                       based data transmission path for datagrams of fixed
                       maximum length; a consumer is required to read an
                       entire packet with each input system call.

       SOCK_RAW        Provides raw network protocol access.

       SOCK_RDM        Provides a reliable datagram layer that does not
                       guarantee ordering.

       SOCK_PACKET     Obsolete and should not be used in new programs; see
                       packet(7).

       Some socket types may not be implemented by all protocol families;
       for example, SOCK_SEQPACKET is not implemented for AF_INET.

       Since Linux 2.6.27, the type argument serves a second purpose: in
       addition to specifying a socket type, it may include the bitwise OR
       of any of the following values, to modify the behavior of socket():

       SOCK_NONBLOCK   Set the O_NONBLOCK file status flag on the new open
                       file description.  Using this flag saves extra calls
                       to fcntl(2) to achieve the same result.

       SOCK_CLOEXEC    Set the close-on-exec (FD_CLOEXEC) flag on the new
                       file descriptor.  See the description of the
                       O_CLOEXEC flag in open(2) for reasons why this may be
                       useful.

       The protocol specifies a particular protocol to be used with the
       socket.  Normally only a single protocol exists to support a
       particular socket type within a given protocol family, in which case
       protocol can be specified as 0.  However, it is possible that many
       protocols may exist, in which case a particular protocol must be
       specified in this manner.  The protocol number to use is specific to
       the "communication domain" in which communication is to take place;
       see protocols(5).  See getprotoent(3) on how to map protocol name
       strings to protocol numbers.

       Sockets of type SOCK_STREAM are full-duplex byte streams, similar to
       pipes.  They do not preserve record boundaries.  A stream socket must
       be in a connected state before any data may be sent or received on
       it.  A connection to another socket is created with a connect(2)
       call.  Once connected, data may be transferred using read(2) and
       write(2) calls or some variant of the send(2) and recv(2) calls.
       When a session has been completed a close(2) may be performed.  Out-
       of-band data may also be transmitted as described in send(2) and
       received as described in recv(2).

       The communications protocols which implement a SOCK_STREAM ensure
       that data is not lost or duplicated.  If a piece of data for which
       the peer protocol has buffer space cannot be successfully transmitted
       within a reasonable length of time, then the connection is considered
       to be dead.  When SO_KEEPALIVE is enabled on the socket the protocol
       checks in a protocol-specific manner if the other end is still alive.
       A SIGPIPE signal is raised if a process sends or receives on a broken
       stream; this causes naive processes, which do not handle the signal,
       to exit.  SOCK_SEQPACKET sockets employ the same system calls as
       SOCK_STREAM sockets.  The only difference is that read(2) calls will
       return only the amount of data requested, and any data remaining in
       the arriving packet will be discarded.  Also all message boundaries
       in incoming datagrams are preserved.

       SOCK_DGRAM and SOCK_RAW sockets allow sending of datagrams to
       correspondents named in sendto(2) calls.  Datagrams are generally
       received with recvfrom(2), which returns the next datagram along with
       the address of its sender.

       SOCK_PACKET is an obsolete socket type to receive raw packets
       directly from the device driver.  Use packet(7) instead.

       An fcntl(2) F_SETOWN operation can be used to specify a process or
       process group to receive a SIGURG signal when the out-of-band data
       arrives or SIGPIPE signal when a SOCK_STREAM connection breaks
       unexpectedly.  This operation may also be used to set the process or
       process group that receives the I/O and asynchronous notification of
       I/O events via SIGIO.  Using F_SETOWN is equivalent to an ioctl(2)
       call with the FIOSETOWN or SIOCSPGRP argument.

       When the network signals an error condition to the protocol module
       (e.g., using a ICMP message for IP) the pending error flag is set for
       the socket.  The next operation on this socket will return the error
       code of the pending error.  For some protocols it is possible to
       enable a per-socket error queue to retrieve detailed information
       about the error; see IP_RECVERR in ip(7).

       The operation of sockets is controlled by socket level options.
       These options are defined in .  The functions
       setsockopt(2) and getsockopt(2) are used to set and get options,
       respectively.

RETURN VALUE         top

       On success, a file descriptor for the new socket is returned.  On
       error, -1 is returned, and errno is set appropriately.

ERRORS         top

       EACCES Permission to create a socket of the specified type and/or
              protocol is denied.

       EAFNOSUPPORT
              The implementation does not support the specified address
              family.

       EINVAL Unknown protocol, or protocol family not available.

       EINVAL Invalid flags in type.

       EMFILE Process file table overflow.

       ENFILE The system limit on the total number of open files has been
              reached.

       ENOBUFS or ENOMEM
              Insufficient memory is available.  The socket cannot be
              created until sufficient resources are freed.

       EPROTONOSUPPORT
              The protocol type or the specified protocol is not supported
              within this domain.

       Other errors may be generated by the underlying protocol modules.

CONFORMING TO         top

       4.4BSD, POSIX.1-2001.

       The SOCK_NONBLOCK and SOCK_CLOEXEC flags are Linux-specific.

       socket() appeared in 4.2BSD.  It is generally portable to/from non-
       BSD systems supporting clones of the BSD socket layer (including
       System V variants).

NOTES         top

       POSIX.1-2001 does not require the inclusion of , and
       this header file is not required on Linux.  However, some historical
       (BSD) implementations required this header file, and portable
       applications are probably wise to include it.

       The manifest constants used under 4.x BSD for protocol families are
       PF_UNIX, PF_INET, and so on, while AF_UNIX, AF_INET, and so on are
       used for address families.  However, already the BSD man page
       promises: "The protocol family generally is the same as the address
       family", and subsequent standards use AF_* everywhere.

EXAMPLE         top

       An example of the use of socket() is shown in getaddrinfo(3).

SEE ALSO         top

       accept(2), bind(2), connect(2), fcntl(2), getpeername(2),
       getsockname(2), getsockopt(2), ioctl(2), listen(2), read(2), recv(2),
       select(2), send(2), shutdown(2), socketpair(2), write(2),
       getprotoent(3), ip(7), socket(7), tcp(7), udp(7), unix(7)

       "An Introductory 4.3BSD Interprocess Communication Tutorial" and "BSD
       Interprocess Communication Tutorial", reprinted in UNIX Programmer's
       Supplementary Documents Volume 1.

COLOPHON         top

       This page is part of release 3.54 of the Linux man-pages project.  A
       description of the project, and information about reporting bugs, can
       be found at http://www.kernel.org/doc/man-pages/.

Linux                            2009-01-19                        SOCKET(2)




你可能感兴趣的:(UNIX网络编程)