chiark / gitweb /
tuntap
authorian <ian>
Fri, 25 Aug 2006 21:25:40 +0000 (21:25 +0000)
committerian <ian>
Fri, 25 Aug 2006 21:25:40 +0000 (21:25 +0000)
Makefile
debian/changelog
tuntap/.cvsignore [new file with mode: 0644]
tuntap/Makefile [new file with mode: 0644]
tuntap/chiark_tcl_tuntap.h [moved from tuntap/tuntap.h with 68% similarity]
tuntap/tuntap.c
tuntap/tuntap.tct

index 56ae963..e815676 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,5 @@
 
-SUBDIRS=       base adns hbytes cdb crypto dgram
-# tuntap
+SUBDIRS=       base adns hbytes cdb crypto dgram tuntap
 
 default: all
 
@@ -9,19 +8,7 @@ clean all:
 
 # To find undefined symbols when implementing, for example:
 #
-# liberator:cdb> ld /usr/bin/tclsh8.3 ../base/chiark-tcl.so ../hbytes/chiark_tcl_hbytes.so ../cdb/chiark_tcl_cdb.so -ltcl8.3
-# ld: warning: cannot find entry symbol _start; defaulting to 00000000080486ac
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdbwr_delete'
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdbwr_lookup_hb'
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdbwr_open_okjunk'
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdb_lookup_hb'
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdbwr_lookup'
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdb_close'
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdb_lookup'
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdbtoplevel_cdb'
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdbwr_update_hb'
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdb_open'
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdbwr_update'
-# ../cdb/chiark_tcl_cdb.so: undefined reference to `cht_do_cdbtoplevel_cdb_wr'
-# Segmentation fault
-# liberator:cdb>
+# liberator:chiark-tcl> LD_LIBRARY_PATH=:adns:base:cdb:crypto:dgram:hbytes:tuntap tclsh8.3
+# % load chiark_tcl_tuntap-1.so
+# couldn't load file "chiark_tcl_tuntap-1.so": tuntap/chiark_tcl_tuntap-1.so: undefined symbol: cht_tunsocket_entries
+# % 
index 418d5e7..439c384 100644 (file)
@@ -1,6 +1,7 @@
 chiark-tcl (1.0.1) unstable; urgency=low
 
   * dgram: New extension for datagram sockets; dgram-socket command.
