From: Ian Jackson Date: Sat, 12 Oct 2019 12:23:27 +0000 (+0100) Subject: test: udp-preload: Introduce sun_prep X-Git-Tag: v0.5.0~107 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=9a4923ae098b328dae5c8af99a2f638421290e21;hp=d9cb092a885e3aae2307ce9f2101ca23438d432a;p=secnet.git test: udp-preload: Introduce sun_prep No functional change. Signed-off-by: Ian Jackson --- diff --git a/test/udp-preload.c b/test/udp-preload.c index 653dfa5..3fb63b2 100644 --- a/test/udp-preload.c +++ b/test/udp-preload.c @@ -159,6 +159,22 @@ static int str2addrport(char *str, return 0; } +static char *sun_prep(struct sockaddr_un *sun) { + const char *dir=getenv("UDP_PRELOAD_DIR"); + if (!dir) { errno=ECHILD; return 0; } + + memset(sun,0,sizeof(*sun)); + sun->sun_family=AF_UNIX; + int dl = strlen(dir); + if (dl + 1 + ADDRPORTSTRLEN + 1 > sizeof(sun->sun_path)) { + errno=ENAMETOOLONG; return 0; + } + strcpy(sun->sun_path,dir); + char *p=sun->sun_path+dl; + *p++='/'; + return p; +} + WRAP(socket) { if (!((domain==AF_INET || domain==AF_INET6) && type==SOCK_DGRAM)) @@ -193,18 +209,8 @@ WRAP(close) { WRAP(bind) { fdinfo *ent=lookup(fd); if (!ent) return old_bind(fd,addr,addrlen); - const char *dir = getenv("UDP_PRELOAD_DIR"); - if (!dir) { errno=ECHILD; return -1; } struct sockaddr_un sun; - memset(&sun,0,sizeof(sun)); - sun.sun_family=AF_UNIX; - int dl = strlen(dir); - if (dl + 1 + ADDRPORTSTRLEN + 1 > sizeof(sun.sun_path)) { - errno=ENAMETOOLONG; return -1; - } - strcpy(sun.sun_path,dir); - char *p=sun.sun_path+dl; - *p++='/'; + char *p=sun_prep(&sun); if (addrport2str(p,addr,addrlen)) return -1; //fprintf(stderr,"binding %s\n",sun.sun_path); if (unlink(sun.sun_path) && errno!=ENOENT) return -1;