chiark / gitweb /
test: udp-preload: Remove now-obsolete `bound'
[secnet.git] / test / udp-preload.c
1 /*
2  *  libauthbind.c - bind(2)-redirector library for authbind
3  *
4  *  authbind is Copyright (C) 1998 Ian Jackson
5  * 
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  * 
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  * 
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software Foundation,
18  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
19  * 
20  */
21
22 #define _GNU_SOURCE
23
24 #include <dlfcn.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <sys/socket.h>
33 #include <sys/wait.h>
34 #include <netinet/in.h>
35
36 #include <sys/socket.h>
37 #include <sys/un.h>
38 #include <arpa/inet.h>
39
40 #define STDERRSTR_CONST(m) write(2,m,sizeof(m)-1)
41 #define STDERRSTR_STRING(m) write(2,m,strlen(m))
42
43 typedef void anyfn_type(void);
44
45 static anyfn_type *find_any(const char *name) {
46   static const char *dlerr;
47   anyfn_type *kv;
48
49   kv= dlsym(RTLD_NEXT,name); if (kv) return kv;
50   dlerr= dlerror(); if (!dlerr) dlerr= "dlsym() failed for no reason";
51   STDERRSTR_CONST("libauthbind: error finding original version of ");
52   STDERRSTR_STRING(name);
53   STDERRSTR_CONST(": ");
54   STDERRSTR_STRING(dlerr);
55   STDERRSTR_STRING("\n");
56   errno= ENOSYS;
57   return 0;
58 }
59
60 #define socket_args int domain, int type, int protocol
61 #define bind_args   int fd, const struct sockaddr *addr, socklen_t addrlen
62 #define WRAPS(X)                                \
63     X(socket, (domain,type,protocol))           \
64     X(bind,   (fd,addr,addrlen))
65
66 #define DEF_OLD(fn,args)                                \
67   typedef int fn##_fn_type(fn##_args);                  \
68   static int find_##fn(fn##_args);                      \
69   static fn##_fn_type find_##fn, *old_##fn=find_##fn;   \
70   static int find_##fn(fn##_args) {                     \
71     anyfn_type *anyfn;                                  \
72     anyfn= find_any(#fn); if (!anyfn) return -1;        \
73     old_##fn= (fn##_fn_type*)anyfn;                     \
74     return old_##fn args;                               \
75   }
76
77 WRAPS(DEF_OLD)
78
79 #define WRAP(fn) int fn(fn##_args)
80
81 typedef struct{
82     int af;
83 } fdinfo;
84 static fdinfo **table;
85 static int tablesz;
86
87 static fdinfo *lookup(int fd) {
88     if (fd>=tablesz) return 0;
89     return table[fd];
90 }
91
92 #define ADDRPORTSTRLEN (INET6_ADDRSTRLEN+1+5) /* not including nul */
93
94 static int addrport2str(char buf[ADDRPORTSTRLEN+1],
95                         const struct sockaddr *addr, socklen_t addrlen) {
96     const void *addrv=addr;
97     const void *iav;
98     const struct sockaddr_in  *sin;
99     const struct sockaddr_in6 *sin6;
100     uint16_t port;
101     socklen_t el;
102     switch (addr->sa_family) {
103     case AF_INET:  sin =addrv; el=sizeof(*sin ); iav=&sin ->sin_addr ; port=sin ->sin_port ; break;
104     case AF_INET6: sin6=addrv; el=sizeof(*sin6); iav=&sin6->sin6_addr; port=sin6->sin6_port; break;
105     default: errno=ESRCH; return -1;
106     }
107 //fprintf(stderr,"af=%lu el=%lu addrlen=%lu\n",
108 //      (unsigned long)addr->sa_family,
109 //      (unsigned long)el,
110 //      (unsigned long)addrlen);
111     if (addrlen!=el) { errno=EINVAL; return -1; }
112     char *p=buf;
113     if (!inet_ntop(addr->sa_family,iav,p,INET6_ADDRSTRLEN)) return -1;
114     p+=strlen(p);
115     sprintf(p,",%u",(unsigned)ntohs(port));
116     return 0;
117 }
118
119 WRAP(socket) {
120     if (!((domain==AF_INET || domain==AF_INET6) &&
121           type==SOCK_DGRAM))
122         return old_socket(domain,type,protocol);
123     int fd=socket(AF_UNIX,SOCK_DGRAM,0);
124     if (fd<0) return fd;
125     if (fd>=tablesz) {
126         int newsz=(fd+1)*2;
127         table=realloc(table,newsz*sizeof(*table));
128         if (!table) goto fail;
129         while (tablesz<newsz) table[tablesz++]=0;
130     }
131     free(table[fd]);
132     table[fd]=malloc(sizeof(*table[fd]));
133     if (!table[fd]) goto fail;
134     table[fd]->af=domain;
135     return fd;
136
137  fail:
138     close(fd);
139     return -1;
140 }
141
142 WRAP(bind) {
143     fdinfo *ent=lookup(fd);
144     if (!ent) return old_bind(fd,addr,addrlen);
145     const char *dir = getenv("UDP_PRELOAD_DIR");
146     if (!dir) { errno=ECHILD; return -1; }
147     struct sockaddr_un sun;
148     memset(&sun,0,sizeof(sun));
149     sun.sun_family=AF_UNIX;
150     int dl = strlen(dir);
151     if (dl + 1 + ADDRPORTSTRLEN + 1 > sizeof(sun.sun_path)) {
152         errno=ENAMETOOLONG; return -1;
153     }
154     strcpy(sun.sun_path,dir);
155     char *p=sun.sun_path+dl;
156     *p++='/';
157     if (addrport2str(p,addr,addrlen)) return -1;
158 //fprintf(stderr,"binding %s\n",sun.sun_path);
159     if (unlink(sun.sun_path) && errno!=ENOENT) return -1;
160     return old_bind(fd,(const void*)&sun,sizeof(sun));
161 }
162
163 //udp (test/tmp/outside.conf:19): setsockopt(,IPV6_V6ONLY,&1,): Operation not supported