Merge pull request #205 from amshinde/set-interface-name

Add ability to change interface name while setting up an interface.
This commit is contained in:
Gao feng
2016-10-15 15:42:32 +08:00
committed by GitHub
3 changed files with 48 additions and 0 deletions
+43
View File
@@ -567,6 +567,44 @@ static int hyper_cleanup_route(struct rtnl_handle *rth, struct hyper_route *rt)
return 0;
}
static int hyper_set_interface_name(struct rtnl_handle *rth,
int ifindex,
char *new_device_name)
{
struct {
struct nlmsghdr n;
struct ifinfomsg i;
char buf[1024];
} req;
if (ifindex < 0 || !new_device_name) {
return -1;
}
memset(&req, 0, sizeof(req));
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
req.n.nlmsg_flags = NLM_F_REQUEST;
req.n.nlmsg_type = RTM_SETLINK;
req.i.ifi_family = AF_UNSPEC;
req.i.ifi_change = 0xFFFFFFFF;
req.i.ifi_index = ifindex;
if (addattr_l(&req.n, sizeof(req), IFLA_IFNAME,
new_device_name,
strlen(new_device_name) + 1)) {
fprintf(stderr, "setup attr failed\n");
return -1;
}
if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0) {
perror("rtnl_talk failed");
return -1;
}
return 0;
}
static int hyper_setup_interface(struct rtnl_handle *rth,
struct hyper_interface *iface)
{
@@ -621,6 +659,11 @@ static int hyper_setup_interface(struct rtnl_handle *rth,
return -1;
}
if (iface->new_device_name && strcmp(iface->new_device_name, iface->device)) {
fprintf(stdout, "Setting interface name to %s\n", iface->new_device_name);
hyper_set_interface_name(rth, ifindex, iface->new_device_name);
}
if (hyper_up_nic(rth, ifindex) < 0) {
fprintf(stderr, "up device %d failed\n", ifindex);
return -1;
+1
View File
@@ -22,6 +22,7 @@ struct hyper_interface {
char *device;
char *ipaddr;
char *mask;
char *new_device_name;
};
struct hyper_route {
+4
View File
@@ -773,6 +773,9 @@ static int hyper_parse_interface(struct hyper_interface *iface,
} else if (json_token_streq(json, &toks[i], "netMask")) {
iface->mask = (json_token_str(json, &toks[++i]));
fprintf(stdout, "net mask is %s\n", iface->mask);
} else if (json_token_streq(json, &toks[i], "newDeviceName")) {
iface->new_device_name = (json_token_str(json, &toks[++i]));
fprintf(stdout, "new interface name is %s\n", iface->new_device_name);
} else {
fprintf(stderr, "get unknown section %s in interfaces\n",
json_token_str(json, &toks[i]));
@@ -786,6 +789,7 @@ fail:
free(iface->device);
free(iface->ipaddr);
free(iface->mask);
free(iface->new_device_name);
return -1;
}