2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-02-14 15:26:06 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-10-11 10:50:42 +01:00
|
|
|
#include <bits/stdint.h>
|
2019-02-14 15:26:06 +01:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
#include <sys/types.h>
|
2019-05-21 21:36:08 +02:00
|
|
|
#include <sys/un.h>
|
2019-02-14 15:26:06 +01:00
|
|
|
|
|
|
|
|
__BEGIN_DECLS
|
|
|
|
|
|
|
|
|
|
#define AF_MASK 0xff
|
|
|
|
|
#define AF_UNSPEC 0
|
|
|
|
|
#define AF_LOCAL 1
|
2019-05-16 10:02:38 +02:00
|
|
|
#define AF_UNIX AF_LOCAL
|
2019-03-12 15:51:42 +01:00
|
|
|
#define AF_INET 2
|
2020-05-10 23:35:20 +03:00
|
|
|
#define AF_MAX 3
|
2019-03-12 15:51:42 +01:00
|
|
|
#define PF_LOCAL AF_LOCAL
|
2019-05-16 10:02:38 +02:00
|
|
|
#define PF_UNIX PF_LOCAL
|
2019-03-12 15:51:42 +01:00
|
|
|
#define PF_INET AF_INET
|
2020-05-01 22:32:58 -04:00
|
|
|
#define PF_UNSPEC AF_UNSPEC
|
2020-05-10 23:35:20 +03:00
|
|
|
#define PF_MAX AF_MAX
|
2019-02-14 15:26:06 +01:00
|
|
|
|
|
|
|
|
#define SOCK_TYPE_MASK 0xff
|
|
|
|
|
#define SOCK_STREAM 1
|
2019-03-13 14:22:27 +01:00
|
|
|
#define SOCK_DGRAM 2
|
2019-03-12 15:51:42 +01:00
|
|
|
#define SOCK_RAW 3
|
2019-02-14 15:26:06 +01:00
|
|
|
#define SOCK_NONBLOCK 04000
|
|
|
|
|
#define SOCK_CLOEXEC 02000000
|
|
|
|
|
|
2020-02-08 00:52:33 +01:00
|
|
|
#define SHUT_RD 1
|
|
|
|
|
#define SHUT_WR 2
|
|
|
|
|
#define SHUT_RDWR 3
|
|
|
|
|
|
2019-09-19 21:40:06 +02:00
|
|
|
#define IPPROTO_IP 0
|
2019-03-12 15:51:42 +01:00
|
|
|
#define IPPROTO_ICMP 1
|
|
|
|
|
#define IPPROTO_TCP 6
|
|
|
|
|
#define IPPROTO_UDP 17
|
|
|
|
|
|
2020-09-16 11:45:00 -04:00
|
|
|
#define MSG_TRUNC 0x1
|
2020-09-16 12:32:45 -04:00
|
|
|
#define MSG_CTRUNC 0x2
|
2019-05-20 03:44:45 +02:00
|
|
|
#define MSG_DONTWAIT 0x40
|
|
|
|
|
|
2020-08-11 21:07:42 +02:00
|
|
|
typedef uint16_t sa_family_t;
|
|
|
|
|
|
2020-09-16 12:32:45 -04:00
|
|
|
struct cmsghdr {
|
|
|
|
|
socklen_t cmsg_len;
|
|
|
|
|
int cmsg_level;
|
|
|
|
|
int cmsg_type;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-16 11:45:00 -04:00
|
|
|
struct msghdr {
|
|
|
|
|
void* msg_name;
|
|
|
|
|
socklen_t msg_namelen;
|
|
|
|
|
struct iovec* msg_iov;
|
|
|
|
|
int msg_iovlen;
|
|
|
|
|
void* msg_control;
|
|
|
|
|
socklen_t msg_controllen;
|
|
|
|
|
int msg_flags;
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-14 15:26:06 +01:00
|
|
|
struct sockaddr {
|
2020-08-11 21:07:42 +02:00
|
|
|
sa_family_t sa_family;
|
2019-02-14 15:26:06 +01:00
|
|
|
char sa_data[14];
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-06 18:38:36 +01:00
|
|
|
struct ucred {
|
|
|
|
|
pid_t pid;
|
|
|
|
|
uid_t uid;
|
|
|
|
|
gid_t gid;
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-13 13:13:23 +01:00
|
|
|
#define SOL_SOCKET 1
|
2019-05-16 09:30:02 +02:00
|
|
|
#define SOMAXCONN 128
|
2019-03-13 13:13:23 +01:00
|
|
|
|
2020-09-16 12:29:48 -04:00
|
|
|
enum {
|
|
|
|
|
SO_RCVTIMEO,
|
|
|
|
|
SO_SNDTIMEO,
|
|
|
|
|
SO_TYPE,
|
|
|
|
|
SO_ERROR,
|
|
|
|
|
SO_PEERCRED,
|
|
|
|
|
SO_REUSEADDR,
|
|
|
|
|
SO_BINDTODEVICE,
|
|
|
|
|
SO_KEEPALIVE,
|
2020-09-16 12:32:45 -04:00
|
|
|
SO_TIMESTAMP,
|
2020-12-01 10:53:54 -03:00
|
|
|
SO_BROADCAST,
|
2020-09-16 12:29:48 -04:00
|
|
|
};
|
|
|
|
|
#define SO_RCVTIMEO SO_RCVTIMEO
|
|
|
|
|
#define SO_SNDTIMEO SO_SNDTIMEO
|
|
|
|
|
#define SO_TYPE SO_TYPE
|
|
|
|
|
#define SO_ERROR SO_ERROR
|
|
|
|
|
#define SO_PEERCRED SO_PEERCRED
|
|
|
|
|
#define SO_REUSEADDR SO_REUSEADDR
|
|
|
|
|
#define SO_BINDTODEVICE SO_BINDTODEVICE
|
|
|
|
|
#define SO_KEEPALIVE SO_KEEPALIVE
|
2020-09-16 12:32:45 -04:00
|
|
|
#define SO_TIMESTAMP SO_TIMESTAMP
|
2020-12-01 10:53:54 -03:00
|
|
|
#define SO_BROADCAST SO_BROADCAST
|
2020-09-16 12:32:45 -04:00
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
SCM_TIMESTAMP,
|
2020-09-20 00:34:36 +01:00
|
|
|
SCM_RIGHTS,
|
2020-09-16 12:32:45 -04:00
|
|
|
};
|
|
|
|
|
#define SCM_TIMESTAMP SCM_TIMESTAMP
|
2020-09-20 00:34:36 +01:00
|
|
|
#define SCM_RIGHTS SCM_RIGHTS
|
2019-03-13 13:13:23 +01:00
|
|
|
|
2020-08-11 18:59:48 +02:00
|
|
|
struct sockaddr_storage {
|
2020-09-09 08:43:53 +01:00
|
|
|
sa_family_t ss_family;
|
2020-08-11 18:59:48 +02:00
|
|
|
union {
|
2020-08-26 00:39:07 +02:00
|
|
|
char data[sizeof(struct sockaddr_un)];
|
2020-08-11 18:59:48 +02:00
|
|
|
void* alignment;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-14 15:26:06 +01:00
|
|
|
int socket(int domain, int type, int protocol);
|
2019-03-14 13:03:32 +01:00
|
|
|
int bind(int sockfd, const struct sockaddr* addr, socklen_t);
|
2019-02-14 15:26:06 +01:00
|
|
|
int listen(int sockfd, int backlog);
|
2019-03-14 13:03:32 +01:00
|
|
|
int accept(int sockfd, struct sockaddr*, socklen_t*);
|
|
|
|
|
int connect(int sockfd, const struct sockaddr*, socklen_t);
|
2020-02-08 00:52:33 +01:00
|
|
|
int shutdown(int sockfd, int how);
|
2019-03-13 17:33:40 +01:00
|
|
|
ssize_t send(int sockfd, const void*, size_t, int flags);
|
2020-09-16 11:45:00 -04:00
|
|
|
ssize_t sendmsg(int sockfd, const struct msghdr*, int flags);
|
2019-03-12 15:51:42 +01:00
|
|
|
ssize_t sendto(int sockfd, const void*, size_t, int flags, const struct sockaddr*, socklen_t);
|
2019-03-13 17:33:40 +01:00
|
|
|
ssize_t recv(int sockfd, void*, size_t, int flags);
|
2020-09-16 11:45:00 -04:00
|
|
|
ssize_t recvmsg(int sockfd, struct msghdr*, int flags);
|
2019-03-13 14:47:21 +01:00
|
|
|
ssize_t recvfrom(int sockfd, void*, size_t, int flags, struct sockaddr*, socklen_t*);
|
2019-03-13 13:13:23 +01:00
|
|
|
int getsockopt(int sockfd, int level, int option, void*, socklen_t*);
|
|
|
|
|
int setsockopt(int sockfd, int level, int option, const void*, socklen_t);
|
2019-05-19 19:55:27 +02:00
|
|
|
int getsockname(int sockfd, struct sockaddr*, socklen_t*);
|
2019-05-20 20:33:03 +02:00
|
|
|
int getpeername(int sockfd, struct sockaddr*, socklen_t*);
|
2020-06-24 22:57:37 +02:00
|
|
|
int sendfd(int sockfd, int fd);
|
|
|
|
|
int recvfd(int sockfd);
|
2019-02-14 15:26:06 +01:00
|
|
|
|
2020-09-16 12:32:45 -04:00
|
|
|
// These three are non-POSIX, but common:
|
|
|
|
|
#define CMSG_ALIGN(x) (((x) + sizeof(void*) - 1) & ~(sizeof(void*) - 1))
|
|
|
|
|
#define CMSG_SPACE(x) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(x))
|
|
|
|
|
#define CMSG_LEN(x) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (x))
|
|
|
|
|
|
|
|
|
|
static inline struct cmsghdr* CMSG_FIRSTHDR(struct msghdr* msg)
|
|
|
|
|
{
|
|
|
|
|
if (msg->msg_controllen < sizeof(struct cmsghdr))
|
|
|
|
|
return 0;
|
|
|
|
|
return (struct cmsghdr*)msg->msg_control;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline struct cmsghdr* CMSG_NXTHDR(struct msghdr* msg, struct cmsghdr* cmsg)
|
|
|
|
|
{
|
|
|
|
|
struct cmsghdr* next = (struct cmsghdr*)((char*)cmsg + CMSG_ALIGN(cmsg->cmsg_len));
|
|
|
|
|
unsigned offset = (char*)next - (char*)msg->msg_control;
|
|
|
|
|
if (msg->msg_controllen < offset + sizeof(struct cmsghdr))
|
2020-10-14 13:57:51 +02:00
|
|
|
return NULL;
|
2020-09-16 12:32:45 -04:00
|
|
|
return next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void* CMSG_DATA(struct cmsghdr* cmsg)
|
|
|
|
|
{
|
|
|
|
|
return (void*)(cmsg + 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 15:26:06 +01:00
|
|
|
__END_DECLS
|