chiark / gitweb /
Socket address stuff. Sockid is broken still.
[chiark-tcl.git] / base / hook.c
index 68df783561431b14b411c22fa274111b5e16fa99..e53af08ba8e8fd8dd01cafa1c263a71df1698899 100644 (file)
@@ -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)),
@@ -147,12 +158,37 @@ int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }
 
+int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
+                    int length, HBytes_Value *result) {
+  Byte *space;
+  int rc;
+  
+  space= hbytes_arrayspace(result, length);
+  rc= get_urandom(ip, space, length);
+  if (rc) { hbytes_free(result); return rc; }
+  return TCL_OK;
+}  
+  
+int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
+                    int length, HBytes_Value *result) {
+  Byte *space;
+  space= hbytes_arrayspace(result, length);
+  memset(space,0,length);
+  return TCL_OK;
+}
+
 int do__hbytes(ClientData cd, Tcl_Interp *ip,
               const HBytes_SubCommand *subcmd,
               int objc, Tcl_Obj *const *objv) {
   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) {
@@ -183,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;
 }