Close #36 增加IPv6支持
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#include "tfe_types.h"
|
||||
#include "tfe_utils.h"
|
||||
const char* tfe_stream_conn_dir_to_str(enum tfe_conn_dir dir)
|
||||
const char * tfe_stream_conn_dir_to_str(enum tfe_conn_dir dir)
|
||||
{
|
||||
return (dir==CONN_DIR_DOWNSTREAM)?"downstream":"upstream";
|
||||
return (dir == CONN_DIR_DOWNSTREAM) ? "downstream" : "upstream";
|
||||
}
|
||||
|
||||
char * tfe_stream_addr_to_str(const struct tfe_stream_addr * addr)
|
||||
@@ -21,7 +21,7 @@ char * tfe_stream_addr_to_str(const struct tfe_stream_addr * addr)
|
||||
asprintf(&__str_ret, "%s %u %s %u", __src_addr, __src_port, __dst_addr, __dst_port);
|
||||
}
|
||||
|
||||
if(addr->addrtype == TFE_ADDR_STREAM_TUPLE4_V6)
|
||||
if (addr->addrtype == TFE_ADDR_STREAM_TUPLE4_V6)
|
||||
{
|
||||
const struct tfe_stream_addr_tuple4_v6 * tuple4_v6 = addr->tuple4_v6;
|
||||
char __src_addr[INET6_ADDRSTRLEN];
|
||||
@@ -36,11 +36,11 @@ char * tfe_stream_addr_to_str(const struct tfe_stream_addr * addr)
|
||||
|
||||
return __str_ret;
|
||||
}
|
||||
void tfe_stream_addr_free(struct tfe_stream_addr *addr)
|
||||
void tfe_stream_addr_free(struct tfe_stream_addr * addr)
|
||||
{
|
||||
free(addr);
|
||||
return;
|
||||
}
|
||||
|
||||
struct tfe_stream_addr * tfe_stream_addr_create_by_fd(int fd, enum tfe_conn_dir dir)
|
||||
{
|
||||
struct tfe_stream_addr * __stream_addr = NULL;
|
||||
@@ -52,40 +52,35 @@ struct tfe_stream_addr * tfe_stream_addr_create_by_fd(int fd, enum tfe_conn_dir
|
||||
struct sockaddr_storage sk_dst_storage{};
|
||||
struct sockaddr * sk_dst_ptr = (struct sockaddr *) &sk_dst_storage;
|
||||
socklen_t sk_dst_len = sizeof(sk_dst_storage);
|
||||
if(dir==CONN_DIR_UPSTREAM)
|
||||
{
|
||||
int ret = getsockname(fd, sk_src_ptr, &sk_src_len);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto __errout;
|
||||
}
|
||||
|
||||
ret = getpeername(fd, sk_dst_ptr, &sk_dst_len);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto __errout;
|
||||
}
|
||||
}
|
||||
else
|
||||
switch(dir)
|
||||
{
|
||||
int ret = getsockname(fd, sk_dst_ptr, &sk_dst_len);
|
||||
if (ret < 0)
|
||||
case CONN_DIR_UPSTREAM:
|
||||
{
|
||||
goto __errout;
|
||||
int ret = getsockname(fd, sk_src_ptr, &sk_src_len);
|
||||
if (ret < 0) goto __errout;
|
||||
|
||||
ret = getpeername(fd, sk_dst_ptr, &sk_dst_len);
|
||||
if (ret < 0) goto __errout;
|
||||
break;
|
||||
}
|
||||
|
||||
ret = getpeername(fd, sk_src_ptr, &sk_src_len);
|
||||
if (ret < 0)
|
||||
case CONN_DIR_DOWNSTREAM:
|
||||
{
|
||||
goto __errout;
|
||||
int ret = getsockname(fd, sk_dst_ptr, &sk_dst_len);
|
||||
if (ret < 0) goto __errout;
|
||||
|
||||
ret = getpeername(fd, sk_src_ptr, &sk_src_len);
|
||||
if (ret < 0) goto __errout;
|
||||
break;
|
||||
}
|
||||
default: { assert(0); goto __errout;}
|
||||
}
|
||||
|
||||
assert(sk_src_ptr->sa_family == sk_dst_ptr->sa_family);
|
||||
if (sk_src_ptr->sa_family == AF_INET)
|
||||
{
|
||||
__stream_addr = (struct tfe_stream_addr *) malloc(
|
||||
sizeof(struct tfe_stream_addr) + sizeof(struct tfe_stream_addr_tuple4_v4));
|
||||
__stream_addr = (struct tfe_stream_addr *) malloc(sizeof(struct tfe_stream_addr) +
|
||||
sizeof(struct tfe_stream_addr_tuple4_v4));
|
||||
|
||||
struct tfe_stream_addr_ipv4 * st_addr_v4 = __stream_addr->ipv4;
|
||||
struct sockaddr_in * sk_v4_src_ptr = (struct sockaddr_in *) sk_src_ptr;
|
||||
@@ -101,30 +96,39 @@ struct tfe_stream_addr * tfe_stream_addr_create_by_fd(int fd, enum tfe_conn_dir
|
||||
}
|
||||
else if (sk_src_ptr->sa_family == AF_INET6)
|
||||
{
|
||||
assert(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
goto __errout;
|
||||
}
|
||||
__stream_addr = (struct tfe_stream_addr *) malloc(sizeof(struct tfe_stream_addr) +
|
||||
sizeof(struct tfe_stream_addr_tuple4_v6));
|
||||
|
||||
struct tfe_stream_addr_ipv6 * st_addr_v6 = __stream_addr->ipv6;
|
||||
struct sockaddr_in6 * sk_v6_src_ptr = (struct sockaddr_in6 *) sk_src_ptr;
|
||||
struct sockaddr_in6 * sk_v6_dst_ptr = (struct sockaddr_in6 *) sk_dst_ptr;
|
||||
|
||||
__stream_addr->addrtype = TFE_ADDR_STREAM_TUPLE4_V6;
|
||||
__stream_addr->addrlen = sizeof(struct tfe_stream_addr_tuple4_v4);
|
||||
|
||||
st_addr_v6->saddr = sk_v6_src_ptr->sin6_addr;
|
||||
st_addr_v6->source = sk_v6_src_ptr->sin6_port;
|
||||
st_addr_v6->daddr = sk_v6_dst_ptr->sin6_addr;
|
||||
st_addr_v6->dest = sk_v6_dst_ptr->sin6_port;
|
||||
}
|
||||
else { assert(0); goto __errout; }
|
||||
return __stream_addr;
|
||||
|
||||
__errout:
|
||||
if (__stream_addr != NULL) free(__stream_addr);
|
||||
return NULL;
|
||||
}
|
||||
char* tfe_string_addr_create_by_fd(int fd, enum tfe_conn_dir dir)
|
||||
char * tfe_string_addr_create_by_fd(int fd, enum tfe_conn_dir dir)
|
||||
{
|
||||
char* addr_str=NULL;
|
||||
struct tfe_stream_addr * stream_addr=tfe_stream_addr_create_by_fd(fd, dir);
|
||||
if(stream_addr)
|
||||
char * addr_str = NULL;
|
||||
struct tfe_stream_addr * stream_addr = tfe_stream_addr_create_by_fd(fd, dir);
|
||||
if (stream_addr)
|
||||
{
|
||||
addr_str= tfe_stream_addr_to_str(stream_addr);
|
||||
addr_str = tfe_stream_addr_to_str(stream_addr);
|
||||
}
|
||||
else
|
||||
{
|
||||
addr_str=tfe_strdup("null");
|
||||
addr_str = tfe_strdup("null");
|
||||
}
|
||||
tfe_stream_addr_free(stream_addr);
|
||||
return addr_str;
|
||||
|
||||
Reference in New Issue
Block a user