main_Lindsay.cpp
作者:互联网
#include <fstream>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include "inetwork_types.h"
#ifdef NETWORK_LOG_TAG
#undef NETWORK_LOG_TAG
#endif
#define NETWORK_LOG_TAG "Main"
#define WLAN_WIFI_SSID 24
enNetworkMessage g_network_layer = g_network_all;
typedef enum
{
enOK = 1,
enInputParaError = 2,
enTypeError = 3,
enInputFileError = 4,
enMax = 99,
} enReturn;
typedef struct wifi_network
{
int netid; // The ID number that the supplicant uses to identify this network configuration entry.
int status; // The current network status
char ssid[WLAN_WIFI_SSID + 1];
char bssid[WLAN_WIFI_SSID];
} *WiFi_Network_Node;
/*!
* @brief check ip address whether valid, and cut redundant 0.
* @example: Invalid IP : 172.1.443.555 172.00001.4430 172.-1.224.001
* @example: valid IP : 172.055.043.005 -> 172.55.43.5
* @param Buf_trans : transformed string
* @param Buf_ori : IPv4 address string
* @param length : length of Buf_trans
* @return ture : Valid IPv4 address ; false: Invalid IPv4 address
*/
bool checkIPv4Addr(char* Buf_ori, int length)
{
NETWORK_TRACE("in\n");
NETWORK_DBG("Buf_ori = %p, length = %d\n", Buf_ori, length);
char* pstr1 = NULL;
char* pstr2 = NULL;
char* Buf_trans = NULL;
char arr[4][4] = {0};
int ip[4] = {0};
int ip_len = 0;
int len_org = 0;
int i = 0;
bool bcheck = true;
if (NULL == Buf_ori)
{
bcheck = false;
NETWORK_EBG("Buf_ori is NULL\n");
NETWORK_DBG("bcheck:%d\n", bcheck);
NETWORK_TRACE("out\n");
return bcheck;
}
else
{
NETWORK_DBG("Buf_ori = %p\n", Buf_ori);
pstr1 = Buf_ori;
pstr2 = Buf_ori;
}
NETWORK_DBG("Buf_ori = %s, length = %d\n", Buf_ori, length);
memset(ip, 0, 4 * sizeof(int));
Buf_ori[length - 1] = '\0';
len_org = strlen(Buf_ori);
if (NULL == pstr2)
{
bcheck = false;
NETWORK_EBG("pstr2 is NULL\n");
NETWORK_TRACE("out\n");
return bcheck;
}
while (*pstr2 != '\0' && (pstr2 - Buf_ori < len_org))
{
if (!((*pstr2 >= '0' && *pstr2 <= '9') || *pstr2 == '.'))
{
NETWORK_DBG("*pstr2= %x invalid char \n", *pstr2);
bcheck = false;
break;
}
else
{
NETWORK_DBG("*pstr2 = %x valid char \n", *pstr2);
}
pstr2++;
}
NETWORK_DBG("*pstr2=%x\n *pstr1=%x\n", *pstr2, *pstr1);
for (i = 0; i < 4; i++)
{
if (pstr1 != NULL)
{
NETWORK_DBG("*pstr2 = %x valid char \n", *pstr2);
pstr2 = strstr(pstr1, ".");
}
else
{
NETWORK_EBG("pstr1 is Null\n");
bcheck = false;
break;
}
if (pstr2 == NULL && i != 3)
{
NETWORK_EBG("pstr2 is Null\n");
bcheck = false;
break;
}
NETWORK_DBG("pstr2:%s\n", pstr2);
if (i == 3)
{
ip_len = Buf_ori + strlen(Buf_ori) - pstr1;
}
else
{
ip_len = pstr2 - pstr1;
}
NETWORK_DBG("ip_len:%d\n", ip_len);
if (ip_len > 0 && ip_len < 4)
{
strncpy(arr[i], pstr1, ip_len);
arr[i][ip_len] = '\0';
ip[i] = atoi(arr[i]);
}
else
{
NETWORK_EBG("ip_len is NULL\n");
bcheck = false;
break;
}
NETWORK_DBG("ip[%d]:%d\n", i, ip[i]);
if (ip[i] < 0 || ip[i] > 255)
{
NETWORK_EBG("ip[i] is wrong\n");
bcheck = false;
break;
}
else
{
NETWORK_DBG("ip[i] = %d\n", ip[i]);
}
pstr1 = pstr2 + 1;
}
Buf_trans = (char *)malloc(length);
if (NULL != Buf_trans)
{
memset(Buf_trans, 0, length);
}
else
{
bcheck = false;
NETWORK_EBG("Buf_trans is NULL\n");
}
snprintf(Buf_trans, length-1, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
if (false == bcheck)
{
NETWORK_EBG("IPv4 address error! %s,%s\n", Buf_ori, Buf_trans);
}
else
{
NETWORK_DBG("IPv4 address OK! %s,%s\n", Buf_ori, Buf_trans);
}
if (NULL != Buf_trans)
{
free(Buf_trans);
Buf_trans = NULL;
}
NETWORK_DBG("bcheck = %d\n", bcheck);
NETWORK_TRACE("out\n");
return bcheck;
}
/*!
* @brief parse the line as the structure of network-id | ssid | bssid | flags
* @example: input line : 3357*TP-LINK*e8bf9ee794a8e794b5e5bdb1*[CURRENT]
* @param[in] char* start
* @param[in] *wifinet
*
* @return true: success, false: failed
*/
bool parse_one_line_conf(char* start, struct wifi_network *wifinet)
{
NETWORK_TRACE("in\n");
NETWORK_DBG("start:%p wifinet:%p\n", start, wifinet);
char* id = NULL;
char* ssid = NULL;
char* bssid = NULL;
char* flags = NULL;
//int ssid_len = 0;
//char* SpecialSymbol = NULL;
bool ret = true;
if (NULL == start || NULL == wifinet)
{
ret = false;
NETWORK_EBG("start or wifinet is NULL\n");
goto EXIT;
}
else
{
NETWORK_DBG("start = %p, wifinet = %p\n", start, wifinet);
}
id = start;
ssid = strchr(id, '*');
if (NULL == ssid)
{
ret = false;
NETWORK_EBG("ssid is NULL\n");
goto EXIT;
}
*ssid++ = '\0';
bssid = strchr(ssid, '*');
if (NULL == bssid)
{
ret = false;
NETWORK_EBG("bssid is NULL\n");
goto EXIT;
}
*bssid++ = '\0';
flags = strchr(bssid, '*');
NETWORK_DBG("ssid = %s bssid = %s \n", ssid,bssid);
if (flags == NULL)
{
ret = false;
NETWORK_EBG("flags is NULL\n");
goto EXIT;
}
*flags++ = '\0';
wifinet->netid = strtol(id, NULL, 10);
if ((WLAN_WIFI_SSID + 1) > strlen(ssid) && WLAN_WIFI_SSID > (strlen(bssid)))
{
strncpy(wifinet->ssid, ssid, WLAN_WIFI_SSID);
wifinet->ssid[WLAN_WIFI_SSID] = '\0';
memcpy(wifinet->bssid, bssid, WLAN_WIFI_SSID);
wifinet->bssid[WLAN_WIFI_SSID] = '\0';
}
else
{
ret = false;
NETWORK_EBG("error\n");
goto EXIT;
}
NETWORK_DBG("flags = %s\n", flags);
if (strstr(flags, "[CURRENT]"))
{
wifinet->status = WIFI_STATUS_CONNECTED;
NETWORK_MSG("wifinet_current has been found\n");
}
else if (strstr(flags, "[DISABLED]"))
{
wifinet->status = WIFI_STATUS_SAVED;
NETWORK_MSG("wifinet_DISABLED has been found\n");
}
else
{
wifinet->status = WIFI_STATUS_DISCONNECTED;
NETWORK_MSG("start = %s\n", start);
}
EXIT:
NETWORK_DBG("ret = %d\n", ret);
NETWORK_TRACE("out\n");
return ret;
}
/*!
* @brief main
* @param[in] argc
* @param[out] argv
* @return enRet
*/
int main(int argc, char* argv[])
{
NETWORK_TRACE("in\n");
NETWORK_DBG("argc = %d\n", argc);
NETWORK_MSG("Usage: ./test check <input file> length OR ./test trans <input file>\n");
enReturn enRet = enOK;
NETWORK_DBG("argv = %p\n", argv);
if (argc != 3 || argc != 4)
{
NETWORK_EBG("argc error\n");
enRet = enInputParaError;
goto EXIT;
}
NETWORK_DBG("argv[0] = %s\n", argv[0]);
NETWORK_DBG("argv[1] = %s\n", argv[1]);
NETWORK_DBG("argv[2] = %s\n", argv[2]);
NETWORK_DBG("argv[3] = %s\n", argv[3]);
std::string type(argv[1]);
std::string inputfile(argv[2]);
int length = atoi(argv[3]);
FILE* fp = NULL;
char* line = NULL;
size_t len = 0;
//char* Buf_trans;
WiFi_Network_Node wifinet = NULL;
wifinet = (struct wifi_network *)malloc(sizeof(struct wifi_network));
if (NULL != wifinet)
{
memset(wifinet, 0, sizeof(struct wifi_network));
}
else
{
NETWORK_EBG("wifinet is NULL\n");
enRet = enInputParaError;
goto EXIT;
}
wifinet->netid = 0;
fp = fopen(inputfile.c_str(), "r");
if (NULL == fp)
{
NETWORK_EBG("fp is NULL\n");
enRet = enInputFileError;
goto EXIT;
}
while (getline(&line, &len, fp) >= 0)
{
if (type == "check")
{
checkIPv4Addr(line, length);
}
else if (type == "trans")
{
parse_one_line_conf(line, wifinet);
if (wifinet->status == WIFI_STATUS_CONNECTED)
{
NETWORK_MSG("Current Wifi SSID is %s\n", wifinet->ssid);
}
else
{
NETWORK_EBG("not find current Wifi SSID!\n");
break;
}
}
else
{
NETWORK_EBG("error\n");
enRet = enTypeError;
}
}
EXIT:
if(wifinet != NULL)
{
free(wifinet);
wifinet = NULL;
}
if (line != NULL)
{
free(line);
line = NULL;
}
if (NULL != fp)
{
fclose(fp);
fp = NULL;
}
NETWORK_DBG("enRet = %d\n", enRet);
NETWORK_TRACE("out\n");
return enRet;
}
标签:NULL,NETWORK,ip,DBG,wifinet,cpp,main,Buf,Lindsay 来源: https://blog.csdn.net/qq_41854428/article/details/120345508