void if_simple_setup(ifnet *interface, int addr, int netmask, int bcast, int net, int router, int def_router) { ifaddr *address; //addr = htonl(addr); //netmask = htonl(netmask); //bcast = htonl(bcast); // set the ip address for this net interface address = malloc(sizeof(ifaddr)); address->addr.len = 4; address->addr.type = ADDR_TYPE_IP; //NETADDR_TO_IPV4(address->addr) = htonl(addr); NETADDR_TO_IPV4(address->addr) = addr; address->netmask.len = 4; address->netmask.type = ADDR_TYPE_IP; //NETADDR_TO_IPV4(address->netmask) = htonl(netmask); NETADDR_TO_IPV4(address->netmask) = netmask; address->broadcast.len = 4; address->broadcast.type = ADDR_TYPE_IP; //NETADDR_TO_IPV4(address->broadcast) = htonl(bcast); NETADDR_TO_IPV4(address->broadcast) = bcast; if_bind_address(interface, address); #if 1 printf("if a "); dump_ipv4_addr(addr); printf(" mask "); dump_ipv4_addr(netmask); printf(" broad "); dump_ipv4_addr(bcast); printf("\n"); #endif // set up an initial routing table int rc; if( (rc = ipv4_route_add( net, netmask, router, interface->id) ) ) { SHOW_ERROR( 1, "Adding route - failed, rc = %d", rc); } else { SHOW_INFO0( 2, "Adding route - ok"); } SHOW_INFO0( 2, "Adding default route..."); if( (rc = ipv4_route_add_default( router, interface->id, def_router ) ) ) { SHOW_ERROR( 1, "Adding route - failed, rc = %d", rc); } else { SHOW_INFO0( 2, "Adding route - ok"); } #if 0 hal_start_kernel_thread_arg( bootp_thread, interface ); #else // Now try to get something real :) bootp(interface); #endif }
errno_t bootp(ifnet *iface) { struct bootp_state _bstate; void * udp_sock; struct bootp_state *bstate = &_bstate; memset( &_bstate, 0, sizeof(struct bootp_state) ); _bstate.expected_dhcpmsgtype = -1; int err = iface->dev->dops.get_address(iface->dev, &_bstate.mac_addr, sizeof(_bstate.mac_addr)); if(err < 0) { SHOW_ERROR0( 0, "can't get interface MAC address"); return ENXIO; } if( xid == 0 ) xid = (int)time(0) ^ 0x1E0A4F; // Some strange number :) int tries = 3; errno_t e; do { if( udp_open(&udp_sock) ) { SHOW_ERROR0( 0, "UDP - can't prepare endpoint"); return ENOTSOCK; } e = do_bootp( bstate, udp_sock, 0); udp_close(udp_sock); } while( e && (tries-- > 0) ); if(e) SHOW_ERROR( 0, "error %d", e); else { SHOW_FLOW( 1, "DHCP netmask: 0x%08X", bstate->smask); SHOW_FLOW( 1, "DHCP ip: %s", inet_ntoa(bstate->myip) ); SHOW_FLOW( 2, "gateway ip: %s", inet_ntoa(bstate->gateip) ); SHOW_FLOW( 2, "root ip: %s", inet_ntoa(bstate->rootip) ); SHOW_FLOW( 2, "server ip: %s", inet_ntoa(bstate->servip) ); SHOW_FLOW( 2, "rootpath: '%s'", bstate->rootpath ); SHOW_FLOW( 2, "hostname: '%s'", bstate->hostname ); SHOW_FLOW( 2, "bootfile: '%s'", bstate->bootfile ); // Now apply it to interface ifaddr *address; // set the ip address for this net interface address = malloc(sizeof(ifaddr)); address->addr.len = 4; address->addr.type = ADDR_TYPE_IP; NETADDR_TO_IPV4(address->addr) = bstate->myip.s_addr; address->netmask.len = 4; address->netmask.type = ADDR_TYPE_IP; NETADDR_TO_IPV4(address->netmask) = bstate->smask; u_int32_t bcast = (bstate->myip.s_addr) | bstate->smask; address->broadcast.len = 4; address->broadcast.type = ADDR_TYPE_IP; NETADDR_TO_IPV4(address->broadcast) = bcast; if_bind_address(iface, address); u_int32_t net = (bstate->myip.s_addr) & ~(bstate->smask); int rc; if( (rc = ipv4_route_add( net, ~(bstate->smask), bstate->myip.s_addr, iface->id) ) ) { SHOW_ERROR( 1, "Adding route - failed, rc = %d", rc); } else { SHOW_INFO0( 1, "Adding route - ok"); } if( (rc = ipv4_route_add_default( bstate->myip.s_addr, iface->id, bstate->gateip.s_addr ) ) ) { SHOW_ERROR( 1, "Adding default route - failed, rc = %d", rc); } else { SHOW_INFO0( 1, "Adding default route - ok"); } // At least one char! if(*bstate->hostname) strlcpy( phantom_uname.nodename, bstate->hostname, _UTSNAME_NODENAME_LENGTH ); } return e; }