1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*************************************************
* rpld - an IBM style RIPL server *
*************************************************/
/* Copyright (c) 1999,2000,2001 James McKenzie.
* All rights reserved
* Copyright (c) 1998,2001 Christopher Lightfoot.
* All rights reserved
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENCE file which can be found at the top level of
* the rpld distribution.
*
* IBM is a trademark of IBM corp.
*
*/
static char rcsid[] =
"$Id: linux-old-nit.c,v 1.3 2001/11/01 15:24:26 root Exp $";
/*
* $Log: linux-old-nit.c,v $
* Revision 1.3 2001/11/01 15:24:26 root
* #
*
* Revision 1.2 2000/09/26 03:48:23 root
* #
*
* Revision 1.1 2000/09/26 03:45:34 root
* #
*
*
*/
/*
* CAUTION THIS CODE COMES WITH NO WARARNTY IT IS COPYRIGHT 1999 BY
* James McKenzie All rights reserved. GPL applies
*/
#include "project.h"
#include "nit.h"
#undef __GLIBC__
#include <linux/socket.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <linux/if_packet.h>
#include <linux/route.h>
#define __GLIBC__
#define NIT_MAX_NAME_LEN 1024
struct nit
{
int fd;
char *name;
unsigned char mac[6];
};
struct machdr
{
unsigned char h_dest[ETH_ALEN];
unsigned char h_source[ETH_ALEN];
short h_len;
};
static void
get_hwaddr (unsigned char *name, unsigned char *addr)
{
struct ifreq ifr;
int fd = socket (AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
{
syslog (LOG_ERR, "socket:%m");
return;
}
bcopy (name, &ifr.ifr_name, sizeof (ifr.ifr_name));
/* find my own hardware address */
if (ioctl (fd, SIOCGIFHWADDR, &ifr) < 0)
{
close (fd);
syslog (LOG_ERR, "ioctl(SIOCGIFHWADDR):%m");
exit (-1);
}
close (fd);
bcopy (&ifr.ifr_hwaddr.sa_data, addr, ETH_ALEN);
}
unsigned char *
nit_mac (struct nit *n)
{
return n->mac;
}
void
nit_close (struct nit *n)
{
if (!n)
return;
close (n->fd);
free (n->name);
/*FIXME: we should demulticast at this point but we need to have some
*mechanism for detecting if anyone else wants it
*/
free (n);
}
struct nit *
nit_open (char *name)
{
struct nit *n;
struct sockaddr sa;
int fd;
syslog (LOG_ERR, "RPLD OLD LINUX-NIT support %s", rcsid);
if (!name)
name = "eth0";
#ifdef PF_PACKET
fd = socket (PF_PACKET, SOCK_PACKET, htons (ETH_P_ALL));
if (fd < 0)
#endif
fd = socket (AF_INET, SOCK_PACKET, htons (ETH_P_ALL));
if (fd < 0)
{
syslog (LOG_ERR, "socket:%m");
return (NULL);
}
bzero (&sa, sizeof (sa));
sa.sa_family = AF_INET;
memcpy (&sa.sa_data, name, sizeof (sa.sa_data));
if (bind (fd, &sa, sizeof (sa)) < 0)
{
close (fd);
syslog (LOG_ERR, "bind:%m");
return (NULL);
}
n = (struct nit *) malloc (sizeof (struct nit));
n->fd = fd;
n->name = (char *) malloc (NIT_MAX_NAME_LEN);
bzero (n->name, NIT_MAX_NAME_LEN);
strcpy (n->name, name);
get_hwaddr (n->name, n->mac);
return (n);
}
int
nit_send (struct nit *n, unsigned char *frame, int len, unsigned char *to)
{
struct sockaddr sa;
int mylen;
unsigned char *myframe = malloc (len + sizeof (struct machdr));
struct machdr *mh = (struct machdr *) myframe;
bcopy (to, mh->h_dest, ETH_ALEN);
bcopy (n->mac, mh->h_source, ETH_ALEN);
mh->h_len = htons (len);
bcopy (frame, myframe + sizeof (struct machdr), len);
mylen = len + sizeof (struct machdr);
bzero (&sa, sizeof (sa));
sa.sa_family = AF_INET;
strncpy (sa.sa_data, n->name, sizeof (sa.sa_data));
#ifdef DEBUG
// hexdump("out",myframe,mylen);
#endif
if (sendto (n->fd, myframe, mylen, 0, &sa, sizeof (sa)) < 0)
syslog (LOG_ERR, "sendto: %m");
free (myframe);
return len;
}
int
nit_multicast (struct nit *n, unsigned char *mcaddr)
{
struct ifreq ifr;
int fd = socket (AF_INET, SOCK_DGRAM, 0);
strncpy (ifr.ifr_name, n->name, sizeof (ifr.ifr_name));
ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
bcopy (mcaddr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
if (ioctl (fd, SIOCADDMULTI, &ifr) < 0)
{
close (fd);
syslog (LOG_ERR, "ioctl(SIOCADDMULTI):%m");
return -1;
}
return 0;
}
int
nit_recv (struct nit *n, unsigned char *buf, int len, unsigned char *from,
struct timeval *tv)
{
fd_set rfds;
int ret;
struct machdr *mh;
buf -= sizeof (struct machdr);
mh = (struct machdr *) buf;
FD_ZERO (&rfds);
FD_SET (n->fd, &rfds);
ret = select ((n->fd) + 1, &rfds, NULL, NULL, tv);
if (ret < 0)
return (ret);
if (FD_ISSET ((n->fd), &rfds))
{
ret = read (n->fd, buf, len);
if (ret > sizeof (struct machdr))
{
if (from)
bcopy (mh->h_source, from, ETH_ALEN);
ret = htons (mh->h_len);
if (ret > len)
{
ret = len;
}
return (ret);
}
else
{
return 0;
}
}
else
{
return (0);
}
}
|