socket_function_part_1

//
//  socket_function.h
//  socket
//
//  Created by Eric's Macbook Pro. on 2017/5/15.
//  Copyright © 2017年 Eric luo. All rights reserved.
//

#include <sys/socket.h>


/**
 * [NAME]:
 *
 * accept - accept a new connection on a socket
 *
 * [SYNOPSIS]:
 *
 * #include <sys/socket.h>
 *
 * int accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);
 *
 * [DESCRIPTION]:
 *
 * The accept() function shall extract the first connection on the queue of pending connections, create a new socket with the same socket type protocol and address family as the specified socket, and allocate a new file descriptor for that socket. The file descriptor shall be allocated as described in File Descriptor Allocation.
 *
 * The accept() function takes the following arguments:
 *
 * @param socket
 * Specifies a socket that was created with socket(), has been bound to an address with bind(), and has issued a successful call to listen().
 * @param address
 * Either a null pointer, or a pointer to a sockaddr structure where the address of the connecting socket shall be returned.
 * @param address_len
 * Either a null pointer, if address is a null pointer, or a pointer to a socklen_t object which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the stored address.
 * If address is not a null pointer, the address of the peer for the accepted connection shall be stored in the sockaddr structure pointed to by address, and the length of this address shall be stored in the object pointed to by address_len.
 *
 * If the actual length of the address is greater than the length of the supplied sockaddr structure, the stored address shall be truncated.
 *
 * If the protocol permits connections by unbound clients, and the peer is not bound, then the value stored in the object pointed to by address is unspecified.
 *
 * If the listen queue is empty of connection requests and O_NONBLOCK is not set on the file descriptor for the socket, accept() shall block until a connection is present. If the listen() queue is empty of connection requests and O_NONBLOCK is set on the file descriptor for the socket, accept() shall fail and set errno to [EAGAIN] or [EWOULDBLOCK].
 *
 * The accepted socket cannot itself accept more connections. The original socket remains open and can accept more connections.
 *
 * [RETURN VALUE]:
 *
 * Upon successful completion, accept() shall return the non-negative file descriptor of the accepted socket. Otherwise, -1 shall be returned, errno shall be set to indicate the error, and any object pointed to by address_len shall remain unchanged.
 *
 * [ERRORS]:
 *
 * The accept() function shall fail if:
 *
 * [EAGAIN] or [EWOULDBLOCK]
 * O_NONBLOCK is set for the socket file descriptor and no connections are present to be accepted.
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [ECONNABORTED]
 * A connection has been aborted.
 * [EINTR]
 * The accept() function was interrupted by a signal that was caught before a valid connection arrived.
 * [EINVAL]
 * The socket is not accepting connections.
 * [EMFILE]
 * All file descriptors available to the process are currently open.
 * [ENFILE]
 * The maximum number of file descriptors in the system are already open.
 * [ENOBUFS]
 * No buffer space is available.
 * [ENOMEM]
 * There was insufficient memory available to complete the operation.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EOPNOTSUPP]
 * The socket type of the specified socket does not support accepting connections.
 * The accept() function may fail if:
 *
 * [EPROTO]
 * A protocol error has occurred; [OB XSR] [Option Start]  for example, the STREAMS protocol stack has not been initialized.
 */
int accept(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);


/**
 * NAME
 *
 * bind - bind a name to a socket
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * int bind(int socket, const struct sockaddr *address, socklen_t address_len);
 *
 * DESCRIPTION
 *
 * The bind() function shall assign a local socket address address to a socket identified by descriptor socket that has no local socket address assigned. Sockets created with the socket() function are initially unnamed; they are identified only by their address family.
 *
 * The bind() function takes the following arguments:
 *
 * @param socket
 * Specifies the file descriptor of the socket to be bound.
 * @param address
 * Points to a sockaddr structure containing the address to be bound to the socket. The length and format of the address depend on the address family of the socket.
 * @param address_len
 * Specifies the length of the sockaddr structure pointed to by the address argument.
 * The socket specified by socket may require the process to have appropriate privileges to use the bind() function.
 *
 * If the address family of the socket is AF_UNIX and the pathname in address names a symbolic link, bind() shall fail and set errno to [EADDRINUSE].
 
 * If the socket address cannot be assigned immediately and O_NONBLOCK is set for the file descriptor for the socket, bind() shall fail and set errno to [EINPROGRESS], but the assignment request shall not be aborted, and the assignment shall be completed asynchronously. Subsequent calls to bind() for the same socket, before the assignment is completed, shall fail and set errno to [EALREADY].
 *
 * When the assignment has been performed asynchronously, pselect(), select(), and poll() shall indicate that the file descriptor for the socket is ready for reading and writing.
 *
 * RETURN VALUE
 *
 * Upon successful completion, bind() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The bind() function shall fail if:
 *
 * [EADDRINUSE]
 * The specified address is already in use.
 * [EADDRNOTAVAIL]
 * The specified address is not available from the local machine.
 * [EAFNOSUPPORT]
 * The specified address is not a valid address for the address family of the specified socket.
 * [EALREADY]
 * An assignment request is already in progress for the specified socket.
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [EINPROGRESS]
 * O_NONBLOCK is set for the file descriptor for the socket and the assignment cannot be immediately performed; the assignment shall be performed asynchronously.
 * [EINVAL]
 * The socket is already bound to an address, and the protocol does not support binding to a new address; or the socket has been shut down.
 * [ENOBUFS]
 * Insufficient resources were available to complete the call.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EOPNOTSUPP]
 * The socket type of the specified socket does not support binding to an address.
 * If the address family of the socket is AF_UNIX, then bind() shall fail if:
 *
 * [EACCES]
 * A component of the path prefix denies search permission, or the requested name requires writing in a directory with a mode that denies write permission.
 * [EDESTADDRREQ] or [EISDIR]
 * The address argument is a null pointer.
 * [EIO]
 * An I/O error occurred.
 * [ELOOP]
 * A loop exists in symbolic links encountered during resolution of the pathname in address.
 * [ENAMETOOLONG]
 * The length of a component of a pathname is longer than {NAME_MAX}.
 * [ENOENT]
 * A component of the path prefix of the pathname in address does not name an existing file or the pathname is an empty string.
 * [ENOENT] or [ENOTDIR]
 * The pathname in address contains at least one non- <slash> character and ends with one or more trailing <slash> characters. If the pathname without the trailing <slash> characters would name an existing file, an [ENOENT] error shall not occur.
 * [ENOTDIR]
 * A component of the path prefix of the pathname in address names an existing file that is neither a directory nor a symbolic link to a directory, or the pathname in address contains at least one non- <slash> character and ends with one or more trailing <slash> characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.
 * [EROFS]
 * The name would reside on a read-only file system.
 * The bind() function may fail if:
 * 
 * [EACCES]
 * The specified address is protected and the current user does not have permission to bind to it.
 * [EINVAL]
 * The address_len argument is not a valid length for the address family.
 * [EISCONN]
 * The socket is already connected.
 * [ELOOP]
 * More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the pathname in address.
 * [ENAMETOOLONG]
 * The length of a pathname exceeds {PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds {PATH_MAX}.
 */
int bind(int socket, const struct sockaddr *address, socklen_t address_len);

