Fixed multiple port support and improved it

This commit is contained in:
2022-06-28 07:57:15 -05:00
parent 47cf044858
commit d843ffab93
8 changed files with 119 additions and 23 deletions

View File

@@ -168,6 +168,15 @@ int sendResponse(Connection *conn, Sitefile *site) {
continue;
if (fullmatch(&site->content[i].host, host))
continue;
{
int j;
const unsigned short currport = site->ports[conn->portind].num;
for (j = 0; j < site->content[i].portcount; ++j)
if (site->content[i].ports[j] == currport)
goto foundport;
continue;
}
foundport:
if (fullmatch(&site->content[i].path, conn->path.data) == 0) {
switch (site->content[i].command) {
case READ:

View File

@@ -174,6 +174,45 @@ error:
return ERROR;
}
static char *getport(char *data, unsigned short *ret) {
*ret = 0;
while (isdigit(data[0])) {
*ret *= 10;
*ret += data[0] - '0';
++data;
}
if (data[0] == ',')
return data + 1;
if (data[0] != '\0')
return NULL;
return data;
}
static int getports(unsigned short **ports, int *portcount, char *data) {
int alloc;
alloc = 10;
*portcount = 0;
*ports = xmalloc(alloc * sizeof **ports);
for (;;) {
if (data[0] == '\0')
return 0;
if (*portcount >= alloc) {
alloc *= 2;
*ports = xrealloc(*ports, alloc * sizeof **ports);
}
{
unsigned short newport;
data = getport(data, &newport);
(*ports)[*portcount] = newport;
++*portcount;
}
if (data == NULL) {
free(*ports);
return 1;
}
}
}
Sitefile *parseSitefile(char *path) {
FILE *file;
RequestType respondto = GET;
@@ -182,14 +221,18 @@ Sitefile *parseSitefile(char *path) {
int argc;
char **argv;
Sitefile *ret;
unsigned short currport;
unsigned short *ports;
int portcount;
currport = 80;
file = fopen(path, "r");
if (file == NULL)
return NULL;
ret = xmalloc(sizeof *ret);
ports = malloc(sizeof *ports);
ports[0] = 80;
portcount = 1;
ret->size = 0;
ret->alloc = 50;
ret->content = xmalloc(ret->alloc * sizeof *ret->content);
@@ -205,6 +248,7 @@ Sitefile *parseSitefile(char *path) {
switch (status) {
int i;
case FILE_END:
free(ports);
for (i = 0; i < ret->portcount; ++i) {
Port *port = ret->ports + i;
if (port->type == TLS &&
@@ -232,8 +276,14 @@ Sitefile *parseSitefile(char *path) {
}
else if (strcmp(argv[1], "host") == 0)
host = xstrdup(argv[2]);
else if (strcmp(argv[1], "port") == 0)
currport = atoi(argv[2]);
else if (strcmp(argv[1], "port") == 0) {
free(ports);
if (getports(&ports, &portcount, argv[2])) {
fprintf(stderr, "Invalid port list %s\n",
argv[2]);
goto error;
}
}
else
goto error;
continue;
@@ -361,7 +411,12 @@ Sitefile *parseSitefile(char *path) {
regcomp(&ret->content[ret->size].host, ".*", cflags);
else
regcomp(&ret->content[ret->size].host, host, cflags);
ret->content[ret->size].port = currport;
ret->content[ret->size].ports = xmalloc(portcount *
sizeof *ret->content[ret->size].ports);
memcpy(ret->content[ret->size].ports, ports, portcount * sizeof *ports);
ret->content[ret->size].portcount = portcount;
ret->size++;
}
error:

View File

@@ -143,22 +143,30 @@ Stream *createStream(Context *context, int flags, int fd) {
case TCP: default:
break;
case TLS:
if (gnutls_init(&ret->session, GNUTLS_SERVER) < 0)
if (gnutls_init(&ret->session, GNUTLS_SERVER) < 0) {
createErrorLog("gnutls_init() failed", errno);
goto error;
}
if (gnutls_priority_set(ret->session,
context->priority) < 0)
context->priority) < 0) {
createErrorLog("gnutls_priority_set() failed", errno);
goto error;
}
if (gnutls_credentials_set(ret->session,
GNUTLS_CRD_CERTIFICATE,
context->creds) < 0)
context->creds) < 0) {
createErrorLog("gnutls_credentials_set() failed", errno);
goto error;
}
gnutls_certificate_server_set_request(ret->session,
GNUTLS_CERT_IGNORE);
gnutls_handshake_set_timeout(ret->session,
GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT);
gnutls_transport_set_int(ret->session, ret->fd);
if (gnutls_handshake(ret->session) < 0)
if (gnutls_handshake(ret->session) < 0) {
createErrorLog("gnutls_handshake() failed", errno);
goto error;
}
break;
}
return ret;

View File

@@ -43,7 +43,8 @@ typedef struct {
Command command;
regex_t path;
char *arg;
unsigned short port;
unsigned short *ports;
int portcount;
} SiteCommand;
typedef struct {