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. CentOS 配置vncserver

    一.安装 以root用户运行以下命令来安装vncserver; yum install tigervnc-server 同样运行以下命令来安装vncviewer; yum install vnc 停止 ...

  2. 物联网操作系统Hello China V1.76(PC串口版)版本发布

    作为向ARM平台移植的基线版本,经过三个多月的努力,Hello China V1.76终于完成并发布.相对原来发布的V1.75版本,该版本主要做了如下修改: 彻底去掉了原来版本源代码中的C++特性,采 ...

  3. iOS Core Telephony Framework

    Core Telephony Framework(核心通讯框架) 概述: 这个库的前缀为CT(Core Telephony),主要用来获得用户通讯相关信息,我们可以使用这些信息来定义外部接口以便自己使 ...

  4. HDFS dfsclient写文件过程 源码分析

    HDFS写入文件的重要概念 HDFS一个文件由多个block构成.HDFS在进行block读写的时候是以packet(默认每个packet为64K)为单位进行的.每一个packet由若干个chunk( ...

  5. eclipse运行mapreduce报错Permission denied

    今天用在eclipse-hadoop平台上运行map reduce(word count)出错了,错误信息为 org.apache.hadoop.security.AccessControlExcep ...

  6. windows 7 ssh server for scp

    Software: BvSshServe. (个人用免费,商业收费) scp localfile.txt user_tst@11.111.12.170:'E:\downloads\SSH\auto.p ...

  7. 《c程序设计语言》读书笔记-字符型0-9转为数字0-9

    #include <stdio.h> #define Num 10 int atoi(char s[]); int main() { int c,i = 0; char s[Num]; i ...

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

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

  9. cdoj 1334 郭大侠与Rabi-Ribi 贪心+数据结构

    郭大侠与Rabi-Ribi Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Su ...

  10. gridview 单击行时如何让SelectedIndexChanging事件响应

    在gridview控件上单击行的时候,是不触发SelectedIndexChanging事件的,那么想要单击时触发SelectedIndexChanging事件时怎么做呢? 我是这样做的: 在grid ...