Benzy Gabay <bgabay@everbee.com> wrote:
: Hi,
: I’m writing a program that has to:
: 1) retrieve routing lines from the routing table
I don’t have an example off hand for this one.
A source for examples could be:
“UNIX Network Programming. Networking APIs: Sockets and XTI”
by W. Richard Stevens (ISBN 0-13-490012-X) or any BSD based
code that uses routing sockets (socket(AF_ROUTE, …)).
: 2) retrieve the nic info, meaning mac,ip address etc’
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ioctl.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <netinet/in.h>
int main (int argc, char **argv)
{
int s, i;
char buf[4096], *cplim;
struct ifconf ifc;
struct ifreq *ifr;
struct sockaddr_dl *sdl;
if(argc < 2)
{
fprintf (stderr, “Must specify interface.\n”);
exit (1);
}
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror (“socket”);
exit(1);
}
ifc.ifc_len = sizeof (buf);
ifc.ifc_buf = buf;
if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)
{
perror (“SIOCGIFCONF”);
exit(1);
}
ifr = ifc.ifc_req;
cplim = (char *)ifr + ifc.ifc_len;
for(; (char )ifr <cplim; ifr= (struct ifreq)((char )ifr + ifr->ifr_addr.sa_len + IFNAMSIZ))
{
/ If sa_family below is AF_INET, you have a sockaddr_in with an ip addr */
if(strcmp(ifr->ifr_name, argv[1]) || ifr->ifr_addr.sa_family != AF_LINK)
continue;
sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
if(sdl->sdl_type != IFT_ETHER)
continue;
printf("%s: ", argv[1]);
for(i = 0; i < sdl->sdl_alen; i++)
printf("%#x ", (unsigned char)sdl->sdl_data[sdl->sdl_nlen + i]);
printf("\n");
exit(EXIT_SUCCESS);
}
fprintf (stderr, “not found\n”);
exit(1);
}
: can anyone please send an example or guide me how to do it?
: –
: ----------------------------------------------
: Benzy Gabay
: R&D
: Everbee Wireless
: mailto:bgabay@everbeewireless.com
: tel. +972-9-956-0135
: fax. +972-9-955-9654