| 1 | m4_dnl hcommon.c |
| 2 | m4_dnl (part of complex test harness, not of the library) |
| 3 | m4_dnl - routines used for both record and playback |
| 4 | |
| 5 | m4_dnl This file is part of adns, which is Copyright (C) 1997-1999 Ian Jackson |
| 6 | m4_dnl |
| 7 | m4_dnl This program is free software; you can redistribute it and/or modify |
| 8 | m4_dnl it under the terms of the GNU General Public License as published by |
| 9 | m4_dnl the Free Software Foundation; either version 2, or (at your option) |
| 10 | m4_dnl any later version. |
| 11 | m4_dnl |
| 12 | m4_dnl This program is distributed in the hope that it will be useful, |
| 13 | m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | m4_dnl GNU General Public License for more details. |
| 16 | m4_dnl |
| 17 | m4_dnl You should have received a copy of the GNU General Public License |
| 18 | m4_dnl along with this program; if not, write to the Free Software Foundation, |
| 19 | m4_dnl Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | |
| 21 | m4_include(hmacros.i4) |
| 22 | |
| 23 | #include <fcntl.h> |
| 24 | #include <string.h> |
| 25 | #include <errno.h> |
| 26 | |
| 27 | #include <sys/socket.h> |
| 28 | #include <netinet/in.h> |
| 29 | #include <arpa/inet.h> |
| 30 | |
| 31 | #include "harness.h" |
| 32 | #include "internal.h" |
| 33 | |
| 34 | vbuf vb; |
| 35 | FILE *Toutputfile= 0; |
| 36 | struct timeval currenttime; |
| 37 | |
| 38 | const struct Terrno Terrnos[]= { |
| 39 | { "EAGAIN", EAGAIN }, |
| 40 | { "EINPROGRESS", EINPROGRESS }, |
| 41 | { "EINTR", EINTR }, |
| 42 | { "EINVAL", EINVAL }, |
| 43 | { "EMSGSIZE", EMSGSIZE }, |
| 44 | { "ENOBUFS", ENOBUFS }, |
| 45 | { "ENOENT", ENOENT }, |
| 46 | { "ENOPROTOOPT", ENOPROTOOPT }, |
| 47 | { "ENOSPC", ENOSPC }, |
| 48 | { "EWOULDBLOCK", EWOULDBLOCK }, |
| 49 | { 0, 0 } |
| 50 | }; |
| 51 | |
| 52 | static vbuf vbw; |
| 53 | |
| 54 | int Hgettimeofday(struct timeval *tv, struct timezone *tz) { |
| 55 | Tmust("gettimeofday","tz",!tz); |
| 56 | *tv= currenttime; |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | int Hwritev(int fd, const struct iovec *vector, size_t count) { |
| 61 | size_t i; |
| 62 | |
| 63 | vbw.used= 0; |
| 64 | for (i=0; i<count; i++, vector++) { |
| 65 | if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem(); |
| 66 | } |
| 67 | return Hwrite(fd,vbw.buf,vbw.used); |
| 68 | } |
| 69 | |
| 70 | m4_define(`hm_syscall', ` |
| 71 | hm_create_proto_q |
| 72 | void Q$1(hm_args_massage($3,void)) { |
| 73 | |
| 74 | vb.used= 0; |
| 75 | Tvba("$1"); |
| 76 | m4_define(`hm_na',`') |
| 77 | m4_define(`hm_arg_nullptr',`') |
| 78 | m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);') |
| 79 | m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);') |
| 80 | m4_define(`hm_arg_timeval_in_rel_null', ` |
| 81 | if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec); |
| 82 | else Tvba(" $'`1=null");') |
| 83 | m4_define(`hm_arg_must', `') |
| 84 | m4_define(`hm_arg_socktype', ` |
| 85 | Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");') |
| 86 | m4_define(`hm_arg_ign', `') |
| 87 | m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);') |
| 88 | m4_define(`hm_arg_fcntl_cmd_arg', ` |
| 89 | if ($'`1 == F_SETFL) { |
| 90 | Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&..."); |
| 91 | } else if ($'`1 == F_GETFL) { |
| 92 | Tvba(" $'`1=F_GETFL"); |
| 93 | } else { |
| 94 | Tmust("$'`1","F_GETFL/F_SETFL",0); |
| 95 | }') |
| 96 | m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);') |
| 97 | m4_define(`hm_arg_bytes_in', `') |
| 98 | m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);') |
| 99 | m4_define(`hm_arg_addr_out', `Tvbf(" *$'`2=%d",$'`2);') |
| 100 | $3 |
| 101 | |
| 102 | hm_create_nothing |
| 103 | m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);') |
| 104 | $3 |
| 105 | |
| 106 | Q_vb(); |
| 107 | } |
| 108 | ') |
| 109 | |
| 110 | m4_include(`hsyscalls.i4') |
| 111 | |
| 112 | |
| 113 | void Tvbaddr(const struct sockaddr *addr, int len) { |
| 114 | const struct sockaddr_in *ai= (const struct sockaddr_in*)addr; |
| 115 | |
| 116 | assert(len==sizeof(struct sockaddr_in)); |
| 117 | assert(ai->sin_family==AF_INET); |
| 118 | Tvbf("%s:%u",inet_ntoa(ai->sin_addr),htons(ai->sin_port)); |
| 119 | } |
| 120 | |
| 121 | void Tvbbytes(const void *buf, int len) { |
| 122 | const byte *bp; |
| 123 | int i; |
| 124 | |
| 125 | if (!len) { Tvba(" empty"); return; } |
| 126 | for (i=0, bp=buf; i<len; i++, bp++) { |
| 127 | if (!(i&31)) Tvba("\n "); |
| 128 | else if (!(i&3)) Tvba(" "); |
| 129 | Tvbf("%02x",*bp); |
| 130 | } |
| 131 | Tvba("."); |
| 132 | } |
| 133 | |
| 134 | void Tvbfdset(int max, const fd_set *fds) { |
| 135 | int i; |
| 136 | const char *comma= ""; |
| 137 | |
| 138 | Tvba("["); |
| 139 | for (i=0; i<max; i++) { |
| 140 | if (!FD_ISSET(i,fds)) continue; |
| 141 | Tvba(comma); |
| 142 | Tvbf("%d",i); |
| 143 | comma= ","; |
| 144 | } |
| 145 | Tvba("]"); |
| 146 | } |
| 147 | |
| 148 | void Tvberrno(int e) { |
| 149 | const struct Terrno *te; |
| 150 | |
| 151 | for (te= Terrnos; te->n && te->v != e; te++); |
| 152 | if (te->n) Tvba(te->n); |
| 153 | else Tvbf("E#%d",e); |
| 154 | } |
| 155 | |
| 156 | void Tvba(const char *str) { |
| 157 | if (!adns__vbuf_appendstr(&vb,str)) Tnomem(); |
| 158 | } |
| 159 | |
| 160 | void Tvbvf(const char *fmt, va_list al) { |
| 161 | char buf[1000]; |
| 162 | buf[sizeof(buf)-2]= '\t'; |
| 163 | vsnprintf(buf,sizeof(buf),fmt,al); |
| 164 | assert(buf[sizeof(buf)-2] == '\t'); |
| 165 | |
| 166 | Tvba(buf); |
| 167 | } |
| 168 | |
| 169 | void Tvbf(const char *fmt, ...) { |
| 170 | va_list al; |
| 171 | va_start(al,fmt); |
| 172 | Tvbvf(fmt,al); |
| 173 | va_end(al); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | void Tmust(const char *call, const char *arg, int cond) { |
| 178 | if (cond) return; |
| 179 | fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg); |
| 180 | exit(-1); |
| 181 | } |
| 182 | |
| 183 | void Tfailed(const char *why) { |
| 184 | fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno)); |
| 185 | exit(-1); |
| 186 | } |
| 187 | |
| 188 | void Tnomem(void) { |
| 189 | Tfailed("unable to malloc/realloc"); |
| 190 | } |
| 191 | |
| 192 | void Toutputerr(void) { |
| 193 | Tfailed("write error on test harness output"); |
| 194 | } |