Back to index

glibc  2.9
socket.h
Go to the documentation of this file.
00001 /* Declarations of socket constants, types, and functions.
00002    Copyright (C) 1991,92,1994-2001,2003,2005,2007,2008
00003    Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifndef       _SYS_SOCKET_H
00022 #define       _SYS_SOCKET_H 1
00023 
00024 #include <features.h>
00025 
00026 __BEGIN_DECLS
00027 
00028 #include <sys/uio.h>
00029 #define       __need_size_t
00030 #include <stddef.h>
00031 #ifdef __USE_GNU
00032 /* Get the __sigset_t definition.  */
00033 # include <bits/sigset.h>
00034 #endif
00035 
00036 
00037 /* This operating system-specific header file defines the SOCK_*, PF_*,
00038    AF_*, MSG_*, SOL_*, and SO_* constants, and the `struct sockaddr',
00039    `struct msghdr', and `struct linger' types.  */
00040 #include <bits/socket.h>
00041 
00042 #ifdef __USE_BSD
00043 /* This is the 4.3 BSD `struct sockaddr' format, which is used as wire
00044    format in the grotty old 4.3 `talk' protocol.  */
00045 struct osockaddr
00046   {
00047     unsigned short int sa_family;
00048     unsigned char sa_data[14];
00049   };
00050 #endif
00051 
00052 /* The following constants should be used for the second parameter of
00053    `shutdown'.  */
00054 enum
00055 {
00056   SHUT_RD = 0,              /* No more receptions.  */
00057 #define SHUT_RD             SHUT_RD
00058   SHUT_WR,           /* No more transmissions.  */
00059 #define SHUT_WR             SHUT_WR
00060   SHUT_RDWR          /* No more receptions or transmissions.  */
00061 #define SHUT_RDWR    SHUT_RDWR
00062 };
00063 
00064 /* This is the type we use for generic socket address arguments.
00065 
00066    With GCC 2.7 and later, the funky union causes redeclarations or
00067    uses with any of the listed types to be allowed without complaint.
00068    G++ 2.7 does not support transparent unions so there we want the
00069    old-style declaration, too.  */
00070 #if defined __cplusplus || !__GNUC_PREREQ (2, 7) || !defined __USE_GNU
00071 # define __SOCKADDR_ARG            struct sockaddr *__restrict
00072 # define __CONST_SOCKADDR_ARG      __const struct sockaddr *
00073 #else
00074 /* Add more `struct sockaddr_AF' types here as necessary.
00075    These are all the ones I found on NetBSD and Linux.  */
00076 # define __SOCKADDR_ALLTYPES \
00077   __SOCKADDR_ONETYPE (sockaddr) \
00078   __SOCKADDR_ONETYPE (sockaddr_at) \
00079   __SOCKADDR_ONETYPE (sockaddr_ax25) \
00080   __SOCKADDR_ONETYPE (sockaddr_dl) \
00081   __SOCKADDR_ONETYPE (sockaddr_eon) \
00082   __SOCKADDR_ONETYPE (sockaddr_in) \
00083   __SOCKADDR_ONETYPE (sockaddr_in6) \
00084   __SOCKADDR_ONETYPE (sockaddr_inarp) \
00085   __SOCKADDR_ONETYPE (sockaddr_ipx) \
00086   __SOCKADDR_ONETYPE (sockaddr_iso) \
00087   __SOCKADDR_ONETYPE (sockaddr_ns) \
00088   __SOCKADDR_ONETYPE (sockaddr_un) \
00089   __SOCKADDR_ONETYPE (sockaddr_x25)
00090 
00091 # define __SOCKADDR_ONETYPE(type) struct type *__restrict __##type##__;
00092 typedef union { __SOCKADDR_ALLTYPES
00093              } __SOCKADDR_ARG __attribute__ ((__transparent_union__));
00094 # undef __SOCKADDR_ONETYPE
00095 # define __SOCKADDR_ONETYPE(type) __const struct type *__restrict __##type##__;
00096 typedef union { __SOCKADDR_ALLTYPES
00097              } __CONST_SOCKADDR_ARG __attribute__ ((__transparent_union__));
00098 # undef __SOCKADDR_ONETYPE
00099 #endif
00100 
00101 
00102 /* Create a new socket of type TYPE in domain DOMAIN, using
00103    protocol PROTOCOL.  If PROTOCOL is zero, one is chosen automatically.
00104    Returns a file descriptor for the new socket, or -1 for errors.  */
00105 extern int socket (int __domain, int __type, int __protocol) __THROW;
00106 
00107 /* Create two new sockets, of type TYPE in domain DOMAIN and using
00108    protocol PROTOCOL, which are connected to each other, and put file
00109    descriptors for them in FDS[0] and FDS[1].  If PROTOCOL is zero,
00110    one will be chosen automatically.  Returns 0 on success, -1 for errors.  */
00111 extern int socketpair (int __domain, int __type, int __protocol,
00112                      int __fds[2]) __THROW;
00113 
00114 /* Give the socket FD the local address ADDR (which is LEN bytes long).  */
00115 extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
00116      __THROW;
00117 
00118 /* Put the local address of FD into *ADDR and its length in *LEN.  */
00119 extern int getsockname (int __fd, __SOCKADDR_ARG __addr,
00120                      socklen_t *__restrict __len) __THROW;
00121 
00122 /* Open a connection on socket FD to peer at ADDR (which LEN bytes long).
00123    For connectionless socket types, just set the default address to send to
00124    and the only address from which to accept transmissions.
00125    Return 0 on success, -1 for errors.
00126 
00127    This function is a cancellation point and therefore not marked with
00128    __THROW.  */
00129 extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
00130 
00131 /* Put the address of the peer connected to socket FD into *ADDR
00132    (which is *LEN bytes long), and its actual length into *LEN.  */
00133 extern int getpeername (int __fd, __SOCKADDR_ARG __addr,
00134                      socklen_t *__restrict __len) __THROW;
00135 
00136 
00137 /* Send N bytes of BUF to socket FD.  Returns the number sent or -1.
00138 
00139    This function is a cancellation point and therefore not marked with
00140    __THROW.  */
00141 extern ssize_t send (int __fd, __const void *__buf, size_t __n, int __flags);
00142 
00143 /* Read N bytes into BUF from socket FD.
00144    Returns the number read or -1 for errors.
00145 
00146    This function is a cancellation point and therefore not marked with
00147    __THROW.  */
00148 extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
00149 
00150 /* Send N bytes of BUF on socket FD to peer at address ADDR (which is
00151    ADDR_LEN bytes long).  Returns the number sent, or -1 for errors.
00152 
00153    This function is a cancellation point and therefore not marked with
00154    __THROW.  */
00155 extern ssize_t sendto (int __fd, __const void *__buf, size_t __n,
00156                      int __flags, __CONST_SOCKADDR_ARG __addr,
00157                      socklen_t __addr_len);
00158 
00159 /* Read N bytes into BUF through socket FD.
00160    If ADDR is not NULL, fill in *ADDR_LEN bytes of it with tha address of
00161    the sender, and store the actual size of the address in *ADDR_LEN.
00162    Returns the number of bytes read or -1 for errors.
00163 
00164    This function is a cancellation point and therefore not marked with
00165    __THROW.  */
00166 extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,
00167                       int __flags, __SOCKADDR_ARG __addr,
00168                       socklen_t *__restrict __addr_len);
00169 
00170 
00171 /* Send a message described MESSAGE on socket FD.
00172    Returns the number of bytes sent, or -1 for errors.
00173 
00174    This function is a cancellation point and therefore not marked with
00175    __THROW.  */
00176 extern ssize_t sendmsg (int __fd, __const struct msghdr *__message,
00177                      int __flags);
00178 
00179 /* Receive a message as described by MESSAGE from socket FD.
00180    Returns the number of bytes read or -1 for errors.
00181 
00182    This function is a cancellation point and therefore not marked with
00183    __THROW.  */
00184 extern ssize_t recvmsg (int __fd, struct msghdr *__message, int __flags);
00185 
00186 
00187 /* Put the current value for socket FD's option OPTNAME at protocol level LEVEL
00188    into OPTVAL (which is *OPTLEN bytes long), and set *OPTLEN to the value's
00189    actual length.  Returns 0 on success, -1 for errors.  */
00190 extern int getsockopt (int __fd, int __level, int __optname,
00191                      void *__restrict __optval,
00192                      socklen_t *__restrict __optlen) __THROW;
00193 
00194 /* Set socket FD's option OPTNAME at protocol level LEVEL
00195    to *OPTVAL (which is OPTLEN bytes long).
00196    Returns 0 on success, -1 for errors.  */
00197 extern int setsockopt (int __fd, int __level, int __optname,
00198                      __const void *__optval, socklen_t __optlen) __THROW;
00199 
00200 
00201 /* Prepare to accept connections on socket FD.
00202    N connection requests will be queued before further requests are refused.
00203    Returns 0 on success, -1 for errors.  */
00204 extern int listen (int __fd, int __n) __THROW;
00205 
00206 /* Await a connection on socket FD.
00207    When a connection arrives, open a new socket to communicate with it,
00208    set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
00209    peer and *ADDR_LEN to the address's actual length, and return the
00210    new socket's descriptor, or -1 for errors.
00211 
00212    This function is a cancellation point and therefore not marked with
00213    __THROW.  */
00214 extern int accept (int __fd, __SOCKADDR_ARG __addr,
00215                  socklen_t *__restrict __addr_len);
00216 
00217 /* Shut down all or part of the connection open on socket FD.
00218    HOW determines what to shut down:
00219      SHUT_RD   = No more receptions;
00220      SHUT_WR   = No more transmissions;
00221      SHUT_RDWR = No more receptions or transmissions.
00222    Returns 0 on success, -1 for errors.  */
00223 extern int shutdown (int __fd, int __how) __THROW;
00224 
00225 
00226 #ifdef __USE_XOPEN2K
00227 /* Determine wheter socket is at a out-of-band mark.  */
00228 extern int sockatmark (int __fd) __THROW;
00229 #endif
00230 
00231 
00232 #ifdef __USE_MISC
00233 /* FDTYPE is S_IFSOCK or another S_IF* macro defined in <sys/stat.h>;
00234    returns 1 if FD is open on an object of the indicated type, 0 if not,
00235    or -1 for errors (setting errno).  */
00236 extern int isfdtype (int __fd, int __fdtype) __THROW;
00237 #endif
00238 
00239 
00240 /* Define some macros helping to catch buffer overflows.  */
00241 #if __USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline
00242 # include <bits/socket2.h>
00243 #endif
00244 
00245 __END_DECLS
00246 
00247 #endif /* sys/socket.h */