Two IP addresses on one IF

Wolfram Wadepohl Wolfram.Wadepohl at ek-automation.com
Mon Oct 5 07:48:32 UTC 2009


Leon Pollak schrieb:
> Hello, all.
> 
> How (if ever) can one assign two different IP addresses (from different 
> subnets) to one Ethernet interface?
> 
> Thanks.
> BR
Hi,

i use the following code snipset

struct {
    struct {
       in_addr_t  Addr;
       in_addr_t  Mask;
       } Local;
    struct {
       in_addr_t  Addr;
       in_addr_t  Mask;
       } Central;
    in_addr_t     Gateway;
    } CcsNetParameter;

AddIpAlias(RTEMS_BSP_NETWORK_DRIVER_NAME,	
CcsNetParameter.Local.Addr,						 CcsNetParameter.Local.Mask);


/* AddIpAlias
  *
  * SYNOPSIS
  *
  *    AddIpAlias(
  *       const char	*IfName,
  *			in_addr_t	IpAddr,
  *			in_addr_t	IpMask
  *       );
  *
  * PARAMETERS
  *		
  *		IfName			interface name
  *		IpAddr			ip address in network representation
  *		IpMask			network mask in network representation
  * FUNCTION
  *
  *    add ip alias
  *
  * RETURNS
  *
  *		0					success
  *		other				failed
  */

static int AddIpAlias(const char *ifname, in_addr_t ipaddr, in_addr_t
ipmask)
{
    struct sockaddr_in *sin;
    struct in_aliasreq ireq;

    /* name */
    strcpy(ireq.ifra_name, ifname);

    /* ip address */
    sin = (struct sockaddr_in *) &(ireq.ifra_addr);
    bzero((caddr_t) sin, sizeof(*sin));
    sin->sin_len = sizeof(*sin);
    sin->sin_family = AF_INET;
    sin->sin_addr.s_addr = ipaddr;

    /* netmask */
    sin = (struct sockaddr_in *) &(ireq.ifra_mask);
    bzero((caddr_t) sin, sizeof(*sin));
    sin->sin_len = sizeof(*sin);
    sin->sin_family = AF_INET;
    sin->sin_addr.s_addr = ipmask;

    /* call socket ioctl function */
    return rtems_bsdnet_ifconfig(ifname, SIOCAIFADDR, &ireq);
}

Hope this helps.

-- 
Schöne Grüße aus Reutlingen

Wolfram Wadepohl
Forschung & Entwicklung

E&K AUTOMATION GmbH
Werk Reutlingen
Siemensstraße 3
72766 Reutlingen
Deutschland

Tel.  +49 7121 514-289
Fax   +49 7121 514-299
eMail Wolfram.Wadepohl at ek-automation.com
       W.Wadepohl at ieee.org
WWW   http://www.ek-automation.com
       http://www.indumat.de

Diese Nachricht ist keine geschäftliche Mitteilung i. S. des EHUG.

Bitte senden Sie mir keine Word- oder PowerPoint- (tm Microsoft) oder
OOXML Anhänge.
Senden Sie mir einfachen Text, HTML oder PDF(ISO/IEC 32000).
Siehe http://www.gnu.org/philosophy/no-word-attachments.de.html




More information about the users mailing list