diff --git a/libs/esl/Makefile b/libs/esl/Makefile index ea4d9e2877..01065f358c 100644 --- a/libs/esl/Makefile +++ b/libs/esl/Makefile @@ -3,24 +3,28 @@ INCS=-I$(PWD)/src/include LIBEDIT_DIR=../../libs/libedit CFLAGS=$(INCS) -g -ggdb -I$(LIBEDIT_DIR)/src/ MYLIB=libesl.a - +LIBS=-lesl -lncurses -lpthread +LDFLAGS=-L. OBJS=src/esl.o src/esl_event.o src/esl_threadmutex.o src/esl_config.o -all: $(MYLIB) fs_cli +all: $(MYLIB) fs_cli testclient testserver $(MYLIB): $(OBJS) $(HEADERS) ar rcs $(MYLIB) $(OBJS) ranlib $(MYLIB) +testserver: $(MYLIB) testserver.c + $(CC) $(CC_CFLAGS) $(CFLAGS) testserver.c -o testserver $(LDFLAGS) $(LIBS) + +testclient: $(MYLIB) testclient.c + $(CC) $(CC_CFLAGS) $(CFLAGS) testclient.c -o testclient $(LDFLAGS) $(LIBS) + +fs_cli: $(MYLIB) fs_cli.c + $(CC) $(CC_CFLAGS) $(CFLAGS) fs_cli.c -o fs_cli $(LDFLAGS) -L$(LIBEDIT_DIR)/src/.libs $(LIBS) -ledit + %.o: %.c $(CC) $(CC_CFLAGS) $(CFLAGS) -c $< -o $@ -test: $(MYLIB) - $(CC) $(CC_CFLAGS) $(CFLAGS) testclient.c -o testclient -L. -lesl - -fs_cli: $(MYLIB) fs_cli.c - $(CC) $(CC_CFLAGS) $(CFLAGS) fs_cli.c -o fs_cli -L. -L$(LIBEDIT_DIR)/src/.libs -lesl -ledit -lncurses -lpthread - clean: - rm -f *.o src/*.o testclient fs_cli libesl.a *~ src/*~ src/include/*~ + rm -f *.o src/*.o testclient testserver fs_cli libesl.a *~ src/*~ src/include/*~ diff --git a/libs/esl/src/esl.c b/libs/esl/src/esl.c index 0f7ea83b28..b5b8f04e11 100644 --- a/libs/esl/src/esl.c +++ b/libs/esl/src/esl.c @@ -323,6 +323,59 @@ char *esl_url_decode(char *s) } +esl_status_t esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback) +{ + esl_socket_t server_sock = ESL_SOCK_INVALID; + int reuse_addr = 1; + struct sockaddr_in addr; + esl_status_t status = ESL_SUCCESS; + + if ((server_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { + return ESL_FAIL; + } + + setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr)); + + memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = htonl(INADDR_ANY); + addr.sin_port = htons(port); + + if (bind(server_sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + status = ESL_FAIL; + goto end; + } + + if (listen(server_sock, 10000) < 0) { + status = ESL_FAIL; + goto end; + } + + for (;;) { + int client_sock; + struct sockaddr_in echoClntAddr; + unsigned int clntLen; + + clntLen = sizeof(echoClntAddr); + + if ((client_sock = accept(server_sock, (struct sockaddr *) &echoClntAddr, &clntLen)) == ESL_SOCK_INVALID) { + status = ESL_FAIL; + goto end; + } + + callback(server_sock, client_sock, echoClntAddr); + } + + end: + + if (server_sock != ESL_SOCK_INVALID) { + close(server_sock); + server_sock = ESL_SOCK_INVALID; + } + + return status; + +} esl_status_t esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password) { diff --git a/libs/esl/src/include/esl.h b/libs/esl/src/include/esl.h index 412c4c322c..4aec7e0d40 100644 --- a/libs/esl/src/include/esl.h +++ b/libs/esl/src/include/esl.h @@ -256,6 +256,9 @@ char *esl_url_decode(char *s); int esl_toupper(int c); int esl_tolower(int c); +typedef void (*esl_listen_callback_t)(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in addr); + +esl_status_t esl_listen(const char *host, esl_port_t port, esl_listen_callback_t callback); esl_status_t esl_connect(esl_handle_t *handle, const char *host, esl_port_t port, const char *password); esl_status_t esl_disconnect(esl_handle_t *handle);