annotate src/lwwire-packet.c @ 16:13be9d3a6927 default tip

update doc to include transactional delay requirement
author Brett Gordon
date Tue, 13 Dec 2016 10:42:01 -0500
parents e4d98cbf95eb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
1 /*
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
2 Packet extension for lwwire
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
3
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
4 Talks Layer 2, Ethernet Frames, sends them to a Linux TAP/TUN device.
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
5
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
6 TODO:
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
7 * better (any) RXMTU and TXMTU, Queue Length handling
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
8
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
9 */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
10
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
11 #include <stdio.h>
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
12 #include "lwwire.h"
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
13
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
14 #include <unistd.h>
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
15 #include <string.h>
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
16 #include <ctype.h>
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
17 #include <sys/ioctl.h>
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
18 #include <sys/types.h>
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
19 #include <sys/stat.h>
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
20 #include <fcntl.h>
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
21 #include <sys/socket.h>
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
22 #include <linux/if.h>
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
23 #include <linux/if_tun.h>
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
24
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
25
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
26 #define MAX_FRAME 1526
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
27 static int tunfd = -1;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
28 static char tundev[10];
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
29 static unsigned char opacket[MAX_FRAME];
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
30 static unsigned char packet[MAX_FRAME];
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
31 static int tunz = -1;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
32 static unsigned char *packet_ptr;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
33 static int csize = 511;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
34 static int rxmtu = 1500;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
35 static int txmtu = 1500;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
36 static int qlen = 10;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
37
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
38 /*
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
39
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
40 NOTE: the __attribute__((used)) tags are there to prevent the functions
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
41 from being optimized out as would often be the case with functions declared
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
42 static. This could be avoided by removing the static declarations. However,
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
43 by declaring everything static except for the lwwire_register() function,
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
44 we avoid polluting the global namespace and possibly having conflicts with
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
45 other extensions.
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
46 */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
47
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
48
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
49 /* Allocate (open) a new tap device
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
50 Take symbolic name of device and tap/tun flags. Returns fd, or < 0 on
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
51 error.
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
52 */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
53 static int tun_alloc(char *dev, int flags)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
54 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
55 struct ifreq ifr;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
56 int fd, err;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
57 char *clonedev = "/dev/net/tun";
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
58
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
59 /* open the clone device */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
60 if ((fd = open(clonedev, O_RDWR)) < 0)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
61 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
62 perror("tun_alloc");
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
63 return fd;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
64 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
65 /* preparation of the struct ifr, of type "struct ifreq" */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
66 memset(&ifr, 0, sizeof(ifr));
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
67 ifr.ifr_flags = flags;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
68 if (*dev)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
69 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
70 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
71 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
72 /* try to create the device */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
73 if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
74 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
75 perror("tun_alloc");
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
76 close(fd);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
77 return err;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
78 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
79 /* write acquired tap device name back to struct */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
80 strcpy(dev, ifr.ifr_name);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
81
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
82 nonblock(fd);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
83 return fd;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
84 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
85
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
86
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
87 /* Tell client how many bytes to expect for the next read. */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
88 static int tun_proto_poll()
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
89 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
90 unsigned char buf[2];
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
91 while (1)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
92 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
93 if (tunz < 1)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
94 tunz = read(tunfd, packet, MAX_FRAME);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
95 if (tunz < rxmtu)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
96 break;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
97 else
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
98 tunz = 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
99 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
100 buf[0] = (unsigned char)(tunz >> 8);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
101 buf[1] = (unsigned char)(tunz & 0xff);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
102 lwwire_write(buf,2);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
103 packet_ptr = packet;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
104 return 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
105 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
106
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
107 /* Drop the current packet */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
108 static int tun_proto_drop()
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
109 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
110 tunz = 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
111 return 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
112 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
113
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
114
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
115 /* receive *entire* frame */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
116 static int tun_proto_recv()
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
117 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
118 if (tunz < 1)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
119 return -1;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
120 lwwire_write(packet, tunz);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
121 tunz = 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
122 return 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
123 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
124
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
125 /* receive next chunk (upto 511 bytes) of frame */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
126 static int tun_proto_recv_chunk()
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
127 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
128 int s;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
129 if (csize < tunz)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
130 s = csize;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
131 else
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
132 s = tunz;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
133 lwwire_write(packet_ptr, s);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
134 packet_ptr += s;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
135 tunz -= s;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
136 return 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
137 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
138
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
139 /* send a frame */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
140 static int tun_proto_send()
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
141 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
142 int len;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
143 unsigned char buf[2];
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
144 if (lwwire_read(buf, 2) < 0)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
145 return -1;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
146 len = (buf[0]<<8) + buf[1];
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
147 if (lwwire_read(opacket, len) < 0)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
148 return -1;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
149 if (write(tunfd, opacket, len) < 0)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
150 return -1;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
151 return 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
152 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
153
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
154
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
155 /* set/return receive mtu */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
156 static int tun_proto_setrxmtu()
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
157 {
14
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
158 unsigned int val;
12
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
159 unsigned char buf[2];
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
160 if (lwwire_read(buf, 2) < 0)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
161 return -1;
14
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
162 val = (buf[0] << 8) + buf[1];
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
163 if (val)
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
164 {
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
165 rxmtu = val;
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
166 }
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
167 else
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
168 {
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
169 buf[0] = rxmtu >> 8;
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
170 buf[1] = rxmtu & 255;
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
171 }
12
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
172 lwwire_write(buf, 2);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
173 return 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
174 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
175
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
176 /* set/return transmit mtu */
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
177 static int tun_proto_settxmtu()
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
178 {
14
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
179 unsigned int val;
12
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
180 unsigned char buf[2];
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
181 if (lwwire_read(buf, 2) < 0)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
182 return -1;
14
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
183 val = (buf[0] << 8) + buf[1];
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
184 if (val)
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
185 {
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
186 txmtu = val;
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
187 }
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
188 else
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
189 {
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
190 buf[0] = txmtu >> 8;
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
191 buf[1] = txmtu & 255;
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
192 }
12
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
193 lwwire_write(buf, 2);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
194 return 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
195 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
196
14
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
197 /* set/return queue length */
12
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
198 static int tun_proto_setqlen()
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
199 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
200 unsigned char buf[1];
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
201 if (lwwire_read(buf, 1) < 0)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
202 return -1;
14
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
203 if (buf[0])
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
204 {
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
205 qlen = buf[0];
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
206 }
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
207 else
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
208 {
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
209 buf[0] = qlen;
e4d98cbf95eb packet: add get value semantics
Brett Gordon
parents: 12
diff changeset
210 }
12
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
211 lwwire_write(buf, 1);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
212 return 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
213 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
214
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
215
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
216
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
217 // this is called to handle an operation for this extension
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
218 // it should return nonzero if the operation code (op) is not
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
219 // supported. Otherwise, it should implement the operation codes.
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
220 __attribute__((used)) static int packet_handler(int op)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
221 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
222 switch (op)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
223 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
224 case 0:
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
225 return tun_proto_poll();
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
226 case 1:
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
227 return tun_proto_recv();
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
228 case 2:
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
229 return tun_proto_send();
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
230 case 3:
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
231 return tun_proto_drop();
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
232 case 4:
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
233 return tun_proto_setrxmtu();
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
234 case 5:
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
235 return tun_proto_setqlen();
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
236 case 6:
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
237 return tun_proto_settxmtu();
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
238 case 7:
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
239 return tun_proto_recv_chunk();
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
240 default:
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
241 return -1;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
242 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
243 return 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
244 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
245
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
246 // this will be called when the extension is enabled
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
247 // return nonzero if enabling the extension cannot happen for some reason
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
248 __attribute__((used)) static int packet_enable(void)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
249 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
250 if (tunfd < 0)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
251 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
252 strcpy(tundev, "tap0");
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
253 tunfd = tun_alloc(tundev, IFF_TAP | IFF_NO_PI);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
254 if (tunfd < 0) return -1;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
255 tunz = -1;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
256 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
257 return 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
258 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
259
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
260 // this will be called when an extension is disabled
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
261 // if this extension cannot be disabled after it is enabled, it should return
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
262 // nonzero; otherwise, this MUST succeed
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
263 __attribute__((used)) static int packet_disable(void)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
264 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
265 close(tunfd);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
266 tunfd = -1;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
267 return 0;
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
268 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
269
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
270 // this call should do anything needed to disable the extension
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
271 // and anything special that might be needed on a protocol reset
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
272 // it will only be called if the extension is enabled when the
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
273 // reset occurs. Even if the extension isn't disablable, this call
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
274 // MUST disable it and reset things as if the extension was never
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
275 // enabled.
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
276 __attribute__((used)) static void packet_reset(void)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
277 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
278 packet_disable();
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
279 }
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
280
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
281 int lwwire_register(void)
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
282 {
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
283 fprintf(stderr, "Registering PACKET extension\n");
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
284 return lwwire_register_extension(0x01, packet_handler, packet_enable, packet_disable, packet_reset);
f8226a33698d Add PACKET extension
Brett Gordon <beretta42@gmail.com>
parents:
diff changeset
285 }