added comment
[ana-net.git] / opt / pflana2.c
blob775c0f2633da829299e914a4a8e24555a6611632
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("eth10");
55 if (idx < 0) {
56 ret = idx;
57 goto out;
60 memset(&sa, 0, sizeof(sa));
61 sa.sa_family = AF_LANA;
62 sa.sa_data[0] = (uint8_t) idx;
64 ret = bind(sock, &sa, sizeof(sa));
65 if (ret < 0) {
66 perror("bind");
67 goto out;
70 ret = sendto(sock, buff, sizeof(buff), 0, &sa, sizeof(sa));
71 if (ret < 0) {
72 perror("sendmsg");
73 goto out;
76 ret = 0;
77 out:
78 close(sock);
79 return ret;