implemented client interactive mode

This commit is contained in:
2025-08-28 19:11:28 +03:00
parent c008d759ae
commit f01c5fc8a9
11 changed files with 234 additions and 105 deletions

View File

@@ -18,20 +18,42 @@ EndpointIPv4 EndpointIPv4_fromSockaddr(struct sockaddr_in saddr){
return end;
}
//TODO Endpoint functions
AddressIPv4 AddressIPv4_fromStr(cstr s);
str AddressIPv4_toStr(AddressIPv4 address);
EndpointIPv4 EndpointIPv4_fromStr(cstr s){
u32 a, b, c, d, p;
sscanf(s, "%u.%u.%u.%u:%u", &a, &b, &c, &d, &p);
EndpointIPv4 e = (EndpointIPv4){
.address = AddressIPv4_fromBytes(a, b, c, d),
.port = p
};
return e;
Result(void) AddressIPv4_parse(cstr s, AddressIPv4* addr){
*addr = AddressIPv4_INVALID;
u32 a, b, c, d;
if(sscanf(s, "%u.%u.%u.%u", &a, &b, &c, &d) != 4){
return RESULT_ERROR_FMT("can't parse as AddressIPv4: '%s'", s);
}
*addr = AddressIPv4_fromBytes(a, b, c, d);
return RESULT_VOID;
}
str EndpointIPv4_toStr(EndpointIPv4 end);
Result(void) EndpointIPv4_parse(cstr s, EndpointIPv4* end){
*end = EndpointIPv4_INVALID;
u32 a, b, c, d, p;
if(sscanf(s, "%u.%u.%u.%u:%u", &a, &b, &c, &d, &p) != 5){
return RESULT_ERROR_FMT("can't parse as EndpointIPv4: '%s'", s);
}
*end = EndpointIPv4_create(AddressIPv4_fromBytes(a, b, c, d), p);
return RESULT_VOID;
}
str AddressIPv4_toStr(AddressIPv4 addr){
char* data = malloc(16);
memset(data, 0, 16);
sprintf(data, "%u.%u.%u.%u",
addr.bytes[0], addr.bytes[1],
addr.bytes[2], addr.bytes[3]);
return str_from_cstr(data);
}
str EndpointIPv4_toStr(EndpointIPv4 end){
char* data = malloc(24);
memset(data, 0, 24);
sprintf(data, "%u.%u.%u.%u:%u",
end.address.bytes[0], end.address.bytes[1],
end.address.bytes[2], end.address.bytes[3],
end.port);
return str_from_cstr(data);
}

View File

@@ -2,7 +2,7 @@
#include "tlibc/std.h"
#include "tlibc/string/str.h"
#define port_INVALID ((port)~0)
#define port_INVALID ((u16)~0)
#define port_is_invalid(PORT) (PORT == port_INVALID)
@@ -18,8 +18,8 @@ typedef union AddressIPv4 {
#define AddressIPv4_fromBytes(A, B, C, D) ((AddressIPv4){ .bytes = {A,B,C,D} })
#define AddressIPv4_fromU32(N) ((AddressIPv4){ .UintBigEndian = N })
AddressIPv4 AddressIPv4_fromStr(cstr s);
str AddressIPv4_toStr(AddressIPv4 address);
Result(void) AddressIPv4_parse(cstr s, AddressIPv4* addr);
str AddressIPv4_toStr(AddressIPv4 addr);
typedef struct EndpointIPv4 {
@@ -31,8 +31,5 @@ typedef struct EndpointIPv4 {
#define EndpointIPv4_is_invalid(ENDP) (AddressIPv4_is_invalid(ENDP.address) || port_is_invalid(ENDP.port))
#define EndpointIPv4_create(ADDR, PORT) ((EndpointIPv4){ADDR, PORT})
EndpointIPv4 EndpointIPv4_fromStr(cstr s);
Result(void) EndpointIPv4_parse(cstr s, EndpointIPv4* end);
str EndpointIPv4_toStr(EndpointIPv4 end);
struct sockaddr_in EndpointIPv4_toSockaddr(EndpointIPv4 end);
EndpointIPv4 EndpointIPv4_fromSockaddr(struct sockaddr_in saddr);

View File

@@ -1,5 +1,6 @@
#pragma once
#include "tlibc/errors.h"
#include "endpoint.h"
#if !defined(KN_USE_WINSOCK)
#if defined(_WIN64) || defined(_WIN32)
@@ -24,3 +25,6 @@
#define RESULT_ERROR_SOCKET() RESULT_ERROR(strerror(errno), false)
#endif
struct sockaddr_in EndpointIPv4_toSockaddr(EndpointIPv4 end);
EndpointIPv4 EndpointIPv4_fromSockaddr(struct sockaddr_in saddr);