This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
| Other format: | [Raw text] | |
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:
pgp00000.pgp
Description: PGP signature
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |