Hash :
a80c4b3f
Author :
Baptiste
Date :
2024-07-25T16:28:02
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
/* 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 <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <libkc3/kc3.h>
#include "socket.h"
#include "socket_buf.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_in addr_in = {0};
socklen_t addr_len;
sw e;
t_socket tmp;
assert(s);
assert(listening);
addr = (struct sockaddr *) &addr_in;
addr_len = sizeof(addr_in);
tmp = accept(*listening, addr, &addr_len);
if (tmp < 0) {
e = errno;
err_write_1("socket_init_accept: ");
err_inspect_s32(listening);
err_write_1(": 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,
const s_str *service)
{
struct addrinfo hints = {0};
struct addrinfo *res;
struct addrinfo *res0;
s32 e;
const char *error_reason = "error";
t_socket sockfd;
assert(s);
assert(host);
hints.ai_family = AF_INET;
e = getaddrinfo(host->ptr.pchar, service->ptr.pchar, &hints, &res0);
if (e) {
err_write_1("socket_init_listen(");
err_write_1(host->ptr.pchar);
err_write_1(", ");
err_write_1(service->ptr.pchar);
err_write_1("): getaddrinfo: ");
err_puts(gai_strerror(e));
assert(! "socket_init_listen: getaddrinfo");
return NULL;
}
e = 0;
sockfd = -1;
res = res0;
while (res) {
sockfd = socket(res->ai_family, SOCK_STREAM, res->ai_protocol);
if (sockfd < 0) {
e = errno;
error_reason = "socket_init_listen: socket: ";
goto next;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(int) {1},
sizeof(int)) < 0) {
error_reason = "setsockopt(SO_REUSEADDR)";
goto next;
}
if (bind(sockfd, res->ai_addr, res->ai_addrlen) < 0) {
e = errno;
error_reason = "socket_init_listen: bind: ";
goto next;
}
if (listen(sockfd, SOMAXCONN) < 0) {
e = errno;
error_reason = "socket_init_listen: listen: ";
goto next;
}
*s = sockfd;
return s;
next:
res = res->ai_next;
}
err_write_1(error_reason);
err_puts(strerror(e));
assert(! "socket_init_listen");
return NULL;
}