summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-04-29 21:46:41 +0200
committerAki <please@ignore.pl>2020-04-29 21:46:41 +0200
commit48868c97a87740b43a16d59bb32adccfd6da379d (patch)
treeec302f76c17ab0d437a6b81bbed29a807c7a91a1
downloadplop-48868c97a87740b43a16d59bb32adccfd6da379d.zip
plop-48868c97a87740b43a16d59bb32adccfd6da379d.tar.gz
plop-48868c97a87740b43a16d59bb32adccfd6da379d.tar.bz2
Initial single and blocking server
-rw-r--r--plop.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/plop.c b/plop.c
new file mode 100644
index 0000000..d51871c
--- /dev/null
+++ b/plop.c
@@ -0,0 +1,75 @@
+#include <netdb.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+int main(int argc, char ** argv)
+{
+ struct addrinfo hints = {
+ .ai_family = AF_UNSPEC,
+ .ai_socktype = SOCK_STREAM,
+ .ai_flags = AI_PASSIVE,
+ };
+
+ struct addrinfo * result;
+ struct addrinfo * it;
+
+ if (2 != argc)
+ {
+ return 4;
+ }
+
+ if (0 != getaddrinfo(NULL, argv[1], &hints, &result))
+ {
+ return 1; // TODO: Handle errors properly
+ }
+
+ int server;
+
+ for (it = result; it != NULL; it = it->ai_next)
+ {
+ server = socket(it->ai_family, it->ai_socktype, it->ai_protocol);
+
+ if (-1 == server)
+ continue;
+
+ if (0 == bind(server, it->ai_addr,it->ai_addrlen))
+ break;
+
+ close(server);
+ }
+
+ if (it == NULL)
+ {
+ return 2; // TODO: Handle errors properly
+ }
+
+ freeaddrinfo(result);
+
+ if (-1 == listen(server, 5))
+ {
+ return 3; // TODO: Handle errors properly
+ }
+
+ int client;
+ static const char * response =
+ "HTTP/1.1 200 OK\r\n"
+ "Content-Type: text/plain; charset=utf-8\r\n"
+ "\r\n"
+ "plop\n";
+
+ for (;;)
+ {
+ if (-1 != (client = accept(server, NULL, NULL)))
+ {
+ if (-1 == send(client, response, strlen(response), 0))
+ {
+ // TODO: Handle errors properly
+ }
+
+ // Ignore the close-related error mess.
+ close(client);
+ }
+ }
+}