嵌入式 hi3518平台获取网关

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

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

原文地址:https://www.cnblogs.com/lidabo/p/5383970.html