Exemplo n.º 1
0
void init_all_network_interfaces(void)
{
    cyg_bool_t use_bootp;
    cyg_bool_t setup_network = false;
    in_addr_t  local_ip_addr = 0;
    in_addr_t  broadcast_addr = 0;
    in_addr_t  server_addr = 0;
    in_addr_t  gateway_addr = 0;
    in_addr_t  network_mask = 0;
    char       server_str[16];
    char       network_str[16];
    char       gateway_str[16];
    char       broadcast_str[16];
    char       ip_addr_str[16];
    int        i;
    int        net_up = false;
    static volatile int in_init_all_network_interfaces = 0;

    cyg_scheduler_lock();
    while ( in_init_all_network_interfaces ) {
        // Another thread is doing this...
        cyg_scheduler_unlock();
        cyg_thread_delay( 10 );
        cyg_scheduler_lock();
    }
    in_init_all_network_interfaces = 1;
    cyg_scheduler_unlock();
 
#ifdef CYGHWR_NET_DRIVER_ETH0

    // Fetch values from saved config data, if available
    CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                "app_bootn", &setup_network, 
                                CYGNUM_FLASH_CFG_OP_CONFIG_BOOL);

    if (setup_network)
    {
        // Set defaults as appropriate
        use_bootp = true;

        // Fetch values from saved config data, if available
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_bootp", &use_bootp, 
                                    CYGNUM_FLASH_CFG_OP_CONFIG_BOOL);
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_bootp_localip_addr", &local_ip_addr,
                                    CYGNUM_FLASH_CFG_OP_CONFIG_IP);
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_bootp_localip_mask", &network_mask, 
                                    CYGNUM_FLASH_CFG_OP_CONFIG_IP);
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_bootp_mygateway_addr", &gateway_addr, 
                                    CYGNUM_FLASH_CFG_OP_CONFIG_IP);
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_bootp_ubroadcast_addr", 
                                    &broadcast_addr, 
                                    CYGNUM_FLASH_CFG_OP_CONFIG_IP);
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_bootp_serverip_addr", &server_addr, 
                                    CYGNUM_FLASH_CFG_OP_CONFIG_IP);

        if (use_bootp)
        {
            // Perform a complete initialization, using BOOTP/DHCP
            eth0_up = true;
//#ifdef CYGHWR_NET_DRIVER_ETH0_DHCP
            eth0_dhcpstate = 0; // Says that initialization is external to dhcp
            if (do_dhcp(eth0_name, &eth0_bootp_data, &eth0_dhcpstate, 
                        &eth0_lease))
//#else
//#ifdef CYGPKG_NET_DHCP
//            eth0_dhcpstate = DHCPSTATE_BOOTP_FALLBACK;
            // so the dhcp machine does no harm if called
//#endif
//            if (do_bootp(eth0_name, &eth0_bootp_data))
//#endif
            {
#ifdef CYGHWR_NET_DRIVER_ETH0_BOOTP_SHOW
                show_bootp(eth0_name, &eth0_bootp_data);
#endif
            } else {
                printf("BOOTP/DHCP failed on eth0\n");
                eth0_up = false;
            }
        }
        else
        {
            eth0_up = true;
            strncpy(ip_addr_str, 
                    inet_ntoa(*(struct in_addr *) &local_ip_addr), 16);
            strncpy(broadcast_str, 
                    inet_ntoa(*(struct in_addr *) &broadcast_addr), 16);
            strncpy(gateway_str, inet_ntoa(*(struct in_addr *) &gateway_addr), 
                    16);
            strncpy(network_str, inet_ntoa(*(struct in_addr *) &network_mask), 
                    16);
            strncpy(server_str, inet_ntoa(*(struct in_addr *) &server_addr), 
                    16);

            /* load flash configuration parameters */
            build_bootp_record(&eth0_bootp_data, eth0_name, ip_addr_str, 
                               network_str,  broadcast_str, gateway_str, 
                               server_str);
#ifdef CYGHWR_NET_DRIVER_ETH0_BOOTP_SHOW
            show_bootp(eth0_name, &eth0_bootp_data);
#endif
        }
    }

#endif

