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
16 #include <sys/ioctl.h>
18 #include <linux/if_tun.h>
20 typedef struct TunSocket {
21 int ix, fd, script_llength;
23 ScriptToInvoke script;
25 unsigned char *msg_buf;
29 int do_tuntap_socket_raw_create(ClientData cd, Tcl_Interp *ip,
30 const char *ifname, void **sock_r) {
35 memset(&ifr,0,sizeof(ifr));
36 ifr.ifr_flags= IFF_TUN | IFF_NO_PI;
39 if (strlen(ifname) > IFNAMSIZ-1) return
40 staticerr(ip,"tun interface name too long","TUNTAP IFNAME LENGTH");
41 strcpy(ifr.ifr_name, ifname);
44 fd= open("/dev/net/tun", O_RDWR);
45 if (fd<0) return posixerr(ip,errno,"open /dev/net/tun");
48 if (r) return posixerr(ip,errno,"setnonblock tun");
50 r= ioctl(fd, TUNSETIFF, (void*)&ifr);
51 if (r) return newfdposixerr(ip,fd,"ioctl TUNSETIFF");
53 sock= TALLOC(sizeof(TuntapSocket));
58 sock->ifname= TALLOC(strlen(ifr.ifr_name)+1);
59 strcpy(sock->ifname, ifr.ifr_name);
60 scriptinv_init(&sock->script);
66 int do_tuntap_socket_raw_receive(ClientData cd, Tcl_Interp *ip,
67 void *sock_v, HBytes_Value data) {
68 TuntapSocket *sock= sock_v;
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);
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;
85 static void cancel(TuntapSocket *sock) {
86 if (sock->script.obj) {
87 scriptinv_cancel(&sock->script);
88 Tcl_DeleteFileHandler(sock->fd);
94 static void read_call(ClientData sock_cd, int mask) {
95 TuntapSocket *sock= (void*)sock_cd;
96 Tcl_Interp *ip= sock->ip;
98 HBytes_Value message_val;
102 sz= read(sock->fd, sock->msg_buf, sock->mtu);
104 if (errno == EAGAIN || errno == EWOULDBLOCK) rc=0;
105 else rc= posixerr(ip,errno,"read tuntap");
109 assert(sz <= sock->mtu);
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);
118 if (rc) Tcl_BackgroundError(ip);
121 int do_tuntap_socket_raw_on_transmit(ClientData cd, Tcl_Interp *ip,
123 long mtu, Tcl_Obj *newscript) {
124 TuntapSocket *sock= sock_v;
128 return staticerr(ip,"tuntap mtu >2^16","TUNTAP MTU OVERRUN");
133 rc= scriptinv_set(&sock->script,ip,newscript);
137 sock->msg_buf= TALLOC(mtu);
138 Tcl_CreateFileHandler(sock->fd, TCL_READABLE, read_call, sock);
143 static void destroy(void *sock_v) {
144 TuntapSocket *sock= sock_v;
146 close(sock->fd); /* nothing useful to be done with errors */
147 TFREE(sock->msg_buf);
151 static void destroy_idtabcb(Tcl_Interp *ip, void *sock_v) {
155 int do_tuntap_socket_raw_close(ClientData cd, Tcl_Interp *ip, void *sock) {
157 tabledataid_disposing(ip,sock,&tuntap_socks);
161 const IdDataSpec tuntap_socks= {
162 "tuntap", "tuntap-table", destroy_idtabcb