?? dhcp_support.c
字號(hào):
ap = " subnet mask"; if (*op == TAG_GATEWAY) ap = " gateway"; if (*op == TAG_IP_BROADCAST) ap = " IP broadcast"; ap = (unsigned char *) & addr[0]; while (len > 0) { len -= sizeof(struct in_addr); ap += sizeof(struct in_addr); } break; case TAG_DOMAIN_SERVER:#ifdef DHCP_FIX dnsServer = 1;#endif ap = (unsigned char *) & addr[0]; len = *(op + 1); for (i = 0; i < len; i++) { *ap++ = *(op + i + 2); } if (*op == TAG_DOMAIN_SERVER) ap = "domain server"; ap = (unsigned char *) & addr[0]; while (len > 0) { len -= sizeof(struct in_addr); ap += sizeof(struct in_addr); } PRIMARYDNS = inet_address((unsigned long)addr[0].s_addr); SECONDARYDNS = inet_address((unsigned long)addr[1].s_addr);#if defined(GATEWAY) && defined(DHCPS)#ifndef DHCP_FIX /* Inform DHCP Server application */ dhcpsSetDNS(PRIMARYDNS, SECONDARYDNS);#endif#endif break; case TAG_DOMAIN_NAME: case TAG_HOST_NAME: for (i = 0; i < *(op + 1); i++) { name[i] = *(op + i + 2); } name[*(op + 1)] = '\0'; if (*op == TAG_DOMAIN_NAME) ap = " domain name"; if (*op == TAG_HOST_NAME) ap = " host name"; break; default: break; } op += *(op + 1) + 2; } }#if defined(GATEWAY) /* Inform Fast Routing module */ //if(mib_dhcpenable==0) //GWYADDR =0; FR_Main_WanIP_update();#ifdef DHCPS#ifdef DHCP_FIX if(defaultGW == 0) GWYADDR = 0; if(dnsServer == 0 && mib_dhcpenable == 1){ PRIMARYDNS = 0; SECONDARYDNS = 0; } /* Inform DHCP Server application */ dhcpsSetDNS(PRIMARYDNS, SECONDARYDNS);#endif#endif#endif#if !defined(GATEWAY) bp = ð1_bootp_data; //mib_IP_LAN.IPAddr = inet_address(eth1_bootp_data.bp_yiaddr.s_addr); IPADDR = inet_address(eth1_bootp_data.bp_yiaddr.s_addr); if (bp->bp_vend[0]) { op = &bp->bp_vend[4]; while (*op != TAG_END) { switch (*op) { case TAG_SUBNET_MASK: ap = (unsigned char *) & tmpaddr; len = *(op + 1); for (i = 0; i < len; i++) { *ap++ = *(op + i + 2); } while (len > 0) { len -= sizeof(struct in_addr); ap += sizeof(struct in_addr); } /*mib_IP_LAN.*/SUBNETMASK = inet_address(tmpaddr); break; default: break; } op += *(op + 1) + 2; } }#endif#if defined(DHCPS) /* notify DHCP Server about new IP */ dhcpsSetIpAddress(mib_IP_LAN.IPAddr, mib_IP_LAN.SubnetMask);#endif}typedef enum{ DHCPC_flag_init_E = 0, /* first init */ DHCPC_flag_set_from_mib = 1, /* set WAN static data (gate way) */ DHCPC_ip_post_config = 2,/* post config to IP layer */}DHCPC_flag_ENT;unsigned int dhcp_del_old_ip_and_reset_interface( const char *if_name, int s, struct ifreq *ifrp /* socket for ioctl operations */){ strcpy(ifrp->ifr_name, if_name); memset(&(ifrp->ifr_addr),0, sizeof(ifrp->ifr_addr)); if (ioctl(s, SIOCGIFADDR, ifrp)) { if(errno != EADDRNOTAVAIL){ eth0_up = false; return false; } } else{ /* found IP so delete */ if (ioctl(s, SIOCDIFADDR, ifrp)) { perror("SIOCDIFADDR1"); } } // Shut down interface so it can be reinitialized ifrp->ifr_flags &= ~(IFF_UP | IFF_RUNNING); if (ioctl(s, SIOCSIFFLAGS, ifrp)) { eth0_up = false; return false; } ifrp->ifr_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING; if (ioctl(s, SIOCSIFFLAGS, ifrp)) { /* set ifnet flags */ perror("SIOCSIFFLAGS up"); return false; } return true;}void init_static_network_interfaces(int flag){/* flag 0 init defaults*//* flag 2 set IP to current configuration *//* flag 4 delete old configuration and prepare configuration for static*//* flag 5 delete old configuration */#if defined(GATEWAY) int s; struct ifreq ifr;// struct sockaddr_in *addrpp;// struct ecos_rtentry route;// struct in_addr netmask, gateway;#else int i; in_addr_t addr; unsigned char *vp; unsigned char cookie[] = VM_RFC1048; struct bootp *bp;#endif#ifdef CYGDBG_NET_DHCP_CHATTER diag_printf("init_static_network_interfaces: flag %d\n",flag);#endif#if defined(GATEWAY) s = socket(AF_INET, SOCK_DGRAM, 0); if (s < 0) { eth0_up = false; return; } if (flag == DHCPC_flag_init_E) { init_all_network_interfaces(); //initialize network interface first /* if WAN has configuration*/#ifdef DHCP_FIX if (mib_dhcpenable == 1 ) { if (!dhcp_del_old_ip_and_reset_interface( eth0_name, s, &ifr)){ close(s); return; } } else if ( (IPADDR != 0xffffffff) && (IPADDR != 0 )) {#else if (mib_dhcpenable == 0 && (IPADDR != 0xffffffff && IPADDR != 0 )) {#endif if (!dhcp_del_old_ip_and_reset_interface( eth0_name, s, &ifr)){ close(s); return; } dhcp_build_bootp(ð0_bootp_data, IPADDR, SUBNETMASK, GWYADDR, 0); /* delete old(default IP) */#ifdef CYGDBG_NET_DHCP_CHATTER diag_printf("Network initialization for eth0 from mib %x\n",mib_DHCP_p->IPAddr);#endif if (!init_net(eth0_name, ð0_bootp_data)) { //diag_printf("Network initialization failed for eth0\n"); eth0_up = false;#ifdef CYGDBG_NET_DHCP_CHATTER diag_printf("Network initialization failed for eth0\n");#endif } } /* update eth1 from MIB */ if ( IPADDR != 0xffffffff && IPADDR != 0) { eth1_up = 1; if (!dhcp_del_old_ip_and_reset_interface( eth1_name, s, &ifr)){ close(s); return; } dhcp_build_bootp(ð1_bootp_data, IPADDR, SUBNETMASK, 0, 0);#ifdef CYGDBG_NET_DHCP_CHATTER diag_printf("Network initialization for eth1 from mib %x\n",mib_IP_LAN.IPAddr);#endif if (!init_net(eth1_name, ð1_bootp_data)) { eth1_up = false;#ifdef CYGDBG_NET_DHCP_CHATTER diag_printf("Network initialization failed for eth1\n");#endif } }#ifdef DHCP_FIX//#endif//#ifdef CYGHWR_NET_DRIVER_ETH1_ADDRS_IP /* default manual IP from ecos\packages\net\tcpip\v1_0b1\src\lib\network_support.c */ else{ /* update lan from default */ IPADDR = inet_addr(string(CYGHWR_NET_DRIVER_ETH1_ADDRS_IP)); IPADDR = htonl(IPADDR); SUBNETMASK = inet_addr(string(CYGHWR_NET_DRIVER_ETH1_ADDRS_NETMASK)); SUBNETMASK = htonl(SUBNETMASK); }#endif#ifdef CYGDBG_NET_DHCP_CHATTER diag_printf("init_static_network_interfaces: Lan IP %x mask %x\n", IPADDR, SUBNETMASK);#endif close(s); return; } else if (flag == DHCPC_flag_set_from_mib){ /* set wan info from new static*/ // Clean old static configuration // get the specific address that was used if (!dhcp_del_old_ip_and_reset_interface( eth0_name, s, &ifr)){ close(s); return; } dhcp_build_bootp(ð0_bootp_data, IPADDR, SUBNETMASK, GWYADDR, 0); if (!init_net(eth0_name, ð0_bootp_data)) { diag_printf("Network initialization failed for eth0\n"); eth0_up = false; } // All done with socket } close(s);#else /* previous code */ if (flag != 2) { if (IPADDR == 0xffffffff || IPADDR == 0) { init_all_network_interfaces(); //initialize network interface first return ; }#ifdef CYGHWR_NET_DRIVER_ETH0 eth0_up = true; bp = ð0_bootp_data; bzero(bp, sizeof(struct bootp)); bp->bp_op = BOOTREPLY; bp->bp_htype = HTYPE_ETHERNET; bp->bp_hlen = 6; for (i = 0; i < bp->bp_hlen; i++) { bp->bp_chaddr[i] = 0xFF; } bp->bp_ciaddr.s_addr = inet_address(mib_DHCP_p->IPAddr); bp->bp_yiaddr.s_addr = inet_address(mib_DHCP_p->IPAddr); bp->bp_siaddr.s_addr = inet_address((mib_DHCP_p->IPAddr & 0xffffff00) + 0x64); bp->bp_giaddr.s_addr = inet_address((mib_DHCP_p->IPAddr & 0xffffff00) + 0x64); vp = &bp->bp_vend[0]; bcopy(&cookie, vp, sizeof(cookie)); vp += sizeof(cookie); addr = inet_address(mib_DHCP_p->SubnetMask); vp = add_tag(vp, TAG_SUBNET_MASK, &addr, sizeof(in_addr_t)); addr = inet_address((mib_DHCP_p->IPAddr & mib_DHCP_p->SubnetMask) ^ (~mib_DHCP_p->SubnetMask)); vp = add_tag(vp, TAG_IP_BROADCAST, &addr, sizeof(in_addr_t)); *vp = TAG_END;#endif#if 0//def CYGHWR_NET_DRIVER_ETH1 eth1_up = true; bp = ð1_bootp_data; bzero(bp, sizeof(struct bootp)); bp->bp_op = BOOTREPLY; bp->bp_htype = HTYPE_ETHERNET; bp->bp_hlen = 6; for (i = 0; i < bp->bp_hlen; i++) { bp->bp_chaddr[i] = 0xFF; } bp->bp_ciaddr.s_addr = inet_address(mib_DHCP_p->IPAddr); bp->bp_yiaddr.s_addr = inet_address(mib_DHCP_p->IPAddr); bp->bp_siaddr.s_addr = inet_address(mib_DHCP_p->GwyAddr & 0xffffff64); bp->bp_giaddr.s_addr = inet_address(mib_DHCP_p->GwyAddr & 0xffffff64); vp = &bp->bp_vend[0]; bcopy(&cookie, vp, sizeof(cookie)); vp += sizeof(cookie); addr = inet_address(mib_DHCP_p->SubnetMask); vp = add_tag(vp, TAG_SUBNET_MASK, &addr, sizeof(in_addr_t)); addr = inet_address((mib_DHCP_p->IPAddr & mib_DHCP_p->SubnetMask) ^ (~mib_DHCP_p->SubnetMask)); vp = add_tag(vp, TAG_IP_BROADCAST, &addr, sizeof(in_addr_t)); *vp = TAG_END;#endif#ifdef CYGHWR_NET_DRIVER_ETH0#ifndef CYGHWR_NET_DRIVER_ETH0_MANUAL if (eth0_up) { if (!init_net(eth0_name, ð0_bootp_data)) { //diag_printf("Network initialization failed for eth0\n"); eth0_up = false; } }#endif#endif#if 0//def CYGHWR_NET_DRIVER_ETH1#ifndef CYGHWR_NET_DRIVER_ETH1_MANUAL if (eth1_up) { if (!init_net(eth1_name, ð1_bootp_data)) { //diag_printf("Network initialization failed for eth1\n"); eth1_up = false; } }#endif#endif }#endif if (flag != DHCPC_flag_init_E ) { update_IP_Info(); cyg_semaphore_post(&ip_ready); }}// ---------------------------------------------------------------------------//// The point of this module is to deal with all the horrid written out in// full stuff of having two interfaces; it's ugly but it's also most// flexible. The dhcp_prot.c module should do all the work...//// ---------------------------------------------------------------------------// return value: 1 => everything OK, no change.// 0 => close your connections, then call do_dhcp_halt() to halt the// interface(s) in question (it knows because the state will be NOTBOUND).// After that you can return to the start and use// init_all_network_interfaces(); as usual, or call do_dhcp_bind() by hand,// or whatever...int dhcp_bind( void ){#ifdef CYGHWR_NET_DRIVER_ETH0 cyg_uint8 old_eth0_dhcpstate = eth0_dhcpstate;#endif#if !defined(GATEWAY)#ifdef CYGHWR_NET_DRIVER_ETH1 cyg_uint8 old_eth1_dhcpstate = eth1_dhcpstate;#endif#endif
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -