Browse Source

[core] get port from sock_addr if AF_INET,AF_INET6

sock_addr_get_port() to get port from sock_addr if AF_INET or AF_INET6,
or else return 0
personal/stbuehler/mod-csrf
Glenn Strauss 5 years ago
parent
commit
55867b5602
  1. 24
      src/configfile-glue.c
  2. 19
      src/http-header-glue.c
  3. 15
      src/inet_ntop_cache.c
  4. 2
      src/inet_ntop_cache.h

24
src/configfile-glue.c

@ -213,14 +213,6 @@ int config_insert_values_global(server *srv, array *ca, const config_values_t cv
return config_insert_values_internal(srv, ca, cv, scope);
}
static unsigned short sock_addr_get_port(sock_addr *addr) {
#ifdef HAVE_IPV6
return ntohs(addr->plain.sa_family ? addr->ipv6.sin6_port : addr->ipv4.sin_port);
#else
return ntohs(addr->ipv4.sin_port);
#endif
}
static const char* cond_result_to_string(cond_result_t cond_result) {
switch (cond_result) {
case COND_RESULT_UNSET: return "unset";
@ -415,6 +407,7 @@ static cond_result_t config_check_cond_nocache(server *srv, connection *con, dat
switch (dc->comp) {
case COMP_HTTP_HOST: {
char *ck_colon = NULL, *val_colon = NULL;
unsigned short port;
if (!buffer_string_is_empty(con->uri.authority)) {
@ -427,6 +420,19 @@ static cond_result_t config_check_cond_nocache(server *srv, connection *con, dat
switch(dc->cond) {
case CONFIG_COND_NE:
case CONFIG_COND_EQ:
switch (srv_sock->addr.plain.sa_family) {
case AF_INET:
port = ntohs(srv_sock->addr.ipv4.sin_port);
break;
#ifdef HAVE_IPV6
case AF_INET6:
port = ntohs(srv_sock->addr.ipv6.sin6_port);
break;
#endif
default:
port = 0;
}
if (0 == port) break;
ck_colon = strchr(dc->string->ptr, ':');
val_colon = strchr(l->ptr, ':');
@ -434,7 +440,7 @@ static cond_result_t config_check_cond_nocache(server *srv, connection *con, dat
/* condition "host:port" but client send "host" */
buffer_copy_buffer(srv->cond_check_buf, l);
buffer_append_string_len(srv->cond_check_buf, CONST_STR_LEN(":"));
buffer_append_int(srv->cond_check_buf, sock_addr_get_port(&(srv_sock->addr)));
buffer_append_int(srv->cond_check_buf, port);
l = srv->cond_check_buf;
} else if (NULL != val_colon && NULL == ck_colon) {
/* condition "host" but client send "host:port" */

19
src/http-header-glue.c

@ -1419,7 +1419,6 @@ int http_cgi_headers (server *srv, connection *con, http_cgi_opts *opts, http_cg
/* CGI-SPEC 6.1.2, FastCGI spec 6.3 and SCGI spec */
int rc = 0;
unsigned short port;
server_socket *srv_sock = con->srv_socket;
const char *s;
size_t n;
@ -1562,14 +1561,7 @@ int http_cgi_headers (server *srv, connection *con, http_cgi_opts *opts, http_cg
}
addr = &srv_sock->addr;
#ifdef HAVE_IPV6
port = addr->plain.sa_family == AF_INET6
? addr->ipv6.sin6_port
: addr->ipv4.sin_port;
#else
port = addr->ipv4.sin_port;
#endif
li_utostrn(buf, sizeof(buf), ntohs(port));
li_utostrn(buf, sizeof(buf), sock_addr_get_port(addr));
rc |= cb(vdata, CONST_STR_LEN("SERVER_PORT"), buf, strlen(buf));
switch (addr->plain.sa_family) {
@ -1629,14 +1621,7 @@ int http_cgi_headers (server *srv, connection *con, http_cgi_opts *opts, http_cg
rc |= cb(vdata, CONST_STR_LEN("REMOTE_ADDR"),
CONST_BUF_LEN(con->dst_addr_buf));
#ifdef HAVE_IPV6
port = con->dst_addr.plain.sa_family == AF_INET6
? con->dst_addr.ipv6.sin6_port
: con->dst_addr.ipv4.sin_port;
#else
port = con->dst_addr.ipv4.sin_port;
#endif
li_utostrn(buf, sizeof(buf), ntohs(port));
li_utostrn(buf, sizeof(buf), sock_addr_get_port(&con->dst_addr));
rc |= cb(vdata, CONST_STR_LEN("REMOTE_PORT"), buf, strlen(buf));
for (n = 0; n < con->request.headers->used; n++) {

15
src/inet_ntop_cache.c

@ -9,6 +9,21 @@
#include <string.h>
unsigned short sock_addr_get_port (const sock_addr *addr)
{
switch (addr->plain.sa_family) {
case AF_INET:
return ntohs(addr->ipv4.sin_port);
#ifdef HAVE_IPV6
case AF_INET6:
return ntohs(addr->ipv6.sin6_port);
#endif
default: /* case AF_UNIX: */
return 0;
}
}
int sock_addr_inet_pton(sock_addr *addr, const char *str,
int family, unsigned short port)
{

2
src/inet_ntop_cache.h

@ -4,6 +4,8 @@
#include "base.h"
unsigned short sock_addr_get_port (const sock_addr *addr);
int sock_addr_inet_pton(sock_addr *addr, const char *str, int family, unsigned short port);
const char * sock_addr_inet_ntop(const sock_addr *addr, char *buf, socklen_t sz);

Loading…
Cancel
Save