Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 42 Next »

The following sections provide sample code describing how to configure IP address (IPv4 and IPv6).

For a complete guide on IP addressing, refer to section IP Address Programming.

Configuring an IP Address on an Interface

Each network interface must be configured with at least one IP address. It could be an IPv4 or an IPv6 address or both depending on which modules the TCP-IP stack has enabled.

IPv4

For IPv4, the address configuration may be performed using µC/DHCPc or manually during run-time. If run-time configuration is chosen, the following functions may be utilized to set the IPv4, network mask, and gateway addresses for a specific interface.

NetASCII_Str_to_IP
NetIPv4_CfgAddrAdd

More than one set of IPv4 addresses may be configured for a specific network interface by calling the functions above. The constant NET_IPv4_CFG_IF_MAX_NBR_ADDR specified in net_cfg.h determines the maximum number of IPv4 addresses that may be assigned to an interface.

Note that on the default interface, the first IPv4 address added will be the default address used for all default IPv4 communication.

The first function aids the developer by converting a string format IPv4 address such as “192.168.1.2” to its hexadecimal equivalent. The second function is used to configure an interface with the specified IPv4, network mask and gateway addresses. An example is shown in listing Listing - IPv4 Address Configuration Example.


IPv4 Address Configuration Example
CPU_BOOLEAN    cfg_success;
NET_IPv4_ADDR  ipv4_addr;
NET_IPv4_ADDR  ipv4_msk;
NET_IPv4_ADDR  ipv4_gateway;
NET_ERR        err;


(void)NetASCII_Str_to_IP((CPU_CHAR*)"192.168.1.2",   &ipv4_addr,    NET_IPv4_ADDR_SIZE, &err);	/* See Note #1 */
(void)NetASCII_Str_to_IP((CPU_CHAR*)"255.255.255.0", &ipv4_msk,     NET_IPv4_ADDR_SIZE, &err);
(void)NetASCII_Str_to_IP((CPU_CHAR*)"192.168.1.1",   &ipv4_gateway, NET_IPv4_ADDR_SIZE, &err);

cfg_success = NetIPv4_CfgAddrAdd(if_nbr,         												/* See Note #2 */
                                 ipv4_addr,      												/* See Note #3 */
                                 ipv4_msk,       												/* See Note #4 */
                                 ipv4_gateway,   												/* See Note #5 */
                                &err);          					 							/* See Note #6 */


IPv6

Currently, the µC/TCP-IP stack only support manual static IPv6 address configuration and IPv6 Stateless Address Auto-Configuration. Dynamic address configuration with DHCPv6 is not yet supported.  

Manual Static Address Configuration

the following functions may be utilized to set the IPv6 address for a specific interface: 

NetASCII_Str_to_IP
NetIPv6_CfgAddrAdd
 
NetIPv6_CfgAddrHookSet 

More than one set of IPv6 addresses may be configured for a specific network interface by calling the functions above. The constant NET_IPv6_CFG_IF_MAX_NBR_ADDR specified in net_cfg.h determines the maximum number of IPv6 addresses that may be assigned to an interface.

Note that on the default interface, the first IPv6 address added will be the default address used for all default IPv6 communication. 

The first function aids the developer by converting a string format IPv6 address such as “fe80::1111:1111” to its network equivalent. The second function is used to configure an interface with the specified IPv6 address. An example is shown in listing Listing - IPv6 Address Configuration Example.

As shown in  Listing - IPv6 Address Configuration Example, the NetIPv6_CfgAddrAdd() function can take as argument a set of network flags. The following options are available :

FlagsDescription

NET_IPv6_FLAG_BLOCK_EN

Enables blocking mode.

NET_IPv6_FLAG_DAD_EN

Enables Duplication Address Detection (DAD) with the address configuration process. 

It is therefore possible to make the function blocking or not, or to enable Duplication Address Detection with the address configuration.  

If the function is made none blocking, it is possible to set a hook function to advertise the application when the address configuration process has finished. The API function NetIPv6_CfgAddrHookSet can be used to set the hook function. Refer to section IPv6 Static Address Configuration Hook Function for all the details on the hook function format and usage.  shows an example of a non-blocking IPv6 static address configuration.

Stateless Address Auto-Configuration

The IPv6 protocol defines an address Auto-Configuration procedure allowing a network interface to set itself an IPv6 Link-Local address based on its Interface ID. The Auto-Configuration process will also query the local network to found an IPv6 router that could send prefix information to set an IPv6 global address.

The µC/TCP-IP stack supports only the EUI-64 format for interface ID. This format creates a 64 bits ID based on the 48 bits MAC address of the interface. Those 64 bits will become the 64 least significant bits of the IPv6 addresses configured with the Stateless Auto-Configuration process. 

The following functions may be used to configure the IPv6 Stateless Auto-Configuration process: 

NetIPv6_AddrAutoCfgEn

NetIPv6_AddrAutoCfgDis

NetIPv6_AddrAutoCfgHookSet

The IPv6 Auto-Configuration procedure inside the µC/TCP-IP stack is a non-blocking process. To recover the result of the Auto-Configuration, a hook function can be configured that will be called by the TCP/IP stack when the Auto-Configuration has finished. The API function used to set the hook function is NetIPv6_AddrAutoCfgHookSet.  Refer to section IPv6 Stateless Address Auto-Configuration Hook Function for all the details on the Auto-Configuration hook function format and usage and refer to section Sample applications for examples of Auto-Configuration.

  • No labels