- // 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;
-
- req.nh.nlmsg_type = RTM_GETLINK;
- req.nh.nlmsg_flags = NLM_F_REQUEST;
-
- req.ifinfo.ifi_family = AF_UNSPEC; // as the man says
-
- req.ifname_rta.rta_len = RTA_LENGTH(strlen(config->tundevice)+1);
- req.ifname_rta.rta_type = IFLA_IFNAME;
- strncpy(req.ifname, config->tundevice, IFNAMSIZ-1);
-
- req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.ifinfo)
- + req.ifname_rta.rta_len);
-
- if(netlink_send(&req.nh, 0) < 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\n");
- exit(1);
- }
-
- memcpy(&ifinfo, NLMSG_DATA(resp_nh), sizeof(ifinfo));
- // got index
- tunidx = ifinfo.ifi_index;