/**
 * NAME
 *
 * connect - connect a socket
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * int connect(int socket, const struct sockaddr *address, socklen_t address_len);
 *
 * DESCRIPTION
 *
 * The connect() function shall attempt to make a connection on a connection-mode socket or to set or reset the peer address of a connectionless-mode socket. The function takes the following arguments:
 *
 * socket
 * Specifies the file descriptor associated with the socket.
 * address
 * Points to a sockaddr structure containing the peer address. The length and format of the address depend on the address family of the socket.
 * address_len
 * Specifies the length of the sockaddr structure pointed to by the address argument.
 * If the socket has not already been bound to a local address, connect() shall bind it to an address which, unless the socket's address family is AF_UNIX, is an unused local address.
 *
 * If the initiating socket is not connection-mode, then connect() shall set the socket's peer address, and no connection is made. For SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent send() functions, and limits the remote sender for subsequent recv() functions. If the sa_family member of address is AF_UNSPEC, the socket's peer address shall be reset. Note that despite no connection being made, the term ``connected'' is used to describe a connectionless-mode socket for which a peer address has been set.
 *
 * If the initiating socket is connection-mode, then connect() shall attempt to establish a connection to the address specified by the address argument. If the connection cannot be established immediately and O_NONBLOCK is not set for the file descriptor for the socket, connect() shall block for up to an unspecified timeout interval until the connection is established. If the timeout interval expires before the connection is established, connect() shall fail and the connection attempt shall be aborted. If connect() is interrupted by a signal that is caught while blocked waiting to establish a connection, connect() shall fail and set errno to [EINTR], but the connection request shall not be aborted, and the connection shall be established asynchronously.
 *
 * If the connection cannot be established immediately and O_NONBLOCK is set for the file descriptor for the socket, connect() shall fail and set errno to [EINPROGRESS], but the connection request shall not be aborted, and the connection shall be established asynchronously. Subsequent calls to connect() for the same socket, before the connection is established, shall fail and set errno to [EALREADY].
 *
 * When the connection has been established asynchronously, pselect(), select(), and poll() shall indicate that the file descriptor for the socket is ready for writing.
 *
 * The socket in use may require the process to have appropriate privileges to use the connect() function.
 *
 * RETURN VALUE
 *
 * Upon successful completion, connect() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The connect() function shall fail if:
 *
 * [EADDRNOTAVAIL]
 * The specified address is not available from the local machine.
 * [EAFNOSUPPORT]
 * The specified address is not a valid address for the address family of the specified socket.
 * [EALREADY]
 * A connection request is already in progress for the specified socket.
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [ECONNREFUSED]
 * The target address was not listening for connections or refused the connection request.
 * [EINPROGRESS]
 * O_NONBLOCK is set for the file descriptor for the socket and the connection cannot be immediately established; the connection shall be established asynchronously.
 * [EINTR]
 * The attempt to establish a connection was interrupted by delivery of a signal that was caught; the connection shall be established asynchronously.
 * [EISCONN]
 * The specified socket is connection-mode and is already connected.
 * [ENETUNREACH]
 * No route to the network is present.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EPROTOTYPE]
 * The specified address has a different type than the socket bound to the specified peer address.
 * [ETIMEDOUT]
 * The attempt to connect timed out before a connection was made.
 * If the address family of the socket is AF_UNIX, then connect() shall fail if:
 
 * [EIO]
 * An I/O error occurred while reading from or writing to the file system.
 * [ELOOP]
 * A loop exists in symbolic links encountered during resolution of the pathname in address.
 * [ENAMETOOLONG]
 * The length of a component of a pathname is longer than {NAME_MAX}.
 * [ENOENT]
 * A component of the pathname does not name an existing file or the pathname is an empty string.
 * [ENOTDIR]
 * A component of the path prefix of the pathname in address names an existing file that is neither a directory nor a symbolic link to a directory, or the pathname in address contains at least one non- <slash> character and ends with one or more trailing <slash> characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.
 * The connect() function may fail if:
 *
 * [EACCES]
 * Search permission is denied for a component of the path prefix; or write access to the named socket is denied.
 * [EADDRINUSE]
 * Attempt to establish a connection that uses addresses that are already in use.
 * [ECONNRESET]
 * Remote host reset the connection request.
 * [EHOSTUNREACH]
 * The destination host cannot be reached (probably because the host is down or a remote router cannot reach it).
 * [EINVAL]
 * The address_len argument is not a valid length for the address family; or invalid address family in the sockaddr structure.
 * [ELOOP]
 * More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the pathname in address.
 * [ENAMETOOLONG]
 * The length of a pathname exceeds {PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds {PATH_MAX}.
 * [ENETDOWN]
 * The local network interface used to reach the destination is down.
 * [ENOBUFS]
 * No buffer space is available.
 * [EOPNOTSUPP]
 * The socket is listening and cannot be connected.
 */
int connect(int socket, const struct sockaddr *address, socklen_t address_len);

