Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
+16
-9
@@ -74,6 +74,8 @@ static struct in_addr BIP_Address;
|
|||||||
static struct in_addr BIP_Broadcast_Addr;
|
static struct in_addr BIP_Broadcast_Addr;
|
||||||
/* enable debugging */
|
/* enable debugging */
|
||||||
static bool BIP_Debug = false;
|
static bool BIP_Debug = false;
|
||||||
|
/* interface name */
|
||||||
|
static char BIP_Interface_Name[IF_NAMESIZE] = { 0 };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Print the IPv4 address with debug info
|
* @brief Print the IPv4 address with debug info
|
||||||
@@ -628,14 +630,13 @@ static void parseRoutes(struct nlmsghdr *nlHdr, struct route_info *rtInfo)
|
|||||||
*/
|
*/
|
||||||
static char *ifname_default(void)
|
static char *ifname_default(void)
|
||||||
{
|
{
|
||||||
static char ifName[IF_NAMESIZE] = { 0 };
|
|
||||||
struct nlmsghdr *nlMsg = NULL;
|
struct nlmsghdr *nlMsg = NULL;
|
||||||
struct route_info *rtInfo = NULL;
|
struct route_info *rtInfo = NULL;
|
||||||
char msgBuf[8192] = { 0 };
|
char msgBuf[8192] = { 0 };
|
||||||
int sock, len, msgSeq = 0;
|
int sock, len, msgSeq = 0;
|
||||||
|
|
||||||
if (ifName[0] != 0) {
|
if (BIP_Interface_Name[0] != 0) {
|
||||||
return ifName;
|
return BIP_Interface_Name;
|
||||||
}
|
}
|
||||||
/* Create Socket */
|
/* Create Socket */
|
||||||
if ((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
|
if ((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) {
|
||||||
@@ -657,13 +658,13 @@ static char *ifname_default(void)
|
|||||||
/* Send the request */
|
/* Send the request */
|
||||||
if (send(sock, nlMsg, nlMsg->nlmsg_len, 0) < 0) {
|
if (send(sock, nlMsg, nlMsg->nlmsg_len, 0) < 0) {
|
||||||
fprintf(stderr, "BIP: Write To Socket Failed...\n");
|
fprintf(stderr, "BIP: Write To Socket Failed...\n");
|
||||||
return ifName;
|
return BIP_Interface_Name;
|
||||||
}
|
}
|
||||||
/* Read the response */
|
/* Read the response */
|
||||||
if ((len = readNlSock(sock, msgBuf, sizeof(msgBuf), msgSeq, getpid())) <
|
if ((len = readNlSock(sock, msgBuf, sizeof(msgBuf), msgSeq, getpid())) <
|
||||||
0) {
|
0) {
|
||||||
fprintf(stderr, "BIP: Read From Socket Failed...\n");
|
fprintf(stderr, "BIP: Read From Socket Failed...\n");
|
||||||
return ifName;
|
return BIP_Interface_Name;
|
||||||
}
|
}
|
||||||
/* Parse and print the response */
|
/* Parse and print the response */
|
||||||
rtInfo = (struct route_info *)malloc(sizeof(struct route_info));
|
rtInfo = (struct route_info *)malloc(sizeof(struct route_info));
|
||||||
@@ -674,17 +675,18 @@ static char *ifname_default(void)
|
|||||||
memset(rtInfo, 0, sizeof(struct route_info));
|
memset(rtInfo, 0, sizeof(struct route_info));
|
||||||
parseRoutes(nlMsg, rtInfo);
|
parseRoutes(nlMsg, rtInfo);
|
||||||
printRoute(rtInfo);
|
printRoute(rtInfo);
|
||||||
if (ifName[0] == 0) {
|
if (BIP_Interface_Name[0] == 0) {
|
||||||
if ((rtInfo->dstAddr == 0) && (rtInfo->ifName[0] != 0)) {
|
if ((rtInfo->dstAddr == 0) && (rtInfo->ifName[0] != 0)) {
|
||||||
/* default route */
|
/* default route */
|
||||||
memcpy(ifName, rtInfo->ifName, sizeof(ifName));
|
memcpy(BIP_Interface_Name, rtInfo->ifName,
|
||||||
|
sizeof(BIP_Interface_Name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(rtInfo);
|
free(rtInfo);
|
||||||
close(sock);
|
close(sock);
|
||||||
|
|
||||||
return ifName;
|
return BIP_Interface_Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -770,13 +772,14 @@ bool bip_init(char *ifname)
|
|||||||
int sock_fd = -1;
|
int sock_fd = -1;
|
||||||
|
|
||||||
if (ifname) {
|
if (ifname) {
|
||||||
|
strncpy(BIP_Interface_Name, ifname, sizeof(BIP_Interface_Name));
|
||||||
bip_set_interface(ifname);
|
bip_set_interface(ifname);
|
||||||
} else {
|
} else {
|
||||||
bip_set_interface(ifname_default());
|
bip_set_interface(ifname_default());
|
||||||
}
|
}
|
||||||
if (BIP_Address.s_addr == 0) {
|
if (BIP_Address.s_addr == 0) {
|
||||||
fprintf(stderr, "BIP: Failed to get an IP address from %s!\n",
|
fprintf(stderr, "BIP: Failed to get an IP address from %s!\n",
|
||||||
ifname ? ifname : ifname_default());
|
BIP_Interface_Name);
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -804,6 +807,10 @@ bool bip_init(char *ifname)
|
|||||||
BIP_Socket = -1;
|
BIP_Socket = -1;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
/* Bind to the proper interface to send without default gateway */
|
||||||
|
setsockopt(sock_fd, SOL_SOCKET, SO_BINDTODEVICE, BIP_Interface_Name,
|
||||||
|
sizeof(BIP_Interface_Name));
|
||||||
|
|
||||||
/* bind the socket to the local port number and IP address */
|
/* bind the socket to the local port number and IP address */
|
||||||
sin.sin_family = AF_INET;
|
sin.sin_family = AF_INET;
|
||||||
sin.sin_addr.s_addr = htonl(INADDR_ANY);
|
sin.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
|||||||
Reference in New Issue
Block a user