maph 1.0.0 High-Performance Memory-Mapped Key-Value Database with O(1) Lookups

Loading...
Searching...
No Matches
bsd.h
Go to the documentation of this file.
1/*
2 * Authored by Alex Hultman, 2018-2019.
3 * Intellectual property of third-party.
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef BSD_H
19#define BSD_H
20
21// top-most wrapper of bsd-like syscalls
22
23// holds everything you need from the bsd/winsock interfaces, only included by internal libusockets.h
24// here everything about the syscalls are inline-wrapped and included
25
26#include "libusockets.h"
27
28#ifdef _WIN32
29#ifndef NOMINMAX
30#define NOMINMAX
31#endif
32#include <winsock2.h>
33#include <ws2tcpip.h>
34#pragma comment(lib, "ws2_32.lib")
35#define SETSOCKOPT_PTR_TYPE const char *
36#define LIBUS_SOCKET_ERROR INVALID_SOCKET
37#else
38#ifndef _GNU_SOURCE
39#define _GNU_SOURCE
40#endif
41/* For socklen_t */
42#include <sys/socket.h>
43#define SETSOCKOPT_PTR_TYPE int *
44#define LIBUS_SOCKET_ERROR -1
45#endif
46
47#define LIBUS_UDP_MAX_SIZE (64 * 1024)
48#define LIBUS_UDP_MAX_NUM 1024
49
50 struct bsd_addr_t {
51 struct sockaddr_storage mem;
52 socklen_t len;
53 char *ip;
54 int ip_length;
55 int port;
56};
57
58 int bsd_sendmmsg(LIBUS_SOCKET_DESCRIPTOR fd, void *msgvec, unsigned int vlen, int flags);
59 int bsd_recvmmsg(LIBUS_SOCKET_DESCRIPTOR fd, void *msgvec, unsigned int vlen, int flags, void *timeout);
60 int bsd_udp_packet_buffer_payload_length(void *msgvec, int index);
61 char *bsd_udp_packet_buffer_payload(void *msgvec, int index);
62 char *bsd_udp_packet_buffer_peer(void *msgvec, int index);
63 int bsd_udp_packet_buffer_local_ip(void *msgvec, int index, char *ip);
64 int bsd_udp_packet_buffer_ecn(void *msgvec, int index);
65 void *bsd_create_udp_packet_buffer();
66 void bsd_udp_buffer_set_packet_payload(struct us_udp_packet_buffer_t *send_buf, int index, int offset, void *payload, int length, void *peer_addr);
67
68 LIBUS_SOCKET_DESCRIPTOR apple_no_sigpipe(LIBUS_SOCKET_DESCRIPTOR fd);
69 LIBUS_SOCKET_DESCRIPTOR bsd_set_nonblocking(LIBUS_SOCKET_DESCRIPTOR fd);
70 void bsd_socket_nodelay(LIBUS_SOCKET_DESCRIPTOR fd, int enabled);
71 void bsd_socket_flush(LIBUS_SOCKET_DESCRIPTOR fd);
72 LIBUS_SOCKET_DESCRIPTOR bsd_create_socket(int domain, int type, int protocol);
73
74 void bsd_close_socket(LIBUS_SOCKET_DESCRIPTOR fd);
75 void bsd_shutdown_socket(LIBUS_SOCKET_DESCRIPTOR fd);
76 void bsd_shutdown_socket_read(LIBUS_SOCKET_DESCRIPTOR fd);
77
78 void internal_finalize_bsd_addr(struct bsd_addr_t *addr);
79
80 int bsd_local_addr(LIBUS_SOCKET_DESCRIPTOR fd, struct bsd_addr_t *addr);
81 int bsd_remote_addr(LIBUS_SOCKET_DESCRIPTOR fd, struct bsd_addr_t *addr);
82
83 char *bsd_addr_get_ip(struct bsd_addr_t *addr);
84 int bsd_addr_get_ip_length(struct bsd_addr_t *addr);
85
86 int bsd_addr_get_port(struct bsd_addr_t *addr);
87
88// called by dispatch_ready_poll
89 LIBUS_SOCKET_DESCRIPTOR bsd_accept_socket(LIBUS_SOCKET_DESCRIPTOR fd, struct bsd_addr_t *addr);
90
91 int bsd_recv(LIBUS_SOCKET_DESCRIPTOR fd, void *buf, int length, int flags);
92 int bsd_send(LIBUS_SOCKET_DESCRIPTOR fd, const char *buf, int length, int msg_more);
93 int bsd_would_block();
94
95// return LIBUS_SOCKET_ERROR or the fd that represents listen socket
96// listen both on ipv6 and ipv4
97 LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket(const char *host, int port, int options);
98
99 LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket_unix(const char *path, int options);
100
101/* Creates an UDP socket bound to the hostname and port */
102 LIBUS_SOCKET_DESCRIPTOR bsd_create_udp_socket(const char *host, int port);
103
104 LIBUS_SOCKET_DESCRIPTOR bsd_create_connect_socket(const char *host, int port, const char *source_host, int options);
105
106 LIBUS_SOCKET_DESCRIPTOR bsd_create_connect_socket_unix(const char *server_path, int options);
107
108#endif // BSD_H
bsd_local_addr
int bsd_local_addr(LIBUS_SOCKET_DESCRIPTOR fd, struct bsd_addr_t *addr)
bsd_shutdown_socket_read
void bsd_shutdown_socket_read(LIBUS_SOCKET_DESCRIPTOR fd)
bsd_addr_t::port
int port
Definition bsd.h:55
bsd_accept_socket
LIBUS_SOCKET_DESCRIPTOR bsd_accept_socket(LIBUS_SOCKET_DESCRIPTOR fd, struct bsd_addr_t *addr)
bsd_remote_addr
int bsd_remote_addr(LIBUS_SOCKET_DESCRIPTOR fd, struct bsd_addr_t *addr)
bsd_recv
int bsd_recv(LIBUS_SOCKET_DESCRIPTOR fd, void *buf, int length, int flags)
bsd_addr_get_ip
char * bsd_addr_get_ip(struct bsd_addr_t *addr)
Definition bsd.c:360
bsd_create_listen_socket
LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket(const char *host, int port, int options)
Definition bsd.c:432
bsd_recvmmsg
int bsd_recvmmsg(LIBUS_SOCKET_DESCRIPTOR fd, void *msgvec, unsigned int vlen, int flags, void *timeout)
internal_finalize_bsd_addr
void internal_finalize_bsd_addr(struct bsd_addr_t *addr)
Definition bsd.c:326
bsd_addr_t::len
socklen_t len
Definition bsd.h:52
bsd_udp_packet_buffer_peer
char * bsd_udp_packet_buffer_peer(void *msgvec, int index)
Definition bsd.c:166
bsd_addr_get_ip_length
int bsd_addr_get_ip_length(struct bsd_addr_t *addr)
Definition bsd.c:364
bsd_addr_t::ip_length
int ip_length
Definition bsd.h:54
bsd_udp_packet_buffer_payload
char * bsd_udp_packet_buffer_payload(void *msgvec, int index)
Definition bsd.c:175
bsd_socket_flush
void bsd_socket_flush(LIBUS_SOCKET_DESCRIPTOR fd)
bsd_send
int bsd_send(LIBUS_SOCKET_DESCRIPTOR fd, const char *buf, int length, int msg_more)
bsd_addr_get_port
int bsd_addr_get_port(struct bsd_addr_t *addr)
Definition bsd.c:368
bsd_shutdown_socket
void bsd_shutdown_socket(LIBUS_SOCKET_DESCRIPTOR fd)
bsd_set_nonblocking
LIBUS_SOCKET_DESCRIPTOR bsd_set_nonblocking(LIBUS_SOCKET_DESCRIPTOR fd)
bsd_udp_packet_buffer_ecn
int bsd_udp_packet_buffer_ecn(void *msgvec, int index)
Definition bsd.c:637
bsd_create_socket
LIBUS_SOCKET_DESCRIPTOR bsd_create_socket(int domain, int type, int protocol)
Definition bsd.c:290
bsd_udp_packet_buffer_payload_length
int bsd_udp_packet_buffer_payload_length(void *msgvec, int index)
Definition bsd.c:184
bsd_udp_packet_buffer_local_ip
int bsd_udp_packet_buffer_local_ip(void *msgvec, int index, char *ip)
Definition bsd.c:141
bsd_sendmmsg
int bsd_sendmmsg(LIBUS_SOCKET_DESCRIPTOR fd, void *msgvec, unsigned int vlen, int flags)
bsd_create_listen_socket_unix
LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket_unix(const char *path, int options)
Definition bsd.c:514
bsd_socket_nodelay
void bsd_socket_nodelay(LIBUS_SOCKET_DESCRIPTOR fd, int enabled)
bsd_create_connect_socket
LIBUS_SOCKET_DESCRIPTOR bsd_create_connect_socket(const char *host, int port, const char *source_host, int options)
Definition bsd.c:668
bsd_addr_t::mem
struct sockaddr_storage mem
Definition bsd.h:51
bsd_create_udp_packet_buffer
void * bsd_create_udp_packet_buffer()
Definition bsd.c:225
bsd_udp_buffer_set_packet_payload
void bsd_udp_buffer_set_packet_payload(struct us_udp_packet_buffer_t *send_buf, int index, int offset, void *payload, int length, void *peer_addr)
Definition bsd.c:193
apple_no_sigpipe
LIBUS_SOCKET_DESCRIPTOR apple_no_sigpipe(LIBUS_SOCKET_DESCRIPTOR fd)
bsd_create_connect_socket_unix
LIBUS_SOCKET_DESCRIPTOR bsd_create_connect_socket_unix(const char *server_path, int options)
Definition bsd.c:706
bsd_addr_t::ip
char * ip
Definition bsd.h:53
bsd_create_udp_socket
LIBUS_SOCKET_DESCRIPTOR bsd_create_udp_socket(const char *host, int port)
Definition bsd.c:550
bsd_would_block
int bsd_would_block()
Definition bsd.c:422
bsd_close_socket
void bsd_close_socket(LIBUS_SOCKET_DESCRIPTOR fd)
bsd_addr_t
Definition bsd.h:50
libusockets.h
LIBUS_SOCKET_DESCRIPTOR
#define LIBUS_SOCKET_DESCRIPTOR
Definition libusockets.h:38