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. Data Base sqlServer 组合主键

    sqlServer   组合主键 创建表时: create table Person ( Name1 ) not null ,Name2 ) not null primary key(Name1,Na ...

  2. Html 全屏切换效果

    来源 http://www.imooc.com/learn/374 pageswitch.js (function ($) { var defaults = { 'container': '#cont ...

  3. rqnoj-106-最大加权矩形-dp

    和我之前做的那个切西瓜的题目相比就是小巫见大巫了.. 运用最长字段和的原理把O(n^4)转化成O(n^3) #include<stdio.h> #include<string.h&g ...

  4. Entity Framework学习 - 3.关联查询

    1.Inner Join(默认) var Goods = from goods in db.T_Goods                    join types in db.T_GoodsTyp ...

  5. kettle的jdk1.7环境变量配置

    1).到官网下载需要安装的kettle版本,目前最新版本4.2,官网地址:http://kettle.pentaho.org,我们是使用的版本是kettle3.2 2).本地安装jdk 1.4或以上版 ...

  6. python3代码

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

  7. SPOJ 1739 Yet Another Equation(Pell方程)

    题目链接:http://www.spoj.com/problems/EQU2/ 题意:给出方程x^2-n*y^2=1的最小整数解. 思路:参见金斌大牛的论文<欧几里得算法的应用>. imp ...

  8. [CF580B]Kefa and Company(滑动窗口)

    题目链接:http://codeforces.com/problemset/problem/580/B 某人有n个朋友,这n个朋友有钱数m和关系s两个属性.问如何选择朋友,使得这些朋友之间s最大差距小 ...

  9. MongoDB 学习笔记(一)基础篇

    1.MongoDB 特点 面向集合存储,存储对象类型的数据方便 模式自由,不需要定义任何模式(schma) 动态查询 完全索引,包含内部对象 复制和故障恢复方便 高效的二进制数据存储 支持c# 平台驱 ...

  10. CSS 中定位的使用

    position relative 设置区块基准点为左上角(相对定位 以区块的左上角为基准点 仍然会暂居原来的位置) a.不影响元素本身的特性: b.不使元素脱离文档流: c.如果没有定位偏移量,对元 ...