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