嵌入式 指定网卡名称的信息以及网络连通状态以及重连机制代码示例

本文提供了一个嵌入式系统中针对指定网卡进行网络状态检测和自动重连的代码示例,涵盖了网络接口状态的查询以及在网络断开后实现自动恢复连接的机制。
摘要由CSDN通过智能技术生成

#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <signal.h>
#include <dirent.h>
#include <pthread.h>

#include <asm/types.h>
#include <arpa/inet.h>

#include <sys/vfs.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/utsname.h>

#include <netdb.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/route.h>
#include <net/if_arp.h>
 
#include <linux/sockios.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>

#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ether.h>
#include <netinet/ip_icmp.h>

#define JOSEPH_LOOP_INTERFACE "lo"
#define JOSEPH_ETH_INTERFACE "eth0"
#define JOSEPH_WIRLESS_INTERFACE "wlan0"
#define ETHTOOL_GLINK 0x0000000a /* Get link status (ethtool_value) */
#define JSOEPH_NET_CHECK_PACKET_SIZE 4096
#define JOSEPH_NET_CHECK_TIME 6000
#define JSOEPH_NET_RMSG_BUFSIZE 8192

typedef struct Joseph_Ethtool_Value {
 unsigned int cmd;
 unsigned int data;
}JOSEPH_ETHTOOL_VALUE;

typedef struct route_info{
 struct in_addr dstAddr;
 struct in_addr srcAddr;
 struct in_addr gateWay;
 char ifName[IF_NAMESIZE];
}JOSEPH_ROUTE_INFO;

#ifdef JOSEPH_CAT_ENUM
/* Routing message attributes */
enum rtattr_type_t {
 RTA_UNSPEC,
 RTA_DST,
 RTA_SRC,
 RTA_IIF,
 RTA_OIF,
 RTA_GATEWAY,
 RTA_PRIORITY,
 RTA_PREFSRC,
 RTA_METRICS,
 RTA_MULTIPATH,
 RTA_PROTOINFO, /* no longer used */
 RTA_FLOW,
 RTA_CACHEINFO,
 RTA_SESSION, /* no longer used */
 RTA_MP_ALGO, /* no longer used */
 RTA_TABLE,
 RTA_MARK,
 __RTA_MAX
};
#endif

typedef struct Joseph_Net_Interface_Info
{
 int net_device_type;// 0 ~ eth0 ; 1 ~ wlan ;3 ~ ppp0
 int net_device_priority;// 0 ~ eth0 ; 1 ~ wlan ;3 ~ ppp0
 int net_device_status;//0 ~ down; 1 ~ up
 int net_device_link_status;//0 ~ no ;1 ~ yes
 char net_device_name[8];
 char net_device_ip[16];
 char net_device_lan_dst[16];//dstaddr in route table
 char net_device_internet_dst[16];//dstaddr in route table
 char net_device_mac_info[32];
 char net_device_gw_info[16];
 char net_device_mask_info[16];
 char net_device_broadcast_info[16];
 
}JOSEPH_NET_INTERFACE_INFO;


/*joseph check net link status*/
enum Joseph_Net_Device_Type
{
 JOSEPH_ETH = 0,
 JOSEPH_WIFI = 1,
 JOSEPH_3G = 2,
}JOSEPH_NET_DEVICE_TYPE;

#include "joseph_av_common.h"

unsigned short Joseph_Cal_Chksum(unsigned short *addr, int len)
{
 int nleft = len;
 int sum = 0;
 unsigned short *w = addr;
 unsigned short answer = 0;

 while(nleft > 1)
 {
   sum += *w++;
   nleft -= 2;
 }

 if( nleft == 1)
 {
   *(unsigned char *)(&answer) = *(unsigned char *)w;
   sum += answer;
 }

 sum = (sum >> 16) + (sum & 0xffff);
 sum += (sum >> 16);
 answer = ~sum;

 return answer;
}

/*
Author : kj
Timer: 2014-08-14
Function :
 0 ~ allow ping int area of Lan and Internet
 1 ~ forbit ping int area of Lan and Internet
*/
int Joseph_Net_Icmp_Control(char *value)
{
 int Qy_Ret = JOSEPH_RET_OK;
 int Joseph_Icmp_Value = 0;
 
 if(strlen(value) != 1)
 {
  Qy_Ret = JOSEPH_RET_ERR1;
  return Qy_Ret;
 }

 Joseph_Icmp_Value = atoi(value);
 switch(Joseph_Icmp_Value)
 {
  case 0:
   {
    system("echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all");
    break;
   }
  case 1:
   {
    system("echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all");
    break;
   }
  default:
   {
    Qy_Ret = JOSEPH_RET_ERR1;
    break;
   }

 }
 
 return Qy_Ret;
}

int Joseph_Ping( char *ips,char *srcip , int timeout)
{

 int n;

 pid_t pid;
 int maxfds = 0;
 fd_set readfds;

 struct ip *iph;
 struct ifreq ifr;
 struct icmp *icmp;
 struct timeval *tval;
 struct timeval timeo;
 struct sockaddr_in addr;
 struct sockaddr_in from;

 int Qy_Ret = JOSEPH_RET_OK;
 
 bzero(&addr,sizeof(addr));
 addr.sin_family = AF_INET;
 addr.sin_addr.s_addr = inet_addr(ips);

 int sockfd;
 sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
 if (sockfd < 0)
 {
   joseph_personal_eth_print("%s:[%d] Socket Create Error ,The Src Ip is %s !\n",\
    __FUNCTION__,__LINE__,ips);
   
   Qy_Ret = JOSEPH_RET_ERR1;
   return Qy_Ret;
 }


 timeo.tv_sec = timeout / 1000;
 timeo.tv_usec = timeout % 1000;

#if 0

 /*set src ip*/
 bzero(&from,sizeof(from)); /* É趨IpÐÅÏ¢ */
 from.sin_family = AF_INET;
 from.sin_addr.s_addr = inet_addr(srcip);

 if (setsockopt(sockfd, IPPROTO_
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值