Home |
Function xn_add_host_table_entry Function xn_delete_host_table_entry Function xn_interface_open_config Function xn_interface_ethernet_statistics Function xn_interface_statistics Function xn_autoip Function xn_send_ethernet_frame Function cb_wr_screen_string_fnc Function cb_rs232_connection_lost_fnc |
Function xn_autoipDetermine and apply a unique IP address via the Auto-IP protocol. int xn_autoip(int iface_no, const BYTE * low_ip_addr, const BYTE * high_ip_addr, const BYTE * ip_mask, BYTE * Result); Parametersiface_noInterface number (returned by xn_interface_open or xn_interface_open_config. low_ip_addrPointer to lowest allowed IP address in network byte order. If NULL is specified, default value 169.254.1.0 is used. high_ip_addrPointer to highest allowed IP address in network byte order. If NULL is specified, default value 169.254.254.255 is used. ip_maskPointer to network mask of the local LAN segment. If NULL is specified, default value 255.255.0.0 is used. ResultPointer to a 4 byte buffer to receive the IP address obtained. The parameter is optional and may be set to NULL if not needed. return valueReturns 0 if successful, otherwise SOCKET_ERROR. If an error occurred, call xn_getlasterror and xn_geterror_string to return the error value. Section Error Codes further describes each error. This function attempts to find a unique IP address in the range given by *low_ip_addr and *high_ip_addr. No DHCP, BOOTP, or other server is required on the LAN for this function to work. The function repeatedly chooses a random IP address and then checks via ARP if it is in use already. If so, a new address is tried until either a free address is found (and applied by calling xn_set_ip) or CFG_MAX_AUTOIP (default: 50) IP addresses have been tried without success (in this case, the function fails). When successful, the function sends out a gratuitous arp using xn_arp_send and the address will be checked every CFG_PROBE_INTERVAL (default: 8) seconds to make sure that no other host on the network is using the local address. If another host claims the local address at a later time and is unwilling to release it, the local host will restart its algorithm to find and apply a unique IP address. This should only happen in misconfigured networks, for example, if the same IP address range has been assigned to DHCP and Auto-IP, or hosts use hardwired IP addresses out of a DHCP or Auto-IP address range. Setting CFG_PROBE_INTERVAL to zero before xn_autoip is called disables periodic ARP probing. Care must be taken to allocate enough addresses to Auto-IP. If, for example, 20 hosts shall use Auto-IP to assign themselves unique IP addresses, and only 20 addresses are available, the last connecting hosts may need a lot of time to find a free address. Moreover, it could happen that a host needs more than CFG_MAX_AUTOIP attempts to find a free address, causing xn_autoip() to fail. If xn_autoip is used to check a single IP address (*low_ip_addr and *high_ip_addr are equal), CFG_MAX_AUTOIP should be set to 1. Unlike DHCP, the Auto-IP protocol is not able to determine the net mask, default gateway, or DNS server address. Example:static BYTE TargetIP[] = { 0, 0, 0, 0}; // will be filled at run-time static BYTE NetMask[] = {255, 255, 255, 0}; static BYTE MinIP[] = {192, 168, 1, 192}; static BYTE MaxIP[] = {192, 168, 1, 254}; static BYTE DefaultGateway[] = {192, 168, 1, 1}; // set to zero if no gateway static BYTE DNSServer[] = {192, 168, 1, 1}; // ditto ... Result = xn_rtip_init(); // Initialize the RTIP stack if (Result == SOCKET_ERROR) Error("xn_rtip_init failed"); Result = BIND_DRIVER(MINOR_0); // bind driver we want (see netcfg.h) if (Result == SOCKET_ERROR) Error("driver initialization failed"); // Open the interface interface = xn_interface_open(DEVICE_ID, MINOR_0); if (interface == SOCKET_ERROR) Error("xn_interface_open failed"); Result = xn_autoip(interface, MinIP, MaxIP, NetMask, TargetIP); if (Result == SOCKET_ERROR) Error("xn_autoip failed"); printf("Auto-assigned IP address %i.%i.%i.%i\n", TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]); // define default gateway and DNS server xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, RT_INF); xn_set_server_list((DWORD*)DNSServer, 1);
|