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

Loading...
Searching...
No Matches
udp.c
Go to the documentation of this file.
1/*
2 * Authored by Alex Hultman, 2018-2021.
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#include "libusockets.h"
19#include "internal/internal.h"
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25 int us_udp_packet_buffer_ecn(struct us_udp_packet_buffer_t *buf, int index) {
26 return bsd_udp_packet_buffer_ecn(buf, index);
27}
28
29 int us_udp_packet_buffer_local_ip(struct us_udp_packet_buffer_t *buf, int index, char *ip) {
30 return bsd_udp_packet_buffer_local_ip(buf, index, ip);
31}
32
33 char *us_udp_packet_buffer_peer(struct us_udp_packet_buffer_t *buf, int index) {
34 return bsd_udp_packet_buffer_peer(buf, index);
35}
36
37 char *us_udp_packet_buffer_payload(struct us_udp_packet_buffer_t *buf, int index) {
38 return bsd_udp_packet_buffer_payload(buf, index);
39}
40
41 int us_udp_packet_buffer_payload_length(struct us_udp_packet_buffer_t *buf, int index) {
42 return bsd_udp_packet_buffer_payload_length(buf, index);
43}
44
45// what should we return? number of sent datagrams?
46 int us_udp_socket_send(struct us_udp_socket_t *s, struct us_udp_packet_buffer_t *buf, int num) {
47 int fd = us_poll_fd((struct us_poll_t *) s);
48
49 // we need to poll out if we failed
50
51 return bsd_sendmmsg(fd, buf, num, 0);
52}
53
54 int us_udp_socket_receive(struct us_udp_socket_t *s, struct us_udp_packet_buffer_t *buf) {
55 int fd = us_poll_fd((struct us_poll_t *) s);
56 return bsd_recvmmsg(fd, buf, LIBUS_UDP_MAX_NUM, 0, 0);
57}
58
59 void us_udp_buffer_set_packet_payload(struct us_udp_packet_buffer_t *send_buf, int index, int offset, void *payload, int length, void *peer_addr) {
60 bsd_udp_buffer_set_packet_payload(send_buf, index, offset, payload, length, peer_addr);
61}
62
63 struct us_udp_packet_buffer_t *us_create_udp_packet_buffer() {
64 return (struct us_udp_packet_buffer_t *) bsd_create_udp_packet_buffer();
65}
66
67 struct us_internal_udp_t {
68 struct us_internal_callback_t cb;
69 struct us_udp_packet_buffer_t *receive_buf;
70 void (*data_cb)(struct us_udp_socket_t *, struct us_udp_packet_buffer_t *, int);
71 void (*drain_cb)(struct us_udp_socket_t *);
72 void *user;
73 /* An UDP socket can only ever be bound to one single port regardless of how
74 * many interfaces it may listen to. Therefore we cache the port after creation
75 * and use it to build a proper and full sockaddr_in or sockaddr_in6 for every received packet */
76 int port;
77};
78
79 int us_udp_socket_bound_port(struct us_udp_socket_t *s) {
80 return ((struct us_internal_udp_t *) s)->port;
81}
82
83/* Internal wrapper, move from here */
84 void internal_on_udp_read(struct us_udp_socket_t *s) {
85
86 // lookup receive buffer and callback here
87 struct us_internal_udp_t *udp = (struct us_internal_udp_t *) s;
88
89 int packets = us_udp_socket_receive(s, udp->receive_buf);
90 //printf("Packets: %d\n", packets);
91
92 // we need to get the socket data and lookup its callback here
93
94
95 udp->data_cb(s, udp->receive_buf, packets);
96}
97
98 void *us_udp_socket_user(struct us_udp_socket_t *s) {
99 struct us_internal_udp_t *udp = (struct us_internal_udp_t *) s;
100
101 return udp->user;
102}
103
104 struct us_udp_socket_t *us_create_udp_socket(struct us_loop_t *loop, struct us_udp_packet_buffer_t *buf, void (*data_cb)(struct us_udp_socket_t *, struct us_udp_packet_buffer_t *, int), void (*drain_cb)(struct us_udp_socket_t *), const char *host, unsigned short port, void *user) {
105
106 LIBUS_SOCKET_DESCRIPTOR fd = bsd_create_udp_socket(host, port);
107 if (fd == LIBUS_SOCKET_ERROR) {
108 return 0;
109 }
110
111 /* If buf is 0 then create one here */
112 if (!buf) {
113 buf = us_create_udp_packet_buffer();
114 }
115
116 int ext_size = 0;
117 int fallthrough = 0;
118
119 struct us_poll_t *p = us_create_poll(loop, fallthrough, sizeof(struct us_internal_udp_t) + ext_size);
120 us_poll_init(p, fd, POLL_TYPE_CALLBACK);
121
122 struct us_internal_udp_t *cb = (struct us_internal_udp_t *) p;
123 cb->cb.loop = loop;
124 cb->cb.cb_expects_the_loop = 0;
125 cb->cb.leave_poll_ready = 1;
126
127 /* Get and store the port once */
128 struct bsd_addr_t tmp;
129 bsd_local_addr(fd, &tmp);
130 cb->port = bsd_addr_get_port(&tmp);
131
132 printf("The port of UDP is: %d\n", cb->port);
133
134 /* There is no udp socket context, only user data */
135 /* This should really be ext like everything else */
136 cb->user = user;
137
138 cb->data_cb = data_cb;
139 cb->receive_buf = buf;
140 cb->drain_cb = drain_cb;
141
142 cb->cb.cb = (void (*)(struct us_internal_callback_t *)) internal_on_udp_read;
143
144 us_poll_start((struct us_poll_t *) cb, cb->cb.loop, LIBUS_SOCKET_READABLE);
145
146 return (struct us_udp_socket_t *) cb;
147}
LIBUS_SOCKET_READABLE
#define LIBUS_SOCKET_READABLE
Definition asio.h:23
bsd_create_udp_socket
int bsd_create_udp_socket(const char *host, int port)
Definition bsd.c:550
bsd_udp_packet_buffer_peer
char * bsd_udp_packet_buffer_peer(void *msgvec, int index)
Definition bsd.c:166
bsd_udp_packet_buffer_payload
char * bsd_udp_packet_buffer_payload(void *msgvec, int index)
Definition bsd.c:175
bsd_addr_get_port
int bsd_addr_get_port(struct bsd_addr_t *addr)
Definition bsd.c:368
bsd_udp_packet_buffer_ecn
int bsd_udp_packet_buffer_ecn(void *msgvec, int index)
Definition bsd.c:637
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(int fd, void *msgvec, unsigned int vlen, int flags)
Definition bsd.c:56
bsd_recvmmsg
int bsd_recvmmsg(int fd, void *msgvec, unsigned int vlen, int flags, void *timeout)
Definition bsd.c:111
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
bsd_local_addr
int bsd_local_addr(int fd, struct bsd_addr_t *addr)
Definition bsd.c:342
LIBUS_UDP_MAX_NUM
#define LIBUS_UDP_MAX_NUM
Definition bsd.h:48
LIBUS_SOCKET_ERROR
#define LIBUS_SOCKET_ERROR
Definition bsd.h:44
bsd_addr_t
Definition bsd.h:50
internal.h
POLL_TYPE_CALLBACK
@ POLL_TYPE_CALLBACK
Definition internal.h:50
libusockets.h
us_poll_init
void us_poll_init(struct us_poll_t *p, int fd, int poll_type)
LIBUS_SOCKET_DESCRIPTOR
#define LIBUS_SOCKET_DESCRIPTOR
Definition libusockets.h:38
us_create_poll
struct us_poll_t * us_create_poll(struct us_loop_t *loop, int fallthrough, unsigned int ext_size)
us_poll_fd
int us_poll_fd(struct us_poll_t *p)
us_poll_start
void us_poll_start(struct us_poll_t *p, struct us_loop_t *loop, int events)
us_poll_t::fd
LIBUS_SOCKET_DESCRIPTOR fd
Definition asio.h:40
us_poll_t
Definition asio.h:37
us_loop_t
Definition asio.h:26
us_internal_callback_t
Definition internal.h:99
us_internal_callback_t::cb
void(* cb)(struct us_internal_callback_t *cb)
Definition internal.h:104
us_internal_udp_t
Definition udp.c:67
us_internal_udp_t::user
void * user
Definition udp.c:72
us_internal_udp_t::receive_buf
struct us_udp_packet_buffer_t * receive_buf
Definition udp.c:69
us_internal_udp_t::cb
struct us_internal_callback_t cb
Definition udp.c:68
us_internal_udp_t::data_cb
void(* data_cb)(struct us_udp_socket_t *, struct us_udp_packet_buffer_t *, int)
Definition udp.c:70
us_internal_udp_t::drain_cb
void(* drain_cb)(struct us_udp_socket_t *)
Definition udp.c:71
us_internal_udp_t::port
int port
Definition udp.c:76
us_create_udp_packet_buffer
struct us_udp_packet_buffer_t * us_create_udp_packet_buffer()
Definition udp.c:63
us_udp_socket_user
void * us_udp_socket_user(struct us_udp_socket_t *s)
Definition udp.c:98
us_udp_socket_receive
int us_udp_socket_receive(struct us_udp_socket_t *s, struct us_udp_packet_buffer_t *buf)
Definition udp.c:54
us_udp_packet_buffer_peer
char * us_udp_packet_buffer_peer(struct us_udp_packet_buffer_t *buf, int index)
Definition udp.c:33
us_udp_socket_bound_port
int us_udp_socket_bound_port(struct us_udp_socket_t *s)
Definition udp.c:79
us_udp_packet_buffer_local_ip
int us_udp_packet_buffer_local_ip(struct us_udp_packet_buffer_t *buf, int index, char *ip)
Definition udp.c:29
us_udp_buffer_set_packet_payload
void us_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 udp.c:59
us_udp_packet_buffer_payload_length
int us_udp_packet_buffer_payload_length(struct us_udp_packet_buffer_t *buf, int index)
Definition udp.c:41
us_udp_packet_buffer_ecn
int us_udp_packet_buffer_ecn(struct us_udp_packet_buffer_t *buf, int index)
Definition udp.c:25
us_udp_packet_buffer_payload
char * us_udp_packet_buffer_payload(struct us_udp_packet_buffer_t *buf, int index)
Definition udp.c:37
us_udp_socket_send
int us_udp_socket_send(struct us_udp_socket_t *s, struct us_udp_packet_buffer_t *buf, int num)
Definition udp.c:46
us_create_udp_socket
struct us_udp_socket_t * us_create_udp_socket(struct us_loop_t *loop, struct us_udp_packet_buffer_t *buf, void(*data_cb)(struct us_udp_socket_t *, struct us_udp_packet_buffer_t *, int), void(*drain_cb)(struct us_udp_socket_t *), const char *host, unsigned short port, void *user)
Definition udp.c:104
internal_on_udp_read
void internal_on_udp_read(struct us_udp_socket_t *s)
Definition udp.c:84