Hash :
600f4487
Author :
Thomas de Grivel
Date :
2024-07-20T19:00:22
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
/* kc3
* Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <libkc3/kc3.h>
#include "socket.h"
void socket_close (p_socket s)
{
assert(s);
close(*s);
*s = -1;
}
p_socket socket_init_accept (p_socket s, p_socket listening)
{
struct sockaddr *addr;
struct sockaddr_storage addr_storage = {0};
socklen_t addr_len;
sw e;
t_socket tmp;
assert(s);
assert(listening);
addr = (struct sockaddr *) &addr_storage;
addr_len = sizeof(addr_storage);
tmp = accept(*listening, addr, &addr_len);
if (tmp < 0) {
e = errno;
err_write_1("socket_init_accept: accept: ");
err_puts(strerror(e));
assert(! "socket_init_accept: accept");
return NULL;
}
*s = tmp;
return s;
}
p_socket socket_init_listen (p_socket s, const s_str *host, u16 port)
{
struct sockaddr *addr;
struct sockaddr_in *addr_inet;
struct sockaddr_in6 *addr_inet6;
socklen_t addr_len;
struct sockaddr_storage addr_storage;
sw e;
struct hostent *hostent;
t_socket tmp;
assert(s);
assert(host);
hostent = gethostbyname2(host->ptr.pchar, AF_INET);
if (! hostent)
hostent = gethostbyname2(host->ptr.pchar, AF_INET6);
if (! hostent) {
e = errno;
err_write_1("socket_init_listen: gethostbyname2: ");
err_puts(strerror(e));
assert(! "socket_init_listen: gethostbyname2");
return NULL;
}
addr = (struct sockaddr *) &addr_storage;
addr_len = hostent->h_length;
memcpy(addr, hostent->h_addr_list[0], addr_len);
switch (addr->sa_family) {
case AF_INET:
addr_inet = (struct sockaddr_in *) addr;
addr_inet->sin_port = htons(port);
break;
case AF_INET6:
addr_inet6 = (struct sockaddr_in6 *) addr;
addr_inet6->sin6_port = htons(port);
break;
default:
err_puts("socket_init_listen: unknown address family");
assert(! "socket_init_listen: unknown address family");
return NULL;
}
tmp = socket(addr->sa_family, SOCK_STREAM, 0);
if (tmp < 0) {
e = errno;
err_write_1("socket_init_listen: socket: ");
err_puts(strerror(e));
assert(! "socket_init_listen: socket");
return NULL;
}
if (bind(tmp, addr, addr_len) < 0) {
e = errno;
err_write_1("socket_init_listen: bind: ");
err_puts(strerror(e));
assert(! "socket_init_listen: bind");
return NULL;
}
if (listen(tmp, SOMAXCONN) < 0) {
e = errno;
err_write_1("socket_init_listen: listen: ");
err_puts(strerror(e));
assert(! "socket_init_listen: listen");
free(addr);
return NULL;
}
*s = tmp;
return s;
}