From d23a32272b954579fe15c78d6ea605e087d0a512 Mon Sep 17 00:00:00 2001 From: ian Date: Sat, 7 Sep 2002 00:59:57 +0000 Subject: [PATCH 1/1] Socket address stuff. Sockid is broken still. --- base/chiark-tcl.h | 37 +++++++- base/hook.c | 28 +++++- base/parse.c | 4 +- base/tables-examples.tct | 19 +++- base/troglodyte-Makefile | 6 +- crypto/bcmode.c | 24 ++--- crypto/crypto.c | 4 +- dgram/dgram.c | 91 +++++++++++++++++++ dgram/misc.c | 2 +- dgram/sockaddr.c | 187 +++++++++++++++++++++++++++++++++++++++ hbytes/chop.c | 1 + hbytes/hbytes.c | 3 +- hbytes/hbytes.h | 37 +++++++- hbytes/hook.c | 28 +++++- hbytes/parse.c | 4 +- 15 files changed, 439 insertions(+), 36 deletions(-) create mode 100644 dgram/dgram.c create mode 100644 dgram/sockaddr.c diff --git a/base/chiark-tcl.h b/base/chiark-tcl.h index 86b2dba..951ae12 100644 --- a/base/chiark-tcl.h +++ b/base/chiark-tcl.h @@ -28,6 +28,12 @@ #include #include +#include +#include +#include +#include +#include +#include #include @@ -35,6 +41,8 @@ typedef unsigned char Byte; /* from hbytes.c */ +int Hbytes_Init(Tcl_Interp *ip); /* called by Tcl's "load" */ + /* Internal representation details: */ #define HBYTES_ISEMPTY(hb) (!(hb)->begin_complex && !(hb)->end_0) #define HBYTES_ISSENTINEL(hb) (!(hb)->begin_complex && (hb)->end_0) @@ -88,13 +96,35 @@ void hbytes_free(const HBytes_Value *frees); /* The value made by hbytes_sentinel should not be passed to * anything except HBYTES_IS..., and hbytes_free. */ +/* from sockaddr.c */ + +typedef struct { + Byte *begin, *end; +} SockAddr_Value; + +extern Tcl_ObjType sockaddr_type; + +void sockaddr_clear(SockAddr_Value*); +void sockaddr_create(SockAddr_Value*, const struct sockaddr *addr, int len); +int sockaddr_len(const SockAddr_Value*); +const struct sockaddr *sockaddr_addr(const SockAddr_Value*); +void sockaddr_free(const SockAddr_Value*); + +/* from dgram.c */ + +extern Tcl_ObjType sockid_type; + /* from hook.c */ int staticerr(Tcl_Interp *ip, const char *m); +int posixerr(Tcl_Interp *ip, int errnoval, const char *m); void objfreeir(Tcl_Obj *o); -void obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l); int get_urandom(Tcl_Interp *ip, Byte *buffer, int l); +void obj_updatestr_array(Tcl_Obj *o, const Byte *array, int l); +void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte, + int l, const char *prefix); + /* from parse.c */ typedef struct { @@ -187,9 +217,14 @@ typedef struct { extern const BlockCipherModeInfo blockciphermodeinfos[]; +/* from misc.c */ + +int setnonblock(int fd, int isnonblock); + /* useful macros */ #define OBJ_HBYTES(o) ((HBytes_Value*)&(o)->internalRep.twoPtrValue) +#define OBJ_SOCKADDR(o) ((SockAddr_Value*)&(o)->internalRep.twoPtrValue) #define TALLOC(s) ((void*)Tcl_Alloc((s))) #define TFREE(f) (Tcl_Free((void*)(f))) diff --git a/base/hook.c b/base/hook.c index b6c0c9e..e53af08 100644 --- a/base/hook.c +++ b/base/hook.c @@ -71,11 +71,18 @@ static void hbytes_t_free(Tcl_Obj *o) { hbytes_free(OBJ_HBYTES(o)); } -void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) { +void obj_updatestr_array_prefix(Tcl_Obj *o, const Byte *byte, + int l, const char *prefix) { char *str; + int pl; + + pl= strlen(prefix); + o->length= l*2+pl; + str= o->bytes= TALLOC(o->length+1); + + memcpy(str,prefix,pl); + str += pl; - str= o->bytes= TALLOC(l*2+1); - o->length= l*2; while (l>0) { sprintf(str,"%02x",*byte); str+=2; byte++; l--; @@ -83,6 +90,10 @@ void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) { *str= 0; } +void obj_updatestr_array(Tcl_Obj *o, const Byte *byte, int l) { + obj_updatestr_array_prefix(o,byte,l,""); +} + static void hbytes_t_ustr(Tcl_Obj *o) { obj_updatestr_array(o, hbytes_data(OBJ_HBYTES(o)), @@ -172,6 +183,12 @@ int do__hbytes(ClientData cd, Tcl_Interp *ip, return subcmd->func(0,ip,objc,objv); } +int do__dgram_socket(ClientData cd, Tcl_Interp *ip, + const DgramSocket_SubCommand *subcmd, + int objc, Tcl_Obj *const *objv) { + return subcmd->func(0,ip,objc,objv); +} + #define URANDOM "/dev/urandom" int get_urandom(Tcl_Interp *ip, Byte *buffer, int l) { @@ -202,6 +219,9 @@ int Hbytes_Init(Tcl_Interp *ip) { Tcl_RegisterObjType(&blockcipherkey_type); Tcl_RegisterObjType(&enum_nearlytype); Tcl_RegisterObjType(&enum1_nearlytype); - Tcl_CreateObjCommand(ip,"hbytes", pa__hbytes,0,0); + Tcl_RegisterObjType(&sockaddr_type); + Tcl_RegisterObjType(&sockid_type); + Tcl_CreateObjCommand(ip, "hbytes", pa__hbytes, 0,0); + Tcl_CreateObjCommand(ip, "dgram-socket", pa__dgram_socket, 0,0); return TCL_OK; } diff --git a/base/parse.c b/base/parse.c index ddb5ac5..b81511c 100644 --- a/base/parse.c +++ b/base/parse.c @@ -42,9 +42,7 @@ int pat_hbv(Tcl_Interp *ip, Tcl_Obj *var, HBytes_Var *agg) { rc= Tcl_ConvertToType(ip,val,&hbytes_type); if (rc) return rc; - agg->hb= OBJ_HBYTES(val); - return TCL_OK; -} + agg->hb= OBJ_HBYTES(val); return TCL_OK; } void fini_hbv(Tcl_Interp *ip, int rc, HBytes_Var *agg) { Tcl_Obj *ro; diff --git a/base/tables-examples.tct b/base/tables-examples.tct index b40198f..5814334 100644 --- a/base/tables-examples.tct +++ b/base/tables-examples.tct @@ -1,15 +1,25 @@ Type hb: HBytes_Value @ Init hb hbytes_sentinel(&@); + Type hbv: HBytes_Var @ Init hbv @.hb=0; @.obj=0; @.var=0; Fini hbv fini_hbv(ip, rc, &@); +Type sockaddr: SockAddr_Value @ +Init sockaddr sockaddr_clear(&@); + +Type sockid: int @ +Init sockid @=-1; + H-Include "hbytes.h" Untabled hbytes subcmd enum(HBytes_SubCommand, "hbytes subcommand") obj ... + dgram-socket + subcmd enum(DgramSocket_SubCommand,"dgram-socket subcommand") + obj ... Table hbytes HBytes_SubCommand raw2h @@ -75,6 +85,9 @@ Table hbytes HBytes_SubCommand length int => hb -#Table udp UDP_SubCommand -# create -# +Table dgram_socket DgramSocket_SubCommand + create + local sockaddr + => sockid +# transmit +# diff --git a/base/troglodyte-Makefile b/base/troglodyte-Makefile index 9794ba4..64160c0 100644 --- a/base/troglodyte-Makefile +++ b/base/troglodyte-Makefile @@ -1,9 +1,12 @@ OBJS= tables.o \ hbytes.o \ enum.o \ + sockaddr.o \ + dgram.o \ chop.o \ hook.o \ bcmode.o \ + misc.o \ algtables.o \ serpent.o \ sha1.o \ @@ -21,7 +24,8 @@ AUTOS= $(AUTO_HDRS) $(AUTO_SRCS) TARGETS= hbytes.so CC_CRYPTO= $(CC) $(CFLAGS) $(CPPFLAGS) -O3 -CFLAGS= -g -Wall $(OPTIMISE) +CFLAGS= -g -Wall -Wmissing-prototypes -Wstrict-prototypes -Werror \ + $(OPTIMISE) OPTIMISE= -O2 all: $(TARGETS) $(AUTOS) diff --git a/crypto/bcmode.c b/crypto/bcmode.c index a40aff0..f7c808d 100644 --- a/crypto/bcmode.c +++ b/crypto/bcmode.c @@ -3,10 +3,10 @@ #include "hbytes.h" -const char *mode_cbc_encrypt(Byte *data, int blocks, - const Byte *iv, Byte *chain, - const BlockCipherAlgInfo *alg, int encr, - int blocksize, const void *sch) { +static const char *mode_cbc_encrypt(Byte *data, int blocks, + const Byte *iv, Byte *chain, + const BlockCipherAlgInfo *alg, int encr, + int blocksize, const void *sch) { memcpy(chain,iv,blocksize); alg->byteswap(chain); @@ -23,10 +23,10 @@ const char *mode_cbc_encrypt(Byte *data, int blocks, return 0; } -const char *mode_cbc_decrypt(Byte *data, int blocks, - const Byte *iv, Byte *chain, - const BlockCipherAlgInfo *alg, int encr, - int blocksize, const void *sch) { +static const char *mode_cbc_decrypt(Byte *data, int blocks, + const Byte *iv, Byte *chain, + const BlockCipherAlgInfo *alg, int encr, + int blocksize, const void *sch) { int cchain= 0; memcpy(chain,iv,blocksize); @@ -46,10 +46,10 @@ const char *mode_cbc_decrypt(Byte *data, int blocks, return 0; } -const char *mode_ecb(Byte *data, int blocks, - const Byte *iv, Byte *chain, - const BlockCipherAlgInfo *alg, int encr, - int blocksize, const void *sch) { +static const char *mode_ecb(Byte *data, int blocks, + const Byte *iv, Byte *chain, + const BlockCipherAlgInfo *alg, int encr, + int blocksize, const void *sch) { while (blocks > 0) { alg->byteswap(data); (encr ? &alg->encrypt : &alg->decrypt)->crypt(sch, data, data); diff --git a/crypto/crypto.c b/crypto/crypto.c index 7cc2914..ec6a6de 100644 --- a/crypto/crypto.c +++ b/crypto/crypto.c @@ -133,8 +133,8 @@ Tcl_ObjType blockcipherkey_type = { key_t_free, key_t_dup, key_t_ustr, key_t_sfa }; -CiphKeyValue *get_key(Tcl_Interp *ip, Tcl_Obj *key_obj, - const void *alg, int want_bufferslen) { +static CiphKeyValue *get_key(Tcl_Interp *ip, Tcl_Obj *key_obj, + const void *alg, int want_bufferslen) { CiphKeyValue *key; int rc; diff --git a/dgram/dgram.c b/dgram/dgram.c new file mode 100644 index 0000000..ecdd2f7 --- /dev/null +++ b/dgram/dgram.c @@ -0,0 +1,91 @@ +/* + */ +/* + * dgram-socket create => + * dgram-socket close + * dgram-socket transmit + * dgram-socket on-receive