From 5bbd0f2e5d772836e0f09a2e6b4a252960d330a3 Mon Sep 17 00:00:00 2001 From: nishi Date: Wed, 17 Jan 2024 01:15:18 +0000 Subject: [PATCH] dns git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@3 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/Makefile | 9 +++++++-- Example/fetch.c | 5 +++++ Library/Core.c | 32 ++++++++++++++++++++++++++++++++ Library/DNS.c | 2 ++ Library/Makefile | 12 +++++++++--- Library/Util.c | 17 +++++++++++++++++ Library/W3Core.h | 15 +++++++++++++++ Library/W3DNS.h | 7 +++++++ Library/W3Util.h | 7 +++++++ Makefile | 15 ++++++++++++--- config.mk | 1 + 11 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 Library/DNS.c create mode 100644 Library/Util.c create mode 100644 Library/W3DNS.h create mode 100644 Library/W3Util.h diff --git a/Example/Makefile b/Example/Makefile index 0b15da1..834103f 100644 --- a/Example/Makefile +++ b/Example/Makefile @@ -1,7 +1,12 @@ -.PHONY: clean +# $Id$: +.PHONY: clean install ./fetch: ./fetch.c - $(CC) -o $@ $< ../Library/libw3.so + $(CC) -o $@ -I ../Library -L ../Library $< -lw3 clean: rm -f fetch *.o *.so *.core + +install: ./fetch + mkdir -p $(PREFIX)/bin + cp ./fetch $(PREFIX)/bin/w3-fetch diff --git a/Example/fetch.c b/Example/fetch.c index bf4f8f7..cef8fc5 100644 --- a/Example/fetch.c +++ b/Example/fetch.c @@ -4,11 +4,16 @@ * */ +#include + #include +#include int main(int argc, char** argv){ if(argc < 2){ fprintf(stderr, "Usage: %s URL\n", argv[0]); return 1; } + W3_Library_Init(); + struct W3* w3 = W3_Create(false, 80); } diff --git a/Library/Core.c b/Library/Core.c index 99b5b5a..e1dcb8d 100644 --- a/Library/Core.c +++ b/Library/Core.c @@ -1,2 +1,34 @@ /* $Id$ */ #include "W3Core.h" + +#include "W3Util.h" + +#include +#include + +#ifdef SSL_SUPPORT +#include +#endif + +int W3_Library_Init(void){ + __W3_Debug("LibW3", "Initializing"); +#ifdef SSL_SUPPORT + if(SSL_library_init() >= 0){ + __W3_Debug("LibW3-SSL", "Initialized"); + }else{ + return 1; + } +#endif +#ifdef SSL_SUPPORT + __W3_Debug("LibW3", "This is LibW3, version " LIBW3_VERSION ", using " OPENSSL_VERSION_TEXT); +#else + __W3_Debug("LibW3", "This is LibW3, version " LIBW3_VERSION ", without SSL support"); +#endif + return 0; +} + +struct W3* W3_Create(bool ssl, int port){ + __W3_Debug("Create", "Creating a struct"); + struct W3* w3 = malloc(sizeof(*w3)); + return w3; +} diff --git a/Library/DNS.c b/Library/DNS.c new file mode 100644 index 0000000..8513ab3 --- /dev/null +++ b/Library/DNS.c @@ -0,0 +1,2 @@ +/* $Id$ */ +#include "W3DNS.h" diff --git a/Library/Makefile b/Library/Makefile index 468ebaa..43d84dd 100644 --- a/Library/Makefile +++ b/Library/Makefile @@ -1,11 +1,17 @@ # $Id$ -.PHONY: clean +.PHONY: clean install -./libw3.so: ./Core.o - $(CC) $(LDFLAGS) -shared -o $@ $^ +./libw3.so: ./Core.o ./Util.o + $(CC) $(LDFLAGS) -shared -o $@ $^ $(LIBS) ./%.o: ./%.c W3%.h $(CC) $(CFLAGS) -c -o $@ $< clean: rm -f *.o *.so *.core + +install: ./libw3.so + mkdir -p $(PREFIX)/lib + cp ./libw3.so $(PREFIX)/lib/ + mkdir -p $(PREFIX)/include/W3 + cp *.h $(PREFIX)/include/W3/ diff --git a/Library/Util.c b/Library/Util.c new file mode 100644 index 0000000..a1ebd9c --- /dev/null +++ b/Library/Util.c @@ -0,0 +1,17 @@ +/* $Id$ */ +#include "W3Util.h" + +#include +#include +#include + +#define __DEBUG_LEN 12 + +void __W3_Debug(const char* title, const char* message){ +#ifdef __DEBUG__ + char* periods = malloc(__DEBUG_LEN - strlen(title) + 1); + periods[__DEBUG_LEN - strlen(title)] = 0; + memset(periods, '.', __DEBUG_LEN - strlen(title)); + fprintf(stderr, "%s%s %s\n", title, periods, message); +#endif +} diff --git a/Library/W3Core.h b/Library/W3Core.h index ff003c8..221dfea 100644 --- a/Library/W3Core.h +++ b/Library/W3Core.h @@ -2,4 +2,19 @@ #ifndef __W3CORE_H__ #define __W3CORE_H__ +#include + +#define LIBW3_VERSION "1.0" + +struct W3 { + int sock; /* Socket */ +#ifdef SSL_SUPPORT + void* ssl; /* Actually SSL*, NULL if no SSL */ + void* ssl_ctx; /* Actually SSL_CTX* */ +#endif +}; + +int W3_Library_Init(void); /* Initialize the Library */ +struct W3* W3_Create(bool ssl, int port); /* Create the struct */ + #endif diff --git a/Library/W3DNS.h b/Library/W3DNS.h new file mode 100644 index 0000000..132413e --- /dev/null +++ b/Library/W3DNS.h @@ -0,0 +1,7 @@ +/* $Id$ */ +#ifndef __W3DNS_H__ +#define __W3DNS_H__ + +int __W3_DNS_Connect(const char* hostname); + +#endif diff --git a/Library/W3Util.h b/Library/W3Util.h new file mode 100644 index 0000000..94bef34 --- /dev/null +++ b/Library/W3Util.h @@ -0,0 +1,7 @@ +/* $Id$ */ +#ifndef __W3UTIL_H__ +#define __W3UTIL_H__ + +void __W3_Debug(const char* title, const char* message); + +#endif diff --git a/Makefile b/Makefile index 37ff8bb..83ab44b 100644 --- a/Makefile +++ b/Makefile @@ -5,18 +5,23 @@ CC := cc CFLAGS := -g -std=c99 -fPIC LDFLAGS := LIBS := +PREFIX := /usr/local ifdef SSL +CFLAGS += -DSSL_SUPPORT LIBS += -lssl -lcrypto -SSL=SSL=YES endif -.PHONY: all clean +ifdef DEBUG +CFLAGS += -D__DEBUG__ +endif + +.PHONY: all clean ./Library/libw3.so ./Example/fetch all: ./Library/libw3.so ./Example/fetch ./Library/libw3.so: - $(MAKE) -C ./Library $(SSL) CC=$(CC) CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" LIBS="$(LIBS)" + $(MAKE) -C ./Library CC=$(CC) CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" LIBS="$(LIBS)" ./Example/fetch: $(MAKE) -C ./Example CC=$(CC) fetch @@ -24,3 +29,7 @@ all: ./Library/libw3.so ./Example/fetch clean: $(MAKE) -C ./Library clean $(MAKE) -C ./Example clean + +install: + $(MAKE) -C ./Library install PREFIX=$(PREFIX) + $(MAKE) -C ./Example install PREFIX=$(PREFIX) diff --git a/config.mk b/config.mk index c92f39f..4452ff4 100644 --- a/config.mk +++ b/config.mk @@ -1 +1,2 @@ SSL=YES +DEBUG=YES -- 2.43.0