#ifdef CYGHWR_NET_DRIVER_ETH1
    setup_network = false;

    // Fetch values from saved config data, if available
    CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                "app_c_bootn", &setup_network, 
                                CYGNUM_FLASH_CFG_OP_CONFIG_BOOL);

    if (setup_network)
    {
        // Set defaults as appropriate
        use_bootp = true;

        // Fetch values from saved config data, if available
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_c_bootp", &use_bootp, 
                                    CYGNUM_FLASH_CFG_OP_CONFIG_BOOL);
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_c_bootp_localip_addr", &local_ip_addr,
                                    CYGNUM_FLASH_CFG_OP_CONFIG_IP);
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_c_bootp_localip_mask", &network_mask, 
                                    CYGNUM_FLASH_CFG_OP_CONFIG_IP);
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_c_bootp_mygateway_addr", &gateway_addr,
                                    CYGNUM_FLASH_CFG_OP_CONFIG_IP);
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_c_bootp_ubroadcast_addr", 
                                    &broadcast_addr, 
                                    CYGNUM_FLASH_CFG_OP_CONFIG_IP);
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_c_bootp_serverip_addr", &server_addr, 
                                    CYGNUM_FLASH_CFG_OP_CONFIG_IP);

        if (use_bootp)
        {
            // Perform a complete initialization, using BOOTP/DHCP
            eth1_up = true;
//#ifdef CYGHWR_NET_DRIVER_ETH1_DHCP
            eth1_dhcpstate = 0; // Says that initialization is external to dhcp
            if (do_dhcp(eth1_name, &eth1_bootp_data, &eth1_dhcpstate, 
                        &eth1_lease))
//#else
//#ifdef CYGPKG_NET_DHCP
//            eth0_dhcpstate = DHCPSTATE_BOOTP_FALLBACK;
            // so the dhcp machine does no harm if called
//#endif
//            if (do_bootp(eth1_name, &eth1_bootp_data))
//#endif
            {
#ifdef CYGHWR_NET_DRIVER_ETH1_BOOTP_SHOW
                show_bootp(eth1_name, &eth1_bootp_data);
#endif
            } else {
                printf("BOOTP/DHCP failed on eth1\n");
                eth1_up = false;
            }
        }
        else
        {
            eth1_up = true;
            strncpy(ip_addr_str, 
                    inet_ntoa(*(struct in_addr *) &local_ip_addr), 16);
            strncpy(broadcast_str, 
                    inet_ntoa(*(struct in_addr *) &broadcast_addr), 16);
            strncpy(gateway_str, inet_ntoa(*(struct in_addr *) &gateway_addr), 
                    16);
            strncpy(network_str, inet_ntoa(*(struct in_addr *) &network_mask), 
                    16);
            strncpy(server_str, inet_ntoa(*(struct in_addr *) &server_addr), 
                    16);

            /* load flash configuration parameters */
            build_bootp_record(&eth1_bootp_data, eth1_name, ip_addr_str, 
                               network_str,  broadcast_str, gateway_str, 
                               server_str);
#ifdef CYGHWR_NET_DRIVER_ETH1_BOOTP_SHOW
            show_bootp(eth1_name, &eth1_bootp_data);
#endif
        }
    }
#endif

#ifdef CYGHWR_NET_DRIVER_ETH0
    if (eth0_up) {
        if (!init_net(eth0_name, &eth0_bootp_data)) {
            printf("Network initialization failed for eth0\n");
            eth0_up = false;
        }
#ifdef CYGHWR_NET_DRIVER_ETH0_IPV6_PREFIX
        if (!init_net_IPv6(eth0_name, &eth0_bootp_data,
                           string(CYGHWR_NET_DRIVER_ETH0_IPV6_PREFIX))) {
            diag_printf("Static IPv6 network initialization failed for eth0\n");
            eth0_up = false;  // ???
        }
#endif
        if (eth0_up)
        {
            unsigned int length = 4;
            struct in_addr temp;
            diag_printf("\nIP: %s", inet_ntoa(eth0_bootp_data.bp_yiaddr));
                
            get_bootp_option(&eth0_bootp_data, TAG_SUBNET_MASK, 
                             (void *)&temp, &length);
            diag_printf("/%s", inet_ntoa(temp));
            get_bootp_option(&eth0_bootp_data, TAG_GATEWAY, 
                             (void *)&temp, &length);
//          diag_printf(", Gateway: %s\n", inet_ntoa(eth0_bootp_data.bp_giaddr));
            diag_printf(", Gateway: %s\n", inet_ntoa(temp));
            diag_printf("Server: %s", inet_ntoa(eth0_bootp_data.bp_siaddr));
            net_up = true;
        }
    }
#endif

#ifdef CYGHWR_NET_DRIVER_ETH1
    if (eth1_up) {
        if (!init_net(eth1_name, &eth1_bootp_data)) {
            printf("Network initialization failed for eth1\n");
            eth1_up = false;
        }
#ifdef CYGHWR_NET_DRIVER_ETH1_IPV6_PREFIX
        if (!init_net_IPv6(eth1_name, &eth1_bootp_data,
                           string(CYGHWR_NET_DRIVER_ETH1_IPV6_PREFIX))) {
            diag_printf("Static IPv6 network initialization failed for eth1\n");
            eth1_up = false;  // ???
        }
#endif
        if (eth1_up)
        {
            unsigned int length = 4;
            struct in_addr temp;
            diag_printf("\nIP: %s", inet_ntoa(eth1_bootp_data.bp_yiaddr));
                
            get_bootp_option(&eth1_bootp_data, TAG_SUBNET_MASK, 
                             (void *)&temp, &length);
            diag_printf("/%s", inet_ntoa(temp));
            get_bootp_option(&eth1_bootp_data, TAG_GATEWAY, 
                             (void *)&temp, &length);
//          diag_printf(", Gateway: %s\n", inet_ntoa(eth1_bootp_data.bp_giaddr));
            diag_printf(", Gateway: %s\n", inet_ntoa(temp));
            diag_printf("Server: %s", inet_ntoa(eth1_bootp_data.bp_siaddr));
            net_up = true;
        }
    }
#endif
#ifdef CYGPKG_NET_NLOOP
#if 0 < CYGPKG_NET_NLOOP
    // Create a local IP Address for each of the DSP's */
    for (i = 0; i < CYGPKG_NET_NLOOP; ++i)
        init_loopback_interface(i);
#endif
#endif

#ifdef CYGOPT_NET_DHCP_DHCP_THREAD
    dhcp_start_dhcp_mgt_thread();
#endif   
 
#ifdef CYGOPT_NET_IPV6_ROUTING_THREAD
    ipv6_start_routing_thread();
#endif
 
#ifdef CYGPKG_NS_DNS_BUILD
    if (net_up)
    {
        struct in_addr dns_addr = {0};
        int        len;
        char       *dns_domainname;
        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_dns_ip", &dns_addr, 
                                    CYGNUM_FLASH_CFG_OP_CONFIG_IP);
      
        cyg_dns_res_init(&dns_addr);

        CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
                                    "app_dns_domain_name", 
                                    &dns_domainname, 
                                    CYGNUM_FLASH_CFG_OP_CONFIG_STRING);
        len = strlen(dns_domainname);
      
        setdomainname(dns_domainname,len);
        diag_printf(", DNS server IP: %s", 
                    inet_ntoa(*(struct in_addr *)&dns_addr));

    }
#endif
    
    if (!net_up)
    {
        diag_printf("Network Disabled");
    }
    diag_printf("\n\n");
}
Exemplo n.º 2
0
void GetPubIPInfo(unsigned long *pulPublicIpAddress,
							unsigned long *pulPublicSubnetMask,
							unsigned long *pulDefaultGateway,
							unsigned long *pulDNSServer)
{
	BOOL bEnable[2];
	int fd;
	struct ifreq ifr;
	bEnable[0] = g_ConfigParam.abAsNetEnable[0];
	bEnable[1] = g_ConfigParam.abAsNetEnable[1];
	
	
#ifndef WLAN
	if (!bEnable[0] && !bEnable[1]) bEnable[0] = TRUE;
   	fd = socket(AF_INET, SOCK_DGRAM, 0, g_Mctest_Buf, MCTEST_BUFFER_LEN);
	strcpy(ifr.ifr_name, g_apcNetworkInterface[0]);
#else
	if (!bEnable[0] && !bEnable[1]) bEnable[1] = TRUE;
    fd = socket(AF_INET, SOCK_DGRAM, 0);
	strcpy(ifr.ifr_name, g_apcNetworkInterface[1]);
#endif
    if (fd < 0) 
    {
        perror("socket");
        return;
    }

  	/*Get the IP Address*/ 
	if(pulPublicIpAddress != NULL)
	{        
#ifndef WLAN  	   
        if (netioctl(fd, SIOCGIFADDR, &ifr, sizeof(ifr),g_Mctest_Buf, MCTEST_BUFFER_LEN) >= 0)
#else
        if (ioctl(fd, SIOCGIFADDR, &ifr) >= 0)
#endif         
            *pulPublicIpAddress = (unsigned int)(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr);
         else
         	*pulPublicIpAddress = 0;
#ifndef WLAN  	   
        diag_printf("ipaddr Address====%s\n",inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr, g_Mctest_Buf, MCTEST_BUFFER_LEN));  
#else
        diag_printf("ipaddr Address====%s\n",inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));  