/**
 * NAME
 *
 * getpeername - get the name of the peer socket
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * int getpeername(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);
 *
 * DESCRIPTION
 *
 * The getpeername() function shall retrieve the peer address of the specified socket, store this address in the sockaddr structure pointed to by the address argument, and store the length of this address in the object pointed to by the address_len argument.
 *
 * The address_len argument points to a socklen_t object which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the stored address. If the actual length of the address is greater than the length of the supplied sockaddr structure, the stored address shall be truncated.
 *
 * If the protocol permits connections by unbound clients, and the peer is not bound, then the value stored in the object pointed to by address is unspecified.
 *
 * RETURN VALUE
 *
 * Upon successful completion, 0 shall be returned. Otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The getpeername() function shall fail if:
 *
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [EINVAL]
 * The socket has been shut down.
 * [ENOTCONN]
 * The socket is not connected or otherwise has not had the peer pre-specified.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EOPNOTSUPP]
 * The operation is not supported for the socket protocol.
 * The getpeername() function may fail if:
 *
 * [ENOBUFS]
 * Insufficient resources were available in the system to complete the call.
 */
int getpeername(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);

/**
 * NAME
 *
 * getsockname - get the socket name
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * int getsockname(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);
 *
 * DESCRIPTION
 *
 * The getsockname() function shall retrieve the locally-bound name of the specified socket, store this address in the sockaddr structure pointed to by the address argument, and store the length of this address in the object pointed to by the address_len argument.
 *
 * The address_len argument points to a socklen_t object which on input specifies the length of the supplied sockaddr structure, and on output specifies the length of the stored address. If the actual length of the address is greater than the length of the supplied sockaddr structure, the stored address shall be truncated.
 *
 * If the socket has not been bound to a local name, the value stored in the object pointed to by address is unspecified.
 *
 * RETURN VALUE
 *
 * Upon successful completion, 0 shall be returned, the address argument shall point to the address of the socket, and the address_len argument shall point to the length of the address. Otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The getsockname() function shall fail if:
 *
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * [EOPNOTSUPP]
 * The operation is not supported for this socket's protocol.
 * The getsockname() function may fail if:
 *
 * [EINVAL]
 * The socket has been shut down.
 * [ENOBUFS]
 * Insufficient resources were available in the system to complete the function.
 */
int getsockname(int socket, struct sockaddr *restrict address, socklen_t *restrict address_len);

/**
 * NAME
 *
 * getsockopt - get the socket options
 * SYNOPSIS
 *
 * #include <sys/socket.h>
 *
 * int getsockopt(int socket, int level, int option_name,
 * void *restrict option_value, socklen_t *restrict option_len);
 *
 * DESCRIPTION
 *
 * The getsockopt() function manipulates options associated with a socket.
 *
 * The getsockopt() function shall retrieve the value for the option specified by the option_name argument for the socket specified by the socket argument. If the size of the option value is greater than option_len, the value stored in the object pointed to by the option_value argument shall be silently truncated. Otherwise, the object pointed to by the option_len argument shall be modified to indicate the actual length of the value.
 *
 * The level argument specifies the protocol level at which the option resides. To retrieve options at the socket level, specify the level argument as SOL_SOCKET. To retrieve options at other levels, supply the appropriate level identifier for the protocol controlling the option. For example, to indicate that an option is interpreted by the TCP (Transmission Control Protocol), set level to IPPROTO_TCP as defined in the <netinet/in.h> header.
 *
 * The socket in use may require the process to have appropriate privileges to use the getsockopt() function.
 *
 * The option_name argument specifies a single option to be retrieved. It can be one of the socket-level options defined in <sys/socket.h> and described in Use of Options.
 *
 * RETURN VALUE
 *
 * Upon successful completion, getsockopt() shall return 0; otherwise, -1 shall be returned and errno set to indicate the error.
 *
 * ERRORS
 *
 * The getsockopt() function shall fail if:
 *
 * [EBADF]
 * The socket argument is not a valid file descriptor.
 * [EINVAL]
 * The specified option is invalid at the specified socket level.
 * [ENOPROTOOPT]
 * The option is not supported by the protocol.
 * [ENOTSOCK]
 * The socket argument does not refer to a socket.
 * The getsockopt() function may fail if:
 *
 * [EACCES]
 * The calling process does not have appropriate privileges.
 * [EINVAL]
 * The socket has been shut down.
 * [ENOBUFS]
 * Insufficient resources are available in the system to complete the function.
 */
int getsockopt(int socket, int level, int option_name, void *restrict option_value, socklen_t *restrict option_len);

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容