chibipub

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 1ead1d5bf6af5550782a16fa0c156b64bfca5fee
parent cf6ffb9d5a17ba2539f497c9395cfebb01d564f3
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 18 Nov 2020 10:58:36 -0800

updates

Diffstat:
Mwolfsocks.c | 54+++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 41 insertions(+), 13 deletions(-)

diff --git a/wolfsocks.c b/wolfsocks.c @@ -9,7 +9,7 @@ #include <netinet/in.h> #include <arpa/inet.h> -#define BUF_SIZE 1024 +#define BUF_SIZE 4096 void error(char *msg) { @@ -17,31 +17,44 @@ void error(char *msg) exit(1); } -int main(int argc, char *argv[]) +void parse_http_request() +{ +} + +void run_http_server() { - static const char buffer[BUF_SIZE]; - int client_len; + static char buffer[BUF_SIZE]; + ssize_t len, wrote; + socklen_t client_len; - FILE *stream; int parent, client; struct sockaddr_in server_addr, client_addr; struct hostent *client_host; char *client_addr_str; + int optval; + + const int port = 5188; if ((parent = socket(AF_INET, SOCK_STREAM, 0)) < 0) { error("socket"); } + optval = 1; + setsockopt(parent, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, + sizeof(optval)); + memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); - server_addr.sin_port = htons(5188); + server_addr.sin_port = htons(port); - if (bind(parent, (struct sockaddr *)&server_addr, + if (bind(parent, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) { error("bind"); } + printf("listening for activities on 0.0.0.0:%d\n", port); + if (listen(parent, 5) < 0) { error("listen"); } @@ -68,15 +81,30 @@ int main(int argc, char *argv[]) error("inet_htoa"); } - if ((stream = open(client, "r+")) == NULL) { - error("fdopen"); + while(1) { + len = read(client, (void*)buffer, BUF_SIZE); + if (len == 0) { + break; + } + + wrote = fwrite(buffer, 1, len, stdout); + if (wrote != len) { + printf("wrote %ld != bufsize %ld\n", wrote, len); + break; + } + + if (len != BUF_SIZE) { + break; + } } - fgets(buffer, BUF_SIZE, stream); - printf("%s\n", buffer); - fclose(stream); - exit(2); + close(client); } +} + +int main(int argc, char *argv[]) +{ + run_http_server(); return 0; }