/* This file is in the public domain. */ #include #include #include #include #include #include #include #include extern const char *__progname; static const char *host = 0; static const char *port = 0; static int restac; static char **restav; static int net; static void handleargs(int ac, char **av) { int skip; int errs; skip = 0; errs = 0; for (ac--,av++;ac;ac--,av++) { if (skip > 0) { skip --; continue; } if (**av != '-') { if (! host) { host = *av; } else if (! port) { port = *av; } else { restac = ac; restav = av; break; } continue; } if (0) { needarg:; fprintf(stderr,"%s: %s needs a following argument\n",__progname,*av); errs = 1; continue; } #define WANTARG() do { if (++skip >= ac) goto needarg; } while (0) if (!strcmp(*av,"-h")) { WANTARG(); host = av[skip]; continue; } if (!strcmp(*av,"-p")) { WANTARG(); host = av[skip]; continue; } if (!strcmp(*av,"--")) { restac = ac - 1; restav = av + 1; break; } #undef WANTARG fprintf(stderr,"%s: unrecognized option `%s'\n",__progname,*av); errs = 1; } if (restac % 3) { fprintf(stderr,"%s: wrong number of arguments (must be triples)\n",__progname); errs = 1; } if (errs) exit(1); } static void open_net(void) { struct addrinfo hints; struct addrinfo *ai0; struct addrinfo *ai; int e; char ihost[NI_MAXHOST]; char iserv[NI_MAXSERV]; const char *emsg; hints.ai_flags = 0; hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = 0; hints.ai_addrlen = 0; hints.ai_addr = 0; hints.ai_canonname = 0; hints.ai_next = 0; e = getaddrinfo(host,port,&hints,&ai0); if (e) { fprintf(stderr,"%s: %s%s%s: %s\n",__progname,host?:"",(host&&port)?"/":"",port?:"",gai_strerror(e)); exit(1); } for (ai=ai0;ai;ai=ai->ai_next) { net = socket(ai->ai_family,ai->ai_socktype,ai->ai_protocol); if (net < 0) { fprintf(stderr,"%s: socket (af %d): %s\n",__progname,ai->ai_family,strerror(errno)); continue; } if (connect(net,ai->ai_addr,ai->ai_addrlen) >= 0) return; emsg = strerror(errno); e = getnameinfo(ai->ai_addr,ai->ai_addrlen,&ihost[0],sizeof(ihost),&iserv[0],sizeof(iserv),NI_NUMERICHOST|NI_NUMERICSERV); if (e) { fprintf(stderr,"%s: connect [can't get numeric address: %s]: %s",__progname,gai_strerror(e),emsg); } else { fprintf(stderr,"%s: connect [%s/%s]: %s",__progname,&ihost[0],&iserv[0],emsg); } close(net); } fprintf(stderr,"%s: can't open network connection\n",__progname); exit(1); } static void protocol(void) { struct timeval tv; unsigned long long int tns; int ax; FILE *f; char *b; int a; int l; int c; int errs; f = fdopen(net,"r+"); fprintf(f,"new-data%c",0); b = 0; a = 0; errs = 0; gettimeofday(&tv,0); tns = (tv.tv_sec * 1000000000ULL) + tv.tv_usec; for (ax=0;ax= a) b = realloc(b,a=l+8); b[l++] = c; if (c == '\0') break; } if (b[0]) { fprintf(stderr,"%s: %s %s %s: %s\n",__progname,restav[ax],restav[ax+1],restav[ax+2],b); errs = 1; } } putc(0,f); fflush(f); if (errs) exit(1); } int main(int, char **); int main(int ac, char **av) { handleargs(ac,av); open_net(); protocol(); return(0); }