示例#1
0
int GPRSClient::connect(const char *host, uint16_t port) {
	if (!attach())
		return 0;

	char cmd[80];
	char fmt[30];
	strcpy_P(fmt, G("AT+CDNSORIP=%d"));
	snprintf(cmd, sizeof(cmd), fmt, isIPAddress(host) ? 0 : 1);
	_gsm->send(cmd);
	if (!_gsm->recvUntil_P(G("OK")))
		return 0;

	// State 4: IP STATUS
	strcpy_P(fmt, G("AT+CIPSTART=\"TCP\",\"%s\",\"%d\""));
	snprintf(cmd, sizeof(cmd), fmt, host, port);
	_gsm->send(cmd);
	if (!_gsm->recvUntil_P(G("OK")))
		return 0;

	_connected = _gsm->recvUntil_P(CONNECT_TIMEOUT, G("CONNECT OK"), G("CONNECT FAIL")) == 1;
	if (_connected) {
		_rx_head = _rx_tail = _size_left = 0;
		_gsm->setCallback_P(0, G("+IPD"), callback, (void *)this);
	}

	return _connected;
}
示例#2
0
bool checkCommandLineInputs(const int argc, char *argv[]) {
    bool rightCommandLineInputs;
    switch (argc) {
        case 1:
            rightCommandLineInputs = true;
            break;
        case 2:
            if (isIPAddress(argv[1])) {
                rightCommandLineInputs = true;
                break;
            }
            else {
                std::cout << "wrong IP address" << std::endl;
                rightCommandLineInputs = false;
                break;
            }
        case 3:
            if (isIPAddress(argv[1]) && isHostPort(argv[2])) {
                rightCommandLineInputs = true;
                break;
            }
            else {
                if (!isIPAddress(argv[1])) {
                    std::cout << "wrong IP address" << std::endl;
                    rightCommandLineInputs = false;
                    break;
                }
                else {
                    std::cout << "wrong Host Port" << std::endl;
                    rightCommandLineInputs = false;
                    break;
                }
            }
        default:
            std::cout << "myRedis-Cli takes 2 arguments" << std::endl;
            rightCommandLineInputs = false;
            break;
    }
    return rightCommandLineInputs;
}
示例#3
0
/*****************************************************************************
 * FUNCTION: do_ifconfig_cmd
 *
 * RETURNS: None
 *
 * PARAMS:    None
 *
 * NOTES:   Responds to the user invoking ifconfig
 *****************************************************************************/
tZGVoidReturn do_ifconfig_cmd(tZGVoidInput)
{
    tZGU8 i;
    tZGU8 address[6];
#if defined(ZG_CONFIG_DHCP) && defined(STACK_USE_DHCP_CLIENT)
    tZGU16 tmp[4]; // uip requires these be 2-byte words.
#endif


   gLineParseState = kZGWaitingForParamKeyword;

    // if user only typed in ifconfig with no other parameters
    if (ARGC == 1u)
    {
        IfconfigDisplayStatus();
    }
    // else if 2 arguments and the second arg is IP address
    else if ( (ARGC == 2u) && (isIPAddress(ARGV[1], address)) )
    {
        if ( ZG_GET_DHCP_STATE() == DHCP_ENABLED )
        {

          sprintf( (char *) g_ConsoleContext.txBuf,
                     "Static IP address should not be set with DHCP enabled.\n\r");

          ZG_PUTSUART( (char *) g_ConsoleContext.txBuf );
          return;
        }

        AppConfig.MyIPAddr.v[0] = address[0];
        AppConfig.MyIPAddr.v[1] = address[1];
        AppConfig.MyIPAddr.v[2] = address[2];
        AppConfig.MyIPAddr.v[3] = address[3];

        /* Microchip DHCP client clobbers static ip on every iteration of loop, even if dhcp is turned off*/
        AppConfig.DefaultIPAddr.v[0] = address[0];
        AppConfig.DefaultIPAddr.v[1] = address[1];
        AppConfig.DefaultIPAddr.v[2] = address[2];
        AppConfig.DefaultIPAddr.v[3] = address[3];

#ifdef USE_LCD
        LCDDisplayIPValue(AppConfig.MyIPAddr);
#endif
    }
    // else if 2 args and second arg is MAC address
    else if ( (ARGC == 2u) && isMacAddress(ARGV[1], address))
    {
        /* Can only set MAC address in idle state */
        if ( ZG_IS_CONNECTED() == kZGBoolTrue )
        {
            sprintf( (char *) g_ConsoleContext.txBuf,
                     "HW MAC address can only be set in idle mode");

            ZG_PUTSUART( (char *) g_ConsoleContext.txBuf );

            return;
        }

        /* update G2100 */
        ZG_SET_MAC_ADDR( address );

    }
    // else loop through each parameter
    else
    {
        // parse each param and set state variables
        for (i = 1; i < ARGC; ++i)
        {
            switch (gLineParseState)
            {
                case kZGWaitingForParamKeyword:
                    if ( !getParam(i) )
                    {
                        return;
                    }
                    break;

                case kZGWaitingForNetmaskValue:

                    if ( ZG_GET_DHCP_STATE() == DHCP_ENABLED )
                    {
                        sprintf( (char *) g_ConsoleContext.txBuf,
                                "The Netmask should not be set with DHCP enabled.\n\r");

                        ZG_PUTSUART( (char *) g_ConsoleContext.txBuf );
                        return;
                    }

                    if ( !isIPAddress(ARGV[i], address) )
                    {
                        sprintf( (char *) g_ConsoleContext.txBuf,
                                 "Invalid netmask value");
                        ZG_PUTSUART( (char *) g_ConsoleContext.txBuf );
                        return;
                    }

                    AppConfig.MyMask.v[0] = address[0];
                    AppConfig.MyMask.v[1] = address[1];
                    AppConfig.MyMask.v[2] = address[2];
                    AppConfig.MyMask.v[3] = address[3];

                    /* Microchip DHCP client clobbers static netmask on every iteration of loop, even if dhcp is turned off*/
                    AppConfig.DefaultMask.v[0] = address[0];
                    AppConfig.DefaultMask.v[1] = address[1];
                    AppConfig.DefaultMask.v[2] = address[2];
                    AppConfig.DefaultMask.v[3] = address[3];


                    gLineParseState = kZGWaitingForParamKeyword;
                    break;

#if defined(ZG_CONFIG_DHCP) && defined(STACK_USE_DHCP_CLIENT)
                case kZGWaitingForDHCP:
                    /* buf[4] contains DHCP enable/disable enum */
                    if ( !ExtractandValidateDHCP(ARGV[i], (tZGU8 *) &tmp[0]) )
                    {
                        return;
                    }

                    ZG_SET_DHCP_STATE( (tZGBool) tmp[0] );
                    gLineParseState = kZGWaitingForParamKeyword;
                    break;
#endif

                case kZGWaitingForGateway:
                    if ( !isIPAddress(ARGV[i], address) )
                    {
                        sprintf( (char *) g_ConsoleContext.txBuf,
                                 "Invalid gateway value");
                        ZG_PUTSUART( (char *) g_ConsoleContext.txBuf );
                        return;
                    }

                    AppConfig.MyGateway.v[0] = address[0];
                    AppConfig.MyGateway.v[1] = address[1];
                    AppConfig.MyGateway.v[2] = address[2];
                    AppConfig.MyGateway.v[3] = address[3];

                    // There is no such thing as AppConfig.DefaultGateway

                    gLineParseState = kZGWaitingForParamKeyword;
                    break;

            } // end switch
//            ZGConsolePrintf("%s ", ARGV[i]);
        } // end for
    }

    if (gLineParseState != (tZGU8)kZGWaitingForParamKeyword)
    {
        sprintf( (char *) g_ConsoleContext.txBuf,
                  "Missing value after last parameter");

        ZG_PUTSUART( (char *) g_ConsoleContext.txBuf );
    }

}
/*****************************************************************************
 * FUNCTION: do_ifconfig_cmd
 *
 * RETURNS: None
 *
 * PARAMS:  None
 *
 * NOTES:   Responds to the user invoking ifconfig
 *****************************************************************************/
void do_ifconfig_cmd(void)
{
     uint8_t   address[6];
     uint8_t conState, cpId;
     NET_CONFIG *p_netConfig;
     
     p_netConfig = GetNetworkConfig();

	
    // if user only typed in ifconfig with no other parameters
    if (ARGC == 1u)
    {
        IfconfigDisplayStatus();
		return;
    }

	if (g_hibernate_state)
	{
		WFConsolePrintStr("The Wi-Fi module is in hibernate mode - command failed.", true);
		return;
	}

#if defined(WF_CM_DEBUG)
	else if ( (ARGC == 2u) && !strcmp((char *) ARGV[1], "info") )
	{
        uint8_t i;
		tWFCMInfoFSMStats cm_stats;

		WF_CMInfoGetFSMStats(&cm_stats);
		for (i = 0; i < 12; i++)
		{
    		sprintf( (char *) g_ConsoleContext.txBuf,
            		"[%02X]: %02X%02X %02X%02X",   
					i, 
					cm_stats.byte[i*4 + 0],
					cm_stats.byte[i*4 + 1],
					cm_stats.byte[i*4 + 2],
					cm_stats.byte[i*4 + 3]
					);
    		WFConsolePrintStr( (char *) g_ConsoleContext.txBuf , true);
		}
	}
	else if ( (ARGC == 2u) && !strcmp((char *) ARGV[1], "scan") )
	{
		WF_Scan(1); // scan, using CP 1
	}
	else if ( (ARGC == 2u) && !strcmp((char *) ARGV[1], "scanget") ) //"scangetresult"
	{
		tWFScanResult pScanResult[1];

		WF_ScanGetResult(0, pScanResult);
	}
	else if ( (ARGC == 2u) && !strcmp((char *) ARGV[1], "cpgete") ) //"cpgetelements"
	{
		tWFCPElements pCPElements[1];

		WF_CPGetElements(1, pCPElements);
	}
#endif
    // else if 2 arguments and the second arg is IP address
    else if ( (ARGC == 2u) && (isIPAddress(ARGV[1], address)) )
    {
        #if defined(TCPIP_STACK_USE_DHCP_CLIENT)
        if (DHCPIsEnabled(0))
        {
          WFConsolePrintStr(
                     "Static IP address should not be set with DHCP enabled", true);
          return;
        }
        #endif

        p_netConfig->MyIPAddr.v[0] = address[0];
        p_netConfig->MyIPAddr.v[1] = address[1];
        p_netConfig->MyIPAddr.v[2] = address[2];
        p_netConfig->MyIPAddr.v[3] = address[3];

        /* Microchip DHCP client clobbers static ip on every iteration of loop, even if dhcp is turned off*/
        p_netConfig->DefaultIPAddr.v[0] = address[0];
        p_netConfig->DefaultIPAddr.v[1] = address[1];
        p_netConfig->DefaultIPAddr.v[2] = address[2];
        p_netConfig->DefaultIPAddr.v[3] = address[3];

        LCDDisplayIPValue(p_netConfig->MyIPAddr);
    }
    // else if 2 args and second arg is MAC address
    else if ( (ARGC == 2u) && isMacAddress(ARGV[1], address))
    {
        /* Can only set MAC address in idle state */
        WF_CMGetConnectionState(&conState, &cpId);
        if ( conState != WF_CSTATE_NOT_CONNECTED )
        {
            WFConsolePrintStr("HW MAC address can only be set in idle mode", true);
            return;
        }

         WF_SetMacAddress( address );
    }
	else if ( (2u <= ARGC) && (strcmp((char *)ARGV[1], (const FAR char *)"netmask") == 0) )
	{
		if (ARGC != 3u)
		{
			missingValue();
			return;
		}

        #if defined(TCPIP_STACK_USE_DHCP_CLIENT)
        if ( DHCPIsEnabled(0) )
        {
            WFConsolePrintStr(
                "The Netmask should not be set with DHCP enabled", true);
            return;
        }
        #endif

        if ( !isIPAddress(ARGV[2], address) )
        {
            WFConsolePrintStr("Invalid netmask value", true);
            return;
        }

        p_netConfig->MyMask.v[0] = address[0];
        p_netConfig->MyMask.v[1] = address[1];
        p_netConfig->MyMask.v[2] = address[2];
        p_netConfig->MyMask.v[3] = address[3];

        /* Microchip DHCP client clobbers static netmask on every iteration of loop, even if dhcp is turned off*/
        p_netConfig->DefaultMask.v[0] = address[0];
        p_netConfig->DefaultMask.v[1] = address[1];
        p_netConfig->DefaultMask.v[2] = address[2];
        p_netConfig->DefaultMask.v[3] = address[3];  
	}
	else if ( (2u <= ARGC) && (strcmp((char *)ARGV[1], (const FAR char *)"gateway") == 0) )
	{
		if (ARGC != 3u)
		{
			missingValue();
			return;
		}

        if ( !isIPAddress(ARGV[2], address) )
        {
            WFConsolePrintStr("Invalid gateway value", true);
            return;
        }

        p_netConfig->MyGateway.v[0] = address[0];
        p_netConfig->MyGateway.v[1] = address[1];
        p_netConfig->MyGateway.v[2] = address[2];
        p_netConfig->MyGateway.v[3] = address[3];
	}
	else if ( (2u <= ARGC) && (strcmp((char*)ARGV[1], "auto-dhcp") == 0) )
	{
		if (ARGC != 3u)
		{
			missingValue();
			return;
		}

        #if defined(TCPIP_STACK_USE_DHCP_CLIENT)
		if (strcmp((char*)ARGV[2], "start") == 0)
		{
			setDHCPState(true);
		}
		else if (strcmp((char*)ARGV[2], "stop") == 0)
		{
			setDHCPState(false);
		}
		else
		#endif
		{
			WFConsolePrintStr("   Invalid dhcp param", true);
			return;
		}
	}
    else
    {
        notHandledParam(1);
	}
}
示例#5
0
文件: client.c 项目: dhavale/netprogB
int main(int argc, char **argv) {
	int sockfd;
	struct sockaddr_in servaddr;
	int len,on=1;
	struct interface_info *head;
	struct sockaddr_in closest;
	generate_ifi_list(&head);
	int ret;

	//	if (argc != 2) //TODO Uncomment
	//		err_sys("usage: client <client.in file>");

	//Read input configuration from server.in
	char line[MAX_LINE];
	FILE* fp = fopen("client.in", "r"); //TODO read from argv[1]
	if (fgets(line, sizeof(line), fp)) {//Line 1 server ip
		len = strlen(line) - 1; //remove the trailing \n
		if (line[len] == '\n')
			line[len] = '\0';
		strcpy(serverIP, line);
		if (!isIPAddress(serverIP)) {
			err_sys_p("Invalid or missing server ip in the configuration file.");
		}
		printf("[INFO] Server ip:%s\n", serverIP);
	} else {
		err_sys_p("Invalid or missing input configuration.");
	}

	if (fgets(line, sizeof(line), fp)) {//Line 2 server port
		server_port = atoi(line);
		if (server_port == 0) {
			err_sys(
					"Invalid or missing server port number in the configuration file.");
		}
		printf("[INFO] Server port:%d\n", server_port);
	} else {
		err_sys_p("Invalid or missing input configuration.");
	}

	if (fgets(line, sizeof(line), fp)) {//Line 3 file name
		len = strlen(line) - 1; //remove the trailing \n
		if (line[len] == '\n')
			line[len] = '\0';
		strcpy(required_file_name, line);
		if (len == 0)
			err_sys("Invalid or missing file name in the configuration file.");
		printf("[INFO] File name:%s\n", required_file_name);
	} else {
		err_sys_p("Invalid or missing input configuration.");
	}

	if (fgets(line, sizeof(line), fp)) {//Line 4 maximum sliding window size.
		max_win_size = atoi(line);
		if (max_win_size == 0) {
			err_sys_p(
					"Invalid or missing Max win size in the configuration file.");
		}
		printf("[INFO] Max win size:%d\n", max_win_size);
	} else {
		err_sys_p("Invalid or missing input configuration.");
	}

	fclose(fp);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(server_port);
	
	bzero(&closest, sizeof(closest));
	ret =closest_match_to_interface(head,serverIP,&closest.sin_addr); 

	//printf("[INFO] closest is %s\n",inet_ntoa(closest.sin_addr));
	cliaddr.sin_addr.s_addr = closest.sin_addr.s_addr;	
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0) {
		err_sys_p("Socket error.");
	}

	if(ret==1)
	{
		/**returned 1, we should use localhost for both server and client**/
		if (inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr) != 1)
		err_sys_p("Cannot convert string IP to binary IP.");

	}

	else if(ret==2)
	{
		/**then its on local n/w , use dont' route**/
		if(setsockopt(sockfd, SOL_SOCKET, SO_DONTROUTE, (void *) &on, sizeof(on)) < 0)
	    err_sys_p("Can't set SO_DONTROUTE on main socket"); 

		if (inet_pton(AF_INET, serverIP, &servaddr.sin_addr) != 1)
		err_sys_p("Cannot convert string IP to binary IP.");

	}

	else
	{
		if (inet_pton(AF_INET, serverIP, &servaddr.sin_addr) != 1)
		err_sys_p("Cannot convert string IP to binary IP.");

	}
	

	dg_cli(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

	exit(0);
}
示例#6
0
文件: main.cpp 项目: gaohr/SEIMS
int main(int argc, const char *argv[])
{
    GDALAllRegister();
    string modelPath = "";
    int scenarioID = -1;  /// By default, no BMPs Scenario is used, in case of lack of BMPs database.
    int i = 0;
    int numThread = 1;
    LayeringMethod layeringMethod = UP_DOWN;
    char mongodbIP[16];
    stringcpy(mongodbIP,"127.0.0.1");
    int port = 27017;
    if (argc < 2)
    {
        cout << "Error: To run the program, use either the Simple Usage option or Usage option as below." << endl;
        goto errexit;
    }
    else if (argc > 2)
        i = 1;
    else
        i = 2;
    while (argc > i)
    {
        if (isPathExists(argv[i]))
        {
            modelPath = argv[i];
            i++;
        }
        else
            goto errexit;
        if (argc > i)
        {
            if (atoi(argv[i]) > 0)
            {
                numThread = atoi(argv[i]);
                i++;
            }
            else
                goto errexit;
        }
        if (argc > i)
        {
            if (atoi(argv[i]) == 0 || atoi(argv[i]) == 1)
            {
                if (atoi(argv[i]) == 0)
                    layeringMethod = UP_DOWN;
                else
                    layeringMethod = DOWN_UP;
                i++;
            }
            else
                goto errexit;
        }
        if (argc > i)
        {
            if (isIPAddress(argv[i]))
            {
                stringcpy(mongodbIP,argv[i]);
                i++;
                if (argc > i && atoi(argv[i]) > 0)
                {
                    port = atoi(argv[i]);
                    i++;
                }
                else
                    goto errexit;
            }
            else
                goto errexit;
        }
        if (argc > i)
        {
            if (atoi(argv[i]) >= 0)
            {
                scenarioID = atoi(argv[i]);
                i++;
            }
            else
                goto errexit;
        }
    }
    if (argc == 2)
        modelPath = argv[1];

    //cout<<modelPath<<endl;
    //cout<<numThread<<endl;
    //cout<<layeringMethod<<endl;
    //cout<<mongodbIP<<":"<<port<<endl;
    //cout<<scenarioID<<endl;
    //omp_set_num_threads(2);

#ifdef MAIN
    while (modelPath.length() == 0)
    {
        cout << "Please input the model path:" << endl;
        cin >> modelPath;
    }
#ifdef USE_MONGODB
    MainMongoDB(modelPath, mongodbIP, port, scenarioID, numThread, layeringMethod);
#endif

#endif

#ifdef TEST
    // run the model
    //Run(strTmp);
    //testSettingInput();
    //testBMP();
    //testSettingOutput();

    //testModule();
    //testRaster();
#endif

    //system("pause");
    return 0;
    errexit:
    cout << "Simple Usage:\n " << argv[0] << " <ModelPath>" << endl;
    cout << "\tBy default: " << endl;
    cout << "\t\tThe MongoDB IP is 127.0.0.1 (i.e., localhost), and the port is 27017." << endl;
    cout << "\t\tThe threads or processor number is 1." << endl;
    cout << "\t\tThe Layering Method is UP_DOWN." << endl;
    cout << "\t\tThe Scenario ID is 0" << endl << endl;
    cout << "Usage: " << argv[0] << "<ModelPath> [<threadsNum> <layeringMethod> <IP> <port> <ScenarioID>]" << endl;
    cout << "\t<ModelPath> is the path of the configuration of the Model." << endl;
    cout << "\t<threadsNum> must be greater or equal than 1." << endl;
    cout << "\t<layeringMethod> can be 0 and 1, which means UP_DOWN and DOWN_UP respectively." << endl;
    cout << "\t<IP> is the address of MongoDB database, and <port> is its port number." << endl;
    cout << "\t<ScenarioID> is the ID of BMPs Scenario which will be defined in BMPs database." << endl;
    exit(0);
}