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. lintcode:数字组合 II

    数字组合 II 给出一组候选数字(C)和目标数字(T),找出C中所有的组合,使组合中数字的和为T.C中每个数字在每个组合中只能使用一次. 注意事项 所有的数字(包括目标数字)均为正整数. 元素组合(a ...

  2. CF 353A Domino

    #include<stdio.h> #include<math.h> int main() { int i,n; int x,y; int m1,m2,m3,m4; while ...

  3. iOS开发--CoreGraphics简单绘图

    一.导入coreGraphics.framework 二.绘制图形 1.绘制矩形 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // 绘制矩形 - (v ...

  4. QQ群共享文件下载很慢解决办法

    QQ群共享文件下载很慢解决办法.我们经常会不群里面共享文件,文件文件稍大,下载非常慢.家庭是20M的网速,一般正常下载能够达到2.5MB左右,而在QQ群实际下载网速却只有80KB左右.如果要下1G,就 ...

  5. swift:入门知识之类和对象

    1.swift中使用class创建一个类.一个类的声明则是在类里作为常量或变量声明的,除了是在类的上下文中.在方法和函数中也是这么写的. 2.swift中使用init(...)作为初始化构造函数 3. ...

  6. linux怎么运行.SH文件

    执行sh xx.sh命令就可以执行.sh文件了.如果直接执行xx.sh文件,就报权限错误 解决办法:执行chmod u+x xx.sh 来添加执行权限

  7. php常用代码(一)

    1.连接MYSQL数据库代码 <?php $connec=mysql_connect("localhost","root","root" ...

  8. JVM的相关知识整理和学习--(转载)

    JVM是虚拟机,也是一种规范,他遵循着冯·诺依曼体系结构的设计原理.冯·诺依曼体系结构中,指出计算机处理的数据和指令都是二进制数,采用存储程序方式不加区分的存储在同一个存储器里,并且顺序执行,指令由操 ...

  9. python3代码

    import urllib.request url="http://mm.taobao.com/json/request_top_list.htm?type=0&page=1&quo ...

  10. 《OD学HBase》20160814

    一.HBase引入 http://hbase.apache.org/ 大数据的数据库 1. 概述 Hadoop生态系统中的一个分布式.可拓展.面向列.可伸缩,具有自动容错功能的数据库. NoSQL数据 ...