Skip to content

Commit f10725f

Browse files
author
Justin Pettit
committed
Return netmask along with IP address when querying through netdev
The call netdev_get_in4() now allows the caller to also retrieve the associated netmask.
1 parent a5f37a2 commit f10725f

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

extras/ezio/ovs-switchui.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2480,7 +2480,7 @@ choose_netdevs(struct svec *choices)
24802480

24812481
retval = netdev_open(name, NETDEV_ETH_TYPE_NONE, &netdev);
24822482
if (!retval) {
2483-
bool exclude = netdev_get_in4(netdev, NULL);
2483+
bool exclude = netdev_get_in4(netdev, NULL, NULL);
24842484
netdev_close(netdev);
24852485
if (exclude) {
24862486
continue;

lib/netdev.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -781,10 +781,12 @@ netdev_set_advertisements(struct netdev *netdev, uint32_t advertise)
781781
return do_ethtool(netdev, &ecmd, ETHTOOL_SSET, "ETHTOOL_SSET");
782782
}
783783

784-
/* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
785-
* 'in4' is non-null) and returns true. Otherwise, returns false. */
784+
/* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address
785+
* and '*mask' to the netmask (if they are non-null) and returns true.
786+
* Otherwise, returns false. */
786787
bool
787-
netdev_nodev_get_in4(const char *netdev_name, struct in_addr *in4)
788+
netdev_nodev_get_in4(const char *netdev_name, struct in_addr *in4,
789+
struct in_addr *mask)
788790
{
789791
struct ifreq ifr;
790792
struct in_addr ip = { INADDR_ANY };
@@ -804,13 +806,25 @@ netdev_nodev_get_in4(const char *netdev_name, struct in_addr *in4)
804806
if (in4) {
805807
*in4 = ip;
806808
}
809+
810+
if (mask) {
811+
if (ioctl(af_inet_sock, SIOCGIFNETMASK, &ifr) == 0) {
812+
struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
813+
*mask = sin->sin_addr;
814+
} else {
815+
VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFNETMASK) failed: %s",
816+
netdev_name, strerror(errno));
817+
}
818+
}
819+
807820
return ip.s_addr != INADDR_ANY;
808821
}
809822

810823
bool
811-
netdev_get_in4(const struct netdev *netdev, struct in_addr *in4)
824+
netdev_get_in4(const struct netdev *netdev, struct in_addr *in4, struct
825+
in_addr *mask)
812826
{
813-
return netdev_nodev_get_in4(netdev->name, in4);
827+
return netdev_nodev_get_in4(netdev->name, in4, mask);
814828
}
815829

816830
static void
@@ -1309,7 +1323,7 @@ netdev_find_dev_by_in4(const struct in_addr *in4, char **netdev_name)
13091323
struct svec dev_list;
13101324

13111325
/* Check the hint first. */
1312-
if (*netdev_name && (netdev_nodev_get_in4(*netdev_name, &dev_in4))
1326+
if (*netdev_name && (netdev_nodev_get_in4(*netdev_name, &dev_in4, NULL))
13131327
&& (dev_in4.s_addr == in4->s_addr)) {
13141328
return true;
13151329
}
@@ -1319,7 +1333,7 @@ netdev_find_dev_by_in4(const struct in_addr *in4, char **netdev_name)
13191333
netdev_enumerate(&dev_list);
13201334

13211335
for (i=0; i<dev_list.n; i++) {
1322-
if ((netdev_nodev_get_in4(dev_list.names[i], &dev_in4))
1336+
if ((netdev_nodev_get_in4(dev_list.names[i], &dev_in4, NULL))
13231337
&& (dev_in4.s_addr == in4->s_addr)) {
13241338
*netdev_name = xstrdup(dev_list.names[i]);
13251339
svec_destroy(&dev_list);

lib/netdev.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ int netdev_get_features(struct netdev *,
9090
uint32_t *current, uint32_t *advertised,
9191
uint32_t *supported, uint32_t *peer);
9292
int netdev_set_advertisements(struct netdev *, uint32_t advertise);
93-
bool netdev_get_in4(const struct netdev *, struct in_addr *);
94-
int netdev_set_in4(struct netdev *, struct in_addr addr, struct in_addr mask);
93+
bool netdev_get_in4(const struct netdev *, struct in_addr *addr,
94+
struct in_addr *mask);
95+
int netdev_set_in4(struct netdev *, struct in_addr in4, struct in_addr mask);
9596
int netdev_add_router(struct in_addr router);
9697
bool netdev_get_in6(const struct netdev *, struct in6_addr *);
9798
int netdev_get_flags(const struct netdev *, enum netdev_flags *);
@@ -107,7 +108,8 @@ int netdev_set_policing(struct netdev *, uint32_t kbits_rate,
107108
void netdev_enumerate(struct svec *);
108109
bool netdev_find_dev_by_in4(const struct in_addr *in4, char **netdev_name);
109110
int netdev_nodev_get_flags(const char *netdev_name, enum netdev_flags *);
110-
bool netdev_nodev_get_in4(const char *netdev_name, struct in_addr *);
111+
bool netdev_nodev_get_in4(const char *netdev_name, struct in_addr *in4,
112+
struct in_addr *mask);
111113
int netdev_nodev_set_etheraddr(const char *name, const uint8_t mac[6]);
112114
int netdev_nodev_get_etheraddr(const char *netdev_name, uint8_t mac[6]);
113115
int netdev_nodev_set_policing(const char *netdev_name, uint32_t kbits_rate,

0 commit comments

Comments
 (0)