added listen addr option

This commit is contained in:
Pin
2022-02-21 00:01:54 -05:00
parent 1c083b90f8
commit 2999c1973e
5 changed files with 78 additions and 16 deletions

View File

@@ -27,7 +27,7 @@ int parseHTTPRequest(char buffer[], struct HTTPRequest *r) {
char temp[1]; // Used to check newlines
char *token = calloc(8, sizeof(char));
int line = 0;
char *checkLine = calloc(1000, sizeof(char));;
char *checkLine = calloc(1000, sizeof(char));
for (int i = 0; i < strlen(buffer); i++) {
temp[0] = buffer[i];
@@ -98,8 +98,14 @@ int main(int argc, char **argv) {
int checkerr = 0; // Used for error checking
int addrlen = sizeof(address);
char *certFile = malloc(0);
char *privKeyFile = malloc(0);
char *certFile = malloc(1);
char *privKeyFile = malloc(1);
certFile[0] = '\0';
privKeyFile[0] = '\0';
uint32_t listenAddrNum = -1;
char *listenAddr = malloc(1);
listenAddr[0] = '\0';
SSL_CTX *ctx = NULL;
@@ -109,6 +115,7 @@ int main(int argc, char **argv) {
static const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"port", required_argument, NULL, 'p'},
{"listen", required_argument, NULL, 'l'},
{"cert", required_argument, NULL, 'c'},
{"privkey", required_argument, NULL, 'k'},
{"verbose", no_argument, &verbose_flag, 1},
@@ -119,6 +126,7 @@ int main(int argc, char **argv) {
"Usage: seaweb [options]\n\n"
" -h --help\t\t\tShows this message\n"
" -p --port\t\t\tStarts webserver on passed port\n"
" -l --listen\t\t\tDefines what addr to listen on (default 0.0.0.0)\n"
" -c --cert\t\t\tPath to certificate\n"
" -k --privkey\t\t\tPath to private key\n"
"\n"
@@ -129,7 +137,7 @@ int main(int argc, char **argv) {
int c;
while (1) {
int option_index = 0;
c = getopt_long(argc, argv, "hp:c:k:", long_options, &option_index);
c = getopt_long(argc, argv, "hp:l:c:k:", long_options, &option_index);
if(c == -1) { // Break if no more options are present to parse
break;
}
@@ -140,6 +148,14 @@ int main(int argc, char **argv) {
case 'p':
sscanf(optarg, "%d", &port);
break;
case 'l':
listenAddr = calloc(strlen(optarg), sizeof(char));
strcpy(listenAddr, optarg);
listenAddrNum = addr2sin_addr(listenAddr);
if (listenAddrNum == -1) {
exit(EXIT_FAILURE);
}
break;
case 'c':
certFile = calloc(strlen(optarg), sizeof(char));
strcpy(certFile, optarg);
@@ -172,13 +188,14 @@ int main(int argc, char **argv) {
if ( enableHTTPS == 1 ) {
printf("Opening secure socket on port: %d\n", port);
checkerr = createSecureSocket(port, &server_fd, &address, &addrlen, &ctx, certFile, privKeyFile);
checkerr = createSecureSocket(port, &server_fd, &address, &addrlen, listenAddrNum,
&ctx, certFile, privKeyFile);
if ( ctx == NULL ) {
printf("Error creating ctx\n");
}
} else {
printf("Opening socket on port: %d\n", port);
checkerr = createSocket(port, &server_fd, &address, &addrlen);
checkerr = createSocket(port, &server_fd, &address, &addrlen, listenAddrNum);
}
if (checkerr != 0) {
@@ -210,6 +227,7 @@ int main(int argc, char **argv) {
free(privKeyFile);
free(certFile);
free(listenAddr);
close(server_fd);
exit(EXIT_SUCCESS);
}