chiark / gitweb /
Sockets all seem to work now erm.
[chiark-tcl.git] / dgram / sockaddr.c
index bedc10977bb25e82090846a8b2a6b255336c9c0c..4df9aa0376414750f1c4e30b72e83b1027e2a24d 100644 (file)
@@ -74,7 +74,8 @@ static void sockaddr_t_free(Tcl_Obj *o) {
 
 static void sockaddr_t_ustr(Tcl_Obj *o) {
   const struct sockaddr *sa;
-  char i46buf[INET6_ADDRSTRLEN];
+  char i46buf[INET6_ADDRSTRLEN], portbuf[50];
+  const struct sockaddr_in *sin;
   int al, sl, pl;
   const char *string, *prepend;
   
@@ -86,17 +87,19 @@ static void sockaddr_t_ustr(Tcl_Obj *o) {
   case AF_INET6:
     assert(sizeof(i46buf) >= INET_ADDRSTRLEN);
     assert(al >= sizeof(struct sockaddr_in));
-    inet_ntop(sa->sa_family, sa, i46buf, al);
-    string= i46buf;
-    prepend= "";
+    sin= (const void*)sa;
+    inet_ntop(sa->sa_family, &sin->sin_addr, i46buf, al);
+    snprintf(portbuf,sizeof(portbuf),",%d",(int)ntohs(sin->sin_port));
+    prepend= i46buf;
+    string= portbuf;
     break;
 
   case AF_UNIX:
-    assert(al >= sizeof(struct sockaddr_un));
     string= ((const struct sockaddr_un*)sa)->sun_path;
     prepend= "";
     if (!string[0]) string="//"; 
-    else if (string[0] != '/' || string[1] == '/') prepend= "./"; 
+    else if (string[0] != '/' || string[1] == '/') prepend= "./";
+    break;
 
   default: /* ouch ! */
     obj_updatestr_array_prefix(o,(const void*)sa,al,"?");
@@ -105,7 +108,8 @@ static void sockaddr_t_ustr(Tcl_Obj *o) {
 
   pl= strlen(prepend);
   sl= strlen(string);
-  o->bytes= TALLOC(sl+1);
+  o->length= pl+sl;
+  o->bytes= TALLOC(pl+sl+1);
   memcpy(o->bytes, prepend, pl);
   memcpy(o->bytes+pl, string, sl+1);
 }
@@ -130,14 +134,14 @@ static int sockaddr_t_sfa(Tcl_Interp *ip, Tcl_Obj *o) {
     sl= sizeof(s.sun);
     s.sun.sun_family= AF_UNIX;
 
-    if (strcmp(str,"//")) path= "";
-    else if (memcmp(str,"./",2) && str[2]) path= str+2;
+    if (!strcmp(str,"//")) path= "";
+    else if (!memcmp(str,"./",2) && str[2]) path= str+2;
     else { assert(str[0]=='/' && str[1]!='/'); path=str; }
 
     if (strlen(str) >= sizeof(s.sun.sun_path))
       return staticerr(ip, "AF_UNIX path too long");
 
-    strcpy(s.sun.sun_path, str);
+    strcpy(s.sun.sun_path, path);
 
   } else if ((comma= strchr(str, ','))) {