1. </pre><pre code_snippet_id="495447" snippet_file_name="blog_20141024_1_7065081" name="code" class="html">/********************************** (C) COPYRIGHT *******************************/
  1. * File Name          : get_gw.c
  2. * Author             : skdkjzz
  3. * Date               : 2014/08/07
  4. * Description        : linux下获取网卡信息
  5. *********************************************************************************/
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <asm/types.h>
  10. #include <netinet/ether.h>
  11. #include <netinet/in.h>
  12. #include <net/if.h>
  13. #include <stdio.h>
  14. #include <sys/socket.h>
  15. #include <sys/ioctl.h>
  16. #include <linux/netlink.h>
  17. #include <linux/rtnetlink.h>
  18. #include <sys/types.h>
  19. #define JSOEPH_NET_RMSG_BUFSIZE 8192
  20. typedef struct route_info{
  21. u_int dstAddr;
  22. u_int srcAddr;
  23. u_int gateWay;
  24. u_int genmask;
  25. char ifName[IF_NAMESIZE];
  26. }JOSEPH_ROUTE_INFO;
  27. #ifdef JOSEPH_CAT_ENUM
  28. /* Routing message attributes */
  29. enum rtattr_type_t {
  30. RTA_UNSPEC,
  31. RTA_DST,
  32. RTA_SRC,
  33. RTA_IIF,
  34. RTA_OIF,
  35. RTA_GATEWAY,
  36. RTA_PRIORITY,
  37. RTA_PREFSRC,
  38. RTA_METRICS,
  39. RTA_MULTIPATH,
  40. RTA_PROTOINFO, /* no longer used */
  41. RTA_FLOW,
  42. RTA_CACHEINFO,
  43. RTA_SESSION, /* no longer used */
  44. RTA_MP_ALGO, /* no longer used */
  45. RTA_TABLE,
  46. RTA_MARK,
  47. __RTA_MAX
  48. };
  49. #endif
  50. int Joseph_ReadNlSock(int sockFd, char *bufPtr, int seqNum, int pId)
  51. {
  52. struct nlmsghdr *nlHdr;
  53. int readLen = 0, msgLen = 0;
  54. do
  55. {
  56. /* Recieve response from the kernel */
  57. if((readLen = recv(sockFd, bufPtr, JSOEPH_NET_RMSG_BUFSIZE - msgLen, 0)) < 0){
  58. printf("SOCK READ Error !\n");
  59. return -1;
  60. }
  61. nlHdr = (struct nlmsghdr *)bufPtr;
  62. /* Check if the header is valid */
  63. if((NLMSG_OK(nlHdr, readLen) == 0) || (nlHdr->nlmsg_type == NLMSG_ERROR))
  64. {
  65. printf("Error in recieved packet !\n");
  66. return -1;
  67. }
  68. /* Check if the its the last message */
  69. if(nlHdr->nlmsg_type == NLMSG_DONE)
  70. {
  71. break;
  72. }
  73. else
  74. {
  75. /* Else move the pointer to buffer appropriately */
  76. bufPtr += readLen;
  77. msgLen += readLen;
  78. }
  79. /* Check if its a multi part message */
  80. if((nlHdr->nlmsg_flags & NLM_F_MULTI) == 0)
  81. {
  82. /* return if its not */
  83. break;
  84. }
  85. } while((nlHdr->nlmsg_seq != seqNum) || (nlHdr->nlmsg_pid != pId));
  86. return msgLen;
  87. }
  88. /* For printing the routes. */
  89. void Joseph_PrintRoute(struct route_info *rtInfo,char *if_name_in)
  90. {
  91. char tempBuf[512];
  92. if(strcmp(rtInfo->ifName,if_name_in) == 0)
  93. {
  94. /* Print Destination address */
  95. if(rtInfo->dstAddr != 0)
  96. strcpy(tempBuf, (char *)inet_ntoa(rtInfo->dstAddr));
  97. else
  98. sprintf(tempBuf,"0.0.0.0\t");
  99. fprintf(stdout,"%s\t", tempBuf);
  100. /* Print Gateway address */
  101. if(rtInfo->gateWay != 0)
  102. strcpy(tempBuf, (char *)inet_ntoa(rtInfo->gateWay));
  103. else
  104. sprintf(tempBuf,"0.0.0.0\t");
  105. fprintf(stdout,"%s\t", tempBuf);
  106. /* Print Interface Name*/
  107. fprintf(stdout,"%s\t", rtInfo->ifName);
  108. /* Print genmask address */
  109. if(rtInfo->genmask != 0)
  110. strcpy(tempBuf, (char *)inet_ntoa(rtInfo->genmask));
  111. else
  112. sprintf(tempBuf,"0.0.0.0\t");
  113. fprintf(stdout,"%s\t", tempBuf);
  114. /* Print Source address */
  115. if(rtInfo->srcAddr != 0)
  116. strcpy(tempBuf, (char *)inet_ntoa(rtInfo->srcAddr));
  117. else
  118. sprintf(tempBuf,"0.0.0.0\t");
  119. fprintf(stdout,"%s\n", tempBuf);
  120. }
  121. }
  122. /* For parsing the route info returned */
  123. int Joseph_ParseRoutes(struct nlmsghdr *nlHdr, struct route_info *rtInfo,char *gateway,char *if_name_in)
  124. {
  125. struct rtmsg *rtMsg;
  126. struct rtattr *rtAttr;
  127. int rtLen;
  128. char *tempBuf = NULL;
  129. tempBuf = (char *)malloc(100);
  130. rtMsg = (struct rtmsg *)NLMSG_DATA(nlHdr);
  131. /* If the route is not for AF_INET or does not belong to main routing table then return. */
  132. if((rtMsg->rtm_family != AF_INET) || (rtMsg->rtm_table != RT_TABLE_MAIN))
  133. {
  134. free(tempBuf);
  135. tempBuf = NULL;
  136. return -1;
  137. }
  138. /* get the rtattr field */
  139. rtAttr = (struct rtattr *)RTM_RTA(rtMsg);
  140. rtLen = RTM_PAYLOAD(nlHdr);
  141. for(;RTA_OK(rtAttr,rtLen);rtAttr = RTA_NEXT(rtAttr,rtLen))
  142. {
  143. switch(rtAttr->rta_type)
  144. {
  145. case RTA_OIF:
  146. if_indextoname(*(int *)RTA_DATA(rtAttr), rtInfo->ifName);
  147. break;
  148. case RTA_GATEWAY:
  149. rtInfo->gateWay = *(u_int *)RTA_DATA(rtAttr);
  150. break;
  151. case RTA_PREFSRC:
  152. rtInfo->srcAddr = *(u_int *)RTA_DATA(rtAttr);
  153. break;
  154. case RTA_DST:
  155. rtInfo->dstAddr = *(u_int *)RTA_DATA(rtAttr);
  156. break;
  157. }
  158. }
  159. //printf("%s\n", (char *)inet_ntoa(rtInfo->dstAddr));
  160. //ADDED BY BOB - ALSO COMMENTED Joseph_PrintRoute
  161. if (strstr((char *)inet_ntoa(rtInfo->dstAddr), "0.0.0.0"))
  162. {
  163. sprintf(gateway,"%s",(char *)inet_ntoa(rtInfo->gateWay));
  164. }
  165. Joseph_PrintRoute(rtInfo,if_name_in);
  166. free(tempBuf);
  167. tempBuf = NULL;
  168. return 0;
  169. }
  170. int Joseph_Get_Gateway(char *gateway,char *if_name)
  171. {
  172. struct nlmsghdr *nlMsg;
  173. struct rtmsg *rtMsg;
  174. struct route_info *rtInfo;
  175. char msgBuf[JSOEPH_NET_RMSG_BUFSIZE];
  176. int sock, len, msgSeq = 0;
  177. char buff[1024];
  178. if(strlen(if_name) == 0 || gateway == NULL)
  179. {
  180. return -1;
  181. }
  182. /* Create Socket */
  183. if((sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0)
  184. {
  185. printf("Socket Creation Error !\n");
  186. return -1;
  187. }
  188. /* Initialize the buffer */
  189. memset(msgBuf, 0, JSOEPH_NET_RMSG_BUFSIZE);
  190. /* point the header and the msg structure pointers into the buffer */
  191. nlMsg = (struct nlmsghdr *)msgBuf;
  192. rtMsg = (struct rtmsg *)NLMSG_DATA(nlMsg);
  193. /* Fill in the nlmsg header*/
  194. nlMsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); // Length of message.
  195. nlMsg->nlmsg_type = RTM_GETROUTE; // Get the routes from kernel routing table .
  196. nlMsg->nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST; // The message is a request for dump.
  197. nlMsg->nlmsg_seq = msgSeq++; // Sequence of the message packet.
  198. nlMsg->nlmsg_pid = getpid(); // PID of process sending the request.
  199. /* Send the request */
  200. if(send(sock, nlMsg, nlMsg->nlmsg_len, 0) < 0)
  201. {
  202. printf("Write To Socket Failed...\n");
  203. close(sock);
  204. return -1;
  205. }
  206. /* Read the response */
  207. if((len = Joseph_ReadNlSock(sock, msgBuf, msgSeq, getpid())) < 0)
  208. {
  209. printf("Read From Socket Failed...\n");
  210. close(sock);
  211. return -1;
  212. }
  213. /* Parse and print the response */
  214. rtInfo = (struct route_info *)malloc(sizeof(struct route_info));
  215. /* THIS IS THE NETTSTAT -RL code I commented out the printing here and in parse routes */
  216. //fprintf(stdout, "Destination\tGateway\tInterface\tSource\n");
  217. for( ; NLMSG_OK(nlMsg,len); nlMsg = NLMSG_NEXT(nlMsg,len))
  218. {
  219. memset(rtInfo, 0, sizeof(struct route_info));
  220. Joseph_ParseRoutes(nlMsg,rtInfo,gateway,if_name);
  221. }
  222. free(rtInfo);
  223. rtInfo = NULL;
  224. close(sock);
  225. return 0;
  226. }
  227. int main(int argc,char *argv[])
  228. {
  229. int itertion = 0;
  230. char gateway[16]={0};
  231. int Qy_Ret = 0;
  232. if(argc != 2)
  233. {
  234. return -1;
  235. }
  236. while(itertion < 30)
  237. {
  238. Qy_Ret = Joseph_Get_Gateway(gateway,argv[1]);
  239. if(Qy_Ret <0)
  240. {
  241. return -1;
  242. }
  243. itertion++;
  244. printf("Gateway:%s\n", gateway);
  245. sleep(1);
  246. }
  247. return 0;
  248. }
  249. </span></span>

