On 12/8/20 1:01 PM, Hangbin Liu wrote:
[...]
+static int get_mac_addr(unsigned int ifindex_out, void *mac_addr)
+{
+       struct ifreq ifr;
+       char ifname[IF_NAMESIZE];
+       int fd = socket(AF_INET, SOCK_DGRAM, 0);
+
+       if (fd < 0)
+               return -1;
+
+       if (!if_indextoname(ifindex_out, ifname))
+               return -1;
+
+       strcpy(ifr.ifr_name, ifname);
+
+       if (ioctl(fd, SIOCGIFHWADDR, &ifr) != 0)
+               return -1;
+
+       memcpy(mac_addr, ifr.ifr_hwaddr.sa_data, 6 * sizeof(char));
+       close(fd);

Given we do bother to close the socket fd here, we should also close it in above
error cases..

+       return 0;
+}
+

Reply via email to