Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / ipfilter / ipsend / ultrix.c
1 /*
2  * (C)opyright 1998 Darren Reed. (from tcplog)
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  */
6 #include <stdio.h>
7 #include <strings.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include <sys/types.h>
12 #include <sys/param.h>
13 #include <sys/socket.h>
14 #include <sys/file.h>
15 #include <sys/ioctl.h>
16 #include <net/if.h>
17 #include <netinet/in.h>
18 #include <netinet/if_ether.h>
19 #include <netdnet/dli_var.h>
20
21
22 static  struct  dli_devid       dli_devid;
23
24
25 int     initdevice(device, sport, tout)
26 char    *device;
27 int     sport, tout;
28 {
29         u_char  *s;
30         int     fd;
31
32         fd = socket(AF_DLI, SOCK_DGRAM, 0);
33         if (fd == -1)
34                 perror("socket(AF_DLI,SOCK_DGRAM)");
35         else {
36                 strncpy(dli_devid.dli_devname, device, DLI_DEVSIZE);
37                 dli_devid.dli_devname[DLI_DEVSIZE] ='\0';
38                 for (s = dli_devid.dli_devname; *s && isalpha((char)*s); s++)
39                         ;
40                 if (*s && isdigit((char)*s)) {
41                         dli_devid.dli_devnumber = atoi(s);
42                 }
43         }
44         return fd;
45 }
46
47
48 /*
49  * output an IP packet onto a fd opened for /dev/bpf
50  */
51 int     sendip(fd, pkt, len)
52 int     fd, len;
53 char    *pkt;
54 {
55         struct sockaddr_dl dl;
56         struct sockaddr_edl *edl = &dl.choose_addr.dli_eaddr;
57
58         dl.dli_family = AF_DLI;
59         dl.dli_substructype = DLI_ETHERNET;
60         bcopy((char *)&dli_devid, (char *)&dl.dli_device, sizeof(dli_devid));
61         bcopy(pkt, edl->dli_target, DLI_EADDRSIZE);
62         bcopy(pkt, edl->dli_dest, DLI_EADDRSIZE);
63         bcopy(pkt + DLI_EADDRSIZE * 2, (char *)&edl->dli_protype, 2);
64         edl->dli_ioctlflg = 0;
65
66         if (sendto(fd, pkt, len, 0, (struct sockaddr *)&dl, sizeof(dl)) == -1)
67             {
68                 perror("send");
69                 return -1;
70             }
71
72         return len;
73 }
74
75
76 char *strdup(str)
77 char *str;
78 {
79         char    *s;
80
81         if ((s = (char *)malloc(strlen(str) + 1)))
82                 return strcpy(s, str);
83         return NULL;
84 }