"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "trafshow-3.1/getarptab.c" of archive trafshow-3.1.tgz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 /*
    2  *	Copyright (c) 1998 JSC Rinet, Novosibirsk, Russia
    3  *
    4  * Redistribution and use in source forms, with and without modification,
    5  * are permitted provided that this entire comment appears intact.
    6  * Redistribution in binary form may occur without any restrictions.
    7  *
    8  * THIS SOFTWARE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND.
    9  */
   10 
   11 /* getarptab.c -- translate MAC addresses to IP addresses using ARP table */
   12 
   13 #ifdef	HAVE_CONFIG_H
   14 #include <config.h>
   15 #endif
   16 
   17 #ifdef	HAVE_RTF_LLINFO		/* BSD systems */
   18 
   19 #include <sys/param.h>
   20 #include <sys/types.h>
   21 #include <sys/socket.h>
   22 #include <sys/sysctl.h>
   23 
   24 #include <net/if.h>
   25 #include <net/if_dl.h>
   26 #include <net/route.h>
   27 #include <netinet/in.h>
   28 #include <netinet/if_ether.h>
   29 
   30 #ifdef	ETHER_ADDR_LEN
   31 #define	MAC_ADDR_LEN	ETHER_ADDR_LEN
   32 #else
   33 #define	MAC_ADDR_LEN	6
   34 #endif
   35 
   36 u_char *
   37 getarptab(ep)
   38 	u_char *ep;
   39 {
   40 	int mib[6];
   41 	size_t needed;
   42 	char *lim, *buf, *next;
   43 	struct rt_msghdr *rtm;
   44 	static struct sockaddr_inarp *sin;
   45 	struct sockaddr_dl *sdl;
   46 
   47 	mib[0] = CTL_NET;
   48 	mib[1] = PF_ROUTE;
   49 	mib[2] = 0;
   50 	mib[3] = AF_INET;
   51 	mib[4] = NET_RT_FLAGS;
   52 	mib[5] = RTF_LLINFO;
   53 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
   54 		error(1, "getarptab: sysctl");
   55 	if ((buf = (char *)malloc(needed)) == NULL)
   56 		error(1, "getarptab: malloc");
   57 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
   58 		error(1, "getarptab: sysctl");
   59 	lim = buf + needed;
   60 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
   61 		rtm = (struct rt_msghdr *)next;
   62 		sin = (struct sockaddr_inarp *)(rtm + 1);
   63 		sdl = (struct sockaddr_dl *)(sin + 1);
   64 		if (sdl->sdl_alen &&
   65 		    !memcmp(ep, (u_char *)LLADDR(sdl), MAC_ADDR_LEN)) {
   66 			free(buf);
   67 			return ((u_char *)&sin->sin_addr);
   68 		}
   69 	}
   70 	free(buf);
   71 	return (u_char *)0;
   72 }
   73 
   74 #elif	HAVE_PROC_NET_ARP	/* Linux systems */
   75 
   76 #include <sys/types.h>
   77 #include <netinet/in.h>
   78 #include <arpa/inet.h>
   79 #include <stdio.h>
   80 #include <string.h>
   81 
   82 u_char *
   83 getarptab(ep)
   84 	u_char *ep;
   85 {
   86 	FILE *fp;
   87 	char *req, *ip, *mac, buf[100];
   88 	static struct in_addr addr;
   89 
   90 	if ((fp = fopen("/proc/net/arp", "r")) != NULL) {
   91 		req = (char *)entoa(ep);
   92 		while (fgets(buf, sizeof(buf), fp) != NULL) {
   93 			buf[sizeof(buf)-1] = '\0';
   94 			if ((ip = strtok(buf, " \t")) == NULL)
   95 				continue;
   96 			while ((mac = strtok(NULL, " \t")) != NULL) {
   97 				if (!strcasecmp(req, mac)) {
   98 					if (inet_aton(ip, &addr)) {
   99 						(void) fclose(fp);
  100 						return ((u_char *)&addr);
  101 					}
  102 					break;
  103 				}
  104 			}
  105 		}
  106 		(void) fclose(fp);
  107 	}
  108 	return (u_char *)0;
  109 }
  110 
  111 #else	/* XXX. Get ARP table not implemented yet for SYSV systems */
  112 
  113 u_char *
  114 getarptab(ep)
  115 	u_char *ep;
  116 {
  117 	return (u_char *)0;
  118 }
  119 
  120 #endif