public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
From: "Sébastien Couret" <sebastien.couret@elios-informatique.fr>
To: mkhoyila@uci.edu
Cc: ecos-discuss@sources.redhat.com
Subject: Re: [ECOS] assign IP address to a specifig mac
Date: Tue, 02 Aug 2005 07:27:00 -0000	[thread overview]
Message-ID: <20050802092841.6839ca12.sebastien.couret@elios-informatique.fr> (raw)
In-Reply-To: <53917.63.87.1.243.1122930471.squirrel@webmail.uci.edu>

[-- Attachment #1: Type: text/plain, Size: 4424 bytes --]

Hi,

May be it's not solving your issue but I can propose you a sample code that can change the MAC address annd IPv4 address for a specific network interface . This code is working well with the FreeBSP TCP/IP stack ported on eCOS and with ethernet devices/drivers that accept changing their MAC adress trough iotcl calls.
The code was originaly written in french, I have translate it quickly without compiling so please forgive my mistakes if any. 

You could use the API  like this :
char mac[ETHER_ADDR_LEN]={0xDE,0xAD,0x00,0xBE,0xEF,0x00};
IP_SetMAC("eth0",mac);           // Will set MAC adress of eth0 interface to DE:AD:00:BE:EF:00 
IP_SetIP("eth0","192.168.0.1");  // Will set the IPv4 adress of eth0 interface to 192.168.0.1

 -----------------------------------------------------
Here comes the code :
 
Note: My own defines :

 #define MACSTRING 18
 

char* ether_print(const u_char cp[ETHER_ADDR_LEN], char *etheraddr,const unsigned int len)
{
 snprintf(etheraddr,len,"%02x:%02x:%02x:%02x:%02x:%02x",cp[0],cp[1],cp[2],cp[3],cp[4],cp[5]);
 return(etheraddr);
}

int IP_SetMAC(const char* interface,unsigned char mac[ETHER_ADDR_LEN])
{
 int test_sock=0;			// Socket PF_INET/SOCK_DGRAM
 unsigned char i=0;
 struct ifreq ifr;
 unsigned char display[MACSTRING];

 test_sock = socket( PF_INET, SOCK_DGRAM, 0 );
 if( test_sock == -1 )
 {
  diag_printf("Cannot obtain socket");
  return (-1);
 }

 memset(&ifr,0,sizeof( struct ifreq ) );
 strncpy(ifr.ifr_name,interface,IFNAMSIZ);
 for (i=0;i<ETHER_ADDR_LEN;i++)
  ifr.ifr_hwaddr.sa_data[i]=mac[i];
 ether_print(mac,display,MACSTRING);
 if( ioctl( test_sock, SIOCSIFHWADDR, &ifr ) == -1 )
 {
  diag_printf("Cannot set MAC adress for '%s' to '%s' because '%s'",interface,display,strerror(errno));
  close(test_sock);
  return (-1);
 }
 else diag_printf("MAC Adress for '%s' set to %s'",interface,display);
 close(test_sock);
 return(0);
}

int IP_SetIP(const char* interface,const char * adresse)
{
 int test_sock=0;
 struct sockaddr_in* addr=NULL;
 struct ifreq ifr;


 test_sock = socket( PF_INET, SOCK_DGRAM, 0 );
 if( test_sock == -1 )
 {
  fprintf(stderr,"Cannot obtain socket");
  return (-1);
 }

 memset(&ifr,0,sizeof( struct ifreq ) );
 strncpy(ifr.ifr_name,interface,IFNAMSIZ);

 if( ioctl( test_sock, SIOCGIFADDR, &ifr ) == -1 )
 {
  diag_printf("Cannot obtain IP address of '%s' because '%s'",interface,strerror(errno));
 }
 else
 {
  if( ioctl( test_sock, SIOCDIFADDR, &ifr ) != 0 )
  {
   diag_printf("Cannot suppress old IP for '%s' because '%s'",interface,strerror(errno));
  }
 }

 memset( &ifr, 0, sizeof( struct ifreq ) );
 addr= (struct sockaddr_in *)&(ifr.ifr_addr);
 memset(addr, 0, sizeof( struct sockaddr_in) );
 addr->sin_len=sizeof(struct sockaddr_in);
 addr->sin_family=AF_INET;
 addr->sin_addr.s_addr=inet_addr(adresse);
 strncpy(ifr.ifr_name,interface,IFNAMSIZ);

 if( ioctl( test_sock, SIOCSIFADDR, &ifr ) != 0 )
 {
  diag_printf("Cannot set IP address of '%s' to '%s' because '%s'",interface,adresse,strerror(errno));
  close(test_sock);
  return (-1);
 }
 else diag_printf("IP address for '%s' set '%s' ",interface,inet_ntoa(addr->sin_addr));
 close(test_sock);
 return(0);
}

Note : I have used the following headers files , they are not all necessary as my original file was doing more work like changing routes and ARP entries.Anyway, I hope this helps.

#include <cyg/kernel/kapi.h>
#include <pkgconf/system.h>
#include <network.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#ifdef CYGPKG_NET_FREEBSD_SYSCTL
#include <sys/sysctl.h>
#endif
#include <sys/param.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>
#include <net/ethernet.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>


On Mon, 1 Aug 2005 14:07:51 -0700 (PDT)
mkhoyila@uci.edu wrote:

> can someone guide me on how to assign an IP address to a mac address from
> eCos ethernet driver? If so, is there a sample code I can look at. thanks.
> 
> Michael
> 
> 
> -- 
> Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
> and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss
> 
> 


-- 

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

      parent reply	other threads:[~2005-08-02  7:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-01 21:05 mkhoyila
2005-08-01 21:58 ` Gary Thomas
     [not found]   ` <21913.63.87.1.243.1122939985.squirrel@webmail.uci.edu>
2005-08-02  0:28     ` Gary Thomas
2005-08-02  7:27 ` Sébastien Couret [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050802092841.6839ca12.sebastien.couret@elios-informatique.fr \
    --to=sebastien.couret@elios-informatique.fr \
    --cc=ecos-discuss@sources.redhat.com \
    --cc=mkhoyila@uci.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).