Ejemplo n.º 1
0
void exec(PRO* pro)
{
	int ret;
		if(pro->flag == 1)
		{
			switch(pro->cmd)
			{
				case '0':
					ret = connect(pro);
					break;
				case '1':
					ret = warning(pro);
					break;
				case '2':
					ret = jdq1(pro);
					break;
				case '3':
					ret = jdq2(pro);
					break;				
				case '4':
					ret = setTime(pro);
					break;
                case '6':
                    ret = set_host_left_right(pro);
                    break;
                case '7':
                    ret = set_disp_mode(pro);
                    break;
                case '8':
                    ret = set_num_enter_mode(pro);
                    break;
                case '9':
                    ret = set_num_out_mode(pro);
                    break;
                case 'A':
                    ret = set_local_ip(pro);
                    break;
				default :
					ret = 9;
					break;
			}
			ack(ret);
			ackBuf[4] = pro->cmd;
			msg.len =sizeof(ackBuf);
			msg.buf = ackBuf;

			udp.send(&msg);
            
			pro->flag = 0;
            pro->cmd = 0;
            pro->type = 0;
            for(int i = 0; i < MAX_LENGTH; i++)
                pro->para[i] = 0;

		}
}
int create_server()
{
    int listenfd;
    socklen_t clilen;
    struct sockaddr_in serveraddr;
    int reuse_port = 1;
    int len = sizeof(reuse_port);
    listenfd = socket(AF_INET,SOCK_STREAM,0);
    if(listenfd < 0)
    {
        perror("socket create wrong!");
        exit(1);
    }

    //setnonblocking(listenfd);
    set_local_ip(&serveraddr,listenfd);
    bzero(&serveraddr,sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    
  //  const char* local_addr = "127.0.0.1";
  //  inet_aton(local_addr,&(serveraddr.sin_addr));
    set_local_ip(&serveraddr,listenfd);
    int SERV_PORT = 50005;
    serveraddr.sin_port = htons(SERV_PORT);
    
    if(bind(listenfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr)) < 0)
    {
        perror("bind wrong");
        exit(1);
    }

    if(/*setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&reuse_port,len) < 0*/0)
    {
        perror("setsockopt");
        exit(1);
    }
    return listenfd;

}
Ejemplo n.º 3
0
int test_miscellany_net()
{
    unsigned char  mac_addr[6];
    char ip_addr[100];

#if 0
    get_mac((char *)"eth0",mac_addr, sizeof(mac_addr));
    print_mac(mac_addr);

    get_local_ip((char *)"eth0",ip_addr);
    print_ipaddr(ip_addr);

    set_local_ip((char *)"eth0","192.168.20.182");
    get_local_ip((char *)"eth0",ip_addr);
    print_ipaddr(ip_addr);

    dbg_str(DBG_DETAIL,"get local netmask");
    get_local_netmask((char *)"eth0",ip_addr); 
    print_ipaddr(ip_addr);
    dbg_str(DBG_DETAIL,"set local netmask");
    set_local_netmask((char *)"eth0",(char *)"255.255.255.0");  
    get_local_netmask((char *)"eth0",ip_addr); 
    print_ipaddr(ip_addr);

    dbg_str(DBG_DETAIL,"ip and str convert");
    inet_str2num(AF_INET,(char *)"192.168.20.2");
    
    inet_num2str(AF_INET,34908352,ip_addr);
#endif

#if 0
    if(inet_is_in_same_subnet("192.168.2.32","eth0")) {
        /*
         *dbg_str(DBG_DETAIL,"this ip addr add local addr is in the same net");
         */
    } else {
        dbg_str(DBG_DETAIL,"this ip addr add local addr is not in the same net");
    }
#endif

    get_broadcast_addr("eth0", ip_addr);
    print_ipaddr(ip_addr);

    /*
     *char ip_addr2[100];
     *set_broadcast_addr("eth0", "192.168.20.255");
     *print_ipaddr(ip_addr2);
     */

    return 0;
}
Ejemplo n.º 4
0
// }}}
// {{{ load_ip_config
static int load_ip_config()
{
    char ip[24] = {0};
    char netmask[24] = {0};
    char gw[24] = {0};
    if (SAFE_GET_CONFIG("dev.ip", ip)
            || SAFE_GET_CONFIG("dev.netmask", netmask)
            || SAFE_GET_CONFIG("dev.gateway", gw))
    {
        LOG((LOG_ERROR, "获取IP配置失败"));
        return -1;
    }
    return set_local_ip(ip, netmask, gw);
}
Ejemplo n.º 5
0
/*
 ******************************************************************************
 * dcs_local_ip_monitor_process                                           *//**
 *
 * \brief - Called by the polling thread that gets an indication that the local
 *          IP address has changed. The function sets the Local IP Address in 
 *          the dcs_local_ip variable
 *
 * \param[in] socket - The socket on which the information arrived
 * \param[in] context - NULL
 *
 * \retval DOVE_STATUS_OK
 *
 ******************************************************************************
 */
static int dcs_local_ip_monitor_process(int socket, void *context)
{
	struct nlmsghdr *nlh;
	char buf[4096];
	int len;
	dove_status status = DOVE_STATUS_OK;

	while ((len = recv(dps_monitor_socket, buf, sizeof(buf), 0)) > 0)
	{

		nlh = (struct nlmsghdr *)buf;
		while ((NLMSG_OK(nlh, (uint32_t)len)) && (nlh->nlmsg_type != NLMSG_DONE))
		{
			if (nlh->nlmsg_type == RTM_NEWADDR || nlh->nlmsg_type == RTM_DELADDR)
			{
				struct ifaddrmsg *ifa = (struct ifaddrmsg *)NLMSG_DATA(nlh);
				struct rtattr *rth = IFA_RTA(ifa);
				int rtl = IFA_PAYLOAD(nlh);

				while (rtl && RTA_OK(rth, rtl))
				{
					if (rth->rta_type == IFA_LOCAL)
					{
						char ifname[IFNAMSIZ];

						if_indextoname(ifa->ifa_index, ifname);

						if (!strcmp(ifname, SVA_INTERFACE_NAME) && (nlh->nlmsg_type == RTM_NEWADDR))
						{
							status = set_local_ip();
							if (status != DOVE_STATUS_OK)
							{
								log_warn(PythonDataHandlerLogLevel,
								         "Cannot get IP Address of SVA");
								break;
							}
							// Register the Local DPS Node with the Cluster
							dcs_set_service_role(dcs_role_assigned);
						}
					}
					rth = RTA_NEXT(rth, rtl);
				}
			}
			nlh = NLMSG_NEXT(nlh, len);
		}
	}

	return DOVE_STATUS_OK;
}
int create_udp_socket()
{
    int udpfd;
    struct sockaddr_in addr;
    bzero(&addr,sizeof(addr));
    udpfd = socket(AF_INET,SOCK_DGRAM,0);
    set_local_ip(&addr,udpfd);
    addr.sin_family = AF_INET;
    int SERV_PORT = 50005;
    addr.sin_port = htons(SERV_PORT);

    if(bind(udpfd,(struct sockaddr*)&addr,sizeof(addr)) < 0)
    {
         perror("bind wrong");
         exit(1);
    }

    return udpfd;
    
}
Ejemplo n.º 7
0
int dcs_initialize(int udp_port, int rest_port, int fDebugCli, int fExitOnCtrlC, char *python_path)
{
	dove_status status = DOVE_STATUS_OK;
	char buf[INET6_ADDRSTRLEN];
	int ret;

	memset(&dcs_local_ip, 0, sizeof(ip_addr_t));
	dcs_local_ip.family = AF_INET;
	dcs_local_ip.ip4 = htonl(INADDR_LOOPBACK);
	dcs_local_ip.port = udp_port;
	dcs_local_ip.port_http = rest_port;
	dcs_local_ip.xport_type = SOCK_DGRAM;
	set_initial_dps_cluster_leader();
	set_initial_controller_location();

	Py_Initialize();
	PyEval_InitThreads();
	do
	{
		log_console = 0;

		//Read version
		dcs_read_version();
		log_notice(PythonDataHandlerLogLevel, "DCS version %s",dsa_version_string);

		if(OSW_OK != osw_init())
		{
			status = DOVE_STATUS_OSW_INIT_FAILED;
			break;
		}
		log_notice(PythonDataHandlerLogLevel, "Initialized OS Wrapper");

		status = python_init_dcs_protocol_interface(python_path);
		if (status != DOVE_STATUS_OK)
		{
			log_emergency(PythonDataHandlerLogLevel,
			              "Cannot initialize the Protocol Interface");
			break;
		}
		log_notice(PythonDataHandlerLogLevel, "Initialized DCS Protocol");

		// Initialize the data handler for the Cluster Database
		status = python_init_cluster_db_interface(python_path);
		if (status != DOVE_STATUS_OK)
		{
			log_emergency(PythonDataHandlerLogLevel,
			              "Cannot initialize the Cluster Database");
			break;
		}
		log_notice(PythonDataHandlerLogLevel, "Initialized Cluster Database");

		// Initialize the data handler for the DPS Controller Protocol
		status = python_init_controller_interface(python_path);
		if (status != DOVE_STATUS_OK)
		{
			log_emergency(PythonDataHandlerLogLevel,
			              "Cannot initialize the Cluster Database");
			break;
		}
		log_notice(PythonDataHandlerLogLevel, "Initialized Controller Interface");

		// Initialize the debug handler
		status = python_init_debug_interface(python_path);
		if (status != DOVE_STATUS_OK)
		{
			log_emergency(PythonDataHandlerLogLevel,
			              "Cannot initialize the Debug Handler");
			break;
		}
		log_notice(PythonDataHandlerLogLevel, "Initialized Debug Interface");

		// Initialize the Retransmit Handler
		status = python_init_retransmit_interface(python_path);
		if (status != DOVE_STATUS_OK)
		{
			log_emergency(PythonDataHandlerLogLevel,
			              "Cannot Initialize Retransmit Handler");
			break;
		}
		log_notice(PythonDataHandlerLogLevel, "Initialized Retransmit Interface");

		// Initialize the UUID Interface
		status = python_init_UUID_interface(python_path);
		if (status != DOVE_STATUS_OK)
		{
			log_emergency(PythonDataHandlerLogLevel,
			              "Cannot initialize the UUID API");
			break;
		}
		log_notice(PythonDataHandlerLogLevel, "Initialized UUID Interface");

		// Get local IP Address
		set_local_ip();
		log_notice(PythonDataHandlerLogLevel, "Initialized Local IP");

		// Initialize the REST SERVER
		if(dps_rest_server_init_ok == 0)
		{
			status = dcs_server_rest_init((short)DPS_REST_HTTPD_PORT);
			if (status != DOVE_STATUS_OK)
			{
				log_emergency(PythonDataHandlerLogLevel,
				              "Cannot Initialize the REST SERVER");
				break;
			}
			dps_rest_server_init_ok = 1;
		}
		log_notice(PythonDataHandlerLogLevel, "Initialized REST Server");

		dove_status status_tmp = dcs_read_svc_app_uuid();
		if (status_tmp != DOVE_STATUS_OK)
		{
			log_emergency(PythonDataHandlerLogLevel,
			              "Could NOT read service appliance UUID from %s/%s",
			              getenv("HOME"),SERVICE_APPLIANCE_UUID_FILE);
			// Create a FAKE UUID interface
			dps_init_uuid();
			write_uuid_to_file();
		}
		log_notice(PythonDataHandlerLogLevel, "READ SVC APP UUID");

		// Register the Local DPS Node with the Cluster
		status = dcs_cluster_node_local_update(&dcs_local_ip);
		if (status != DOVE_STATUS_OK)
		{
			log_emergency(PythonDataHandlerLogLevel,
			              "DCS: Cannot add local Node to Cluster");
			break;
		}
		log_notice(PythonDataHandlerLogLevel, "Registered Local Node with Cluster");

		// Initialize the Statistics
		status = dcs_statistics_init(python_path);
		if (status != DOVE_STATUS_OK)
		{
			log_emergency(PythonDataHandlerLogLevel,
			              "DCS: Cannot Initialize Statistics Thread");
			break;
		}
		log_notice(PythonDataHandlerLogLevel, "Initialized Statistics Thread");

		// Initialize the Heartbeat
		status = dcs_heartbeat_init(python_path);
		if (status != DOVE_STATUS_OK)
		{
			log_emergency(PythonDataHandlerLogLevel,
			              "DCS: Cannot Initialize Heartbeat Thread");
			break;
		}
		log_notice(PythonDataHandlerLogLevel, "Initialized Heartbeat Thread");

		dcs_rest_sync_init();

		log_notice(PythonDataHandlerLogLevel, "Initialized Controller Sync Thread");

		if (fDebugCli)
		{
			// Initialize the CLI Thread PYTHON Interface. This will
			// start the CLI Thread
			status = python_lib_embed_cli_thread_start(fExitOnCtrlC);
			if (status != DOVE_STATUS_OK)
			{
				log_emergency(PythonDataHandlerLogLevel,
				              "DCS: Cannot start the CLI thread");
				break;
			}

			// Initialize the CLI
			status = dcs_server_cli_init();
			if (status != DOVE_STATUS_OK)
			{
				log_emergency(PythonDataHandlerLogLevel,
				              "DCS: Cannot Initialize the CLI");
				break;
			}

			fDebugCLIInitialized = 1;
		}
		else
		{
			//Set net.core.rmem_max to 64M
			ret = system("sysctl -w net.core.rmem_max=67108864 &> /dev/null");
			if (WEXITSTATUS(ret) != 0)
			{
				log_emergency(PythonDataHandlerLogLevel,
				              "DCS: Failed to set net.core.rmem_max to 64M");
			}
			ret = system("sysctl -w net.core.rmem_default=67108864 &> /dev/null");
			if (WEXITSTATUS(ret) != 0)
			{
				log_emergency(PythonDataHandlerLogLevel,
				              "DCS: Failed to set net.core.rmem_default to 64M");
			}
			//Read from file
			dcs_read_role();
		}

		Py_BEGIN_ALLOW_THREADS

		// Initialize the CORE APIs
		status = fd_process_init();
		if (status != DOVE_STATUS_OK)
		{
			log_emergency(PythonDataHandlerLogLevel,
			              "DCS: Cannot Initialize CORE processing");
			break;
		}

		// Initialize the DCS Server
		dps_svr_proto_init((uint32_t)udp_port);

		// Print the starting parameters
		inet_ntop(dcs_local_ip.family, dcs_local_ip.ip6, buf, INET6_ADDRSTRLEN);
		log_notice(PythonDataHandlerLogLevel,
		           "DCS Server Started: IP Address <%s>, Port <%d>",
		           buf, dcs_local_ip.port);

		//Initialize local IP monitor
		dcs_local_ip_monitor_init();

		// Start the CORE APIs Communication
		fd_process_start();

		Py_END_ALLOW_THREADS

	}while(0);

	//show_print("Quitting Abnormally: Not cleaning up threads!!!");

	Py_Finalize();

	return -1;
}
Ejemplo n.º 8
0
static void sys_cfg_set_local_network()
{
    CLEAR_SCREEN;
    const uint32 input_length = 190;
    input_dst* ip_input, *net_input, *gw_input;
    ip_input = input_init(FREE_MOD);
    net_input = input_init(FREE_MOD);
    gw_input = input_init(FREE_MOD);

    printf_str(MAIN_DISP, DISP_X_COOR, DISP_Y_COOR, "输入IP信息", 1);
    input_props_set(ip_input, input_one_property);
    input_property_set(ip_input, INPUT_HIND, "IP");
    input_property_set(ip_input, INPUT_MAX_LEN, 15);
    input_property_set(ip_input, INPUT_RET_STA, 150, INPUT_Y_COOR);
    input_property_set(ip_input, INPUT_RET_SIZ, INPUT_HEIGHT, input_length);

    input_props_set(net_input, input_two_property);
    input_property_set(net_input, INPUT_HIND, "掩码");
    input_property_set(net_input, INPUT_MAX_LEN, 15);
    input_property_set(net_input, INPUT_RET_STA, 150, INPUT_TWO_Y_COOR);
    input_property_set(net_input, INPUT_RET_SIZ, INPUT_HEIGHT, input_length);

    input_props_set(gw_input, input_three_property);
    input_property_set(gw_input, INPUT_HIND, "网关");
    input_property_set(gw_input, INPUT_MAX_LEN, 15);
    input_property_set(gw_input, INPUT_RET_STA, 150, INPUT_THREE_Y_COOR);
    input_property_set(gw_input, INPUT_RET_SIZ, INPUT_HEIGHT, input_length);

    input_dis(MAIN_DISP, ip_input);
    input_dis(MAIN_DISP, net_input);
    input_dis(MAIN_DISP, gw_input);

    lcd_160_upd();

    input_key_manege(MAIN_DISP, ip_input, lcd_160_upd);
    input_key_manege(MAIN_DISP, net_input, lcd_160_upd);
    input_key_manege(MAIN_DISP, gw_input, lcd_160_upd);

    char ip[24] = {0};
    char netmask[24] = {0};
    char gw[24] = {0};

    input_str_get(ip_input, ip);
    input_str_get(net_input, netmask);
    input_str_get(gw_input, gw);

    input_destory(ip_input);
    input_destory(net_input);
    input_destory(gw_input);

    if (check_ip_valid(ip)
            || check_ip_valid(netmask)
            || check_ip_valid(gw))
    {
        disp_msg("输入格式错误", 10);
        return;
    }

    if (set_local_ip(ip, netmask, gw))
    {
        disp_msg("修改IP失败", 10);
    }
    else
    {
        update_config("dev.ip", ip);
        update_config("dev.netmask", netmask);
        update_config("dev.gateway", gw);
        disp_msg("设置IP成功", 5);
    }

}