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