+  * tuntap: New extension for tun/tap interfaces (currently, tun only).
   * New way of doing toplevels with tcmdifgen dispatch() primitive.
   * Replace #include <tcl8.3/tcl.h> with <tcl.h> and in build system find
     Tcl version and pass appropriate -I option.  (Closes: #362806.)
diff --git a/tuntap/.cvsignore b/tuntap/.cvsignore
new file mode 100644 (file)
index 0000000..fc2ec4f
--- /dev/null
@@ -0,0 +1,2 @@
+*+tcmdif.[ch]
+*.d
diff --git a/tuntap/Makefile b/tuntap/Makefile
new file mode 100644 (file)
index 0000000..2d6808d
--- /dev/null
@@ -0,0 +1,9 @@
+BASE_DIR =     ../base
+EXTBASE =      tuntap
+CFILES =       tuntap
+OTHER_TCTS =   ../hbytes/hbytes-base.tct
+OTHER_EXTS =   hbytes/hbytes dgram/dgram
+##LDLIBS +=    -ladns
+
+include ../base/extension.make
+
similarity index 68%
rename from tuntap/tuntap.h
rename to tuntap/chiark_tcl_tuntap.h
index a17e228..d548f10 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * tuntap - Tcl extension for tun/tap network device
+ * tuntap - Tcl bindings for tun/tap userspace network interfaces
  * Copyright 2006 Ian Jackson
  *
  * This program is free software; you can redistribute it and/or
  * 02110-1301, USA.
  */
 
+#ifndef TUNTAPTCL_H
+#define TUNTAPTCL_H
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <linux/if.h>
+#include <linux/if_tun.h>
+
+#include "hbytes.h"
+#include "dgram.h"
+#include "tuntap+tcmdif.h"
 
 /* from tuntap.c */
 
-extern const IdDataSpec tuntap_socks;
+extern const IdDataSpec cht_tuntap_socks;
 
+#endif /*TUNTAPTCL_H*/
index 60a8cdc..9486a9a 100644 (file)
  *    if script not supplied, cancel
  */
 
-#include "tables.h"
-#include "hbytes.h"
-
-#include <sys/ioctl.h>
-#include <linux/if.h>
-#include <linux/if_tun.h>
+#include "chiark_tcl_tuntap.h"
 
 typedef struct TunSocket {
   int ix, fd, script_llength;
@@ -26,7 +21,7 @@ typedef struct TunSocket {
   char *ifname;
 } TuntapSocket;
 
-int do_tuntap_socket_raw_create(ClientData cd, Tcl_Interp *ip,
+int cht_do_tuntapsocket_create_tun(ClientData cd, Tcl_Interp *ip,
                                const char *ifname, void **sock_r) {
   int fd, r;
   struct ifreq ifr;
@@ -37,18 +32,18 @@ int do_tuntap_socket_raw_create(ClientData cd, Tcl_Interp *ip,
   
   if (ifname) {
     if (strlen(ifname) > IFNAMSIZ-1) return
-      staticerr(ip,"tun interface name too long","TUNTAP IFNAME LENGTH");
+      cht_staticerr(ip,"tun interface name too long","TUNTAP IFNAME LENGTH");
     strcpy(ifr.ifr_name, ifname);
   }
 
   fd= open("/dev/net/tun", O_RDWR);
-  if (fd<0) return posixerr(ip,errno,"open /dev/net/tun");
+  if (fd<0) return cht_posixerr(ip,errno,"open /dev/net/tun");
 
-  r= setnonblock(fd,1);
-  if (r) return posixerr(ip,errno,"setnonblock tun");
+  r= cht_setnonblock(fd,1);
+  if (r) return cht_posixerr(ip,errno,"setnonblock tun");
   
   r= ioctl(fd, TUNSETIFF, (void*)&ifr);
-  if (r) return newfdposixerr(ip,fd,"ioctl TUNSETIFF");
+  if (r) return cht_newfdposixerr(ip,fd,"ioctl TUNSETIFF");
 
   sock= TALLOC(sizeof(TuntapSocket));
   sock->ix= -1;
@@ -57,25 +52,25 @@ int do_tuntap_socket_raw_create(ClientData cd, Tcl_Interp *ip,
   sock->msg_buf= 0;
   sock->ifname= TALLOC(strlen(ifr.ifr_name)+1);
   strcpy(sock->ifname, ifr.ifr_name);
-  scriptinv_init(&sock->script);
+  cht_scriptinv_init(&sock->script);
 
   *sock_r= sock;
   return TCL_OK;
 }
 
-int do_tuntap_socket_raw_receive(ClientData cd, Tcl_Interp *ip,
+int cht_do_tuntapsocket_receive(ClientData cd, Tcl_Interp *ip,
                                 void *sock_v, HBytes_Value data) {
   TuntapSocket *sock= sock_v;
   int l, r;
 
   r= write(sock->fd,
-          hbytes_data(&data), l=hbytes_len(&data));
-  if (r==-1) return posixerr(ip,errno,"write tuntap");
-  else if (r!=l) return staticerr(ip,"write tuntap gave wrong answer",0);
+          cht_hb_data(&data), l=cht_hb_len(&data));
+  if (r==-1) return cht_posixerr(ip,errno,"write tuntap");
+  else if (r!=l) return cht_staticerr(ip,"write tuntap gave wrong answer",0);
   return TCL_OK;
 }
 
-int do_tuntap_socket_raw_ifname(ClientData cd, Tcl_Interp *ip,
+int cht_do_tuntapsocket_ifname(ClientData cd, Tcl_Interp *ip,
                                void *sock_v, const char **result) {
   TuntapSocket *sock= sock_v;
   *result= sock->ifname;
@@ -83,8 +78,8 @@ int do_tuntap_socket_raw_ifname(ClientData cd, Tcl_Interp *ip,
 }
 
 static void cancel(TuntapSocket *sock) {
-  if (sock->script.obj) {
-    scriptinv_cancel(&sock->script);
+  if (sock->script.script) {
+    cht_scriptinv_cancel(&sock->script);
     Tcl_DeleteFileHandler(sock->fd);
     TFREE(sock->msg_buf);
     sock->msg_buf= 0;
@@ -102,35 +97,35 @@ static void read_call(ClientData sock_cd, int mask) {
     sz= read(sock->fd, sock->msg_buf, sock->mtu);
     if (sz == -1) {
       if (errno == EAGAIN || errno == EWOULDBLOCK) rc=0;
-      else rc= posixerr(ip,errno,"read tuntap");
+      else rc= cht_posixerr(ip,errno,"read tuntap");
       goto x_rc;
     }
 
     assert(sz <= sock->mtu);
 
-    hbytes_array(&message_val, sock->msg_buf, sz);
-    args[0]= ret_hb(ip, message_val);  hbytes_empty(&message_val);
-    args[1]= ret_iddata(ip, sock, &tuntap_socks);
-    scriptinv_invoke(&sock->script, 2, args);
+    cht_hb_array(&message_val, sock->msg_buf, sz);
+    args[0]= cht_ret_hb(ip, message_val);  cht_hb_empty(&message_val);
+    args[1]= cht_ret_iddata(ip, sock, &cht_tuntap_socks);
+    cht_scriptinv_invoke(&sock->script, 2, args);
   }
 
 x_rc:
   if (rc) Tcl_BackgroundError(ip);
 }
 
-int do_tuntap_socket_raw_on_transmit(ClientData cd, Tcl_Interp *ip,
+int cht_do_tuntapsocket_on_transmit(ClientData cd, Tcl_Interp *ip,
                                     void *sock_v,
                                     long mtu, Tcl_Obj *newscript) {
   TuntapSocket *sock= sock_v;
   int rc;
 
   if (mtu > 65536)
-    return staticerr(ip,"tuntap mtu >2^16","TUNTAP MTU OVERRUN");
+    return cht_staticerr(ip,"tuntap mtu >2^16","TUNTAP MTU OVERRUN");
 
   cancel(sock);
   
   if (newscript) {
-    rc= scriptinv_set(&sock->script,ip,newscript,0);
+    rc= cht_scriptinv_set(&sock->script,ip,newscript,0);
     if (rc) return rc;
     
     sock->mtu= mtu;
@@ -152,12 +147,14 @@ static void destroy_idtabcb(Tcl_Interp *ip, void *sock_v) {
   destroy(sock_v);
 }
 
-int do_tuntap_socket_raw_close(ClientData cd, Tcl_Interp *ip, void *sock) {
-  tabledataid_disposing(ip,sock,&tuntap_socks);
+int cht_do_tuntapsocket_close(ClientData cd, Tcl_Interp *ip, void *sock) {
+  cht_tabledataid_disposing(ip,sock,&cht_tuntap_socks);
   destroy(sock);
   return TCL_OK;
 }
 
-const IdDataSpec tuntap_socks= {
+const IdDataSpec cht_tuntap_socks= {
   "tuntap", "tuntap-table", destroy_idtabcb
 };
+
+CHT_INIT(tuntap, { }, CHTI_COMMANDS(cht_tuntaptoplevel_entries))
index 698f97f..87f1396 100644 (file)
 # 02110-1301, USA.
 
 
+Table *tuntaptoplevel TopLevel_Command
+       tuntap-socket
+           dispatch(TunTapSocket/_SubCommand,"tuntap-socket-raw subcommand")
 
-Table tuntap_socket_raw TunSocket_SubCommand
-       create
+Table tuntapsocket TunTapSocket_SubCommand
+       create-tun
                ?ifname string
-               =>      iddata(&tuntap_socks)
+               =>      iddata(&cht_tuntap_socks)
        close
-               sock    iddata(&tuntap_socks)
+               sock    iddata(&cht_tuntap_socks)
        ifname
-               sock    iddata(&tuntap_socks)
+               sock    iddata(&cht_tuntap_socks)
                =>      string
        receive
-               sock    iddata(&tuntap_socks)
+               sock    iddata(&cht_tuntap_socks)
                data    hb
        on-transmit
-               sock    iddata(&tuntap_socks)
+               sock    iddata(&cht_tuntap_socks)
                mtu     long
                ?script obj