#endif 
	}   
	     
	/*Get the Netmask*/
	if(pulPublicSubnetMask != NULL)
	{         	
#ifndef WLAN  	   
        if (netioctl(fd, SIOCGIFNETMASK, &ifr,sizeof(ifr), g_Mctest_Buf, MCTEST_BUFFER_LEN) >= 0)
#else
        if (ioctl(fd, SIOCGIFNETMASK, &ifr) == 0)
#endif         
            *pulPublicSubnetMask = (unsigned int)(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr);
        else
            *pulPublicSubnetMask = 0;
#ifndef WLAN  	   
        diag_printf("Netmask====%s\n",inet_ntoa(((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr, g_Mctest_Buf, MCTEST_BUFFER_LEN));    
#else
        diag_printf("Netmask====%s\n",inet_ntoa(((struct sockaddr_in *)&ifr.ifr_netmask)->sin_addr));    
#endif   
	}
	
     /*Get the Gateway*/
	if(pulDefaultGateway != NULL)
	{
#ifndef WLAN         
        {
        	unsigned long Gateway;
        	char tmpgw[16];
        	char *c,*p;
        	int i = 0;
        	Gateway = wb740getgateway((char*)(ifr.ifr_name),g_Mctest_Buf, MCTEST_BUFFER_LEN);
        	diag_printf("Gateway====%s\n",inet_ntoa(*(struct in_addr*)&Gateway, g_Mctest_Buf, MCTEST_BUFFER_LEN));
        	if(Gateway != 0L)
        	{
        		strcpy(tmpgw,inet_ntoa(*(struct in_addr*)&Gateway, g_Mctest_Buf, MCTEST_BUFFER_LEN));
        		p = tmpgw;
        		while(*p != '\0')
        		{
        			c = p;
        			while((*p != '.') && (*p != '\0'))
        				p++;
        			*p = '\0';
        			ipmc[dev_num].gwaddr[i] = (unsigned char)atoi(c);          			
        			if(i < 3)
        			{p++; i++;  }  		
        		}	
				
			} 
			else 
				memset(ipmc[dev_num].gwaddr,0 ,4);
			
		}
#else
		if (g_ConfigParam.aucIPAssignedWay[1] == IP_ASSIGNMENT_MANUALLY)
			*pulDefaultGateway = g_ConfigParam.ulAsGateway[1];
		else
		{
			struct in_addr gateway;
			unsigned int length = sizeof(gateway);
			get_bootp_option(&wlan0_bootp_data, TAG_GATEWAY, (void*)&gateway,&length);
			diag_printf("gateway: %x\n", gateway.s_addr);
			*pulDefaultGateway = gateway.s_addr;
		}
#endif
	}
#ifndef WLAN
    netclose(fd, g_Mctest_Buf, MCTEST_BUFFER_LEN);
#else
    close(fd);
#endif
	if(pulDNSServer != NULL)
	{
		if (g_ConfigParam.aucIPAssignedWay[1] == IP_ASSIGNMENT_MANUALLY)
		{
			*pulDNSServer = g_ConfigParam.aulAsDNS[0];
   			*(pulDNSServer+1) = g_ConfigParam.aulAsDNS[1];
   			*(pulDNSServer+2) = g_ConfigParam.aulAsDNS[2];
   		}
   		else
   		{
			struct in_addr gateway;
			unsigned int length = sizeof(gateway);
			get_bootp_option(&wlan0_bootp_data, TAG_GATEWAY, (void*)&gateway,&length);
			diag_printf("gateway as dns: %x\n", gateway.s_addr);
			
			*pulDefaultGateway = gateway.s_addr;
			*pulDNSServer = gateway.s_addr;
   			*(pulDNSServer+1) = 0;
   			*(pulDNSServer+2) = 0;
   		}
	}
	
	if (g_ConfigParam.aucIPAssignedWay[1] == IP_ASSIGNMENT_DHCP
		&& wlan0_dhcpstate != DHCPSTATE_BOOTP_FALLBACK
		&& wlan0_dhcpstate != DHCPSTATE_BOUND
		)
	{
		if (g_WebCamState.bDHCP_Finished)
		{
			unsigned int GetRandomIP(void);
			unsigned int ulIP = GetRandomIP();
			if (pulPublicIpAddress != NULL)
				*pulPublicIpAddress = ulIP;
			if (pulPublicSubnetMask != NULL)
				*pulPublicSubnetMask = 0x0000FFFF;
			if (pulDefaultGateway != NULL)
				*pulDefaultGateway = ulIP;
		}
		else
		{
			if (pulPublicIpAddress != NULL)
				*pulPublicIpAddress = 0;
			if (pulPublicSubnetMask != NULL)
				*pulPublicSubnetMask = 0xFFFFFFFF;
			if (pulDefaultGateway != NULL)
				*pulDefaultGateway = 0;
		}
		if (pulDNSServer != NULL)
		{
			*pulDNSServer = 0;
			*(pulDNSServer + 1) = 0;
			*(pulDNSServer + 2) = 0;
		}
	}
    return;

}