Re: Fwd: Mac Address in C

Stefano Canepa <[email protected]>
Newsgroups gmane.linux.debian.devel.italian
Message-ID <1181077726.17540.176.camel@localhost>
Il giorno lun, 04/06/2007 alle 13.32 -0700, Fabrizio Gattuso ha scritto:
> On 4 Giu, 22:10, "Andrea Ferraresi" <[email protected]>
> wrote:
> > scusate il repost ma ho mandato la mail a fabrizio in privato
> >
> > ---------- Forwarded message ----------
> > From: Andrea Ferraresi <[email protected]>
> > Date: 4-giu-2007 22.06
> > Subject: Re: Mac Address in C
> > To: Fabrizio Gattuso <[email protected]>
> >
> > tutte le informazioni della scheda vengono date dal comando ifconfig
> > quindi basta dare il seguente comando
> >
> > ifconfig | grep HW
> >
> > ciao.
> 
> intendevo in C  :)  non da shell!

struct  ifreq ifr;
char mac[20];     

int s = socket (AF_INET,SOCK_DGRAM,0);
strcpy(ifr.ifr_name, "eth1");
/* get mac address of the interface */
if (ioctl(s, SIOCGIFHWADDR, &ifr) < 0) {
            cerr << "ioctl SIOCGIFHWADDR non riuscita" << endl;
}
else
{
            unsigned char *pmac = (u_char
*)&ifr.ifr_ifru.ifru_hwaddr.sa_data[0];
            sprintf(mac, "%02x:%02x:%02x:%02x:%02x:%02x",
                    *pmac, *(pmac + 1), *(pmac + 2), *(pmac + 3),
                    *(pmac + 4), *(pmac + 5) );
}

Ciao
sc

-- 
Stefano Canepa aka sc: [email protected]  http://www.stefanocanepa.it
Three great virtues of a programmer: laziness, impatience and hubris.
Le tre grandi virtù di un programmatore: pigrizia, impazienza e
arroganza. (Larry Wall)
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQBGZdDel8XQY46MbvERAiD0AJ9GrGS/agaeMmwftcAPnGzEMrEQYwCcDPBl
eo63QuKVD25vDYNNjF58bXs=
=lLAn
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.