{"id":547,"date":"2012-10-18T12:24:47","date_gmt":"2012-10-18T12:24:47","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=547"},"modified":"2014-09-26T15:05:29","modified_gmt":"2014-09-26T15:05:29","slug":"network-interface-code-snippets","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/tcpip\/network-interface-code-snippets","title":{"rendered":"Network Interface Code Snippets"},"content":{"rendered":"<p>\nThese are bits of code we&#39;ve found for linux that don&#39;t necessarily relate exactly to raspbian on the RPi, but may be useful\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<h4>\nNetwork Config<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\r\n\/\/Code sourced from: http:\/\/www.network-builders.com\/programatically-change-ip-address-t58439.html\r\n\/\/The RPi doesn&#39;t have a folder &quot;\/etc\/sysconfig\/&quot; so this isn&#39;t usable as currently implemented\r\n#include &lt;stdio.h&gt;\r\n#include &lt;errno.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;sys\/ioctl.h&gt;\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;sys\/socket.h&gt;\r\n#include &lt;net\/if.h&gt;\r\n#include &lt;net\/route.h&gt;\r\n#include &lt;arpa\/inet.h&gt;\r\n\r\n\r\n\r\nint SetNetworkConfig(char *ifname,in_addr_t ip,in_addr_t mask,in_addr_t gw);\r\nin_addr_t IP2Broadcast(in_addr_t IP, in_addr_t Mask);\r\nin_addr_t IP2Network(in_addr_t IP, in_addr_t Mask);\r\nin_addr_t ValidateMask(in_addr_t Mask);\r\nint GetIP(char *ifname, in_addr_t *ip, in_addr_t *mask);\r\nstatic int set_default_gw( int sockfd, in_addr_t gip );\r\nvoid UpdateIP(const char *ifname, in_addr_t ip, in_addr_t mask, in_addr_t gip);\r\nint GetMacAddress(char *ifname, char *addr);\r\nint SetMacAddress(char *ifname, char *addr);\r\n\r\n\r\nstatic const char *l_pszConfigFile = &quot;\/etc\/sysconfig\/network\/ifcfg-&quot;;\r\n\r\n\r\n\/\/==============================================================================\r\n\/\/ function: SetNetworkConfig\r\n\/\/ description: updates the network configuration file\r\n\/\/==============================================================================\r\nint SetNetworkConfig(char *ifname, in_addr_t ip, in_addr_t mask, in_addr_t gw)\r\n{\r\n\tchar szTemp[256];\r\n\tin_addr_t bc,nw;\r\n\r\n\tmask = ValidateMask(mask);\r\n\tnw = IP2Network(ip,mask);\r\n\tbc = IP2Broadcast(ip,mask);\r\n\r\n\tsprintf(szTemp,&quot;%s%s&quot;, l_pszConfigFile, ifname);\r\n\tprintf(&quot;open -&gt; %s\\n&quot;,szTemp);\r\n\tFILE *file = fopen(szTemp,&quot;wr&quot;);\r\n\tif (file == NULL)\r\n\t{\r\n\t\tperror(&quot;SetNetworkConfig&quot;);\r\n\t\treturn -1;\r\n\t}\r\n\r\n\tfprintf(file, &quot;IPADDR=\\&quot;%s\\&quot;\\n&quot;, inet_ntoa(inet_makeaddr(ip,0)));\r\n\tfprintf(file, &quot;NETMASK=\\&quot;%s\\&quot;\\n&quot;, inet_ntoa(inet_makeaddr(mask,0)));\r\n\tfprintf(file, &quot;NETWORK=\\&quot;%s\\&quot;\\n&quot;, inet_ntoa(inet_makeaddr(nw,0)));\r\n\tfprintf(file, &quot;BROADCAST=\\&quot;%s\\&quot;\\n&quot;, inet_ntoa(inet_makeaddr(bc,0)));\r\n\tif ( gw != 0 )\r\n\t\tfprintf(file, &quot;GATEWAY=\\&quot;%s\\&quot;\\n&quot;, inet_ntoa(inet_makeaddr(gw,0)));\r\n\r\n\tfclose(file);\r\n\tsystem(&quot;\/etc\/init.d\/network restart&quot;);\r\n\r\n\treturn 0;\r\n}\r\n\r\n\/\/==============================================================================\r\n\/\/ function: IP2Broadcast\r\n\/\/ description: Takes a subnet mask and IP returns a subnet broadcast address\r\n\/\/==============================================================================\r\nin_addr_t IP2Broadcast(in_addr_t IP, in_addr_t Mask)\r\n{\r\n\tin_addr_t bcIP;\r\n\tin_addr_t bit;\r\n\tin_addr_t notbit;\r\n\tint i;\r\n\r\n\tbit = 0x80000000;\r\n\tnotbit = 0xFFFFFFFF;\r\n\tbcIP = 0;\r\n\tfor ( i=0; i&lt;31; i++)\r\n\t{\r\n\t\tif ((bit &amp; Mask) == 0)\r\n\t\t\tbreak;\r\n\r\n\t\tbcIP |= (IP &amp; bit);\r\n\t\tnotbit &amp;= ~bit;\r\n\t\tbit = bit &gt;&gt; 1;\r\n\t}\r\n\t\/\/bcIP != notbit;\r\n\t\/\/printf(&quot;bcIP = %s\\n&quot;,inet_ntoa(inet_makeaddr(bcIP,0)));\r\n\treturn bcIP;\r\n}\r\n\r\n\/\/==============================================================================\r\n\/\/ function: IP2Network\r\n\/\/ description: Takes a subnet mask and IP returns a subnet address\r\n\/\/==============================================================================\r\nin_addr_t IP2Network(in_addr_t IP, in_addr_t Mask)\r\n{\r\n\tin_addr_t netIP;\r\n\tin_addr_t bit;\r\n\tint i;\r\n\r\n\tbit = 0x80000000;\r\n\tnetIP = 0;\r\n\tfor ( i=0; i&lt;31; i++)\r\n\t{\r\n\t\tif ((bit &amp; Mask) == 0)\r\n\t\t\tbreak;\r\n\t\tnetIP |= (IP &amp; bit);\r\n\t\tbit = bit &gt;&gt; 1;\r\n\t}\r\n\t\/\/ printf(&quot;Network = %s\\n&quot;,inet_ntoa(inet_makeaddr(netIP,0)));\r\n\treturn netIP;\r\n}\r\n\r\n\/\/==============================================================================\r\n\/\/ function: ValidateMask\r\n\/\/ description: Takes a subnet mask and returns a valid version\r\n\/\/==============================================================================\r\nin_addr_t ValidateMask(in_addr_t Mask)\r\n{\r\n\tin_addr_t newMask;\r\n\tin_addr_t bit;\r\n\tint i;\r\n\r\n\tbit = 0x80000000;\r\n\tnewMask = 0;\r\n\tfor ( i=0; i&lt;31; i++)\r\n\t{\r\n\t\tif ((bit &amp; Mask) == 0)\r\n\t\t\tbreak;\r\n\t\tnewMask |= bit;\r\n\t\tbit = bit &gt;&gt; 1;\r\n\t}\r\n\t\/\/ printf(&quot;Mask = %s\\n&quot;,inet_ntoa(inet_makeaddr(Mask,0)));\r\n\treturn Mask;\r\n}\r\n\r\n\/\/==============================================================================\r\n\/\/ function: GetLocalIP\r\n\/\/ description: retrieve current network ip and mask\r\n\/\/==============================================================================\r\nint GetIP(char *ifname, in_addr_t *ip, in_addr_t *mask)\r\n{\r\n\tstruct ifreq ifr;\r\n\tint sock = socket(AF_INET,SOCK_DGRAM,0);\r\n\r\n\t\/\/ Get the interface IP address\r\n\tstrcpy( ifr.ifr_name, ifname );\r\n\tifr.ifr_addr.sa_family = AF_INET;\r\n\r\n\tif (ioctl( sock, SIOCGIFADDR, &amp;ifr ) &lt; 0)\r\n\t\tperror( &quot;SIOCGIFADDR&quot; );\r\n\r\n\t*ip = ((struct sockaddr_in *)&amp;ifr.ifr_addr)-&gt;sin_addr.s_addr;\r\n\tshutdown(sock,SHUT_RDWR);\r\n\r\n\treturn 0;\r\n}\r\n\r\n\/\/==============================================================================\r\n\/\/ function: set_default_gw\r\n\/\/ description: sets routing table&#39;s default gateway to the sock peeraddr\r\n\/\/==============================================================================\r\nstatic int set_default_gw( int sockfd, in_addr_t gip )\r\n{\r\n\tstruct sockaddr_in *dst, *gw, *mask;\r\n\tstruct rtentry route;\r\n\r\n\tmemset(&amp;route,0,sizeof(struct rtentry));\r\n\r\n\tdst = (struct sockaddr_in *)(&amp;(route.rt_dst));\r\n\tgw = (struct sockaddr_in *)(&amp;(route.rt_gateway));\r\n\tmask = (struct sockaddr_in *)(&amp;(route.rt_genmask));\r\n\r\n\t\/* Make sure we&#39;re talking about IP here *\/\r\n\tdst-&gt;sin_family = AF_INET;\r\n\tgw-&gt;sin_family = AF_INET;\r\n\tmask-&gt;sin_family = AF_INET;\r\n\r\n\t\/* Set up the data for removing the default route *\/\r\n\tdst-&gt;sin_addr.s_addr = 0;\r\n\tgw-&gt;sin_addr.s_addr = 0;\r\n\tmask-&gt;sin_addr.s_addr = 0;\r\n\troute.rt_flags = RTF_UP | RTF_GATEWAY;\r\n\r\n\t\/* Remove the default route *\/\r\n\tioctl(sockfd,SIOCDELRT,&amp;route);\r\n\r\n\t\/* Set up the data for adding the default route *\/\r\n\tdst-&gt;sin_addr.s_addr = 0;\r\n\tgw-&gt;sin_addr.s_addr = gip;\r\n\tmask-&gt;sin_addr.s_addr = 0;\r\n\troute.rt_metric = 1;\r\n\troute.rt_flags = RTF_UP | RTF_GATEWAY;\r\n\r\n\t\/* Remove this route if it already exists *\/\r\n\tioctl(sockfd,SIOCDELRT,&amp;route);\r\n\r\n\t\/* Add the default route *\/\r\n\tif( ioctl(sockfd,SIOCADDRT,&amp;route) == -1 )\r\n\t{\r\n\t\tfprintf( stderr,&quot;Adding default route: %d&quot;, errno);\r\n\t\treturn -1;\r\n\t}\r\n\r\n\t\/\/fprintf( stdout,&quot;Added default route successfully.&quot; );\r\n\treturn 0;\r\n}\r\n\r\n\/\/==============================================================================\r\n\/\/ function: UpdateIP\r\n\/\/ description: updates the networks parameters\r\n\/\/==============================================================================\r\nvoid UpdateIP(const char *ifname, in_addr_t ip, in_addr_t mask, in_addr_t gip)\r\n{\r\n\tstruct ifreq ifr;\r\n\tchar *myip;\r\n\tint sock = socket(AF_INET,SOCK_DGRAM,0);\r\n\tin_addr_t IP;\r\n\tstruct rtentry rt;\r\n\tstruct sockaddr_in sa;\r\n\tmemset(&amp;sa, 0, sizeof(sa));\r\n\tmemset(&amp;rt, 0, sizeof(rt));\r\n\r\n\t\/\/ Get the interface IP address\r\n\tstrcpy( ifr.ifr_name, ifname );\r\n\tifr.ifr_addr.sa_family = AF_INET;\r\n\r\n\tif (ioctl( sock, SIOCGIFADDR, &amp;ifr ) &lt; 0)\r\n\t\tperror( &quot;SIOCGIFADDR&quot; );\r\n\r\n\tmyip = inet_ntoa( ((struct sockaddr_in *)&amp;ifr.ifr_addr)-&gt;sin_addr );\r\n\r\n\tIP = ((struct sockaddr_in *)&amp;ifr.ifr_addr)-&gt;sin_addr.s_addr;\r\n\t((struct sockaddr_in *)&amp;ifr.ifr_addr)-&gt;sin_addr.s_addr = ip;\r\n\tif (ioctl( sock, SIOCSIFADDR, &amp;ifr ) &lt; 0)\r\n\t\tperror( &quot;SIOCSIFADDR&quot; );\r\n\r\n\tset_default_gw(sock, gip);\r\n\tshutdown(sock,SHUT_RDWR);\r\n}\r\n\r\n\/\/==============================================================================\r\n\/\/ function: GetMacAddress\r\n\/\/ description: retrieve current network address\r\n\/\/==============================================================================\r\nint GetMacAddress(char *ifname, char *addr)\r\n{\r\n\tstruct ifreq ifr;\r\n\tint sock = socket(AF_INET,SOCK_DGRAM,0);\r\n\r\n\t\/\/ Get the interface IP address\r\n\tstrcpy( ifr.ifr_name, ifname );\r\n\tifr.ifr_addr.sa_family = AF_INET;\r\n\r\n\tif (ioctl( sock, SIOCGIFHWADDR, &amp;ifr ) &lt; 0)\r\n\t{\r\n\t\tperror( &quot;SIOCGHWADDR&quot; );\r\n\t\treturn -1;\r\n\t}\r\n\tmemcpy(addr,ifr.ifr_hwaddr.sa_data,6);\r\n\t\/\/uint8_t *hwaddr = (uint8_t*)addr;\r\n\t\/\/printf(&quot;The hardware address (SIOCGIFHWADDR) of %s is type %d &quot;\r\n\t\/\/ &quot;%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x.\\n&quot;, ifname,\r\n\t\/\/ ifr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],\r\n\t\/\/ hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);\r\n\tshutdown(sock,SHUT_RDWR);\r\n\treturn 0;\r\n}\r\n\r\n\/\/==============================================================================\r\n\/\/ function: SetMacAddress\r\n\/\/ description: Set network address\r\n\/\/==============================================================================\r\nint SetMacAddress(char *ifname, char *addr)\r\n{\r\n\tstruct ifreq ifr;\r\n\tint sock = socket(AF_INET,SOCK_DGRAM,0);\r\n\r\n\t\/\/ Set the interface IP address\r\n\tstrcpy( ifr.ifr_name, ifname );\r\n\tifr.ifr_addr.sa_family = AF_UNIX;\r\n\tmemcpy(ifr.ifr_hwaddr.sa_data,addr,6);\r\n\r\n\tif (ioctl( sock, SIOCSIFHWADDR, &amp;ifr ) &lt; 0)\r\n\t{\r\n\t\tperror( &quot;SIOCSIFHWADDR&quot; );\r\n\t\treturn -1;\r\n\t}\r\n\tshutdown(sock,SHUT_RDWR);\r\n\treturn 0;\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>These are bits of code we&#39;ve found for linux that don&#39;t necessarily relate exactly to raspbian on the RPi, but may be useful &nbsp; Network Config \/\/Code sourced from: http:\/\/www.network-builders.com\/programatically-change-ip-address-t58439.html \/\/The RPi doesn&#39;t have a folder &quot;\/etc\/sysconfig\/&quot; so this isn&#39;t usable as currently implemented #include &lt;stdio.h&gt; #include &lt;errno.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;sys\/ioctl.h&gt; #include [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[],"class_list":["post-547","post","type-post","status-publish","format-standard","hentry","category-tcpip"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=547"}],"version-history":[{"count":2,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"predecessor-version":[{"id":549,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/547\/revisions\/549"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}