Re(2): Ethernet address format question
Re(2): Ethernet address format question
- Subject: Re(2): Ethernet address format question
- From: "Peter Lovell" <email@hidden>
- Date: Thu, 2 Jun 2005 06:49:57 -0400
The original was a bit big and is wedged in the size-filter. So here's
part one ...
==============================================================
On Wed, Jun 1, 2005, Alex Kettner <email@hidden> wrote:
>Peter,
>
>Thanks for the reply. I am trying to keep my code as cross platform as
>possible, thus the UNIX calls. I looked at the linked list of ifaddrs
>and there are three "en0" structs. It looks like the first one is the
>one I would need to use but how would I know this programatically? I
>also looked at the ethernet address, ifa_addr.sa_data and it looks like
>the actual ethernet address (the one I got from the System Profiler)
>starts at the 10th byte (ifa_addr.sa_data[9]). Is this standard? What
>do the first nine bytes represent?
Hi Alex,
having just written something like this, I know it can be a real pain. A
REAL pain.
So here's a routine which will do what you need, or close to it. You can
also see the stuff needed to get the various pieces.
WHat it does is build a linked list of structs, one per interface. It
gets the name, ethernet address and IPv4 address into each entry, as far
as possible. It ignores alias IP address, and also doesn't deal with link
addresses longer than 6 bytes (such as firewire interface "fw0" for
example). In my case I have no need of it.
I'm not sure if the mailing-list software allows attachments so I've
included the .h and .c files here directly. You'll also need a config.h
which you can probably get locally. If not I can send you a reasonably
close sample (bigger than would be polite for the list).
This basic code came from Stevens Unix Network Programming, Vol 1, 3 ed.
I've added some other things, and adapted for various systems.
It runs (or seems to :) on Mac OS X, FreeBSD, Linux, AIX and Solaris.
Cheers.....Peter
/* header for interface list routines */
#ifndef SP_GET_IFLIST_H
#define SP_GET_IFLIST_H
//what follows is for *nix only, not win32
#ifndef WIN32
#include "config.h" /* the configure script results */
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
/*
* The number of bytes in an ethernet (MAC) address.
* (cribbed from <net/ethernet.h> )
*/
#define ETHER_ADDR_LEN 6
struct ifi_list {
struct ifi_list* ifi_next; //pointer to next
char ifi_name[IFNAMSIZ]; //copy of kernel's name
unsigned char ifi_haddr[ETHER_ADDR_LEN]; //support only
ethernet, for now
in_addr_t ifi_v4addr; //IPv4 primary address
};
struct ifi_list* get_ifi_list(void);
void free_ifi_list(struct ifi_list* );
#else
#error include file <net/if.h> is required
#endif // HAVE_NET_IF_H
#endif // HAVE_CONFIG_H
#endif // SP_GET_IFLIST_H
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden