chiark / gitweb /
tuntap seems to sort of work
[chiark-tcl.git] / tuntap / tuntap.c
1 /*
2  */
3 /*
4  * tuntap-socket-raw create [<ifname>] => <sockid>
5  * tuntap-socket-raw ifname <sockid> => <ifname>
6  * tuntap-socket-raw close <sockid>
7  * tuntap-socket-raw receive <sockid> <data>
8  * tuntap-socket-raw on-transmit <sockid> <mtu> [<script>]
9  *    calls, effectively,  eval <script> [list <data> <socket>]
10  *    if script not supplied, cancel
11  */
12
13 #include "tables.h"
14 #include "hbytes.h"
15 #include "hbytes.h"
16
17 #include <sys/ioctl.h>
18 #include <linux/if.h>
19 #include <linux/if_tun.h>
20
21 typedef struct TunSocket {
22   int ix, fd, script_llength;
23   Tcl_Interp *ip;
24   ScriptToInvoke script;
25   int mtu;
26   unsigned char *msg_buf;
27   char *ifname;
28 } TuntapSocket;
29
30 IdDataTable tuntap_socks= { "tuntap" };
31
32 int do_tuntap_socket_raw_create(ClientData cd, Tcl_Interp *ip,
33                                 const char *ifname, void **sock_r) {
34   int fd, r;
35   struct ifreq ifr;
36   TuntapSocket *sock;
37
38   memset(&ifr,0,sizeof(ifr));
39   ifr.ifr_flags= IFF_TUN | IFF_NO_PI;
40   
41   if (ifname) {
42     if (strlen(ifname) > IFNAMSIZ-1) return
43       staticerr(ip,"tun interface name too long","TUNTAP IFNAME LENGTH");
44     strcpy(ifr.ifr_name, ifname);
45   }
46
47   fd= open("/dev/net/tun", O_RDWR);
48   if (fd<0) return posixerr(ip,errno,"open /dev/net/tun");
49
50   r= setnonblock(fd,1);
51   if (r) return posixerr(ip,errno,"setnonblock tun");
52   
53   r= ioctl(fd, TUNSETIFF, (void*)&ifr);
54   if (r) return newfdposixerr(ip,fd,"ioctl TUNSETIFF");
55
56   sock= TALLOC(sizeof(TuntapSocket));
57   sock->ix= -1;
58   sock->fd= fd;
59   sock->mtu= 0;
60   sock->msg_buf= 0;
61   sock->ifname= TALLOC(strlen(ifr.ifr_name)+1);
62   strcpy(sock->ifname, ifr.ifr_name);
63   scriptinv_init(&sock->script);
64
65   *sock_r= sock;
66   return TCL_OK;
67 }
68
69 int do_tuntap_socket_raw_receive(ClientData cd, Tcl_Interp *ip,
70                                  void *sock_v, HBytes_Value data) {
71   TuntapSocket *sock= sock_v;
72   int l, r;
73
74   r= write(sock->fd,
75            hbytes_data(&data), l=hbytes_len(&data));
76   if (r==-1) return posixerr(ip,errno,"write tuntap");
77   else if (r!=l) return staticerr(ip,"write tuntap gave wrong answer",0);
78   return TCL_OK;
79 }
80
81 int do_tuntap_socket_raw_ifname(ClientData cd, Tcl_Interp *ip,
82                                 void *sock_v, const char **result) {
83   TuntapSocket *sock= sock_v;
84   *result= sock->ifname;
85   return TCL_OK;
86 }
87
88 static void cancel(TuntapSocket *sock) {
89   if (sock->script.obj) {
90     scriptinv_cancel(&sock->script);
91     Tcl_DeleteFileHandler(sock->fd);
92     TFREE(sock->msg_buf);
93     sock->msg_buf= 0;
94   }
95 }
96
97 static void read_call(ClientData sock_cd, int mask) {
98   TuntapSocket *sock= (void*)sock_cd;
99   Tcl_Interp *ip= sock->ip;
100   int sz, rc;
101   HBytes_Value message_val;
102   Tcl_Obj *args[2];
103
104   for (;;) {
105     sz= read(sock->fd, sock->msg_buf, sock->mtu);
106     if (sz == -1) {
107       if (errno == EAGAIN || errno == EWOULDBLOCK) rc=0;
108       else rc= posixerr(ip,errno,"read tuntap");
109       goto x_rc;
110     }
111
112     assert(sz <= sock->mtu);
113
114     hbytes_array(&message_val, sock->msg_buf, sz);
115     args[0]= ret_hb(ip, message_val);  hbytes_empty(&message_val);
116     args[1]= ret_iddata(ip, sock, &tuntap_socks);
117     scriptinv_invoke(&sock->script, 2, args);
118   }
119
120 x_rc:
121   if (rc) Tcl_BackgroundError(ip);
122 }
123
124 int do_tuntap_socket_raw_on_transmit(ClientData cd, Tcl_Interp *ip,
125                                      void *sock_v,
126                                      long mtu, Tcl_Obj *newscript) {
127   TuntapSocket *sock= sock_v;
128   int rc;
129
130   if (mtu > 65536)
131     return staticerr(ip,"tuntap mtu >2^16","TUNTAP MTU OVERRUN");
132
133   cancel(sock);
134   
135   if (newscript) {
136     rc= scriptinv_set(&sock->script,ip,newscript);
137     if (rc) return rc;
138     
139     sock->mtu= mtu;
140     sock->msg_buf= TALLOC(mtu);
141     Tcl_CreateFileHandler(sock->fd, TCL_READABLE, read_call, sock);
142   }
143   return TCL_OK;
144 }
145
146 int do_tuntap_socket_raw_close(ClientData cd, Tcl_Interp *ip, void *sock_v) {
147   TuntapSocket *sock= sock_v;
148   
149   int sockix;
150   cancel(sock);
151   close(sock->fd); /* nothing useful to be done with errors */
152   sockix= sock->ix;
153   TFREE(sock->msg_buf);
154   TFREE(sock);
155   tuntap_socks.a[sockix]= 0;
156   return TCL_OK;
157 }