Hash :
5876aac9
Author :
Baptiste
Date :
2024-07-24T20:15:58
CI :
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 <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);
}
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;
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_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;
}