slinux.c revision 53024
1/*
2 * (C)opyright 1992-1998 Darren Reed. (from tcplog)
3 *
4 * Redistribution and use in source and binary forms are permitted
5 * provided that this notice is preserved and due credit is given
6 * to the original author and the contributors.
7 */
8
9#include <stdio.h>
10#include <string.h>
11#include <netdb.h>
12#include <ctype.h>
13#include <signal.h>
14#include <errno.h>
15#include <sys/types.h>
16#include <sys/time.h>
17#include <sys/timeb.h>
18#include <sys/socket.h>
19#include <sys/file.h>
20#include <sys/ioctl.h>
21#include <sys/dir.h>
22#include <linux/netdevice.h>
23#include <net/if.h>
24#include <netinet/in.h>
25#include <netinet/in_systm.h>
26#include <netinet/ip.h>
27#include <netinet/tcp.h>
28#include "ipsend.h"
29
30#if !defined(lint)
31static const char sccsid[] = "@(#)slinux.c	1.2 8/25/95";
32static const char rcsid[] = "@(#)$Id: slinux.c,v 2.1 1999/08/04 17:31:14 darrenr Exp $";
33#endif
34
35#define	CHUNKSIZE	8192
36#define BUFSPACE	(4*CHUNKSIZE)
37
38/*
39 * Be careful to only include those defined in the flags option for the
40 * interface are included in the header size.
41 */
42
43static	int	timeout;
44static	char	*eth_dev = NULL;
45
46
47int	initdevice(dev, sport, spare)
48char	*dev;
49int	sport, spare;
50{
51	int fd;
52
53	eth_dev = strdup(dev);
54	if ((fd = socket(AF_INET, SOCK_PACKET, htons(ETHERTYPE_IP))) == -1)
55	    {
56		perror("socket(SOCK_PACKET)");
57		exit(-1);
58	    }
59
60	return fd;
61}
62
63
64/*
65 * output an IP packet onto a fd opened for /dev/nit
66 */
67int	sendip(fd, pkt, len)
68int	fd, len;
69char	*pkt;
70{
71	struct	sockaddr	s;
72	struct	ifreq	ifr;
73
74	strncpy(ifr.ifr_name, eth_dev, sizeof(ifr.ifr_name));
75	if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1)
76	    {
77		perror("SIOCGIFHWADDR");
78		return -1;
79	    }
80	bcopy(ifr.ifr_hwaddr.sa_data, pkt + 6, 6);
81	s.sa_family = ETHERTYPE_IP;
82	strncpy(s.sa_data, eth_dev, sizeof(s.sa_data));
83
84	if (sendto(fd, pkt, len, 0, &s, sizeof(s)) == -1)
85	    {
86		perror("send");
87		return -1;
88	    }
89
90	return len;
91}
92