linux下发sockect连通性测试,Linux下用Socket检测设备的挂载(实现设备的自动挂载)...

/**********************************************************************************

2012.12.29 -->> Linux下用Socket检测设备的挂载(实现设备的自动挂载)

关于Wifi网络名称和密码配置:

方案一:

在硬盘的根目录下新建一个文件:wifi_cfg.txt.

内容(模板)如下:

WLAN_NAME=TEST WLan

WLAN_PASSWORD=23456789

其中"TEST WLan"可以替换为其他的名称作为“Wifi网络名”。

"23456789"为连接本网络的密码。

系统在检测到USB Wifi之后,就会去探测这个文件,如果有,并且数据有效,

就会按照本配置来设置网络,否则系统使用方案二。

方案二:

如果在硬盘的根目录下没有找到文件:wifi_cfg.txt.

则系统会使用一个默认的Wifi网络名和密码.

默认的网络名称为:HACK WLan

默认的网络密码为:12345678

(本默认网络名和密码,你们可以提出更改为其他名称 作为默认)

注意:

1.在系统中,我限制了网络名称为64个字节以内。

2.设置网络密码时,如果位数小于8位,视为无效密码,则系统使用默认配置。

***********************************************************************************/

#define TMP_BUF_LEN(64)

#define WifiConfigName"/mnt/hd1/wifi_cfg.txt"

char pFixedUSBWifiName[TMP_BUF_LEN]={0};

char pFixedUSBWifiPassWord[TMP_BUF_LEN]={0};

static void CheckStringNULL(char *pInputString,int len)

{

int i=0;

for(i=0;i=line_idx))

break;

}

if(pOutputBuf && (i==line_idx))

{

int length=strlen(line)>bufferLen?bufferLen:strlen(line);

memcpy(pOutputBuf,line,length);

}else

{

i=-1;

}

if (line != NULL)free(line);

if (fp != NULL)fclose(fp);

return i;

}

/*

return :

-1 : name or password is invalid.

0 : name and password are valid.

*/

static int GetWifiNameAndPassword()

{

char *p=NULL;

char *pTempBuffer=NULL;

if ( access(WifiConfigName, F_OK ) == 0 )

{

memset(pFixedUSBWifiName,0,TMP_BUF_LEN);

memset(pFixedUSBWifiPassWord,0,TMP_BUF_LEN);

if( (1==get_file_line(WifiConfigName,pFixedUSBWifiName,TMP_BUF_LEN,1))

&& (2==get_file_line(WifiConfigName,pFixedUSBWifiPassWord,TMP_BUF_LEN,2)) )

{// name and password exist.

if(NULL!=(pTempBuffer=malloc(sizeof(char)*TMP_BUF_LEN)))

{

memcpy(pTempBuffer,pFixedUSBWifiName,TMP_BUF_LEN);

memset(pFixedUSBWifiName,0,TMP_BUF_LEN);

p=strchr(pTempBuffer,'=');

if(NULL!=p)memcpy(pFixedUSBWifiName,p+1,pTempBuffer+TMP_BUF_LEN-p);

else

goto ret_error;

CheckStringNULL(pFixedUSBWifiName,pTempBuffer+TMP_BUF_LEN-p);

//printf("000 pFixedUSBWifiName === [%s] \n",pFixedUSBWifiName);

memcpy(pTempBuffer,pFixedUSBWifiPassWord,TMP_BUF_LEN);

memset(pFixedUSBWifiPassWord,0,TMP_BUF_LEN);

p=strchr(pTempBuffer,'=');

if(NULL!=p)memcpy(pFixedUSBWifiPassWord,p+1,pTempBuffer+TMP_BUF_LEN-p);

else

goto ret_error;

//pFixedUSBWifiPassWord[pTempBuffer+TMP_BUF_LEN-p]="\0";

CheckStringNULL(pFixedUSBWifiPassWord,pTempBuffer+TMP_BUF_LEN-p);

//printf("000 pFixedUSBWifiPassWord === [%s] [%d]\n",pFixedUSBWifiPassWord,strlen(pFixedUSBWifiPassWord));

if(strlen(pFixedUSBWifiPassWord)<8)goto ret_error;

if(NULL!=pTempBuffer)free(pTempBuffer);

return 0;

}else

goto ret_error;

}else

goto ret_error;

}else

{// donot exist wifi config file.

goto ret_error;

}

ret_error:

//printf("111 pFixedUSBWifiName === [%s] \n",pFixedUSBWifiName);

//printf("111 pFixedUSBWifiPassWord === [%s] \n",pFixedUSBWifiPassWord);

if(NULL!=pTempBuffer)free(pTempBuffer);

return -1;

}

static void MountUSBWiFi(void)

{

char *pTempCmdString=NULL;

system("killall dhcpd");

system("ifconfig ra0 192.168.0.1 netmask 255.255.255.0");

system("/etc/Wireless/RT2870AP/iwpriv ra0 set AuthMode=WPA2PSK");

system("/etc/Wireless/RT2870AP/iwpriv ra0 set EncrypType=TKIP");

#if 1

if(0==GetWifiNameAndPassword())

{// use fixed name and password.

if(NULL!=(pTempCmdString=malloc(sizeof(char)*TMP_BUF_LEN*2)))

{

memset(pTempCmdString,0,TMP_BUF_LEN*2);

sprintf(pTempCmdString,"/etc/Wireless/RT2870AP/iwpriv ra0 set SSID=\"%s\"",pFixedUSBWifiName);

//printf(" pTempCmdString ========== [%s] \n",pTempCmdString);

system(pTempCmdString);

memset(pTempCmdString,0,TMP_BUF_LEN*2);

sprintf(pTempCmdString,"/etc/Wireless/RT2870AP/iwpriv ra0 set WPAPSK=%s",pFixedUSBWifiPassWord);

//printf(" pTempCmdString ========== [%s] \n",pTempCmdString);

system(pTempCmdString);

memset(pTempCmdString,0,TMP_BUF_LEN*2);

sprintf(pTempCmdString,"/etc/Wireless/RT2870AP/iwpriv ra0 set SSID=\"%s\"",pFixedUSBWifiName);

//printf(" pTempCmdString ========== [%s] \n",pTempCmdString);

system(pTempCmdString);

free(pTempCmdString);

pTempCmdString=NULL;

}

}

else

#endif

{// use default name and password.

system("/etc/Wireless/RT2870AP/iwpriv ra0 set SSID=\"HACK WLan\"");

system("/etc/Wireless/RT2870AP/iwpriv ra0 set WPAPSK=12345678");

system("/etc/Wireless/RT2870AP/iwpriv ra0 set SSID=\"HACK WLan\"");

}

system("/etc/Wireless/RT2870AP/dhcpd &");

return 0;

}

static int InitHotplugSock(void)

{

struct sockaddr_nl snl;

const int buffersize = 16 * 1024;

int retval;

memset(&snl, 0x00, sizeof(struct sockaddr_nl));

snl.nl_family = AF_NETLINK;

snl.nl_pid = getpid();

snl.nl_groups = 1;

int hotplug_sock = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);

if (hotplug_sock == -1) {

printf("error getting socket: %s", strerror(errno));

return -1;

}

/* set receive buffersize */

setsockopt(hotplug_sock, SOL_SOCKET, SO_RCVBUFFORCE, &buffersize, sizeof(buffersize));

retval = bind(hotplug_sock, (struct sockaddr *) &snl, sizeof(struct sockaddr_nl));

if (retval < 0) {

printf("bind failed: %s", strerror(errno));

close(hotplug_sock);

hotplug_sock = -1;

return -1;

}

return hotplug_sock;

}

void CheackUSBWiFi(void)

{

#define UEVENT_BUFFER_SIZE 2048

int hotplug_sock;

char buf_cache[UEVENT_BUFFER_SIZE],*pTempSeekPt,*pTempSeek2;

int tTempNum,tMntValue;

int tMntFlag;

int tRemoveFlag = 0;

hotplug_sock = InitHotplugSock();

memset(buf_cache, 0 ,sizeof(buf_cache));

while(1)

{

tTempNum=recv(hotplug_sock,buf_cache, sizeof(buf_cache), 0);

if( tTempNum<0 )

{

printf("======usb hot plug error!!!=======\n");

break;

}

if( tTempNum!=0 )

{

buf_cache[tTempNum]=0;

//printf("buf_cache (pinggle print): %s \n", buf_cache);

if( NULL!=strstr(buf_cache,"add@/devices/virtual/net/ra0") )

{

printf("usb ==== ADD pinggle print \n");

MountUSBWiFi();

}

if( NULL!=strstr(buf_cache,"remove@/devices/virtual/net/ra0") )

{

printf("usb ==== Remove pinggle print \n");

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值