ktcc: http_tcp_demo refactoring & net/network.h small fix

git-svn-id: svn://kolibrios.org@8450 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
Rustem Gimadutdinov (rgimad) 2020-12-18 22:44:07 +00:00
parent 459cb3ec5b
commit 51f9df912f
2 changed files with 14 additions and 12 deletions

View File

@ -48,7 +48,7 @@ struct addrinfo {
extern int networklib_init (); extern int networklib_init ();
extern int (*inet_addr __attribute__ ((stdcall)))(const char* hostname); extern int (*inet_addr __attribute__ ((stdcall)))(const char* hostname);
extern char* (*inet_ntoa __attribute__ ((stdcall)))(int ip_addr); extern char* (*inet_ntoa __attribute__ ((stdcall)))(int ip_addr);
extern int (*getaddrinfo __attribute__ ((stdcall)))(char* hostname, int servname, struct addrinfo* hints, struct addrinfo** res); extern int (*getaddrinfo __attribute__ ((stdcall)))(const char* hostname, const char* servname, const struct addrinfo* hints, struct addrinfo** res);
extern void (*freeaddrinfo __attribute__ ((stdcall)))(struct addrinfo* ai); extern void (*freeaddrinfo __attribute__ ((stdcall)))(struct addrinfo* ai);
#endif #endif

View File

@ -14,29 +14,27 @@ int main() {
printf("Connecting to %s on port %d\n", host, port); printf("Connecting to %s on port %d\n", host, port);
struct addrinfo *addr_info; struct addrinfo *addr_info;
if (getaddrinfo(host, 0, 0, &addr_info) != 0) { char port_str[16]; sprintf(port_str, "%d", port);
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // IPv4 or IPv6 doesnt matter
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
if (getaddrinfo(host, port_str, 0, &addr_info) != 0) {
printf("Host %s not found!\n", host); printf("Host %s not found!\n", host);
freeaddrinfo(addr_info); freeaddrinfo(addr_info);
exit(-1); exit(-1);
} }
unsigned int host_sin_addr = addr_info->ai_addr->sin_addr; printf("IP address of %s is %s\n", host, inet_ntoa(addr_info->ai_addr->sin_addr));
printf("IP address of %s is %s\n", host, inet_ntoa(host_sin_addr)); //printf("Host port = %d\n", addr_info->ai_addr->sin_port >> 8);
//printf("Host port = %d\n", addr_info->ai_addr->sin_addr >> 8);
freeaddrinfo(addr_info);
char request[256]; char request[256];
sprintf(request, "GET /en/ HTTP/1.1\r\nHost: %s\r\n\r\n", host); sprintf(request, "GET /en/ HTTP/1.1\r\nHost: %s\r\n\r\n", host);
printf("request = %s\n", request); printf("request = %s\n", request);
int sock = socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP); int sock = socket(AF_INET4, SOCK_STREAM, IPPROTO_TCP);
sockaddr sock_addr;
sock_addr.sin_family = AF_INET4;
sock_addr.sin_port = 80 << 8; // dirty hack, normally need to use htons(80) but there is no such here in libraries
sock_addr.sin_addr = host_sin_addr;
sock_addr.sin_zero = 0;
puts("Connecting...\n"); puts("Connecting...\n");
if (connect(sock, &sock_addr, sizeof(sock_addr)) != 0) { if (connect(sock, addr_info->ai_addr, addr_info->ai_addrlen) != 0) {
printf("Connection failed, err_code = %d\n", err_code); printf("Connection failed, err_code = %d\n", err_code);
exit(err_code); exit(err_code);
} }
@ -57,6 +55,10 @@ int main() {
printf("Response = %s\n", buf); printf("Response = %s\n", buf);
freeaddrinfo(addr_info);
close(sock);
puts("\n goodbye)\n");
con_exit(0); con_exit(0);
return 0; return 0;
} }