From 152cd33d83bdeda2950162c382f4dd83d05ef89a Mon Sep 17 00:00:00 2001 From: Felicia Seo Date: Sun, 11 Dec 2022 18:51:41 -0500 Subject: [PATCH] 95/95git add http.c socket.c! --- src/http.c | 27 +++++++++++++++++++++++--- src/socket.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/src/http.c b/src/http.c index 610003a..694b2b0 100644 --- a/src/http.c +++ b/src/http.c @@ -123,12 +123,19 @@ http_process_headers(struct http_transaction *ta) * are zero-terminated strings. */ if (!strcasecmp(field_name, "Cookie")) { - if ((field_value == NULL) || (strlen(field_value) <= 11)) + + char* cookie2; + char* cookie1 = strtok_r(field_value, ";", &cookie2); + cookie2++; + + ta->extra_cookie = cookie2; + + if ((cookie1 == NULL) || (strlen(cookie1) <= 11)) { ta->resp_status = HTTP_PERMISSION_DENIED; return false; } - ta->token = field_value + 11; // + 11 gets rid of "auth_token=" heading. + ta->token = cookie1 + 11; // + 11 gets rid of "auth_token=" heading. ta->valid_token = validate_token_exp(ta, ta->token); } @@ -664,7 +671,21 @@ handle_private(struct http_transaction *ta) ta->resp_status = HTTP_OK; return handle_static_asset(ta, server_root); } - return send_error(ta, HTTP_PERMISSION_DENIED, "invalid token"); + + + if ((ta->extra_cookie == NULL) || (strlen(ta->extra_cookie) <= 11)) { + return send_error(ta, HTTP_PERMISSION_DENIED, "???"); + } + + ta->token = ta->extra_cookie + 11; // + 11 gets rid of "auth_token=" heading. + ta->valid_token = validate_token_exp(ta, ta->token); + + if (ta->valid_token) { + ta->resp_status = HTTP_OK; + return handle_static_asset(ta, server_root); + } + + return send_error(ta, HTTP_PERMISSION_DENIED, "???"); } /* Handle a single HTTP transaction. Returns true on success. */ diff --git a/src/socket.c b/src/socket.c index 0e59813..b38516d 100644 --- a/src/socket.c +++ b/src/socket.c @@ -54,6 +54,7 @@ socket_open_bind_listen(char * port_number_string, int backlog) } char printed_addr[1024]; + for (pinfo = info; pinfo; pinfo = pinfo->ai_next) { assert (pinfo->ai_protocol == IPPROTO_TCP); int rc = getnameinfo(pinfo->ai_addr, pinfo->ai_addrlen, @@ -73,8 +74,58 @@ socket_open_bind_listen(char * port_number_string, int backlog) /* Skip any non-IPv4 addresses. * Adding support for protocol independence/IPv6 is part of the project. */ - //if (pinfo->ai_family != AF_INET) - //continue; + if (pinfo->ai_family != AF_INET6) + continue; + + int s = socket(pinfo->ai_family, pinfo->ai_socktype, pinfo->ai_protocol); + if (s == -1) { + perror("socket"); + return -1; + } + + // See https://stackoverflow.com/a/3233022 for a good explanation of what this does + int opt = 1; + setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)); + + rc = bind(s, pinfo->ai_addr, pinfo->ai_addrlen); + if (rc == -1) { + perror("bind"); + close(s); + return -1; + } + + rc = listen(s, backlog); + if (rc == -1) { + perror("listen"); + close(s); + return -1; + } + + freeaddrinfo(info); + return s; + } + + for (pinfo = info; pinfo; pinfo = pinfo->ai_next) { + assert (pinfo->ai_protocol == IPPROTO_TCP); + int rc = getnameinfo(pinfo->ai_addr, pinfo->ai_addrlen, + printed_addr, sizeof printed_addr, NULL, 0, + NI_NUMERICHOST); + if (rc != 0) { + fprintf(stderr, "getnameinfo error: %s\n", gai_strerror(rc)); + return -1; + } + + /* Uncomment this to see the address returned + printf("%s: %s\n", pinfo->ai_family == AF_INET ? "AF_INET" : + pinfo->ai_family == AF_INET6 ? "AF_INET6" : "?", + printed_addr); + */ + + /* Skip any non-IPv4 addresses. + * Adding support for protocol independence/IPv6 is part of the project. + */ + if (pinfo->ai_family != AF_INET) + continue; int s = socket(pinfo->ai_family, pinfo->ai_socktype, pinfo->ai_protocol); if (s == -1) {