Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit fcc968a

Browse files
authored
Merge pull request #205 from amshinde/set-interface-name
Add ability to change interface name while setting up an interface.
2 parents bbb111e + 467d776 commit fcc968a

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/net.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,44 @@ static int hyper_cleanup_route(struct rtnl_handle *rth, struct hyper_route *rt)
567567
return 0;
568568
}
569569

570+
static int hyper_set_interface_name(struct rtnl_handle *rth,
571+
int ifindex,
572+
char *new_device_name)
573+
{
574+
struct {
575+
struct nlmsghdr n;
576+
struct ifinfomsg i;
577+
char buf[1024];
578+
} req;
579+
580+
if (ifindex < 0 || !new_device_name) {
581+
return -1;
582+
}
583+
584+
memset(&req, 0, sizeof(req));
585+
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
586+
req.n.nlmsg_flags = NLM_F_REQUEST;
587+
req.n.nlmsg_type = RTM_SETLINK;
588+
589+
req.i.ifi_family = AF_UNSPEC;
590+
req.i.ifi_change = 0xFFFFFFFF;
591+
req.i.ifi_index = ifindex;
592+
593+
if (addattr_l(&req.n, sizeof(req), IFLA_IFNAME,
594+
new_device_name,
595+
strlen(new_device_name) + 1)) {
596+
fprintf(stderr, "setup attr failed\n");
597+
return -1;
598+
}
599+
600+
if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0) {
601+
perror("rtnl_talk failed");
602+
return -1;
603+
}
604+
605+
return 0;
606+
}
607+
570608
static int hyper_setup_interface(struct rtnl_handle *rth,
571609
struct hyper_interface *iface)
572610
{
@@ -621,6 +659,11 @@ static int hyper_setup_interface(struct rtnl_handle *rth,
621659
return -1;
622660
}
623661

662+
if (iface->new_device_name && strcmp(iface->new_device_name, iface->device)) {
663+
fprintf(stdout, "Setting interface name to %s\n", iface->new_device_name);
664+
hyper_set_interface_name(rth, ifindex, iface->new_device_name);
665+
}
666+
624667
if (hyper_up_nic(rth, ifindex) < 0) {
625668
fprintf(stderr, "up device %d failed\n", ifindex);
626669
return -1;

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct hyper_interface {
2222
char *device;
2323
char *ipaddr;
2424
char *mask;
25+
char *new_device_name;
2526
};
2627

2728
struct hyper_route {

src/parse.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,9 @@ static int hyper_parse_interface(struct hyper_interface *iface,
773773
} else if (json_token_streq(json, &toks[i], "netMask")) {
774774
iface->mask = (json_token_str(json, &toks[++i]));
775775
fprintf(stdout, "net mask is %s\n", iface->mask);
776+
} else if (json_token_streq(json, &toks[i], "newDeviceName")) {
777+
iface->new_device_name = (json_token_str(json, &toks[++i]));
778+
fprintf(stdout, "new interface name is %s\n", iface->new_device_name);
776779
} else {
777780
fprintf(stderr, "get unknown section %s in interfaces\n",
778781
json_token_str(json, &toks[i]));
@@ -786,6 +789,7 @@ static int hyper_parse_interface(struct hyper_interface *iface,
786789
free(iface->device);
787790
free(iface->ipaddr);
788791
free(iface->mask);
792+
free(iface->new_device_name);
789793
return -1;
790794
}
791795

0 commit comments

Comments
 (0)