From: nishi Date: Wed, 17 Jan 2024 01:26:01 +0000 (+0000) Subject: dns X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=61d437cde249f6da2197a6092201597fcb52d8be;p=libw3.git dns git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@4 d27a3e52-49c5-7645-884c-6793ebffc270 --- diff --git a/Example/fetch.c b/Example/fetch.c index cef8fc5..afd1134 100644 --- a/Example/fetch.c +++ b/Example/fetch.c @@ -15,5 +15,5 @@ int main(int argc, char** argv){ return 1; } W3_Library_Init(); - struct W3* w3 = W3_Create(false, 80); + struct W3* w3 = W3_Create(false, argv[1], 80); } diff --git a/Library/Core.c b/Library/Core.c index e1dcb8d..2710c63 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -1,6 +1,7 @@ /* $Id$ */ #include "W3Core.h" +#include "W3DNS.h" #include "W3Util.h" #include @@ -27,8 +28,9 @@ int W3_Library_Init(void){ return 0; } -struct W3* W3_Create(bool ssl, int port){ +struct W3* W3_Create(bool ssl, const char* hostname, int port){ __W3_Debug("Create", "Creating a struct"); struct W3* w3 = malloc(sizeof(*w3)); + w3->sock = __W3_DNS_Connect(hostname, ssl, port); return w3; } diff --git a/Library/DNS.c b/Library/DNS.c index 8513ab3..ef48222 100644 --- a/Library/DNS.c +++ b/Library/DNS.c @@ -1,2 +1,49 @@ /* $Id$ */ #include "W3DNS.h" + +#include "W3Util.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port){ + __W3_Debug("DNS-Connect", "Resolving"); + struct addrinfo hints; + struct addrinfo* result; + struct addrinfo* rp; + int s; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = 0; + hints.ai_protocol = 0; + char* strport = malloc(6); /* Enough for 5 digits */ + memset(strport, 0, 6); + sprintf(strport, "%d", port); + s = getaddrinfo(hostname, strport, &hints, &result); + if(s != 0){ + __W3_Debug("Resolve", "Failed"); + return -1; /* Failed to resolve */ + } + int sock; + for(rp = result; rp != NULL; rp = rp->ai_next){ + sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); + if(sock == -1) continue; + if(connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) break; + close(sock); + } + freeaddrinfo(result); + free(strport); + if(rp == NULL){ + __W3_Debug("Connect", "Failed to connect"); + return -1; /* Failed to connect */ + } + __W3_Debug("Connect", "Conencted"); +} diff --git a/Library/Makefile b/Library/Makefile index 43d84dd..307b964 100644 --- a/Library/Makefile +++ b/Library/Makefile @@ -1,7 +1,7 @@ # $Id$ .PHONY: clean install -./libw3.so: ./Core.o ./Util.o +./libw3.so: ./Core.o ./Util.o ./DNS.o $(CC) $(LDFLAGS) -shared -o $@ $^ $(LIBS) ./%.o: ./%.c W3%.h diff --git a/Library/W3Core.h b/Library/W3Core.h index 221dfea..05552ea 100644 --- a/Library/W3Core.h +++ b/Library/W3Core.h @@ -14,7 +14,7 @@ struct W3 { #endif }; -int W3_Library_Init(void); /* Initialize the Library */ -struct W3* W3_Create(bool ssl, int port); /* Create the struct */ +int W3_Library_Init(void); /* Initialize the Library */ +struct W3* W3_Create(bool ssl, const char* hostname, int port); /* Create the struct */ #endif diff --git a/Library/W3DNS.h b/Library/W3DNS.h index 132413e..ee09e96 100644 --- a/Library/W3DNS.h +++ b/Library/W3DNS.h @@ -2,6 +2,9 @@ #ifndef __W3DNS_H__ #define __W3DNS_H__ -int __W3_DNS_Connect(const char* hostname); +#include +#include + +int __W3_DNS_Connect(const char* hostname, bool ssl, uint16_t port); #endif diff --git a/Makefile b/Makefile index 83ab44b..fa88a14 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ include config.mk CC := cc -CFLAGS := -g -std=c99 -fPIC +CFLAGS := -g -std=c99 -fPIC -D_BSD_SOURCE LDFLAGS := LIBS := PREFIX := /usr/local