⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dhcp_support.c

📁 Mavell 无线模块原厂IC AP32源代码。DHCP客户端与服务端源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
#ifdef CYGDBG_NET_DHCP_CHATTER     diag_printf("dhcp_bind\n");#endif    // If there are no interfaces at all, init it every time, doesn't    // matter.  In case we are called from elsewhere...    if ( 1#ifdef CYGHWR_NET_DRIVER_ETH0            && eth0_dhcpstate == 0#endif#if !defined(GATEWAY)#ifdef CYGHWR_NET_DRIVER_ETH1            && eth1_dhcpstate == 0#endif#endif       )        cyg_semaphore_init( &dhcp_needs_attention, 0 );    // Run the state machine...#ifdef CYGHWR_NET_DRIVER_ETH0    if (eth0_up            && DHCPSTATE_FAILED != eth0_dhcpstate )        eth0_up = do_dhcp(eth0_name, &eth0_bootp_data, &eth0_dhcpstate, &eth0_lease);#endif#if !defined(GATEWAY)#ifdef CYGHWR_NET_DRIVER_ETH1    if (eth1_up            && DHCPSTATE_FAILED != eth1_dhcpstate )        eth1_up = do_dhcp(eth1_name, &eth1_bootp_data, &eth1_dhcpstate, &eth1_lease);#endif#endif    // If the interface newly came up, initialize it:    // (this duplicates the code in init_all_network_interfaces() really).#ifdef CYGHWR_NET_DRIVER_ETH0#if 0    if ( eth0_up            && eth0_dhcpstate == DHCPSTATE_BOUND            && old_eth0_dhcpstate != eth0_dhcpstate )    {        if (!init_net(eth0_name, &eth0_bootp_data))        {            eth0_up = false;        }    }#endif#endif#if !defined(GATEWAY)#ifdef CYGHWR_NET_DRIVER_ETH1    if ( eth1_up            && eth1_dhcpstate == DHCPSTATE_BOUND            && old_eth1_dhcpstate != eth1_dhcpstate )    {        if (!init_net(eth1_name, &eth1_bootp_data))        {            eth1_up = false;        }    }#endif#endif#ifdef CYGHWR_NET_DRIVER_ETH0    if ( old_eth0_dhcpstate == DHCPSTATE_BOUND &&            eth0_dhcpstate == DHCPSTATE_NOTBOUND )        return 0; // a lease timed out; we became unbound#endif#if !defined(GATEWAY)#ifdef CYGHWR_NET_DRIVER_ETH1    if ( old_eth1_dhcpstate == DHCPSTATE_BOUND &&            eth1_dhcpstate == DHCPSTATE_NOTBOUND )        return 0; // a lease timed out; we became unbound#endif#endif    if (eth0_up)        return 1; // all is well    else        return 0;}// Shutdown any interface whose state is DHCPSTATE_NOTBOUND.int dhcp_halt( void ){#ifdef CYGDBG_NET_DHCP_CHATTER     diag_printf("dhcp_halt\n");#endif#ifdef CYGHWR_NET_DRIVER_ETH0    if ( eth0_up            && eth0_dhcpstate != DHCPSTATE_FAILED )    {        do_dhcp_down_net(eth0_name, &eth0_bootp_data, &eth0_dhcpstate, &eth0_lease);//        eth0_bootp_data.bp_ciaddr.s_addr = 32;    }    eth0_up = false;#endif#if !defined(GATEWAY)#ifdef CYGHWR_NET_DRIVER_ETH1    if ( eth1_up            && eth1_dhcpstate != DHCPSTATE_FAILED )    {        do_dhcp_down_net(eth1_name, &eth1_bootp_data, &eth1_dhcpstate, &eth1_lease);    }    eth1_up = false;#endif#endif    return 1;}// Release (and set state to DHCPSTATE_NOTBOUND) all interfaces - we are// closing down.  (unlikely but maybe useful for testing)int dhcp_release( void ){#ifdef CYGDBG_NET_DHCP_CHATTER     diag_printf("dhcp_release\n");#endif#ifdef CYGHWR_NET_DRIVER_ETH0    if (eth0_up)        do_dhcp_release(eth0_name, &eth0_bootp_data, &eth0_dhcpstate, &eth0_lease);#endif#if !defined(GATEWAY)#ifdef CYGHWR_NET_DRIVER_ETH1    if (eth1_up)        do_dhcp_release(eth1_name, &eth1_bootp_data, &eth1_dhcpstate, &eth1_lease);#endif#endif    return 1;}// ------------------------------------------------------------------------// The management thread functionvoid dhcp_mgt_entry( cyg_addrword_t loop_on_failure ){#if 1    struct ifreq ifr;    int    s;	unsigned int dhcp_enable_old_val=-1;#ifdef CYGHWR_NET_DRIVER_ETH0    cyg_uint8 *cl_stste_PTR = &eth0_dhcpstate;#endif    if(mib_dhcpenable)    {		dhcp_enable_old_val = mib_dhcpenable;        if ( !dhcp_bind() ) // a lease expired        {            dhcp_halt(); // tear everything down            init_my_network_interfaces(INIT_WAN_ONLY);			dhcp_enable_old_val  = !mib_dhcpenable;        }    }    update_IP_Info();    cyg_semaphore_post(&ip_ready);#if PROD_NAME == 0	TimerFireEvery(&LinkStatusTimer, 1, &LinkStatusCheckTimeOut, NULL, 50 );#endif    while(1)    {        while ( 1 )        {			if(dhcp_enable_old_val != mib_dhcpenable){               	if(dhcp_enable_old_val==1)               	{                	dhcp_halt(); // tear everything down               	}				else				{					eth0_up = true;#ifdef DHCP_FIX                    s = socket(AF_INET, SOCK_DGRAM, 0);                    if(s >= 0){                         dhcp_del_old_ip_and_reset_interface( eth0_name,                                                             s,                                                             &ifr);                        close(s);                    }                    memset(&eth0_bootp_data,0 , sizeof(eth0_bootp_data));#endif               }               *cl_stste_PTR = DHCPSTATE_INIT;			}            if (mib_dhcpenable)            {				int retries = 3;				while(retries--)				{                    	dhcp_halt(); // tear everything down                	init_my_network_interfaces(INIT_WAN_ONLY);                	if ( !dhcp_bind() ) // a lease expired                	{                	    dhcp_halt(); // tear everything down                	    init_my_network_interfaces(INIT_WAN_ONLY);						dhcp_enable_old_val  = !mib_dhcpenable;                	}					else					{						dhcp_enable_old_val  = mib_dhcpenable;						break;				}            }            }            else			{                init_my_network_interfaces(INIT_WAN_ONLY);				dhcp_enable_old_val  = !mib_dhcpenable;            }            update_IP_Info();            cyg_semaphore_post(&ip_ready);#ifdef DHCPS			DHCPSReinit();#endif            break; // If we need to re-bind        }#ifdef CYGPKG_SNMPAGENT        SnmpdShutDown(0) // Cycle the snmpd state#endif        cyg_semaphore_wait( &dhcp_needs_attention );    }#endif#if 0	unsigned int dhcp_enable_old_val, retry_flag = 0;        struct ifreq ifr;        int    s;#ifdef CYGHWR_NET_DRIVER_ETH0    cyg_uint8 *cl_stste_PTR = &eth0_dhcpstate;#endif#if !defined(GATEWAY)#ifdef CYGHWR_NET_DRIVER_ETH1    cyg_uint8 *cl_stste_PTR = &eth1_dhcpstate;#endif#endif#ifdef CYGDBG_NET_DHCP_CHATTER     diag_printf("dhcp_mgt_entry\n");#endif    init_static_network_interfaces(DHCPC_flag_init_E);    cyg_semaphore_wait( &dhcp_needs_attention );    if (mib_dhcpenable)    {        /* clear junk mib and update */        memset(MIB_DHCP_P,0,sizeof(MIB_DHCP));#ifndef AP31E        SetUserConfig();#endif        /* notify users on change */        init_static_network_interfaces(DHCPC_ip_post_config);        if ( !dhcp_bind() )        {            retry_flag = 1;        }        else        {            init_static_network_interfaces(DHCPC_ip_post_config);        }    }    else    {        init_static_network_interfaces(DHCPC_ip_post_config);    }    while ( 1 )    {        while ( 1 )        {            dhcp_enable_old_val  = mib_dhcpenable; /* avoid fail for ever                                                    when re enabling DHCP */            if(retry_flag)                cyg_thread_delay( 100 );            else            cyg_semaphore_wait( &dhcp_needs_attention );            retry_flag = 0;#ifndef AP31E            SetUserConfig();#endif			if(dhcp_enable_old_val != mib_dhcpenable){               if(dhcp_enable_old_val){               	   dhcp_halt();               }               else{                   eth0_up = true; /* reset current status */#ifdef DHCP_FIX                    s = socket(AF_INET, SOCK_DGRAM, 0);                    if(s >= 0){                         dhcp_del_old_ip_and_reset_interface( eth0_name,                                                             s,                                                             &ifr);                        close(s);                    }                    memset(&eth0_bootp_data,0 , sizeof(eth0_bootp_data));                    /* clear configuration */                    memset(MIB_DHCP_P,0,sizeof(MIB_DHCP));#ifndef AP31E            		SetUserConfig();#endif                    /* notify users on change */                    init_static_network_interfaces(DHCPC_ip_post_config);#endif               }               *cl_stste_PTR = DHCPSTATE_INIT;            }            if (mib_dhcpenable)            {                if(*cl_stste_PTR != DHCPSTATE_BOUND){                	 *cl_stste_PTR = DHCPSTATE_INIT;				}#ifdef CYGDBG_NET_DHCP_CHATTER                diag_printf("dhcp_mgt_entry :dhcp state = %d\n", eth0_dhcpstate);                diag_printf("dhcp_mgt_entry: dhcp bind 3\n");#endif                if ( ! dhcp_bind() ) // a lease expired                {                    dhcp_halt();	                retry_flag = 1;                    eth0_up = true;                    continue; /* cause retry */                }#ifdef CYGDBG_NET_DHCP_CHATTER                diag_printf("dhcp_mgt_entry :new state = %d", eth0_dhcpstate);#endif            }            else                init_static_network_interfaces(DHCPC_flag_set_from_mib);            init_static_network_interfaces(DHCPC_ip_post_config);/*            if((mib_DHCP_p->SubnetMask & 0xff000000) != 0xff000000)            {                if( (mib_DHCP_p->IPAddr & 0xC0000000) == 0xC0000000)                    mib_DHCP_p->SubnetMask = 0xffffff00;                else if( (mib_DHCP_p->IPAddr & 0xC0000000) == 0x80000000)                    mib_DHCP_p->SubnetMask = 0xffff0000;                else                    mib_DHCP_p->SubnetMask = 0xff000000;                init_static_network_interfaces(0);            }*/            break; // If we need to re-bind        }#if 0        dhcp_halt(); // tear everything down        if ( !loop_on_failure )            return ; // exit the thread/return        init_static_network_interfaces(1); // re-initialize        for ( j = 0; j < CYGPKG_NET_NLOOP; j++ )            init_loopback_interface( j );#endif#ifdef CYGPKG_SNMPAGENT        SnmpdShutDown(0) // Cycle the snmpd state#endif    }#endif}#ifdef CYGOPT_NET_DHCP_DHCP_THREAD // Then we provide such a thread...cyg_handle_t dhcp_mgt_thread_h = 0;cyg_thread dhcp_mgt_thread;/* Need this because this is a port of a later tcpip package */#define CYGOPT_NET_DHCP_DHCP_THREAD_PARAM 1#define CYGOPT_NET_DHCP_DHCP_THREAD_PARAM_1#define CYGPKG_NET_DHCP_THREAD_PRIORITY 8#define CYGPKG_NET_DHCP_THREAD_PRIORITY_8#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + sizeof(struct bootp))static cyg_uint8 dhcp_mgt_stack[ STACK_SIZE ];void dhcp_start_dhcp_mgt_thread( void ){    cyg_semaphore_init (&ip_ready, 0);    init_my_network_interfaces(INIT_ALL);    update_IP_Info();    if ( ! dhcp_mgt_thread_h )    {        cyg_semaphore_init( &dhcp_needs_attention, 0 );        cyg_thread_create(            CYGPKG_NET_DHCP_THREAD_PRIORITY,  /* scheduling info (eg pri) */            dhcp_mgt_entry,              /* entry point function */            CYGOPT_NET_DHCP_DHCP_THREAD_PARAM,  /* entry data */            "DHCP lease mgt",            /* optional thread name */            dhcp_mgt_stack,              /* stack base, NULL = alloc */            STACK_SIZE,                  /* stack size, 0 = default */            &dhcp_mgt_thread_h,          /* returned thread handle */            &dhcp_mgt_thread           /* put thread here */        );        cyg_thread_resume(dhcp_mgt_thread_h);    }}void initiate_dhcp(int force){    static int dhcp_init = 0;    if ( !dhcp_init || force)    {        dhcp_init++;        cyg_semaphore_post( &dhcp_needs_attention);    }}void wait_ipReady(void){    cyg_semaphore_wait (&ip_ready);    cyg_semaphore_post(&ip_ready);}#endif // CYGOPT_NET_DHCP_DHCP_THREAD#endif // CYGPKG_NET_DHCP// EOF dhcp_support.c

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -