* include/sys/socket.h
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __INCLUDE_SYS_SOCKET_H
#define __INCLUDE_SYS_SOCKET_H
* Included Files
****************************************************************************/
#include <sys/types.h>
#include <sys/uio.h>
#include <stdint.h>
* Pre-processor Definitions
****************************************************************************/
* selects the protocol family which will be used for communication.
*/
#define PF_UNSPEC 0
#define PF_UNIX 1
#define PF_LOCAL 1
#define PF_INET 2
#define PF_INET6 10
#define PF_NETLINK 16
#define PF_ROUTE PF_NETLINK
#define PF_PACKET 17
#define PF_CAN 29
#define PF_BLUETOOTH 31
#define PF_IEEE802154 36
#define PF_VSOCK 40
#define PF_PKTRADIO 64
#define PF_RPMSG 65
* AF_UNIX, AF_INET and AF_INET6.
*/
#define AF_UNSPEC PF_UNSPEC
#define AF_UNIX PF_UNIX
#define AF_LOCAL PF_LOCAL
#define AF_INET PF_INET
#define AF_INET6 PF_INET6
#define AF_NETLINK PF_NETLINK
#define AF_ROUTE PF_ROUTE
#define AF_PACKET PF_PACKET
#define AF_CAN PF_CAN
#define AF_BLUETOOTH PF_BLUETOOTH
#define AF_IEEE802154 PF_IEEE802154
#define AF_VSOCK PF_VSOCK
#define AF_PKTRADIO PF_PKTRADIO
#define AF_RPMSG PF_RPMSG
* the communication semantics.
*/
#define SOCK_UNSPEC 0
#define SOCK_STREAM 1
* connection-based byte streams. An out-of-band data
* transmission mechanism may be supported.
*/
#define SOCK_DGRAM 2
* messages of a fixed maximum length).
*/
#define SOCK_RAW 3
#define SOCK_RDM 4
* guarantee ordering.
*/
#define SOCK_SEQPACKET 5
* connection-based data transmission path for
* datagrams of fixed maximum length; a consumer is
* required to read an entire packet with each read
* system call.
*/
#define SOCK_CTRL 6
* when we just want a socket for performing driver
* ioctls. This definition is not POSIX compliant.
*/
#define SOCK_SMS 7
* This definition is not POSIX compliant.
*/
#define SOCK_PACKET 10
#define SOCK_CLOEXEC 02000000
* descriptor(s).
*/
#define SOCK_NONBLOCK 00004000
#define SOCK_MAX (SOCK_PACKET + 1)
#define SOCK_TYPE_MASK 0xf
* The remaining bits are used as flags.
*/
* recognized by Linux, not all are supported by NuttX.
*/
#define MSG_OOB 0x000001
#define MSG_PEEK 0x000002
#define MSG_DONTROUTE 0x000004
#define MSG_CTRUNC 0x000008
#define MSG_PROXY 0x000010
#define MSG_TRUNC 0x000020
#define MSG_DONTWAIT 0x000040
#define MSG_EOR 0x000080
#define MSG_WAITALL 0x000100
#define MSG_FIN 0x000200
#define MSG_SYN 0x000400
#define MSG_CONFIRM 0x000800
#define MSG_RST 0x001000
#define MSG_ERRQUEUE 0x002000
#define MSG_NOSIGNAL 0x004000
#define MSG_MORE 0x008000
#define MSG_CMSG_CLOEXEC 0x100000
* descriptor received through SCM_RIGHTS.
*/
#define SOL_SOCKET 1
#define SO_ACCEPTCONN 0
* (get only).
* arg: pointer to integer containing a boolean
* value
*/
#define SO_BROADCAST 1
* arg: pointer to integer containing a boolean
* value
*/
#define SO_DEBUG 2
* (get/set).
* arg: pointer to integer containing a boolean
* value
*/
#define SO_DONTROUTE 3
* routing (get/set)
* arg: pointer to integer containing a boolean
* value
*/
#define SO_ERROR 4
* arg: returns an integer value
*/
#define SO_KEEPALIVE 5
* transmission of messages (get/set).
* arg: pointer to integer containing a boolean int
* value
*/
#define SO_LINGER 6
* arg: struct linger
*/
#define SO_OOBINLINE 7
* urgent) inline
* (get/set) arg: pointer to integer containing a
* boolean value
*/
#define SO_RCVBUF 8
* arg: integer value (get/set).
*/
#define SO_RCVLOWAT 9
* socket input (get/set).
* arg: integer value
*/
#define SO_RCVTIMEO 10
* amount of time an input function waits until it
* completes (get/set).
* arg: struct timeval
*/
#define SO_REUSEADDR 11
* arg: pointer to integer containing a boolean
* value
*/
#define SO_SNDBUF 12
* arg: integer value
*/
#define SO_SNDLOWAT 13
* socket output (get/set).
* arg: integer value
*/
#define SO_SNDTIMEO 14
* time that an output function blocks because flow
* control prevents data from being sent(get/set).
* arg: struct timeval
*/
#define SO_TYPE 15
* return: int
*/
#define SO_TIMESTAMP 16
* arg: integer value
*/
#define SO_BINDTODEVICE 17
*/
#define SO_PEERCRED 18
* connected to this socket.
*/
* and portability
*/
#define SO_SNDBUFFORCE 32
#define SO_RCVBUFFORCE 33
#define SO_RXQ_OVFL 40
#define SOL_IP IPPROTO_IP
#define SOL_IPV6 IPPROTO_IPV6
#define SOL_TCP IPPROTO_TCP
#define SOL_UDP IPPROTO_UDP
#define SOL_HCI 0
#define SOL_L2CAP 6
#define SOL_SCO 17
#define SOL_RFCOMM 18
#define SOL_PACKET 19
#define __SO_PROTOCOL 16
#define SHUT_RD 1
#define SHUT_WR 2
#define SHUT_RDWR 3
* operations
*/
#ifdef CONFIG_NET_TCPBACKLOG_CONNS
# define SOMAXCONN CONFIG_NET_TCPBACKLOG_CONNS
#else
# define SOMAXCONN 8
#endif
#define CMSG_NXTHDR(mhdr, cmsg) cmsg_nxthdr((mhdr), (cmsg))
#define CMSG_ALIGN(len) \
(((len) + sizeof(long) - 1) & ~(sizeof(long) - 1))
#define CMSG_DATA(cmsg) \
((FAR void *)((FAR char *)(cmsg) + CMSG_ALIGN(sizeof(struct cmsghdr))))
#define CMSG_SPACE(len) \
(CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
#define CMSG_LEN(len) \
(CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
#define __CMSG_FIRSTHDR(ctl, len) \
((len) >= sizeof(struct cmsghdr) ? (FAR struct cmsghdr *)(ctl) : NULL)
#define CMSG_FIRSTHDR(msg) \
__CMSG_FIRSTHDR((msg)->msg_control, (msg)->msg_controllen)
#define CMSG_OK(mhdr, cmsg) ((cmsg)->cmsg_len >= sizeof(struct cmsghdr) && \
(cmsg)->cmsg_len <= (unsigned long) \
((mhdr)->msg_controllen - \
((FAR char *)(cmsg) - (FAR char *)(mhdr)->msg_control)))
#define for_each_cmsghdr(cmsg, msg) \
for (cmsg = CMSG_FIRSTHDR(msg); \
cmsg; \
cmsg = CMSG_NXTHDR(msg, cmsg))
#define SCM_RIGHTS 0x01
#define SCM_CREDENTIALS 0x02
#define SCM_SECURITY 0x03
#define SCM_TIMESTAMP SO_TIMESTAMP
#define SS_MAXSIZE 128
#define SS_ALIGNSIZE (sizeof(int64_t))
#define SS_PAD1SIZE (SS_ALIGNSIZE - sizeof(sa_family_t))
#define SS_PAD2SIZE (SS_MAXSIZE - (sizeof(sa_family_t) + \
SS_PAD1SIZE + SS_ALIGNSIZE))
#define DENY_INET_SOCK_ENABLE 0x01
#define DENY_INET_SOCK_DISABLE 0x02
* Type Definitions
****************************************************************************/
* accommodate all supported protocol-specific address structures, and (2)
* aligned at an appropriate boundary so that pointers to it can be cast
* as pointers to protocol-specific address structures and used to access
* the fields of those structures without alignment problems.
*/
struct sockaddr_storage
{
sa_family_t ss_family;
begin_packed_struct struct
{
char ss_pad1[SS_PAD1SIZE];
* pad up to alignment field that follows explicit in
* the data structure */
int64_t ss_align;
char ss_pad2[SS_PAD2SIZE];
* value minus size of ss_family ss_pad1, ss_align
* fields is 112. */
}
end_packed_struct ss_data[1];
};
* in the bind(), connect(), getpeername(), getsockname(), recvfrom(), and
* sendto() functions.
*/
struct sockaddr
{
sa_family_t sa_family;
char sa_data[14];
};
struct linger
{
int l_onoff;
int l_linger;
};
struct msghdr
{
FAR void *msg_name;
socklen_t msg_namelen;
FAR struct iovec *msg_iov;
unsigned long msg_iovlen;
FAR void *msg_control;
unsigned long msg_controllen;
unsigned int msg_flags;
};
struct cmsghdr
{
unsigned long cmsg_len;
int cmsg_level;
int cmsg_type;
};
struct ucred
{
pid_t pid;
uid_t uid;
gid_t gid;
};
* Inline Functions
****************************************************************************/
static inline FAR struct cmsghdr *__cmsg_nxthdr(FAR void *__ctl,
unsigned long __size,
FAR struct cmsghdr *__cmsg)
{
size_t len = CMSG_ALIGN(__cmsg->cmsg_len);
FAR struct cmsghdr *__ptr =
(FAR struct cmsghdr *)(((FAR char *)__cmsg) + len);
if (len < sizeof(*__cmsg) ||
(unsigned long)((FAR char *)(__ptr + 1) - (FAR char *)__ctl) > __size)
{
return NULL;
}
return __ptr;
}
static inline FAR struct cmsghdr *cmsg_nxthdr(FAR struct msghdr *__msg,
FAR struct cmsghdr *__cmsg)
{
return __cmsg_nxthdr(__msg->msg_control, __msg->msg_controllen, __cmsg);
}
* Public Function Prototypes
****************************************************************************/
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
int socket(int domain, int type, int protocol);
int socketpair(int domain, int type, int protocol, int sv[2]);
int bind(int sockfd, FAR const struct sockaddr *addr, socklen_t addrlen);
int connect(int sockfd, FAR const struct sockaddr *addr, socklen_t addrlen);
int listen(int sockfd, int backlog);
int accept(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen);
int accept4(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen,
int flags);
ssize_t send(int sockfd, FAR const void *buf, size_t len, int flags);
ssize_t sendto(int sockfd, FAR const void *buf, size_t len, int flags,
FAR const struct sockaddr *to, socklen_t tolen);
ssize_t recv(int sockfd, FAR void *buf, size_t len, int flags);
ssize_t recvfrom(int sockfd, FAR void *buf, size_t len, int flags,
FAR struct sockaddr *from, FAR socklen_t *fromlen);
int shutdown(int sockfd, int how);
int setsockopt(int sockfd, int level, int option,
FAR const void *value, socklen_t value_len);
int getsockopt(int sockfd, int level, int option,
FAR void *value, FAR socklen_t *value_len);
int getsockname(int sockfd, FAR struct sockaddr *addr,
FAR socklen_t *addrlen);
int getpeername(int sockfd, FAR struct sockaddr *addr,
FAR socklen_t *addrlen);
ssize_t recvmsg(int sockfd, FAR struct msghdr *msg, int flags);
ssize_t sendmsg(int sockfd, FAR struct msghdr *msg, int flags);
#if CONFIG_FORTIFY_SOURCE > 0
fortify_function(send) ssize_t send(int sockfd, FAR const void *buf,
size_t len, int flags)
{
fortify_assert(len <= fortify_size(buf, 0));
return __real_send(sockfd, buf, len, flags);
}
fortify_function(sendto) ssize_t sendto(int sockfd, FAR const void *buf,
size_t len, int flags,
FAR const struct sockaddr *to,
socklen_t tolen)
{
fortify_assert(len <= fortify_size(buf, 0));
return __real_sendto(sockfd, buf, len, flags, to, tolen);
}
fortify_function(recv) ssize_t recv(int sockfd, FAR void *buf,
size_t len, int flags)
{
fortify_assert(len <= fortify_size(buf, 0));
return __real_recv(sockfd, buf, len, flags);
}
fortify_function(recvfrom) ssize_t recvfrom(int sockfd, FAR void *buf,
size_t len, int flags,
FAR struct sockaddr *from,
FAR socklen_t *fromlen)
{
fortify_assert(len <= fortify_size(buf, 0));
return __real_recvfrom(sockfd, buf, len, flags, from, fromlen);
}
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif