1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2016-2026 The TokTok team.
* Copyright © 2013 Tox project.
*/
#include "net.h"
int net_socket_to_native(Socket sock)
{
return (force int)sock.value;
}
Socket net_socket_from_native(int sock)
{
const Socket res = {(force Socket_Value)sock};
return res;
}
int ns_close(const Network *ns, Socket sock)
{
return ns->funcs->close(ns->obj, sock);
}
Socket ns_accept(const Network *ns, Socket sock)
{
return ns->funcs->accept(ns->obj, sock);
}
int ns_bind(const Network *ns, Socket sock, const IP_Port *addr)
{
return ns->funcs->bind(ns->obj, sock, addr);
}
int ns_listen(const Network *ns, Socket sock, int backlog)
{
return ns->funcs->listen(ns->obj, sock, backlog);
}
int ns_connect(const Network *ns, Socket sock, const IP_Port *addr)
{
return ns->funcs->connect(ns->obj, sock, addr);
}
int ns_recvbuf(const Network *ns, Socket sock)
{
return ns->funcs->recvbuf(ns->obj, sock);
}
int ns_recv(const Network *ns, Socket sock, uint8_t *buf, size_t len)
{
return ns->funcs->recv(ns->obj, sock, buf, len);
}
int ns_recvfrom(const Network *ns, Socket sock, uint8_t *buf, size_t len, IP_Port *addr)
{
return ns->funcs->recvfrom(ns->obj, sock, buf, len, addr);
}
int ns_send(const Network *ns, Socket sock, const uint8_t *buf, size_t len)
{
return ns->funcs->send(ns->obj, sock, buf, len);
}
int ns_sendto(const Network *ns, Socket sock, const uint8_t *buf, size_t len, const IP_Port *addr)
{
return ns->funcs->sendto(ns->obj, sock, buf, len, addr);
}
Socket ns_socket(const Network *ns, int domain, int type, int proto)
{
return ns->funcs->socket(ns->obj, domain, type, proto);
}
int ns_socket_nonblock(const Network *ns, Socket sock, bool nonblock)
{
return ns->funcs->socket_nonblock(ns->obj, sock, nonblock);
}
int ns_getsockopt(const Network *ns, Socket sock, int level, int optname, void *optval, size_t *optlen)
{
return ns->funcs->getsockopt(ns->obj, sock, level, optname, optval, optlen);
}
int ns_setsockopt(const Network *ns, Socket sock, int level, int optname, const void *optval, size_t optlen)
{
return ns->funcs->setsockopt(ns->obj, sock, level, optname, optval, optlen);
}
int ns_getaddrinfo(const Network *ns, const Memory *mem, const char *address, int family, int protocol, IP_Port **addrs)
{
return ns->funcs->getaddrinfo(ns->obj, mem, address, family, protocol, addrs);
}
int ns_freeaddrinfo(const Network *ns, const Memory *mem, IP_Port *addrs)
{
return ns->funcs->freeaddrinfo(ns->obj, mem, addrs);
}
size_t net_pack_bool(uint8_t *bytes, bool v)
{
bytes[0] = v ? 1 : 0;
return 1;
}
size_t net_pack_u16(uint8_t *bytes, uint16_t v)
{
bytes[0] = (v >> 8) & 0xff;
bytes[1] = v & 0xff;
return sizeof(v);
}
size_t net_pack_u32(uint8_t *bytes, uint32_t v)
{
uint8_t *p = bytes;
p += net_pack_u16(p, (v >> 16) & 0xffff);
p += net_pack_u16(p, v & 0xffff);
return p - bytes;
}
size_t net_pack_u64(uint8_t *bytes, uint64_t v)
{
uint8_t *p = bytes;
p += net_pack_u32(p, (v >> 32) & 0xffffffff);
p += net_pack_u32(p, v & 0xffffffff);
return p - bytes;
}
size_t net_unpack_bool(const uint8_t *bytes, bool *v)
{
*v = bytes[0] != 0;
return 1;
}
size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v)
{
const uint8_t hi = bytes[0];
const uint8_t lo = bytes[1];
*v = ((uint16_t)hi << 8) | lo;
return sizeof(*v);
}
size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v)
{
const uint8_t *p = bytes;
uint16_t hi;
uint16_t lo;
p += net_unpack_u16(p, &hi);
p += net_unpack_u16(p, &lo);
*v = ((uint32_t)hi << 16) | lo;
return p - bytes;
}
size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v)
{
const uint8_t *p = bytes;
uint32_t hi;
uint32_t lo;
p += net_unpack_u32(p, &hi);
p += net_unpack_u32(p, &lo);
*v = ((uint64_t)hi << 32) | lo;
return p - bytes;
}
static const Family family_unspec = {TOX_AF_UNSPEC};
static const Family family_ipv4 = {TOX_AF_INET};
static const Family family_ipv6 = {TOX_AF_INET6};
static const Family family_tcp_server = {TCP_SERVER_FAMILY};
static const Family family_tcp_client = {TCP_CLIENT_FAMILY};
static const Family family_tcp_ipv4 = {TCP_INET};
static const Family family_tcp_ipv6 = {TCP_INET6};
static const Family family_tox_tcp_ipv4 = {TOX_TCP_INET};
static const Family family_tox_tcp_ipv6 = {TOX_TCP_INET6};
Family net_family_unspec(void)
{
return family_unspec;
}
Family net_family_ipv4(void)
{
return family_ipv4;
}
Family net_family_ipv6(void)
{
return family_ipv6;
}
Family net_family_tcp_server(void)
{
return family_tcp_server;
}
Family net_family_tcp_client(void)
{
return family_tcp_client;
}
Family net_family_tcp_ipv4(void)
{
return family_tcp_ipv4;
}
Family net_family_tcp_ipv6(void)
{
return family_tcp_ipv6;
}
Family net_family_tox_tcp_ipv4(void)
{
return family_tox_tcp_ipv4;
}
Family net_family_tox_tcp_ipv6(void)
{
return family_tox_tcp_ipv6;
}
bool net_family_is_unspec(Family family)
{
return family.value == family_unspec.value;
}
bool net_family_is_ipv4(Family family)
{
return family.value == family_ipv4.value;
}
bool net_family_is_ipv6(Family family)
{
return family.value == family_ipv6.value;
}
bool net_family_is_tcp_server(Family family)
{
return family.value == family_tcp_server.value;
}
bool net_family_is_tcp_client(Family family)
{
return family.value == family_tcp_client.value;
}
bool net_family_is_tcp_ipv4(Family family)
{
return family.value == family_tcp_ipv4.value;
}
bool net_family_is_tcp_ipv6(Family family)
{
return family.value == family_tcp_ipv6.value;
}
bool net_family_is_tox_tcp_ipv4(Family family)
{
return family.value == family_tox_tcp_ipv4.value;
}
bool net_family_is_tox_tcp_ipv6(Family family)
{
return family.value == family_tox_tcp_ipv6.value;
}
const char *net_family_to_string(Family family)
{
if (net_family_is_unspec(family)) {
return "TOX_AF_UNSPEC";
}
if (net_family_is_ipv4(family)) {
return "TOX_AF_INET";
}
if (net_family_is_ipv6(family)) {
return "TOX_AF_INET6";
}
if (net_family_is_tcp_server(family)) {
return "TCP_SERVER_FAMILY";
}
if (net_family_is_tcp_client(family)) {
return "TCP_CLIENT_FAMILY";
}
if (net_family_is_tcp_ipv4(family)) {
return "TCP_INET";
}
if (net_family_is_tcp_ipv6(family)) {
return "TCP_INET6";
}
if (net_family_is_tox_tcp_ipv4(family)) {
return "TOX_TCP_INET";
}
if (net_family_is_tox_tcp_ipv6(family)) {
return "TOX_TCP_INET6";
}
return "<invalid Family>";
}
|