Hash :
011506f4
Author :
Thomas de Grivel
Date :
2024-07-26T20:01:12
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
/* 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_addr.h"
#include "socket_buf.h"
void socket_buf_clean (s_socket_buf *sb)
{
assert(sb);
close(sb->sockfd);
buf_rw_clean(&sb->buf_rw);
if (sb->addr)
socket_addr_delete(sb->addr);
}
s_socket_buf * socket_buf_init (s_socket_buf *sb, t_socket sockfd,
struct sockaddr *addr,
u32 addr_len)
{
s_socket_buf tmp = {0};
assert(sb);
assert(sockfd >= 0);
assert(addr);
assert(addr_len);
if (! buf_rw_init_alloc(&tmp.buf_rw, BUF_SIZE)) {
err_puts("socket_buf_init: buf_rw_init_alloc");
assert(! "socket_buf_init: buf_rw_init_alloc");
return NULL;
}
tmp.buf_rw.r.user_ptr = sb;
tmp.buf_rw.w.user_ptr = sb;
if (! buf_rw_fd_open(&tmp.buf_rw, sockfd)) {
err_puts("socket_buf_init: buf_rw_fd_open");
assert(! "socket_buf_init: buf_rw_fd_open");
close(sockfd);
buf_rw_clean(&tmp.buf_rw);
return NULL;
}
tmp.sockfd = sockfd;
tmp.addr = socket_addr_new_copy(addr, addr_len);
if (! tmp.addr) {
err_puts("socket_buf_init: socket_addr_new_copy");
assert(! "socket_buf_init: socket_addr_new_copy");
return NULL;
}
tmp.addr_len = addr_len;
*sb = tmp;
return sb;
}
s_socket_buf * socket_buf_init_accept (s_socket_buf *sb, p_socket listening)
{
struct sockaddr *addr;
struct sockaddr_storage addr_storage = {0};
socklen_t addr_len;
sw e;
t_socket sockfd;
s_socket_buf tmp = {0};
assert(sb);
assert(listening);
addr = (struct sockaddr *) &addr_storage;
addr_len = sizeof(addr_storage);
sockfd = accept(*listening, addr, &addr_len);
if (sockfd < 0) {
e = errno;
err_write_1("socket_buf_init_accept: ");
err_inspect_s32(listening);
err_write_1(": accept: ");
err_puts(strerror(e));
assert(! "socket_buf_init_accept: accept");
return NULL;
}
if (! socket_buf_init(&tmp, sockfd, addr, addr_len)) {
err_puts("socket_buf_init_accept: socket_buf_init");
assert(! "socket_buf_init_accept: socket_buf_init");
close(sockfd);
return NULL;
}
*sb = tmp;
return sb;
}
s_socket_buf * socket_buf_init_connect (s_socket_buf *sb,
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;
s_socket_buf tmp;
assert(sb);
assert(host);
assert(service);
hints.ai_family = AF_INET;
e = getaddrinfo(host->ptr.pchar, service->ptr.pchar, &hints, &res0);
if (e) {
err_write_1("socket_buf_init_connect(");
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_buf_init_connect: 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_buf_init_connect: socket: ";
goto next;
}
if (connect(sockfd, res->ai_addr, res->ai_addrlen) < 0) {
e = errno;
error_reason = "socket_buf_init_connect: connect: ";
goto next;
}
break;
next:
res = res->ai_next;
}
if (sockfd < 0) {
err_write_1(error_reason);
err_puts(strerror(e));
assert(! "socket_buf_init_connect");
return NULL;
}
if (! socket_buf_init(&tmp, sockfd, res->ai_addr, res->ai_addrlen)) {
err_puts("socket_buf_init_connect: socket_buf_init");
assert(! "socket_buf_init_connect: socket_buf_init");
return NULL;
}
freeaddrinfo(res0);
*sb = tmp;
return sb;
}