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