from:http://blog.csdn.net/skdkjzz/article/details/40427171

嵌入式 hi3518平台获取网关的更多相关文章

  1. 嵌入式 hi3518平台获取网络环境中的ip、netmask、broadcast等信息

    <span style="font-family:Courier New;"> /********************************** (C) COPY ...

  2. 微服务监控平台获取网关(zuul)配置列表

    步骤: (1)读取zuul的配置文件,获取路由配置项信息: private static Properties props; static { String fileName = "appl ...

  3. 嵌入式 hi3518平台uboot引导nfs文件系统

    首先贴出来我的bootargs的设置(注没有换行符!!!): setenv bootargs noinitrd mem=64M root=/dev/nfs init=/linuxrc rw nfsro ...

  4. 嵌入式 hi3518平台检测网线是否插上

    /********************************** (C) COPYRIGHT ******************************* * File Name        ...

  5. 嵌入式 hi3518平台指定网卡测试是否通外网

    版权声明:本文为博主原创文章,未经博主允许不得转载. /********************************** (C) COPYRIGHT *********************** ...

  6. 嵌入式 hi3518平台增加路由代码

    <span style="font-family:Courier New;"> /********************************** (C) COPY ...

  7. 嵌入式 hi3518平台以太网网络模块设计包括重连机制和网线检测机制

    <span style="font-family:Courier New;"> #include <sys/types.h> #include <st ...

  8. 嵌入式 hi3518平台多路码流添加osd

    <span style="font-family:Courier New;"> /******************************************* ...

  9. 使用腾讯开发平台获取QQ用户数据资料

    <今天是七夕:祝大家七夕嗨皮,前可么么哒,后可啪啪啪> Tips:本篇博客将教你如何使用腾讯开发平台获取QQ用户资料 ----------------------------------- ...

随机推荐

  1. converntion

    One convention that we have is to use the names of fruits and vegetables for variables(only in small ...

  2. 定制CentOS (Redhat AS 5.1)安装盘

    CentOS(Redhat)提供了一套完整的自动化安装机制,利用该机制,我们可以自己定制无人值守的自动安装光盘,也可以进行系统裁减,甚至可以以CentOS为基础制作自己软件系统的系统安装盘.以下全部内 ...

  3. Maven的安装

    我对maven的了解,仅仅局限在百度百科. 由于近期公司需求,我找到了个maven教程:http://wentao365.iteye.com/blog/903396 安装maven其实很简单,就是在A ...

  4. 修改bigbluebutton白板上传中文乱码

          中文命名的文档,上传是乱码 -- 显示的 打开后, 中文部分是乱码 Comment 1 by project member ffdixon, Nov 08, 2010 Translatio ...

  5. ios开发--GCD使用介绍:4-延迟执行操作

    在开发过程中,我们有时会希望把一些操作封装起来延迟一段时间后再执行.iOS开发中,有两种常用的方法可以实现延迟执行,一种是使用GCD,另外一种是使用NSRunLoop类中提供的方法. 1.使用GCD实 ...

  6. iOS开发--iOS及Mac开源项目和学习资料

    文/零距离仰望星空(简书作者)原文链接:http://www.jianshu.com/p/f6cdbc8192ba著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文出处:codecl ...

  7. 配置Tomcat的服务端口

  8. Mysql笔记——DML

    数据操纵语言DML(Data Manipulation Language),用户通过它可以实现对数据库的基本操作. ========================== 1 插入数据 语法:INSER ...

  9. Redis的Order Set操作

    有序集合 zadd key score1 value1 score2 value2 .. 添加元素 127.0.0.1:6379> zadd class 12 lily 13 lucy 18 l ...

  10. GraphicsMagick / ImageMagick缺少lib报错no decode delegate for this image format

    下载相应的lib,编译安装就行了 cd ~ #下载包 wget http://www.imagemagick.org/download/delegates/zlib-1.2.7.tar.gz wget ...