Fixed for ipv6

This commit is contained in:
Laan Tungir
2026-04-24 06:51:09 -04:00
parent 693e21423e
commit ba97b1a6e3
3 changed files with 39 additions and 29 deletions

View File

@@ -1 +1 @@
0.6.2
0.6.3

View File

@@ -2,10 +2,10 @@
#define NOSTR_CORE_H
// Version information (auto-updated by increment_and_push.sh)
#define VERSION "v0.6.2"
#define VERSION "v0.6.3"
#define VERSION_MAJOR 0
#define VERSION_MINOR 6
#define VERSION_PATCH 2
#define VERSION_PATCH 3
/*
* NOSTR Core Library - Complete API Reference

View File

@@ -418,34 +418,44 @@ const char* nostr_ws_strerror(int error_code) {
static int tcp_connect(void* ctx, const char* host, int port) {
tcp_transport_t* tcp = (tcp_transport_t*)ctx;
// Create socket
tcp->socket_fd = socket(AF_INET, SOCK_STREAM, 0);
struct addrinfo hints;
struct addrinfo* result = NULL;
struct addrinfo* rp = NULL;
char port_str[16];
int ret;
tcp->socket_fd = -1;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
snprintf(port_str, sizeof(port_str), "%d", port);
ret = getaddrinfo(host, port_str, &hints, &result);
if (ret != 0 || !result) {
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
int fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (fd < 0) {
continue;
}
if (connect(fd, rp->ai_addr, rp->ai_addrlen) == 0) {
tcp->socket_fd = fd;
break;
}
close(fd);
}
freeaddrinfo(result);
if (tcp->socket_fd < 0) {
return -1;
}
// Resolve hostname
struct hostent* he = gethostbyname(host);
if (!he) {
close(tcp->socket_fd);
tcp->socket_fd = -1;
return -1;
}
// Set up address
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
memcpy(&addr.sin_addr, he->h_addr_list[0], he->h_length);
// Connect
if (connect(tcp->socket_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
close(tcp->socket_fd);
tcp->socket_fd = -1;
return -1;
}
// Safety timeout: prevent indefinite blocking in recv/send on half-broken sockets.
struct timeval io_timeout;
@@ -453,7 +463,7 @@ static int tcp_connect(void* ctx, const char* host, int port) {
io_timeout.tv_usec = 0;
(void)setsockopt(tcp->socket_fd, SOL_SOCKET, SO_RCVTIMEO, &io_timeout, sizeof(io_timeout));
(void)setsockopt(tcp->socket_fd, SOL_SOCKET, SO_SNDTIMEO, &io_timeout, sizeof(io_timeout));
return 0;
}