chiark / gitweb /
test: udp-preload: Fix inet_ntop calling convention
[secnet.git] / test / udp-preload.c
index 94b399a1e7c1a0412a120b101975ff433cc7ad87..49457ad42af888c8018d9c342f2f139fc9ef96f8 100644 (file)
@@ -96,7 +96,7 @@ static fdinfo **table;
 static int tablesz;
 
 static fdinfo *lookup(int fd) {
-    if (fd>=tablesz) return 0;
+    if (fd<0 || fd>=tablesz) return 0;
     return table[fd];
 }
 
@@ -150,7 +150,10 @@ static int str2addrport(char *str,
     char *comma=strchr(str,',');
     if (!comma) { errno=ESRCH; return -1; }
     *comma++=0;
-    if (inet_pton(af,str,iav)) return -1;
+    int r=inet_pton(af,str,iav);
+//fprintf(stderr,"inet_pton(%d,\"%s\",)=%d\n",af,str,r);
+    if (r<0) return -1;
+    if (r==0) { errno=ENOTTY; return -1; }
 
     char *ep;
     errno=0;
@@ -203,7 +206,7 @@ WRAP(socket) {
 }
 
 WRAP(close) {
-    if (fd<tablesz) {
+    if (fd>=0 && fd<tablesz) {
        free(table[fd]);
        table[fd]=0;
     }
@@ -244,7 +247,9 @@ WRAP(getsockname) {
 //     (unsigned long)sunlen);
        errno=EDOM; return -1;
     }
-    if (str2addrport(sun.sun_path,addr,addrlen)) return -1;
+    char *slash=strrchr(sun.sun_path,'/');
+    if (str2addrport(slash ? slash+1 : sun.sun_path,
+                    addr,addrlen)) return -1;
     return 0;
 }