dsniff  2.4b2
About: A collection of tools for network auditing
  Fossies Dox: dsniff-2.4b2.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

arp.c
Go to the documentation of this file.
1 /*
2  * arp.c
3  *
4  * ARP cache routines.
5  *
6  * Copyright (c) 1999 Dug Song <dugsong@monkey.org>
7  *
8  * $Id: arp.c,v 1.8 2001/03/15 08:32:58 dugsong Exp $
9  */
10 
11 #include "config.h"
12 
13 #include <sys/types.h>
14 #include <sys/param.h>
15 #include <sys/socket.h>
16 #ifdef BSD
17 #include <sys/sysctl.h>
18 #include <net/if_dl.h>
19 #include <net/route.h>
20 #ifdef __FreeBSD__ /* XXX */
21 #define ether_addr_octet octet
22 #endif
23 #else /* !BSD */
24 #include <sys/ioctl.h>
25 #ifndef __linux__
26 #include <sys/sockio.h>
27 #endif
28 #endif /* !BSD */
29 #include <net/if.h>
30 #include <netinet/in_systm.h>
31 #include <netinet/in.h>
32 #include <netinet/if_ether.h>
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 #include "arp.h"
39 
40 #ifdef BSD
41 int
42 arp_cache_lookup(in_addr_t ip, struct ether_addr *ether)
43 {
44  int mib[6];
45  size_t len;
46  char *buf, *next, *end;
47  struct rt_msghdr *rtm;
48  struct sockaddr_inarp *sin;
49  struct sockaddr_dl *sdl;
50 
51  mib[0] = CTL_NET;
52  mib[1] = AF_ROUTE;
53  mib[2] = 0;
54  mib[3] = AF_INET;
55  mib[4] = NET_RT_FLAGS;
56  mib[5] = RTF_LLINFO;
57 
58  if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
59  return (-1);
60 
61  if ((buf = (char *)malloc(len)) == NULL)
62  return (-1);
63 
64  if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
65  free(buf);
66  return (-1);
67  }
68  end = buf + len;
69 
70  for (next = buf ; next < end ; next += rtm->rtm_msglen) {
71  rtm = (struct rt_msghdr *)next;
72  sin = (struct sockaddr_inarp *)(rtm + 1);
73  sdl = (struct sockaddr_dl *)(sin + 1);
74 
75  if (sin->sin_addr.s_addr == ip && sdl->sdl_alen) {
76  memcpy(ether->ether_addr_octet, LLADDR(sdl),
78  free(buf);
79  return (0);
80  }
81  }
82  free(buf);
83 
84  return (-1);
85 }
86 
87 #else /* !BSD */
88 
89 #ifndef ETHER_ADDR_LEN /* XXX - Solaris */
90 #define ETHER_ADDR_LEN 6
91 #endif
92 
93 int
94 arp_cache_lookup(in_addr_t ip, struct ether_addr *ether)
95 {
96  int sock;
97  struct arpreq ar;
98  struct sockaddr_in *sin;
99 
100  memset((char *)&ar, 0, sizeof(ar));
101 #ifdef __linux__
102  strncpy(ar.arp_dev, "eth0", sizeof(ar.arp_dev)); /* XXX - *sigh* */
103 #endif
104  sin = (struct sockaddr_in *)&ar.arp_pa;
105  sin->sin_family = AF_INET;
106  sin->sin_addr.s_addr = ip;
107 
108  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
109  return (-1);
110  }
111  if (ioctl(sock, SIOCGARP, (caddr_t)&ar) == -1) {
112  close(sock);
113  return (-1);
114  }
115  close(sock);
116  memcpy(ether->ether_addr_octet, ar.arp_ha.sa_data, ETHER_ADDR_LEN);
117 
118  return (0);
119 }
120 
121 #endif /* !BSD */
buf
static u_char buf[BUFSIZ]
Definition: filenamesnarf.c:29
ETHER_ADDR_LEN
#define ETHER_ADDR_LEN
Definition: arp.c:90
arp_cache_lookup
int arp_cache_lookup(u_int32_t ip, struct ether_addr *ether)
Definition: arp.c:94
buf
Definition: buf.h:14
in_addr_t
#define in_addr_t
Definition: config.h:32
config.h
if
if(screen !=save_screen)
Definition: vroot.h:71
arp.h