转一段使用C与wpa设置wifi的代码

来源:互联网 发布:国外免费linux服务器 编辑:程序博客网 时间:2024/05/01 04:06


/******************************************************************
 Name:        iSTCClientOpen
 Description: open a socket for wifi
 ******************************************************************/
int iSTCClientOpen(void)
    {
    Client_t *pClient = (Client_t *)calloc(1, sizeof(Client_t));


    if (pClient != NULL)
        {
        pClient->sockFd = socket(AF_INET, SOCK_STREAM, 0);
        if (pClient->sockFd < 0)
            {
            printf("@err: socket() for Send\n");
            goto fail;
            }


        pClient->thisAddr.sin_family = AF_INET;
        pClient->thisAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        pClient->thisAddr.sin_port = ntohs(INADDR_ANY);


        if (bind(pClient->sockFd, (struct sockaddr *) &pClient->thisAddr, sizeof(pClient->thisAddr)) < 0)
            {
            printf("@err: bind error(%x).\n", errno);
            goto fail;
            }


        pClient->peerAddr.sin_family = AF_INET;
        pClient->peerAddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        pClient->peerAddr.sin_port = ntohs(DEFAULT_PORT);


        if (connect(pClient->sockFd, (struct sockaddr *)&pClient->peerAddr, sizeof(pClient->peerAddr)) < 0)
            {
            printf("@err: connect %d:%s\n", errno,strerror(errno));
            goto fail;
            }
        }


    return (int)pClient;


fail:
    if (pClient)
        {
        if (pClient->sockFd >= 0)
            close(pClient->sockFd);
        free(pClient);
        }


    return 0;
    }


int iSTC_WIFI_Connect(char *ESSID, char *Password,int Encryptiontype, char *ifname, int timeout)
    {
    int ret = -1;
    int len;
    char buf[256];
    int cid = iSTCClientOpen();
    if(cid == 0)
    {
        return -1;
    }
    Client_t *pClient = (Client_t *)cid;


    iSTCHead_t *pHead = (iSTCHead_t *)buf;
    iSTCWifiConnect_t *pConnect = (iSTCWifiConnect_t *)pHead->data;


    strncpy(pConnect->ESSID, ESSID,sizeof(pConnect->ESSID));
    strncpy(pConnect->Password, Password,sizeof(pConnect->Password));
#ifndef USEISTCDAEMON
    strncpy(pConnect->ifname,ifname,sizeof(pConnect->ifname));
    pConnect->Encryption_type = Encryptiontype;
#endif
    pConnect->timeout = timeout;


    pHead->version  = iSTC_VERSION;
    pHead->type     = iSTC_TYPE_REQUEST;
    pHead->length   = sizeof(iSTCWifiConnect_t);
    pHead->seq      = 0;
    pHead->command  = iSTC_CMD_WIFI_CONNECT;
    pHead->checksum = 0;
    printf("Encryptiontype %d ,%d,%d\n",Encryptiontype,sizeof(iSTCHead_t),pHead->length);
    len = send(pClient->sockFd, pHead, sizeof(iSTCHead_t) + pHead->length, 0);
    if (len != sizeof(iSTCHead_t) + pHead->length) goto bye;
    printf("send over\n");
    len = read(pClient->sockFd, pHead, sizeof(iSTCHead_t));
    if (len != sizeof(iSTCHead_t)) goto bye;
printf("read over\n");
    if (pHead->response == iSTC_OK)
        ret = 0;
    else
        ret = -1;
if(pHead->response == iSTC_ERROR_SYSTEM)
{
printf("----------------wpa error ---------\n");
system("killall wpa_cli\n");
system("killall wpa_supplicant\n");
sleep(1);
system("wpa_supplicant -B -c /etc/wpa_supplicant/wpa_supplicant.conf -i wlan0\n");
sleep(1);
    system("wpa_cli &\n");
}
bye:
    iSTCClientClose(cid);
    return ret;
    }





0 0