chiark / gitweb /
Rename *.test.tcl to test-load.tcl
[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 rc;
93   ssize_t sz;
94   HBytes_Value message_val;
95   Tcl_Obj *args[2];
96
97   for (;;) {
98     sz= read(sock->fd, sock->msg_buf, sock->mtu);
99     if (sz == -1) {
100       if (errno == EAGAIN || errno == EWOULDBLOCK) rc=0;
101       else rc= cht_posixerr(ip,errno,"read tuntap");
102       goto x_rc;
103     }
104
105     assert(sz <= sock->mtu);
106
107     cht_hb_array(&message_val, sock->msg_buf, sz);
108     args[0]= cht_ret_hb(ip, message_val);  cht_hb_empty(&message_val);
109     args[1]= cht_ret_iddata(ip, sock, &cht_tuntap_socks);
110     cht_scriptinv_invoke(&sock->script, 2, args);
111   }
112
113 x_rc:
114   if (rc) Tcl_BackgroundError(ip);
115 }
116
117 int cht_do_tuntapsocket_on_transmit(ClientData cd, Tcl_Interp *ip,
118                                      void *sock_v,
119                                      long mtu, Tcl_Obj *newscript) {
120   TuntapSocket *sock= sock_v;
121   int rc;
122
123   if (mtu > 65536)
124     return cht_staticerr(ip,"tuntap mtu >2^16","TUNTAP MTU OVERRUN");
125
126   cancel(sock);
127   
128   if (newscript) {
129     rc= cht_scriptinv_set(&sock->script,ip,newscript,0);
130     if (rc) return rc;
131     
132     sock->mtu= mtu;
133     sock->msg_buf= TALLOC(mtu);
134     Tcl_CreateFileHandler(sock->fd, TCL_READABLE, read_call, sock);
135   }
136   return TCL_OK;
137 }
138
139 static void destroy(void *sock_v) {
140   TuntapSocket *sock= sock_v;
141   cancel(sock);
142   close(sock->fd); /* nothing useful to be done with errors */
143   TFREE(sock->msg_buf);
144   TFREE(sock);
145 }
146
147 static void destroy_idtabcb(Tcl_Interp *ip, void *sock_v) {
148   destroy(sock_v);
149 }
150
151 int cht_do_tuntapsocket_close(ClientData cd, Tcl_Interp *ip, void *sock) {
152   cht_tabledataid_disposing(ip,sock,&cht_tuntap_socks);
153   destroy(sock);
154   return TCL_OK;
155 }
156
157 const IdDataSpec cht_tuntap_socks= {
158   "tuntap", "tuntap-table", destroy_idtabcb
159 };
160
161 CHT_INIT(tuntap, { }, CHTI_COMMANDS(cht_tuntaptoplevel_entries))