{"id":3804,"date":"2015-06-24T14:15:54","date_gmt":"2015-06-24T14:15:54","guid":{"rendered":"http:\/\/blog.bachi.net\/?p=3804"},"modified":"2015-06-24T14:27:26","modified_gmt":"2015-06-24T14:27:26","slug":"linux-network-programming","status":"publish","type":"post","link":"https:\/\/blog.bachi.net\/?p=3804","title":{"rendered":"Linux Network Programming"},"content":{"rendered":"<h3>Manual Pages<\/h3>\n<p><a href=\"http:\/\/man7.org\/linux\/man-pages\/man3\/getifaddrs.3.html\">getifaddrs, freeifaddrs &#8211; get interface addresses<\/a><br \/>\n<a href=\"http:\/\/man7.org\/linux\/man-pages\/man7\/packet.7.html\">packet &#8211; packet interface on device level<\/a><\/p>\n<h3>NETLINK<\/h3>\n<p><a href=\"http:\/\/www.linuxfoundation.org\/collaborate\/workgroups\/networking\/generic_netlink_howto\">Generic NETLINK HowTo<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/15215865\/netlink-sockets-in-c-using-the-3-x-linux-kernel\">Netlink Sockets in C using the 3.X linux kernel<\/a><\/p>\n<h3>Tutorials &#038; Blogs<\/h3>\n<p><a href=\"http:\/\/codingrelic.geekhold.com\/2010\/11\/code-snippet-getifaddrs.html\">Code Snippet: getifaddrs<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/212528\/get-the-ip-address-of-the-machine\">Get the IP address of the machine<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/1779715\/how-to-get-mac-address-of-your-machine-using-a-c-program\">How to get MAC address of your machine using a C program?<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/6767296\/how-to-get-local-ip-and-mac-address-c\">How to get local IP and MAC address C [duplicate]<\/a><br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/6762766\/mac-address-with-getifaddrs\">MAC address with getifaddrs<\/a> (wrong answer!!)<\/p>\n<h3>Code Stippet<\/h3>\n<p><a href=\"https:\/\/github.com\/vincentbernat\/lldpd\/blob\/master\/src\/daemon\/interfaces-linux.c\">lldpd\/src\/daemon\/interfaces-linux.c<\/a><br \/>\n<a href=\"https:\/\/stevedxor.wordpress.com\/2013\/02\/06\/get-network-interfaces-mac-address\/\">Get network interface\u2019s MAC address and IP address<\/a><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;sys\/ioctl.h&gt;\r\n#include &lt;sys\/socket.h&gt;\r\n#include &lt;sys\/un.h&gt;\r\n#include &lt;sys\/param.h&gt;\r\n\r\n#include &lt;netinet\/in.h&gt;\r\n#include &lt;net\/ethernet.h&gt;\r\n#include &lt;net\/if.h&gt;\r\n\r\n#include &lt;linux\/if_link.h&gt;\r\n#include &lt;linux\/if_vlan.h&gt;\r\n#include &lt;linux\/sockios.h&gt;\r\n\r\n#include &lt;ifaddrs.h&gt;\r\n\r\nbool\r\nnetif_init(netif_t *netif, const char *name)\r\n{\r\n    bool                        result = true;\r\n    int                         sockfd;\r\n    struct ifaddrs             *ifas;\r\n    struct ifaddrs             *ifa;\r\n    struct ifreq                ifr;\r\n    struct rtnl_link_stats     *stats;\r\n    struct vlan_ioctl_args      ifv;\r\n    \r\n    \/* string copy name *\/\r\n    strncpy(netif-&gt;name, name, NETIF_NAME_SIZE);\r\n    \r\n    \/* create socket (required for ioctl) *\/\r\n    if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0) {\r\n        LOG_ERRNO(LOG_NETWORK_INTERFACE, LOG_ERROR, errno, (&quot;socket failed&quot;));\r\n        return false;\r\n    }\r\n    \r\n    \/* get interface addresses *\/\r\n    if (getifaddrs(&amp;ifas) != 0) {\r\n        return false;\r\n    }\r\n    \r\n    for (ifa = ifas; ifa != NULL; ifa = ifa-&gt;ifa_next) {\r\n        if ((ifa-&gt;ifa_addr)           == NULL) continue;\r\n        if ((ifa-&gt;ifa_flags &amp; IFF_UP) == 0)    continue;\r\n        \r\n        \r\n        \/* network interface name matches *\/\r\n        if (strcmp(name, ifa-&gt;ifa_name) == 0) {\r\n            \r\n            switch (ifa-&gt;ifa_addr-&gt;sa_family) {\r\n                case AF_INET:   netif_add_ipv4_address(netif,      IPV4_ADDRESS(&amp;(INADDR(ifa-&gt;ifa_addr)-&gt;sin_addr)),\r\n                                                                   IPV4_ADDRESS(&amp;(INADDR(ifa-&gt;ifa_netmask)-&gt;sin_addr)),\r\n                                                                   IPV4_ADDRESS(&amp;(INADDR(ifa-&gt;ifa_broadaddr)-&gt;sin_addr)),\r\n                                                                   NULL);\r\n                                break;\r\n                \r\n                case AF_INET6:  netif_add_ipv6_address(netif,      IPV6_ADDRESS(&amp;(INADDR6(ifa-&gt;ifa_addr)-&gt;sin6_addr)),\r\n                                                                   IPV6_ADDRESS(&amp;(INADDR6(ifa-&gt;ifa_netmask)-&gt;sin6_addr)),\r\n                                                                   IPV6_STATE_VALID);\r\n                                break;\r\n\r\n                case AF_PACKET: stats = ifa-&gt;ifa_data;\r\n                                LOG_PRINTLN(LOG_NETWORK_INTERFACE, LOG_DEBUG, (&quot;tx packet: %&quot; PRIu32 &quot; rx packet: %&quot; PRIu32 &quot; tx bytes: %&quot; PRIu32 &quot; rx bytes: %&quot; PRIu32,\r\n                                                                               stats-&gt;tx_packets, stats-&gt;rx_packets, stats-&gt;tx_bytes, stats-&gt;rx_bytes));\r\n\r\n                                bzero((char *) &amp;ifr, sizeof(ifr));\r\n                                strncpy(ifr.ifr_name, netif-&gt;name, NETIF_NAME_SIZE);\r\n                                if (ioctl(sockfd, SIOCGIFHWADDR, &amp;ifr) != -1) {\r\n                                    netif_add_mac_address(netif,   MAC_ADDRESS(LLADDR(LADDR(ifr.ifr_hwaddr.sa_data))));\r\n                                    bzero((char *) &amp;ifv, sizeof(ifv));\r\n                                    ifv.cmd = GET_VLAN_VID_CMD;\r\n                                    strncpy(ifv.device1, netif-&gt;name, sizeof(ifv.device1));\r\n                                    if (ioctl(sockfd, SIOCGIFVLAN, &amp;ifv) != -1) {\r\n                                        netif_add_vid(netif, ifv.u.VID);\r\n                                    }\r\n                                } else {\r\n                                    LOG_PRINTLN(LOG_NETWORK_INTERFACE, LOG_ERROR, (&quot;couldn't get MAC address&quot;));\r\n                                    result = false;\r\n                                    goto netif_init_exit;\r\n                                }\r\n                                break;\r\n\r\n                default:        continue;\r\n            }\r\n        }\r\n    }\r\n    \r\nnetif_init_exit:\r\n    freeifaddrs(ifas);\r\n\r\n    return result;\r\n}\r\n<\/pre>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n$ grep -r vlan_ioctl \/usr\/include\r\n\/usr\/include\/linux\/if_vlan.h:\/* Passed in vlan_ioctl_args structure to determine behaviour. *\/\r\n\/usr\/include\/linux\/if_vlan.h:enum vlan_ioctl_cmds {\r\n\/usr\/include\/linux\/if_vlan.h:struct vlan_ioctl_args {\r\n\/usr\/include\/linux\/if_vlan.h:\tint cmd; \/* Should be one of the vlan_ioctl_cmds enum above. *\/\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Manual Pages getifaddrs, freeifaddrs &#8211; get interface addresses packet &#8211; packet interface on device level NETLINK Generic NETLINK HowTo Netlink Sockets in C using the 3.X linux kernel Tutorials &#038; Blogs Code Snippet: getifaddrs Get the IP address of the machine How to get MAC address of your machine using a C program? How to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3804","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3804"}],"version-history":[{"count":6,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3804\/revisions"}],"predecessor-version":[{"id":3810,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=\/wp\/v2\/posts\/3804\/revisions\/3810"}],"wp:attachment":[{"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bachi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}