Check for socket exhaustion and fix memory leak

This commit is contained in:
ValdikSS
2017-04-12 23:13:01 +03:00
parent 332769d700
commit 1f8f9b591b

15
net.c
View File

@@ -68,7 +68,7 @@ int bindsocket( host_t *sock_h) {
fd = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP );
}
if( -1 == bind( fd, (struct sockaddr*)sock_h->sock, sock_h->size ) ) {
if( ! ( fd >= 0 && -1 != bind( fd, (struct sockaddr*)sock_h->sock, sock_h->size ) ) ) {
err = 1;
}
@@ -270,14 +270,11 @@ void handle_inside(int inside, host_t *listen_h, host_t *bind_h, host_t *dst_h)
len = recvfrom( inside, buffer, sizeof( buffer ), 0,
(struct sockaddr*)src, (socklen_t *)&size );
if(len > 0) {
if(listen_h->is_v6)
src_h = get_host(NULL, 0, NULL, (struct sockaddr_in6 *)src);
else
src_h = get_host(NULL, 0, (struct sockaddr_in *)src, NULL);
free(src);
if(len > 0) {
/* do we know it ? */
client = client_find_src(src_h);
if(client != NULL) {
@@ -293,6 +290,7 @@ void handle_inside(int inside, host_t *listen_h, host_t *bind_h, host_t *dst_h)
else {
client_seen(client);
}
host_clean(src_h);
}
else {
/* unknown client, open new out socket */
@@ -301,7 +299,7 @@ void handle_inside(int inside, host_t *listen_h, host_t *bind_h, host_t *dst_h)
verb_prbind(bind_h);
output = bindsocket(bind_h);
if (output >= 0) {
/* send req out */
if(sendto(output, buffer, len, 0, (struct sockaddr*)dst_h->sock, dst_h->size) < 0) {
fprintf(stderr, "unable to forward to %s:%d\n", dst_h->ip, dst_h->port);
@@ -328,8 +326,13 @@ void handle_inside(int inside, host_t *listen_h, host_t *bind_h, host_t *dst_h)
client_add(client);
}
}
else {
host_clean(src_h);
}
}
}
free(src);
}
/* handle answer from the outside */
void handle_outside(int inside, int outside, host_t *outside_h) {