Added documentation
[ana-net.git] / opt / pflana2.c
blob88de45248ea516a08b56db26223775a6dfa6079d
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <unistd.h>
4 #include <signal.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <ctype.h>
8 #include <assert.h>
9 #include <sys/socket.h>
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
12 #include <net/if.h>
14 #define AF_LANA 27
16 /* TODO: add support for SIOCGIFINDEX into AF_LANA */
17 static int device_ifindex(const char *ifname)
19 int ret, sock, index;
20 struct ifreq ifr;
22 sock = socket(AF_INET, SOCK_DGRAM, 0);
23 if (sock < 0)
24 return sock;
26 memset(&ifr, 0, sizeof(ifr));
27 strncpy(ifr.ifr_name, ifname, strlen(ifname));
29 ret = ioctl(sock, SIOCGIFINDEX, &ifr);
30 if (!ret)
31 index = ifr.ifr_ifindex;
32 else
33 index = -1;
35 close(sock);
36 return index;
39 int main(void)
41 int sock, ret, idx;
42 struct sockaddr sa;
43 char buff[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
45 0xac, 0xdc, 0xee, 0xee, 0xee, 0xee };
47 sock = socket(AF_LANA, SOCK_RAW, 0);
48 if (sock < 0) {
49 perror("socket");
50 return 0;
53 /* XXX: #include <net/if.h> -> if_nametoindex(3) */
54 idx = device_ifindex("eth0");
55 if (idx < 0) {
56 ret = idx;
57 goto out;
60 printf("Hit key1!\n");
61 getchar();
63 memset(&sa, 0, sizeof(sa));
64 sa.sa_family = AF_LANA;
65 sa.sa_data[0] = (uint8_t) idx;
67 ret = bind(sock, &sa, sizeof(sa));
68 if (ret < 0) {
69 perror("bind");
70 goto out;
73 while (1) {
74 ret = sendto(sock, buff, sizeof(buff), 0, &sa, sizeof(sa));
75 if (ret < 0) {
76 perror("sendmsg");
77 goto out;
79 printf("sent packet!\n");
82 ret = 0;
83 out:
84 close(sock);
85 return ret;