From 51f9df912feac832f8fb46c221b7ad4f3bbdacc6 Mon Sep 17 00:00:00 2001 From: "Rustem Gimadutdinov (rgimad)" Date: Fri, 18 Dec 2020 22:44:07 +0000 Subject: [PATCH] ktcc: http_tcp_demo refactoring & net/network.h small fix git-svn-id: svn://kolibrios.org@8450 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../ktcc/trunk/libc/include/net/network.h | 2 +- .../ktcc/trunk/samples/net/http_tcp_demo.c | 24 ++++++++++--------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/programs/develop/ktcc/trunk/libc/include/net/network.h b/programs/develop/ktcc/trunk/libc/include/net/network.h index 879548f783..38158193ba 100644 --- a/programs/develop/ktcc/trunk/libc/include/net/network.h +++ b/programs/develop/ktcc/trunk/libc/include/net/network.h @@ -48,7 +48,7 @@ struct addrinfo { extern int networklib_init (); extern int (*inet_addr __attribute__ ((stdcall)))(const char* hostname); 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); #endif diff --git a/programs/develop/ktcc/trunk/samples/net/http_tcp_demo.c b/programs/develop/ktcc/trunk/samples/net/http_tcp_demo.c index b80f7106e9..2764f91f5d 100644 --- a/programs/develop/ktcc/trunk/samples/net/http_tcp_demo.c +++ b/programs/develop/ktcc/trunk/samples/net/http_tcp_demo.c @@ -14,29 +14,27 @@ int main() { printf("Connecting to %s on port %d\n", host, port); 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); freeaddrinfo(addr_info); exit(-1); } - unsigned int host_sin_addr = 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_addr >> 8); - freeaddrinfo(addr_info); + printf("IP address of %s is %s\n", host, inet_ntoa(addr_info->ai_addr->sin_addr)); + //printf("Host port = %d\n", addr_info->ai_addr->sin_port >> 8); char request[256]; sprintf(request, "GET /en/ HTTP/1.1\r\nHost: %s\r\n\r\n", host); printf("request = %s\n", request); 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"); - 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); exit(err_code); } @@ -57,6 +55,10 @@ int main() { printf("Response = %s\n", buf); + freeaddrinfo(addr_info); + + close(sock); + puts("\n goodbye)\n"); con_exit(0); return 0; } \ No newline at end of file