From 26aeb38a8160c76b5085e2e44d325dbc1401b769 Mon Sep 17 00:00:00 2001 From: Emil Mikulic Date: Thu, 20 Feb 2003 04:25:12 +0000 Subject: [PATCH] Initial revision - open and close a socket. --- trunk/Makefile | 12 +++++++++ trunk/darkhttpd.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 trunk/Makefile create mode 100644 trunk/darkhttpd.c diff --git a/trunk/Makefile b/trunk/Makefile new file mode 100644 index 0000000..f2e27c1 --- /dev/null +++ b/trunk/Makefile @@ -0,0 +1,12 @@ +CFLAGS = -pipe -ansi -pedantic -g \ +-Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes \ +-Wpointer-arith -Winline -Wcast-qual -W \ +-Wbad-function-cast -Wcast-align -Wchar-subscripts -Wshadow \ +-Wwrite-strings -Wmissing-prototypes + +all: + gcc -o out $(CFLAGS) darkhttpd.c + +splint: + splint -skipposixheaders +posixlib +stats darkhttpd.c + diff --git a/trunk/darkhttpd.c b/trunk/darkhttpd.c new file mode 100644 index 0000000..ddc2909 --- /dev/null +++ b/trunk/darkhttpd.c @@ -0,0 +1,66 @@ +/* + * darkhttpd + * copyright (c) 2003 Emil Mikulic. + * $Id$ + */ + +#include +#include +#include +#include +#include +#include + + + +/* + * Defaults - these can be overridden by various command-line arguments. + * Run `darkhttpd --help' for help. + */ +#define DEFAULT_PORT 80 +#define DEFAULT_BINDADDR INADDR_ANY +#define DEFAULT_MAXCONN -1 /* kern.ipc.somaxconn */ + +/*@unused@*/ +static const char server_version[] = "darkhttpd/0.1"; + + + +static int sockin; + +/*int main(int argc, char *argv[])*/ +int main() +{ + struct sockaddr_in addrin; + int sockopt; + + /* create incoming socket */ + sockin = socket(PF_INET, SOCK_STREAM, 0); + if (sockin == -1) err(1, "socket()"); + + /* reuse address */ + sockopt = 1; + if (setsockopt(sockin, SOL_SOCKET, SO_REUSEADDR, + &sockopt, sizeof(sockopt)) == -1) + err(1, "setsockopt(SO_REUSEADDR)"); + + /* bind socket */ + addrin.sin_family = (u_char)PF_INET; + addrin.sin_port = htons(DEFAULT_PORT); + addrin.sin_addr.s_addr = DEFAULT_BINDADDR; + memset(&(addrin.sin_zero), 0, 8); + if (bind(sockin, (struct sockaddr *)&addrin, + sizeof(struct sockaddr)) == -1) + err(1, "bind()"); + + /* listen on socket */ + if (listen(sockin, DEFAULT_MAXCONN) == -1) + err(1, "listen()"); + + /* --- do stuff here --- */ + + (void) close(sockin); + return 0; +} + +/* vim:set tabstop=4 shiftwidth=4 expandtab tw=78: */