Fix fd2fd return value which should not be -1 on error

This commit is contained in:
yrutschle 2019-05-13 15:21:22 +02:00
parent e528f519bc
commit d5baed3f18
3 changed files with 15 additions and 15 deletions

View File

@ -239,7 +239,7 @@ int bind_peer(int fd, int fd_from)
/* getpeername can fail with ENOTCONN if connection was dropped before we /* getpeername can fail with ENOTCONN if connection was dropped before we
* got here */ * got here */
res = getpeername(fd_from, from.ai_addr, &from.ai_addrlen); res = getpeername(fd_from, from.ai_addr, &from.ai_addrlen);
CHECK_RES_RETURN(res, "getpeername"); CHECK_RES_RETURN(res, "getpeername", res);
/* if the destination is the same machine, there's no need to do bind */ /* if the destination is the same machine, there's no need to do bind */
if (is_same_machine(&from)) if (is_same_machine(&from))
@ -251,16 +251,16 @@ int bind_peer(int fd, int fd_from)
#else #else
if (from.ai_addr->sa_family==AF_INET) { /* IPv4 */ if (from.ai_addr->sa_family==AF_INET) { /* IPv4 */
res = setsockopt(fd, IPPROTO_IP, IP_BINDANY, &trans, sizeof(trans)); res = setsockopt(fd, IPPROTO_IP, IP_BINDANY, &trans, sizeof(trans));
CHECK_RES_RETURN(res, "setsockopt IP_BINDANY"); CHECK_RES_RETURN(res, "setsockopt IP_BINDANY", res);
#ifdef IPV6_BINDANY #ifdef IPV6_BINDANY
} else { /* IPv6 */ } else { /* IPv6 */
res = setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &trans, sizeof(trans)); res = setsockopt(fd, IPPROTO_IPV6, IPV6_BINDANY, &trans, sizeof(trans));
CHECK_RES_RETURN(res, "setsockopt IPV6_BINDANY"); CHECK_RES_RETURN(res, "setsockopt IPV6_BINDANY", res);
#endif /* IPV6_BINDANY */ #endif /* IPV6_BINDANY */
} }
#endif /* IP_TRANSPARENT / IP_BINDANY */ #endif /* IP_TRANSPARENT / IP_BINDANY */
res = bind(fd, from.ai_addr, from.ai_addrlen); res = bind(fd, from.ai_addr, from.ai_addrlen);
CHECK_RES_RETURN(res, "bind"); CHECK_RES_RETURN(res, "bind", res);
return 0; return 0;
} }
@ -281,7 +281,7 @@ int connect_addr(struct connection *cnx, int fd_from)
from.ai_addrlen = sizeof(ss); from.ai_addrlen = sizeof(ss);
res = getpeername(fd_from, from.ai_addr, &from.ai_addrlen); res = getpeername(fd_from, from.ai_addr, &from.ai_addrlen);
CHECK_RES_RETURN(res, "getpeername"); CHECK_RES_RETURN(res, "getpeername", res);
for (a = cnx->proto->saddr; a; a = a->ai_next) { for (a = cnx->proto->saddr; a; a = a->ai_next) {
/* When transparent, make sure both connections use the same address family */ /* When transparent, make sure both connections use the same address family */
@ -304,7 +304,7 @@ int connect_addr(struct connection *cnx, int fd_from)
if (cfg.transparent) { if (cfg.transparent) {
res = bind_peer(fd, fd_from); res = bind_peer(fd, fd_from);
CHECK_RES_RETURN(res, "bind_peer"); CHECK_RES_RETURN(res, "bind_peer", res);
} }
res = connect(fd, a->ai_addr, a->ai_addrlen); res = connect(fd, a->ai_addr, a->ai_addrlen);
if (res == -1) { if (res == -1) {
@ -321,7 +321,7 @@ int connect_addr(struct connection *cnx, int fd_from)
} else { } else {
if (cnx->proto->keepalive) { if (cnx->proto->keepalive) {
res = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&one, sizeof(one)); res = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char*)&one, sizeof(one));
CHECK_RES_RETURN(res, "setsockopt(SO_KEEPALIVE)"); CHECK_RES_RETURN(res, "setsockopt(SO_KEEPALIVE)", res);
} }
return fd; return fd;
} }
@ -428,7 +428,7 @@ int fd2fd(struct queue *target_q, struct queue *from_q)
} }
} }
CHECK_RES_RETURN(size_r, "read"); CHECK_RES_RETURN(size_r, "read",FD_CNXCLOSED);
if (size_r == 0) if (size_r == 0)
return FD_CNXCLOSED; return FD_CNXCLOSED;
@ -453,7 +453,7 @@ int fd2fd(struct queue *target_q, struct queue *from_q)
return FD_STALLED; return FD_STALLED;
} }
CHECK_RES_RETURN(size_w, "write"); CHECK_RES_RETURN(size_w, "write", FD_CNXCLOSED);
return size_w; return size_w;
} }
@ -625,7 +625,7 @@ int check_access_rights(int in_socket, const char* service)
int res; int res;
res = getpeername(in_socket, &peer.saddr, &size); res = getpeername(in_socket, &peer.saddr, &size);
CHECK_RES_RETURN(res, "getpeername"); CHECK_RES_RETURN(res, "getpeername", res);
/* extract peer address */ /* extract peer address */
res = getnameinfo(&peer.saddr, size, addr_str, sizeof(addr_str), NULL, 0, NI_NUMERICHOST); res = getnameinfo(&peer.saddr, size, addr_str, sizeof(addr_str), NULL, 0, NI_NUMERICHOST);

View File

@ -42,10 +42,10 @@
exit(1); \ exit(1); \
} }
#define CHECK_RES_RETURN(res, str) \ #define CHECK_RES_RETURN(res, str, ret) \
if (res == -1) { \ if (res == -1) { \
log_message(LOG_CRIT, "%s:%d:%s:%d:%s\n", __FILE__, __LINE__, str, errno, strerror(errno)); \ log_message(LOG_CRIT, "%s:%d:%s:%d:%s\n", __FILE__, __LINE__, str, errno, strerror(errno)); \
return res; \ return ret; \
} }
#define CHECK_ALLOC(a, str) \ #define CHECK_ALLOC(a, str) \

View File

@ -44,12 +44,12 @@ int set_nonblock(int fd)
int flags; int flags;
flags = fcntl(fd, F_GETFL); flags = fcntl(fd, F_GETFL);
CHECK_RES_RETURN(flags, "fcntl"); CHECK_RES_RETURN(flags, "fcntl", -1);
flags |= O_NONBLOCK; flags |= O_NONBLOCK;
flags = fcntl(fd, F_SETFL, flags); flags = fcntl(fd, F_SETFL, flags);
CHECK_RES_RETURN(flags, "fcntl"); CHECK_RES_RETURN(flags, "fcntl", -1);
return flags; return flags;
} }
@ -93,7 +93,7 @@ int accept_new_connection(int listen_socket, struct connection *cnx[], int* cnx_
struct connection *new; struct connection *new;
in_socket = accept(listen_socket, 0, 0); in_socket = accept(listen_socket, 0, 0);
CHECK_RES_RETURN(in_socket, "accept"); CHECK_RES_RETURN(in_socket, "accept", -1);
if (!fd_is_in_range(in_socket)) { if (!fd_is_in_range(in_socket)) {
close(in_socket); close(in_socket);