chiark / gitweb /
test: udp-preload: Fix copyright dates and error message
[secnet.git] / test / udp-preload.c
index 94b399a1e7c1a0412a120b101975ff433cc7ad87..31fc7d2aabed0479ac33ed35b8b712662114e9a4 100644 (file)
@@ -1,11 +1,12 @@
 /*
- *  libauthbind.c - bind(2)-redirector library for authbind
+ *  udp-preload.c - testing mock library for secnet udp
+ *  This file is part of secnet.
  *
- *  authbind is Copyright (C) 1998 Ian Jackson
+ *  Copyright (C) 1998,2003-2004,2012,2017,2019 Ian Jackson
  * 
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2, or (at your option)
+ *  the Free Software Foundation; either version 3, or (at your option)
  *  any later version.
  * 
  *  This program is distributed in the hope that it will be useful,
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  * 
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software Foundation,
- *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
- * 
+ * You should have received a copy of the GNU General Public License
+ * version 3 along with secnet; if not, see
+ * https://www.gnu.org/licenses/gpl.html.
  */
 
 #define _GNU_SOURCE
@@ -48,7 +48,7 @@ static anyfn_type *find_any(const char *name) {
 
   kv= dlsym(RTLD_NEXT,name); if (kv) return kv;
   dlerr= dlerror(); if (!dlerr) dlerr= "dlsym() failed for no reason";
-  STDERRSTR_CONST("libauthbind: error finding original version of ");
+  STDERRSTR_CONST("udp-preload: error finding original version of ");
   STDERRSTR_STRING(name);
   STDERRSTR_CONST(": ");
   STDERRSTR_STRING(dlerr);
@@ -62,14 +62,17 @@ static anyfn_type *find_any(const char *name) {
 #define bind_args   int fd, const struct sockaddr *addr, socklen_t addrlen
 #define sendto_args int fd, const void *buf, size_t len, int flags, \
                     const struct sockaddr *addr, socklen_t addrlen
+#define recvfrom_args  int fd, void *buf, size_t len, int flags, \
+                       struct sockaddr *addr, socklen_t *addrlen
 #define setsockopt_args  int fd, int level, int optname, \
                          const void *optval, socklen_t optlen
 #define getsockname_args int fd, struct sockaddr *addr, socklen_t *addrlen
-#define WRAPS(X)                                       \
+#define WRAPS(X)                                               \
     X(socket,     int,     (domain,type,protocol))             \
-    X(close,      int,     (fd))                                       \
+    X(close,      int,     (fd))                               \
     X(bind,       int,     (fd,addr,addrlen))                  \
     X(sendto,     ssize_t, (fd,buf,len,flags,addr,addrlen))    \
+    X(recvfrom,   ssize_t, (fd,buf,len,flags,addr,addrlen))    \
     X(setsockopt, int,     (fd,level,optname,optval,optlen))   \
     X(getsockname,int,     (fd,addr,addrlen))
 
@@ -96,7 +99,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 +153,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 +209,7 @@ WRAP(socket) {
 }
 
 WRAP(close) {
-    if (fd<tablesz) {
+    if (fd>=0 && fd<tablesz) {
        free(table[fd]);
        table[fd]=0;
     }
@@ -244,7 +250,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;
 }
 
@@ -280,3 +288,40 @@ ssize_t TWRAP(sendto) {
 
     return sendmsg(fd,&m,0);
 }
+
+ssize_t TWRAP(recvfrom) {
+    fdinfo *ent=lookup(fd);
+    if (!ent) return old_recvfrom(fd,buf,len,flags,addr,addrlen);
+
+//fprintf(stderr,"recvfrom %d len=%lu flags=%d al=%lu\n",
+//     fd, (unsigned long)len, flags, (unsigned long)*addrlen);
+
+    if (flags) { errno=ENOEXEC; return -1; }
+
+    char tbuf[ADDRPORTSTRLEN+1];
+
+    struct iovec iov[2];
+    iov[0].iov_base=tbuf;
+    iov[0].iov_len=sizeof(tbuf);
+    iov[1].iov_base=buf;
+    iov[1].iov_len=len;
+
+    struct msghdr m;
+    memset(&m,0,sizeof(m));
+    m.msg_iov=iov;
+    m.msg_iovlen=2;
+
+    ssize_t rr=recvmsg(fd,&m,0);
+    if (rr==-1) return rr;
+    if (rr<sizeof(tbuf)) { errno=ENXIO; return -1; }
+    if (tbuf[ADDRPORTSTRLEN]) { errno=E2BIG; return -1; }
+    if (str2addrport(tbuf,addr,addrlen)) {
+       fprintf(stderr, "recvfrom str2addrport `%s' %s\n",tbuf,
+               strerror(errno));
+       return -1;
+    }
+
+    rr -= sizeof(tbuf);
+//fprintf(stderr,"recvfrom %s %lu ok\n",tbuf,(unsigned long)rr);
+    return rr;
+}