Skip to content

Commit 6b9bd97

Browse files
committed
netdev: Change netdev_get_in4() to return an error code.
Until now, netdev_get_in4() and netdev_nodev_get_in4() have returned a bool that represents success or failure. This commit changes the return value to an int that can indicate what kind of error occurred, which is both more consistent with the rest of the netdev interfaces and more meaningful, and updates all callers to the new interface. (Currently netdev_get_in4() won't ever return an error, but other future implementations might.)
1 parent 3d22212 commit 6b9bd97

3 files changed

Lines changed: 13 additions & 9 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) == 0;
24842484
netdev_close(netdev);
24852485
if (exclude) {
24862486
continue;

lib/netdev.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,14 @@ netdev_set_advertisements(struct netdev *netdev, uint32_t advertise)
792792
}
793793

794794
/* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
795-
* 'in4' is non-null) and returns true. Otherwise, returns false. */
796-
bool
795+
* 'in4' is non-null) and returns 0. Otherwise, returns a positive errno value
796+
* and sets '*in4' to INADDR_ANY (0). */
797+
int
797798
netdev_nodev_get_in4(const char *netdev_name, struct in_addr *in4)
798799
{
799800
struct ifreq ifr;
800801
struct in_addr ip = { INADDR_ANY };
802+
int error;
801803

802804
init_netdev();
803805

@@ -807,17 +809,19 @@ netdev_nodev_get_in4(const char *netdev_name, struct in_addr *in4)
807809
if (ioctl(af_inet_sock, SIOCGIFADDR, &ifr) == 0) {
808810
struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
809811
ip = sin->sin_addr;
812+
error = ip.s_addr != INADDR_ANY ? 0 : EADDRNOTAVAIL;
810813
} else {
811814
VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFADDR) failed: %s",
812815
netdev_name, strerror(errno));
816+
error = errno;
813817
}
814818
if (in4) {
815819
*in4 = ip;
816820
}
817-
return ip.s_addr != INADDR_ANY;
821+
return error;
818822
}
819823

820-
bool
824+
int
821825
netdev_get_in4(const struct netdev *netdev, struct in_addr *in4)
822826
{
823827
return netdev_nodev_get_in4(netdev->name, in4);
@@ -1322,7 +1326,7 @@ netdev_find_dev_by_in4(const struct in_addr *in4, char **netdev_name)
13221326
struct svec dev_list;
13231327

13241328
/* Check the hint first. */
1325-
if (*netdev_name && (netdev_nodev_get_in4(*netdev_name, &dev_in4))
1329+
if (*netdev_name && !netdev_nodev_get_in4(*netdev_name, &dev_in4)
13261330
&& (dev_in4.s_addr == in4->s_addr)) {
13271331
return true;
13281332
}
@@ -1332,7 +1336,7 @@ netdev_find_dev_by_in4(const struct in_addr *in4, char **netdev_name)
13321336
netdev_enumerate(&dev_list);
13331337

13341338
for (i=0; i<dev_list.n; i++) {
1335-
if ((netdev_nodev_get_in4(dev_list.names[i], &dev_in4))
1339+
if (!netdev_nodev_get_in4(dev_list.names[i], &dev_in4)
13361340
&& (dev_in4.s_addr == in4->s_addr)) {
13371341
*netdev_name = xstrdup(dev_list.names[i]);
13381342
svec_destroy(&dev_list);

lib/netdev.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ int netdev_get_features(struct netdev *,
9191
uint32_t *current, uint32_t *advertised,
9292
uint32_t *supported, uint32_t *peer);
9393
int netdev_set_advertisements(struct netdev *, uint32_t advertise);
94-
bool netdev_get_in4(const struct netdev *, struct in_addr *);
94+
int netdev_get_in4(const struct netdev *, struct in_addr *);
9595
int netdev_set_in4(struct netdev *, struct in_addr addr, struct in_addr mask);
9696
int netdev_add_router(struct in_addr router);
9797
bool netdev_get_in6(const struct netdev *, struct in6_addr *);
@@ -108,7 +108,7 @@ int netdev_set_policing(struct netdev *, uint32_t kbits_rate,
108108
void netdev_enumerate(struct svec *);
109109
bool netdev_find_dev_by_in4(const struct in_addr *in4, char **netdev_name);
110110
int netdev_nodev_get_flags(const char *netdev_name, enum netdev_flags *);
111-
bool netdev_nodev_get_in4(const char *netdev_name, struct in_addr *);
111+
int netdev_nodev_get_in4(const char *netdev_name, struct in_addr *);
112112
int netdev_nodev_set_etheraddr(const char *name, const uint8_t mac[6]);
113113
int netdev_nodev_get_etheraddr(const char *netdev_name, uint8_t mac[6]);
114114
int netdev_nodev_set_policing(const char *netdev_name, uint32_t kbits_rate,

0 commit comments

Comments
 (0)