- // get the interface index
- struct {
- struct nlmsghdr nh;
- struct ifinfomsg ifinfo __attribute__ ((aligned(NLMSG_ALIGNTO)));
- struct rtattr ifname_rta __attribute__ ((aligned(RTA_ALIGNTO)));
- char ifname[IFNAMSIZ];
- } req;
- char buf[4096];
- ssize_t len;
- struct nlmsghdr *resp_nh;
-
- req.nh.nlmsg_type = RTM_GETLINK;
- req.nh.nlmsg_flags = NLM_F_REQUEST;
- req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.ifinfo));
-
- req.ifinfo.ifi_family = AF_UNSPEC; // as the man says
-
- netlink_addattr(&req.nh, IFLA_IFNAME, config->tundevice, strlen(config->tundevice)+1);
-
- if(netlink_send(&req.nh) < 0 || (len = netlink_recv(buf, sizeof(buf))) < 0)
- {
- LOG(0, 0, 0, "Error getting tun ifindex: %s\n", strerror(errno));
- exit(1);
- }
-
- resp_nh = (struct nlmsghdr *)buf;
- if (!NLMSG_OK (resp_nh, len))
- {
- LOG(0, 0, 0, "Malformed answer getting tun ifindex %ld\n", len);
- exit(1);
- }
-
- memcpy(&ifinfo, NLMSG_DATA(resp_nh), sizeof(ifinfo));
- // got index
- tunidx = ifinfo.ifi_index;