@ -21,6 +21,7 @@
# include <sys/socket.h>
# include <sys/socket.h>
# include <unistd.h>
# include <unistd.h>
# include <net/if.h>
# include <net/if.h>
# include <poll.h>
# elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
# elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
# include <sys/socket.h>
# include <sys/socket.h>
# include <net/route.h>
# include <net/route.h>
@ -45,7 +46,7 @@ struct nl_req {
} ;
} ;
static int netlink_route ( int ifindex , uint32_t network , uint8_t prefix_len , int cmd , int flags ) {
static int netlink_route ( int ifindex , uint32_t network , uint8_t prefix_len , int cmd , int flags ) {
int fd = socket ( AF_NETLINK , SOCK_DGRAM , NETLINK_ROUTE ) ;
int fd = socket ( AF_NETLINK , SOCK_DGRAM | SOCK_NONBLOCK , NETLINK_ROUTE ) ;
if ( fd < 0 ) {
if ( fd < 0 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_TUN , " Failed to create netlink socket: %s " , strerror ( errno ) ) ;
DEBUG_ERROR ( DEBUG_CATEGORY_TUN , " Failed to create netlink socket: %s " , strerror ( errno ) ) ;
return - 1 ;
return - 1 ;
@ -87,6 +88,19 @@ static int netlink_route(int ifindex, uint32_t network, uint8_t prefix_len, int
return - 1 ;
return - 1 ;
}
}
// Wait for response with timeout using poll
struct pollfd pfd = { . fd = fd , . events = POLLIN } ;
int poll_ret = poll ( & pfd , 1 , 1000 ) ; // 1 second timeout
if ( poll_ret < 0 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_TUN , " poll() failed: %s " , strerror ( errno ) ) ;
close ( fd ) ;
return - 1 ;
} else if ( poll_ret = = 0 ) {
DEBUG_ERROR ( DEBUG_CATEGORY_TUN , " Timeout waiting for netlink response " ) ;
close ( fd ) ;
return - 1 ;
}
// Receive response
// Receive response
char reply [ 4096 ] ;
char reply [ 4096 ] ;
ssize_t len = recv ( fd , reply , sizeof ( reply ) , 0 ) ;
ssize_t len = recv ( fd , reply , sizeof ( reply ) , 0 ) ;