Beispiel #1
0
/* fill database form filesystem */
void fill_db()
{
  // XXX: Fixme
  buffer_putsflush(buffer_2, ARGV0 "reading directory database\n");
  traversedirhier("/var/service/ddnsd/root/dot", readfileintodb);
  flagchanged++;
  flagdumpasap++;
  buffer_putsflush(buffer_2, ARGV0 "done\n");
}
Beispiel #2
0
void dodump()
{
  stralloc dummy = {0};

  buffer_putsflush(buffer_2, ARGV0 "dumping\n");

  write_fifodir("snapdir", &dummy, snap_dump);

  buffer_putsflush(buffer_2, ARGV0 "dumping ready\n");
}
Beispiel #3
0
int main (int argc, char const *const *argv)
{
  stralloc sa = STRALLOC_ZERO ;
  for (;;)
  {
    struct tm tm ;
    uint64 tt ;
    time_t t ;
    char *p ;
    int r ;
    char fmt[UINT64_FMT] ;
    sa.len = 0 ;
    r = skagetln(buffer_0, &sa, '\n') ;
    if (r < 0) strerr_diefu1sys(111, "read from stdin") ;
    if (!r) break ;
    sa.s[sa.len-1] = 0 ;
    if (!strptime(sa.s, "+%Y-%m-%d", &tm)) continue ;
    tm.tm_sec = 59 ;
    tm.tm_min = 59 ;
    tm.tm_hour = 23 ;
    t = mktime(&tm) ;
    if (t < 0) strerr_diefu1sys(111, "mktime") ;
    tt = t + 10 ;
    add_leapsecs(&tt) ;
    if (!genalloc_append(uint64, &table, &tt))
      strerr_diefu1sys(111, "genalloc_append") ;
    fmt[uint64_fmt(fmt, tt)] = 0 ;
    buffer_puts(buffer_1, "  TAI_MAGIC + ") ;
    buffer_puts(buffer_1, fmt) ;
    buffer_puts(buffer_1, ",\n") ;
  }
  buffer_unput(buffer_1, 2) ;
  buffer_putsflush(buffer_1, "\n") ;
  return 0 ;
}
Beispiel #4
0
void doit()
{
  char ch;
  int match = 1;
  long linenum = 0;
  stralloc fifoline = { 0 };

  linenum = 0;

  /* try duming data in 23 seconds */
  alarm(dumpfreq);

  buffer_putsflush(buffer_2, ARGV0 "entering main loop\n");

  /* forever read from pipe line by line and handle it */
  while(1)
    {
      while(match) 
	{
	  if(flagdumpasap == 1) 
	    dumpcheck(flagdumpasap, flagchanged, flagchildrunning, flagsighup);

	  ++linenum;
	  if(buffer_get(&wr, &ch, 1) == 1)
	    {
	      if(getln(&wr, &fifoline, &match, '\n') == -1)
		continue;
	      buffer_put(buffer_2, &ch, 1);
	      buffer_putflush(buffer_2, fifoline.s, fifoline.len);
	      handle_line(&fifoline, ch);
	    }
	}
    }
}
Beispiel #5
0
int main()
{
  char *x;
  int udp53;

  x = env_get("IP");
  if (!x)
    strerr_die2x(111,fatal,"$IP not set");
  if (!ip4_scan(x,ip))
    strerr_die3x(111,fatal,"unable to parse IP address ",x);

  udp53 = socket_udp();
  if (udp53 == -1)
    strerr_die2sys(111,fatal,"unable to create UDP socket: ");
  if (socket_bind4_reuse(udp53,ip,53) == -1)
    strerr_die2sys(111,fatal,"unable to bind UDP socket: ");

  droproot(fatal);

  initialize();
  
  ndelay_off(udp53);
  socket_tryreservein(udp53,65536);

  buffer_putsflush(buffer_2,starting);

  for (;;) {
    len = socket_recv4(udp53,buf,sizeof buf,ip,&port);
    if (len < 0) continue;
    if (!doit()) continue;
    if (response_len > 512) response_tc();
    socket_send4(udp53,response,response_len,ip,port);
    /* may block for buffer space; if it fails, too bad */
  }
}
Beispiel #6
0
void unurl(const char* s) {
  char* buf;
  unsigned long len;
  buf=alloca(strlen(s)+1);
  if (s[scan_urlencoded(s,buf,&len)]) {
    buffer_putsflush(buffer_2,"parse error!\n");
    return;
  }
  buf[len]=0;
  if (strcmp(s,buf)) {
    buffer_puts(buffer_1,"renaming \"");
    buffer_puts(buffer_1,s);
    buffer_puts(buffer_1,"\" to \"");
    buffer_puts(buffer_1,buf);
    buffer_putsflush(buffer_1,"\"...");
    if (rename(s,buf)==-1)
      buffer_putsflush(buffer_1," failed!\n");
    else
      buffer_putsflush(buffer_1,"\n");
  }
}
Beispiel #7
0
int main() {
  stralloc sa;
  int res;

  stralloc_init(&sa);
  res=buffer_get_token_sa_pred(buffer_0,&sa,ishttp);
  buffer_puts(buffer_1,"buffer_get_token_sa_pred returned ");
  buffer_putlong(buffer_1,res);
  buffer_putsflush(buffer_1,".\n\n");
  buffer_putsa(buffer_1,&sa);
  buffer_flush(buffer_1);
  return 0;
}
Beispiel #8
0
void
slicd_die_version (void)
{
    buffer_puts (buffer_1small, PROG);
    buffer_puts (buffer_1small, " v" SLICD_VERSION "\n");
    buffer_putsflush (buffer_1small,
            "Copyright (C) 2016 Olivier Brunel - http://jjacky.com/slicd\n"
            "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
            "This is free software: you are free to change and redistribute it.\n"
            "There is NO WARRANTY, to the extent permitted by law.\n"
            );
    _exit (0);
}
Beispiel #9
0
void smtpd()
{
  if (!env_get("PROTO")) {
    if (!env_puts("RELAYCLIENT=")) nomem();
    if (!env_puts("DATABYTES=0")) nomem();
    if (!env_puts("PROTO=TCP")) nomem();
    if (!env_puts("TCPLOCALIP=127.0.0.1")) nomem();
    if (!env_puts("TCPLOCALHOST=localhost")) nomem();
    if (!env_puts("TCPREMOTEIP=127.0.0.1")) nomem();
    if (!env_puts("TCPREMOTEHOST=localhost")) nomem();
    if (!env_puts("TCPREMOTEINFO=sendmail-bs")) nomem();
  }
  execv(*smtpdarg,smtpdarg);
  buffer_putsflush(buffer_2,"sendmail: fatal: unable to run qmail-smtpd\n");
  _exit(111);
}
Beispiel #10
0
int main() {
  stralloc sa;
  buffer b;
  stralloc line;

  stralloc_init(&sa);
  stralloc_init(&line);
  stralloc_copys(&sa,"this is a test\nline 2\n");
  buffer_fromsa(&b,&sa);

  while (buffer_getline_sa(&b,&line)==0) {
    buffer_puts(buffer_1,"got line: \"");
    if (stralloc_chop(&line)!='\n') break;
    buffer_putsa(buffer_1,&line);
    buffer_putsflush(buffer_1,"\"\n");
    stralloc_copys(&line,"");
  }
  return 0;
}
Beispiel #11
0
void dump_db_to_cdb()
{
  char *z;

  z = "0";
  *z =+ flagchanged;
  buffer_puts(buffer_2, z);
  buffer_puts(buffer_2, " ");
  
  z = "0";
  *z =+ flagchildrunning;
  buffer_puts(buffer_2, z);
  buffer_putsflush(buffer_2, " checking if a dump is needed\n");

  if(flagsighup)
    {
      flagsighup = 0;
      buffer_putsflush(buffer_2, ARGV0 "SIGHUP recived, dumping withouth further asking\n");
    }
  if(flagchanged && !flagchildrunning)
    {
      flagchanged = 0;
      flagchildrunning++;

      buffer_putsflush(buffer_2, ARGV0 "yep, forking\n");

      /* fork of a child to do this */
      switch(fork()) 
	{
	case 0:
	  /* this is the child */
	  /* XXX close fifos? */
	  sig_ignore(sig_alarm);
	  sig_ignore(sig_hangup);
	  buffer_putsflush(buffer_2, ARGV0 "child started\n");
	  dodump();
	  buffer_putsflush(buffer_2, ARGV0 "child exiting\n");
	  _exit(0);
	case -1:
	  strerr_warn2(ARGV0, "unable to fork: ", &strerr_sys);
	  break;
	}
     
      /* this is the parent */
      flagdumpasap = 0;
      buffer_putsflush(buffer_2, ARGV0 "parent\n");
    } 
}
Beispiel #12
0
int main() {
  int64 pfd[2];
  char buf[20480];
  unsigned int i;
  if (!io_pipe(pfd)) return 111;
  io_nonblock(pfd[1]);
  if (!io_fd(pfd[1])) return 111;
  switch (fork()) {
  case -1: return 111;
  case 0: /* child */
	   io_close(pfd[1]);
	   sleep(1);
	   for (;;) {
	    switch (io_waitread(pfd[0],buf,sizeof buf)) {
	    case -1: buffer_putsflush(buffer_2,"io_waitread returned -1!\n"); return 111;
	    case -3: buffer_puts(buffer_2,"io_waitread: ");
		      buffer_puterror(buffer_2);
		      buffer_putnlflush(buffer_2);
		      return 111;
	    case 0: io_close(pfd[0]);
		    return 0;
	    }
	   }
  }
  io_close(pfd[0]);
  for (i=0; i<sizeof(buf); ++i) buf[i]="abcdefghihjklmnopqrstuvwxyz"[i%26];
  for (i=0; i<1000; ++i) {
    int64 r;
    if ((r=io_waitwrite(pfd[1],buf,sizeof buf))!=sizeof buf) {
      buffer_puts(buffer_2,"io_waitwrite returned ");
      buffer_putlonglong(buffer_2,r);
      buffer_putnlflush(buffer_2);
    }
  }
  return 0;
}
void doit(int t) {
  int fakev4=0;
  int j;
  SSL *ssl;
  int wstat;
  uint32 scope_id;
  int sslctl[2];
  char *s;
  unsigned long tmp_long;
  char sslctl_cmd;
  stralloc ssl_env = { 0 };
  buffer ssl_env_buf;

  if (pipe(pi) == -1) strerr_die2sys(111,DROP,"unable to create pipe: ");
  if (pipe(po) == -1) strerr_die2sys(111,DROP,"unable to create pipe: ");
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sslctl) == -1) strerr_die2sys(111,DROP,"unable to create socketpair: ");

  switch(fork()) {
    case -1:
      strerr_die2sys(111,DROP,"unable to fork: ");
    case 0:
      /* Child */
      break;
    default:
      /* Parent */

      close(pi[0]); close(po[1]); close(sslctl[1]);

      if ((s=env_get("SSL_CHROOT")))
        if (chroot(s) == -1)
          strerr_die2x(111,DROPSSL,"unable to chroot");

      if ((s=env_get("SSL_GID"))) {
        scan_ulong(s,&tmp_long);
        gid = tmp_long;
      }
      if (gid) if (prot_gid(gid) == -1) strerr_die2sys(111,DROPSSL,"unable to set gid: ");

      if ((s=env_get("SSL_UID"))) {
        scan_ulong(s,&tmp_long);
        uid = tmp_long;
      }
      if (uid) if (prot_uid(uid) == -1)
        strerr_die2sys(111,DROPSSL,"unable to set uid: ");

      /* This will exit on a fatal error or if the client quits
       * without activating SSL
       */
      sslctl_cmd = ucspitls_master_wait_for_activation(sslctl[0]);

      /* If we got here, SSL must have been activated */
      ssl = ssl_new(ctx,t);
      if (!ssl) strerr_die2x(111,DROP,"unable to create SSL instance");
      if (ndelay_on(t) == -1)
        strerr_die2sys(111,DROP,"unable to set socket options: ");
      if (ssl_timeoutaccept(ssl,ssltimeout) == -1)
        strerr_die3x(111,DROP,"unable to accept SSL: ",ssl_error_str(ssl_errno));

      if (verbosity >= 2) {
        strnum[fmt_ulong(strnum,getpid())] = 0;
        strerr_warn3("sslserver: ssl ",strnum," accept ",0);
      }

      if (flagclientcert) {
        switch(ssl_verify(ssl,verifyhost)) {
          case -1:
            strerr_die2x(111,DROP,"unable to verify client certificate");
          case -2:
            strerr_die2x(111,DROP,"no client certificate");
          case -3:
            strerr_die2x(111,DROP,"client name does not match certificate");
          default: break;
        }
      }

      if (sslctl_cmd == 'Y') {
        ssl_server_env(ssl, &ssl_env);
        stralloc_0(&ssl_env); /* Add another NUL */

        buffer_init(&ssl_env_buf,buffer_unixwrite,sslctl[0],NULL,0);
        if (buffer_putflush(&ssl_env_buf, ssl_env.s, ssl_env.len) == -1) {
          strerr_die2sys(111, FATAL, "unable to write SSL environment: ");
        }
      } else if (sslctl_cmd != 'y') {
        strerr_die2x(111,DROP,"Protocol error on SSL control descriptor: invalid command character read");
      }

      if (close(sslctl[0]) != 0) {
        strerr_die2sys(111, DROP, "Error closing SSL control socket: ");
      }

      if (ssl_io(ssl,pi[1],po[0],io_opt) != 0)
        strerr_die3x(111,DROP,"unable to speak SSL: ",ssl_error_str(ssl_errno));
      if (wait_nohang(&wstat) > 0)
        _exit(wait_exitcode(wstat));
      ssl_close(ssl);
      _exit(0);
  }

  /* Child-only below this point */
  if (close(sslctl[0]) != 0) { 
    strerr_die2sys(111, DROP, "Error closing SSL control socket: ");
  }

  if (!forcev6 && ip6_isv4mapped(remoteip))
    fakev4=1;
  if (fakev4)
    remoteipstr[ip4_fmt(remoteipstr,remoteip+12)] = 0;
  else
    remoteipstr[ip6_fmt(remoteipstr,remoteip)] = 0;

  if (verbosity >= 2) {
    strnum[fmt_ulong(strnum,getpid())] = 0;
    strerr_warn4("sslserver: pid ",strnum," from ",remoteipstr,0);
  }

  if (socket_local6(t,localip,&localport,&scope_id) == -1)
    strerr_die2sys(111,DROP,"unable to get local address: ");

  if (fakev4)
    localipstr[ip4_fmt(localipstr,localip+12)] = 0;
  else
    localipstr[ip6_fmt(localipstr,localip)] = 0;
  remoteportstr[fmt_ulong(remoteportstr,remoteport)] = 0;

  if (!localhost)
    if (dns_name6(&localhostsa,localip) == 0)
      if (localhostsa.len) {
	if (!stralloc_0(&localhostsa)) drop_nomem();
	localhost = localhostsa.s;
      }
  env("PROTO",fakev4?"SSL":"SSL6");
  env("SSLLOCALIP",localipstr);
  env("SSL6LOCALIP",localipstr);
  env("SSLLOCALPORT",localportstr);
  env("SSL6LOCALPORT",localportstr);
  env("SSLLOCALHOST",localhost);
  env("SSL6LOCALHOST",localhost);
  if (!fakev4 && scope_id)
    env("SSL6INTERFACE",socket_getifname(scope_id));

  if (flagtcpenv) {
    env("TCPLOCALIP",localipstr);
    env("TCP6LOCALIP",localipstr);
    env("TCPLOCALPORT",localportstr);
    env("TCP6LOCALPORT",localportstr);
    env("TCPLOCALHOST",localhost);
    env("TCP6LOCALHOST",localhost);
    if (!fakev4 && scope_id)
      env("TCP6INTERFACE",socket_getifname(scope_id));
  }

  if (flagremotehost)
    if (dns_name6(&remotehostsa,remoteip) == 0)
      if (remotehostsa.len) {
	if (flagparanoid) {
	  verifyhost = remoteipstr;
	  if (dns_ip6(&tmp,&remotehostsa) == 0)
	    for (j = 0;j + 16 <= tmp.len;j += 16)
	      if (byte_equal(remoteip,16,tmp.s + j)) {
		flagparanoid = 0;
		break;
	      }
	  }
	if (!flagparanoid) {
	  if (!stralloc_0(&remotehostsa)) drop_nomem();
	  remotehost = remotehostsa.s;
	  verifyhost = remotehostsa.s;
	}
      }
  env("SSLREMOTEIP",remoteipstr);
  env("SSL6REMOTEIP",remoteipstr);
  remoteipstr[ip6_fmt(remoteipstr,remoteip)]=0;
  env("SSLREMOTEPORT",remoteportstr);
  env("SSL6REMOTEPORT",remoteportstr);
  env("SSLREMOTEHOST",remotehost);
  env("SSL6REMOTEHOST",remotehost);
  if (flagtcpenv) {
    env("TCPREMOTEIP",remoteipstr);
    env("TCP6REMOTEIP",remoteipstr);
    env("TCPREMOTEPORT",remoteportstr);
    env("TCP6REMOTEPORT",remoteportstr);
    env("TCPREMOTEHOST",remotehost);
    env("TCP6REMOTEHOST",remotehost);
  }

  if (flagremoteinfo) {
    if (remoteinfo6(&tcpremoteinfo,remoteip,remoteport,localip,localport,timeout,netif) == -1)
      flagremoteinfo = 0;
    if (!stralloc_0(&tcpremoteinfo)) drop_nomem();
  }
  env("SSLREMOTEINFO",flagremoteinfo ? tcpremoteinfo.s : 0);
  env("SSL6REMOTEINFO",flagremoteinfo ? tcpremoteinfo.s : 0);
  if (flagtcpenv) {
    env("TCPREMOTEINFO",flagremoteinfo ? tcpremoteinfo.s : 0);
    env("TCP6REMOTEINFO",flagremoteinfo ? tcpremoteinfo.s : 0);
  }

  if (fnrules) {
    int fdrules;
    fdrules = open_read(fnrules);
    if (fdrules == -1) {
      if (errno != error_noent) drop_rules();
      if (!flagallownorules) drop_rules();
    }
    else {
      int fakev4=0;
      char* temp;
      if (!forcev6 && ip6_isv4mapped(remoteip))
	fakev4=1;
      if (fakev4)
	temp=remoteipstr+7;
      else
	temp=remoteipstr;
      if (rules(found,fdrules,temp,remotehost,flagremoteinfo ? tcpremoteinfo.s : 0) == -1) drop_rules();
      close(fdrules);
    }
  }

  if (verbosity >= 2) {
    strnum[fmt_ulong(strnum,getpid())] = 0;
    if (!stralloc_copys(&tmp,"sslserver: ")) drop_nomem();
    safecats(flagdeny ? "deny" : "ok");
    cats(" "); safecats(strnum);
    cats(" "); if (localhost) safecats(localhost);
    cats(":"); safecats(localipstr);
    cats(":"); safecats(localportstr);
    cats(" "); if (remotehost) safecats(remotehost);
    cats(":"); safecats(remoteipstr);
    cats(":"); if (flagremoteinfo) safecats(tcpremoteinfo.s);
    cats(":"); safecats(remoteportstr);
    cats("\n");
    buffer_putflush(buffer_2,tmp.s,tmp.len);
  }

  if (flagdeny) _exit(100);

  if (gid) if (prot_gid(gid) == -1)
    strerr_die2sys(111,FATAL,"unable to set gid: ");
  if (uid) if (prot_uid(uid) == -1)
    strerr_die2sys(111,FATAL,"unable to set uid: ");

  close(pi[1]); close(po[0]);

  sig_uncatch(sig_child);
  sig_unblock(sig_child);
  sig_uncatch(sig_term);
  sig_uncatch(sig_pipe);

  if (fcntl(sslctl[1],F_SETFD,0) == -1)
    strerr_die2sys(111,FATAL,"unable to clear close-on-exec flag");
  strnum[fmt_ulong(strnum,sslctl[1])]=0;
  setenv("SSLCTLFD",strnum,1);

  if (fcntl(pi[0],F_SETFD,0) == -1)
    strerr_die2sys(111,FATAL,"unable to clear close-on-exec flag");
  strnum[fmt_ulong(strnum,pi[0])]=0;
  setenv("SSLREADFD",strnum,1);

  if (fcntl(po[1],F_SETFD,0) == -1)
    strerr_die2sys(111,FATAL,"unable to clear close-on-exec flag");
  strnum[fmt_ulong(strnum,po[1])]=0;
  setenv("SSLWRITEFD",strnum,1);
  
  if (flagsslwait) {
    if (fd_copy(0,t) == -1)
      strerr_die2sys(111,DROP,"unable to set up descriptor 0: ");
    if (fd_copy(1,t) == -1)
      strerr_die2sys(111,DROP,"unable to set up descriptor 1: ");
  } else {
    if (fd_move(0,pi[0]) == -1)
      strerr_die2sys(111,DROP,"unable to set up descriptor 0: ");
    if (fd_move(1,po[1]) == -1)
      strerr_die2sys(111,DROP,"unable to set up descriptor 1: ");
  }

  if (flagkillopts)
    socket_ipoptionskill(t);
  if (!flagdelay)
    socket_tcpnodelay(t);

  if (*banner) {
    buffer_init(&b,buffer_unixwrite,1,bspace,sizeof bspace);
    if (buffer_putsflush(&b,banner) == -1)
      strerr_die2sys(111,DROP,"unable to print banner: ");
  }

  if (!flagsslwait) {
    strnum[fmt_ulong(strnum,flagsslenv)] = 0;
    strerr_warn2("flagsslenv: ", strnum, 0);
    ucspitls(flagsslenv,0,1);
  }

  pathexec(prog);
  strerr_die4sys(111,DROP,"unable to run ",*prog,": ");
}
Beispiel #14
0
void server(int argc,const char * const *argv) {
  char *x;

  buffer_puts(buffer_1,"\nPROTO=");
  x = env_get("PROTO");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSLLOCALHOST=");
  x = env_get("SSLLOCALHOST");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSLLOCALIP=");
  x = env_get("SSLLOCALIP"); 
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSLLOCALPORT=");
  x = env_get("SSLLOCALPORT"); 
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSLREMOTEHOST=");
  x = env_get("SSLREMOTEHOST"); 
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSLREMOTEIP=");
  x = env_get("SSLREMOTEIP"); 
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSLREMOTEPORT=");
  x = env_get("SSLREMOTEPORT"); 
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSLREMOTEINFO=");
  x = env_get("SSLREMOTEINFO"); 
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nTCPLOCALHOST=");
  x = env_get("TCPLOCALHOST");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nTCPLOCALIP=");
  x = env_get("TCPLOCALIP"); 
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nTCPLOCALPORT=");
  x = env_get("TCPLOCALPORT"); 
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nTCPREMOTEHOST=");
  x = env_get("TCPREMOTEHOST"); 
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nTCPREMOTEIP=");
  x = env_get("TCPREMOTEIP"); 
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nTCPREMOTEPORT=");
  x = env_get("TCPREMOTEPORT"); 
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nTCPREMOTEINFO=");
  x = env_get("TCPREMOTEINFO"); 
  buffer_puts(buffer_1,x ? x : "unset");


  buffer_puts(buffer_1,"\nTCP6REMOTEHOST=");
  x = env_get("TCP6REMOTEHOST");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nTCP6REMOTEIP=");
  x = env_get("TCP6REMOTEIP");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nTCP6REMOTEPORT=");
  x = env_get("TCP6REMOTEPORT");
  buffer_puts(buffer_1,x ? x : "unset");


  buffer_puts(buffer_1,"\nSSL_PROTOCOL=");
  x = env_get("SSL_PROTOCOL");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SESSION_ID=");
  x = env_get("SSL_SESSION_ID");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CIPHER=");
  x = env_get("SSL_CIPHER");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CIPHER_EXPORT=");
  x = env_get("SSL_CIPHER_EXPORT");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CIPHER_USEKEYSIZE=");
  x = env_get("SSL_CIPHER_USEKEYSIZE");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CIPHER_ALGKEYSIZE=");
  x = env_get("SSL_CIPHER_ALGKEYSIZE");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_VERSION_INTERFACE=");
  x = env_get("SSL_VERSION_INTERFACE");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_VERSION_LIBRARY=");
  x = env_get("SSL_VERSION_LIBRARY");
  buffer_puts(buffer_1,x ? x : "unset");


  buffer_puts(buffer_1,"\nSSL_SERVER_M_VERSION=");
  x = env_get("SSL_SERVER_M_VERSION");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_M_SERIAL=");
  x = env_get("SSL_SERVER_M_SERIAL");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN=");
  x = env_get("SSL_SERVER_S_DN");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_C=");
  x = env_get("SSL_SERVER_S_DN_C");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_ST=");
  x = env_get("SSL_SERVER_S_DN_ST");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_L=");
  x = env_get("SSL_SERVER_S_DN_L");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_O=");
  x = env_get("SSL_SERVER_S_DN_O");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_OU=");
  x = env_get("SSL_SERVER_S_DN_OU");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_CN=");
  x = env_get("SSL_SERVER_S_DN_CN");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_T=");
  x = env_get("SSL_SERVER_S_DN_T");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_I=");
  x = env_get("SSL_SERVER_S_DN_I");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_G=");
  x = env_get("SSL_SERVER_S_DN_G");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_S=");
  x = env_get("SSL_SERVER_S_DN_S");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_D=");
  x = env_get("SSL_SERVER_S_DN_D");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_UID=");
  x = env_get("SSL_SERVER_S_DN_UID");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_S_DN_Email=");
  x = env_get("SSL_SERVER_S_DN_Email");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN=");
  x = env_get("SSL_SERVER_I_DN");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_C=");
  x = env_get("SSL_SERVER_I_DN_C");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_ST=");
  x = env_get("SSL_SERVER_I_DN_ST");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_L=");
  x = env_get("SSL_SERVER_I_DN_L");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_O=");
  x = env_get("SSL_SERVER_I_DN_O");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_OU=");
  x = env_get("SSL_SERVER_I_DN_OU");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_CN=");
  x = env_get("SSL_SERVER_I_DN_CN");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_T=");
  x = env_get("SSL_SERVER_I_DN_T");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_I=");
  x = env_get("SSL_SERVER_I_DN_I");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_G=");
  x = env_get("SSL_SERVER_I_DN_G");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_S=");
  x = env_get("SSL_SERVER_I_DN_S");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_D=");
  x = env_get("SSL_SERVER_I_DN_D");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_UID=");
  x = env_get("SSL_SERVER_I_DN_UID");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_I_DN_Email=");
  x = env_get("SSL_SERVER_I_DN_Email");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_V_START=");
  x = env_get("SSL_SERVER_V_START");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_V_END=");
  x = env_get("SSL_SERVER_V_END");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_A_SIG=");
  x = env_get("SSL_SERVER_A_SIG");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_A_KEY=");
  x = env_get("SSL_SERVER_A_KEY");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_CERT=");
  x = env_get("SSL_SERVER_CERT");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_DIGEST_SHA1=");
  x = env_get("SSL_SERVER_DIGEST_SHA1");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_SERVER_DIGEST_SHA256=");
  x = env_get("SSL_SERVER_DIGEST_SHA256");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_M_VERSION=");
  x = env_get("SSL_CLIENT_M_VERSION");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_M_SERIAL=");
  x = env_get("SSL_CLIENT_M_SERIAL");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN=");
  x = env_get("SSL_CLIENT_S_DN");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_C=");
  x = env_get("SSL_CLIENT_S_DN_C");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_ST=");
  x = env_get("SSL_CLIENT_S_DN_ST");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_L=");
  x = env_get("SSL_CLIENT_S_DN_L");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_O=");
  x = env_get("SSL_CLIENT_S_DN_O");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_OU=");
  x = env_get("SSL_CLIENT_S_DN_OU");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_CN=");
  x = env_get("SSL_CLIENT_S_DN_CN");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_T=");
  x = env_get("SSL_CLIENT_S_DN_T");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_I=");
  x = env_get("SSL_CLIENT_S_DN_I");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_G=");
  x = env_get("SSL_CLIENT_S_DN_G");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_S=");
  x = env_get("SSL_CLIENT_S_DN_S");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_D=");
  x = env_get("SSL_CLIENT_S_DN_D");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_UID=");
  x = env_get("SSL_CLIENT_S_DN_UID");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_S_DN_Email=");
  x = env_get("SSL_CLIENT_S_DN_Email");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN=");
  x = env_get("SSL_CLIENT_I_DN");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_C=");
  x = env_get("SSL_CLIENT_I_DN_C");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_ST=");
  x = env_get("SSL_CLIENT_I_DN_ST");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_L=");
  x = env_get("SSL_CLIENT_I_DN_L");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_O=");
  x = env_get("SSL_CLIENT_I_DN_O");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_OU=");
  x = env_get("SSL_CLIENT_I_DN_OU");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_CN=");
  x = env_get("SSL_CLIENT_I_DN_CN");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_T=");
  x = env_get("SSL_CLIENT_I_DN_T");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_I=");
  x = env_get("SSL_CLIENT_I_DN_I");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_G=");
  x = env_get("SSL_CLIENT_I_DN_G");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_S=");
  x = env_get("SSL_CLIENT_I_DN_S");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_D=");
  x = env_get("SSL_CLIENT_I_DN_D");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_UID=");
  x = env_get("SSL_CLIENT_I_DN_UID");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_I_DN_Email=");
  x = env_get("SSL_CLIENT_I_DN_Email");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_V_START=");
  x = env_get("SSL_CLIENT_V_START");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_V_END=");
  x = env_get("SSL_CLIENT_V_END");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_A_SIG=");
  x = env_get("SSL_CLIENT_A_SIG");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_A_KEY=");
  x = env_get("SSL_CLIENT_A_KEY");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_CERT=");
  x = env_get("SSL_CLIENT_CERT");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_DIGEST_SHA1=");
  x = env_get("SSL_CLIENT_DIGEST_SHA1");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_DIGEST_SHA256=");
  x = env_get("SSL_CLIENT_DIGEST_SHA256");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_CERT_CHAIN_0=");
  x = env_get("SSL_CLIENT_CERT_CHAIN_0");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_puts(buffer_1,"\nSSL_CLIENT_CERT_CHAIN_1=");
  x = env_get("SSL_CLIENT_CERT_CHAIN_1");
  buffer_puts(buffer_1,x ? x : "unset");

  buffer_putsflush(buffer_1,"\n");

  if (++n > 1) {
    environ = e;
  }
}
void doit(int t) {
  int j;
  SSL *ssl;
  int wstat;
  int sslctl[2];
  char *s;
  unsigned long tmp_long;
  char ssl_cmd;
  stralloc ssl_env = { 0 };
  int bytesleft;
  char envbuf[8192];
  int childpid;
  
  if (pipe(pi) == -1) strerr_die2sys(111,DROP,"unable to create pipe: ");
  if (pipe(po) == -1) strerr_die2sys(111,DROP,"unable to create pipe: ");
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, sslctl) == -1) strerr_die2sys(111,DROP,"unable to create socketpair: ");
 
  if ((j = ip_fmt(&remoteipsa,&remoteaddr)))
    strerr_die3x(111,DROP,"unable to print remote ip",gai_strerror(j));

  if (flagremotehost) {
    if (dns_name(&remotehostsa,&remoteaddr) == 0)
      if (remotehostsa.len) {
	if (flagparanoid) {
	  struct addrinfo *reverse, hints = {0};
	  verifyhost = remoteipsa.s;
	  hints.ai_family = remoteaddr.sa4.sin_family;
	  if (remoteaddr.sa6.sin6_family == AF_INET6) {
	    hints.ai_flags = AI_V4MAPPED | AI_ALL;
	  }
	  if (getaddrinfo(remotehostsa.s, NULL, &hints, &reverse) == 0) {
	    hints.ai_next = reverse;
	    while (hints.ai_next) {
	      if (hints.ai_next->ai_family == AF_INET
		  && remoteaddr.sa4.sin_family == AF_INET
		  && byte_equal(&remoteaddr.sa4.sin_addr, 4, &((struct sockaddr_in*) hints.ai_next->ai_addr)->sin_addr)
		  || hints.ai_next->ai_family == AF_INET6
		     && remoteaddr.sa6.sin6_family == AF_INET6
		     && byte_equal(remoteaddr.sa6.sin6_addr.s6_addr, 16,
				   &((struct sockaddr_in6*) hints.ai_next->ai_addr)->sin6_addr.s6_addr)) {
		flagparanoid = 0;
		break;
	      }
	      hints.ai_next = hints.ai_next->ai_next;
	    }
	    freeaddrinfo(reverse);
	  }
	}
	if (!flagparanoid) {
	  remotehost = remotehostsa.s;
	  verifyhost = remotehostsa.s;
	}
      }
  }

  switch(childpid=fork()) {
    case -1:
      strerr_die2sys(111,DROP,"unable to fork: ");
    case 0:
      /* Child */
      close(sslctl[0]);
      break;
    default:
      /* Parent */
      
      close(pi[0]); close(po[1]); close(sslctl[1]);

      if ((s=env_get("SSL_CHROOT")))
        if (chroot(s) == -1) {
          kill(childpid, SIGTERM);
          strerr_die2x(111,DROP,"unable to chroot");
        }
      
      if ((s=env_get("SSL_GID"))) {
        scan_ulong(s,&tmp_long);
        gid = tmp_long;
      }
      if (gid) if (prot_gid(gid) == -1) {
        kill(childpid, SIGTERM);
        strerr_die2sys(111,FATAL,"unable to set gid: ");
      }

      if ((s=env_get("SSL_UID"))) {
        scan_ulong(s,&tmp_long);
        uid = tmp_long;
      }
      if (uid)
        if (prot_uid(uid) == -1) {
          kill(childpid, SIGTERM);
          strerr_die2sys(111,FATAL,"unable to set uid: ");
        }

      /* Read the TLS command socket.  This will block until/unless
       * TLS is requested.
       */
      if (read(sslctl[0],&ssl_cmd,1) == 1) {
        ssl = ssl_new(ctx,t);
        if (!ssl) {
          kill(childpid, SIGTERM);
          strerr_die2x(111,DROP,"unable to create SSL instance");
        }
        if (ndelay_on(t) == -1) {
          kill(childpid, SIGTERM);
          strerr_die2sys(111,DROP,"unable to set socket options: ");
        }
        if (ssl_timeoutaccept(ssl,ssltimeout) == -1) {
          kill(childpid, SIGTERM);
          strerr_die3x(111,DROP,"unable to accept SSL: ",ssl_error_str(ssl_errno));
        }
      }
        
      if (verbosity >= 2) {
        strnum[fmt_ulong(strnum,getpid())] = 0;
        strerr_warn3("sslserver: ssl ",strnum," accept ",0);
      }
        
      if (flagclientcert) {
        switch(ssl_verify(ssl,verifyhost)) {
          case -1:
	    kill(childpid, SIGTERM);
            strerr_die2x(111,DROP,"unable to verify client certificate");
          case -2:
	    kill(childpid, SIGTERM);
            strerr_die2x(111,DROP,"no client certificate");
          case -3:
	    kill(childpid, SIGTERM);
            strerr_die3x(111,DROP,"certificate name does not match client fqdn: ",verifyhost);
          default: break;
        }
      }
      
      if (ssl_cmd == 'Y') {
        ssl_server_env(ssl, &ssl_env);
        if(!stralloc_0(&ssl_env)) drop_nomem(); /* Add another NUL */
        env("SSLCTL",ssl_env.s); 

        for(bytesleft = ssl_env.len; bytesleft>0; bytesleft-=j)
          if ( (j=write(sslctl[0], ssl_env.s, bytesleft)) < 0) {
            kill(childpid, SIGTERM);
            strerr_die2sys(111, FATAL, "unable to write SSL environment: ");
          }
      }

      if (ssl_cmd == 'Y' || ssl_cmd == 'y') {
        if (ssl_io(ssl,pi[1],po[0],progtimeout) != 0) {
          kill(childpid, SIGTERM);
          strerr_die3x(111,DROP,"unable to speak SSL: ",ssl_error_str(ssl_errno));
        }
        if (wait_nohang(&wstat) > 0)
          _exit(wait_exitcode(wstat)); 
        ssl_close(ssl);
      }
      kill(childpid, SIGTERM);
      _exit(0);
  }

  /* Child-only below this point */

  if (verbosity >= 2) {
    strnum[fmt_ulong(strnum,getpid())] = 0;
    strerr_warn4("sslserver: pid ",strnum," from ",remoteipsa.s,0);
  }

  if (socket_local(t,&localaddr,&localport) == -1)
    strerr_die2sys(111,DROP,"unable to get local address: ");

  if ((j = ip_fmt(&localipsa,&localaddr)))
    strerr_die3x(111,DROP,"unable to print local address: ",gai_strerror(j));
  remoteportstr[fmt_ulong(remoteportstr,remoteport)] = 0;

  if (!localhost)
    if (dns_name(&localhostsa,&localaddr) == 0)
      if (localhostsa.len) {
	if (!stralloc_0(&localhostsa)) drop_nomem();
	localhost = localhostsa.s;
      }
  /* If remoteipsa.s contain ':' colon character will assume it is IPv6 */
  if (byte_chr(remoteipsa.s, remoteipsa.len, ':') < remoteipsa.len)
    env("PROTO","SSL6");
  else
    env("PROTO","SSL");
  env("SSLLOCALIP",localipsa.s);
  env("SSLLOCALPORT",localportstr);
  env("SSLLOCALHOST",localhost);
  if (flagtcpenv) {
    env("TCPLOCALIP",localipsa.s);
    env("TCPLOCALPORT",localportstr);
    env("TCPLOCALHOST",localhost);
  }

  env("SSLREMOTEIP",remoteipsa.s);
  env("SSLREMOTEPORT",remoteportstr);
  env("SSLREMOTEHOST",remotehost);
  if (flagtcpenv) {
    env("TCPREMOTEIP",remoteipsa.s);
    env("TCPREMOTEPORT",remoteportstr);
    env("TCPREMOTEHOST",remotehost);
  }

  if (flagremoteinfo) {
    if (remoteinfo(&tcpremoteinfo,&remoteaddr,&localaddr,timeout) == -1)
      flagremoteinfo = 0;
    if (!stralloc_0(&tcpremoteinfo)) drop_nomem();
  }
  env("SSLREMOTEINFO",flagremoteinfo ? tcpremoteinfo.s : 0);
  if (flagtcpenv)
    env("TCPREMOTEINFO",flagremoteinfo ? tcpremoteinfo.s : 0);

  if (fnrules) {
    int fdrules;
    fdrules = open_read(fnrules);
    if (fdrules == -1) {
      if (errno != error_noent) drop_rules();
      if (!flagallownorules) drop_rules();
    }
    else {
      if (rules(found,fdrules,&remoteaddr,remotehost,flagremoteinfo ? tcpremoteinfo.s : 0) == -1)
	drop_rules();
      close(fdrules);
    }
  }

  if (verbosity >= 2) {
    strnum[fmt_ulong(strnum,getpid())] = 0;
    if (!stralloc_copys(&tmp,"sslserver: ")) drop_nomem();
    safecats(flagdeny ? "deny" : "ok");
    cats(" "); safecats(strnum);
    cats(" "); if (localhost) safecats(localhost);
    cats(":"); safecats(localipsa.s);
    cats(":"); safecats(localportstr);
    cats(" "); if (remotehost) safecats(remotehost);
    cats(":"); safecats(remoteipsa.s);
    cats(":"); if (flagremoteinfo) safecats(tcpremoteinfo.s);
    cats(":"); safecats(remoteportstr);
    cats("\n");
    buffer_putflush(buffer_2,tmp.s,tmp.len);
  }

  if (flagdeny) _exit(100);

  if (gid) if (prot_gid(gid) == -1)
    strerr_die2sys(111,FATAL,"unable to set gid: ");
  if (uid) if (prot_uid(uid) == -1)
    strerr_die2sys(111,FATAL,"unable to set uid: ");

  close(pi[1]); close(po[0]); close(sslctl[0]);

  sig_uncatch(sig_child);
  sig_unblock(sig_child);
  sig_uncatch(sig_term);
  sig_uncatch(sig_pipe);

  if (fcntl(sslctl[1],F_SETFD,0) == -1)
    strerr_die2sys(111,FATAL,"unable to clear close-on-exec flag");
  strnum[fmt_ulong(strnum,sslctl[1])]=0;
  env("SSLCTLFD",strnum);

  if (fcntl(pi[0],F_SETFD,0) == -1)
    strerr_die2sys(111,FATAL,"unable to clear close-on-exec flag");
  strnum[fmt_ulong(strnum,pi[0])]=0;
  env("SSLREADFD",strnum);

  if (fcntl(po[1],F_SETFD,0) == -1)
    strerr_die2sys(111,FATAL,"unable to clear close-on-exec flag");
  strnum[fmt_ulong(strnum,po[1])]=0;
  env("SSLWRITEFD",strnum);

  if (flagsslwait) {
    if (fd_copy(0,t) == -1)
      strerr_die2sys(111,DROP,"unable to set up descriptor 0: ");
    if (fd_copy(1,t) == -1)
      strerr_die2sys(111,DROP,"unable to set up descriptor 1: ");
  } else {
    if (fd_move(0,pi[0]) == -1)
      strerr_die2sys(111,DROP,"unable to set up descriptor 0: ");
    if (fd_move(1,po[1]) == -1)
      strerr_die2sys(111,DROP,"unable to set up descriptor 1: ");
  }

  if (flagkillopts)
    socket_ipoptionskill(t);
  if (!flagdelay)
    socket_tcpnodelay(t);

  if (*banner) {
    buffer_init(&b,buffer_unixwrite,1,bspace,sizeof bspace);
    if (buffer_putsflush(&b,banner) == -1)
      strerr_die2sys(111,DROP,"unable to print banner: ");
  }

  if (!flagsslwait) {
    ssl_cmd = flagsslenv ? 'Y' : 'y';
    if (write(sslctl[1], &ssl_cmd, 1) < 1)
      strerr_die2sys(111,DROP,"unable to start SSL: ");
    if (flagsslenv) {
      while ((j=read(sslctl[1],envbuf,8192)) > 0) {
        stralloc_catb(&ssl_env,envbuf,j);
        if (ssl_env.len >= 2 && ssl_env.s[ssl_env.len-2]==0 && ssl_env.s[ssl_env.len-1]==0)
          break;
      }
      if (j < 0)
        strerr_die2sys(111,DROP,"unable to read SSL environment: ");
      pathexec_multienv(&ssl_env);
    }
  }
      
  pathexec(prog);
  strerr_die4sys(111,DROP,"unable to run ",*prog,": ");
}
Beispiel #16
0
int main(int argc, char **argv)
{
  int fdfifo, fdfifowrite;
  char *x;
  unsigned long id;

  VERSIONINFO;

  x = env_get("WORKDIR");
  if (!x)
    strerr_die2x(111, FATAL, "$WORKDIR not set");
  if (chdir(x) == -1)
    strerr_die4sys(111, FATAL, "unable to chdir to ", x, ": ");

  x = env_get("GID");
  if (!x)
    strerr_die2x(111, FATAL, "$GID not set");
  scan_ulong(x,&id);
  if (prot_gid((int) id) == -1)
    strerr_die2sys(111, FATAL, "unable to setgid: ");

  x = env_get("UID");
  if (!x)
    strerr_die2x(111, FATAL, "$UID not set");
  scan_ulong(x,&id);

  /* undocumented feature */
  if(id == 0)
    if(!env_get("IWANTTORUNASROOTANDKNOWWHATIDO"))
      strerr_die2x(111, FATAL, "unable to run under uid 0: please change $UID");

  if (prot_uid((int) id) == -1)
    strerr_die2sys(111, FATAL, "unable to setuid: ");

  buffer_putsflush(buffer_2, ARGV0 "starting\n");

  if(fifo_make(FIFONAME, 0620) == -1)
    strerr_warn4(ARGV0, "unable to create fifo ", FIFONAME, " ", &strerr_sys);

  fdfifo = open_read(FIFONAME);
  if(fdfifo == -1)
    strerr_die4sys(111, FATAL, "unable to open for read ", FIFONAME, " ");
  coe(fdfifo);
  ndelay_on(fdfifo); /* DJB says: shouldn't be necessary */

  /* we need this to keep the fifo from beeing closed */
  fdfifowrite = open_write(FIFONAME);
  if (fdfifowrite == -1)
    strerr_die4sys(111, FATAL, "unable to open for write ", FIFONAME, " ");
  coe(fdfifowrite);

  /* init a buffer for nonblocking reading */
  buffer_init(&wr, waitread, fdfifo, waitreadspace, sizeof waitreadspace);

  t = dAVLAllocTree();

  /* read snapshot of dnsdatatree */ 
  fill_db();

  /* SIGALRM can be used to check if dumping the database is needed */
  sig_catch(sig_alarm, sigalrm);

  /* SIGHUP can be used to force dumping the database */
  sig_catch(sig_hangup, sighup);  

  /* check if out child is done */
  sig_catch(sig_child, sigchld);  

  // XXX SIGINT, SIGTERM,

  /* do our normal workloop */
  doit();

  /* we shouldn't get here */
  return 1;
}
Beispiel #17
0
int main(int argc,char* argv[]) {
  unsigned long count=1000;
  int v6;

  v6=0;

  {
    struct rlimit rl;
    rl.rlim_cur=RLIM_INFINITY; rl.rlim_max=RLIM_INFINITY;
    setrlimit(RLIMIT_NOFILE,&rl);
#ifdef RLIMIT_NPROC
    setrlimit(RLIMIT_NPROC,&rl);
#endif
  }

  for (;;) {
    int i;
    int c=getopt(argc,argv,"h6c:");
    if (c==-1) break;
    switch (c) {
    case 'c':
      i=scan_ulong(optarg,&count);
      if (i==0 || optarg[i]) {
	buffer_puts(buffer_2,"httpbench: warning: could not parse count: ");
	buffer_puts(buffer_2,optarg+i+1);
	buffer_putsflush(buffer_2,"\n");
      }
      break;
    case '6':
      v6=1;
      break;
    case 'h':
      buffer_putsflush(buffer_2,
		  "usage: bindbench [-h] [-6] [-c count]\n"
		  "\n"
		  "\t-h\tprint this help\n"
		  "\t-c n\tbind n sockets to port 0 (default: 1000)\n"
		  "\t-6\tbind IPv6 sockets instead of IPV4\n");
      return 0;
    }
  }


  {
    int i,r;
    char ip[16];
    int port;
#ifdef __i386__
    unsigned long long a,b,c;
#else
    struct timeval a,b,c;
    unsigned long d;
#endif
    int *socks=alloca(count*sizeof(int));
    port=0; byte_zero(ip,16);
    for (i=0; i<count; ++i) {
#ifdef __i386__
      rdtscl(a);
#else
      gettimeofday(&a,0);
#endif
      socks[i]=v6?socket_tcp6():socket_tcp4();
#ifdef __i386__
      rdtscl(b);
#else
      gettimeofday(&b,0);
#endif
      if (socks[i]==-1) {
	buffer_puts(buffer_2,"socket() failed: ");
	buffer_puterror(buffer_2);
	buffer_putnlflush(buffer_2);
	exit(1);
      }
      if (v6)
	r=socket_bind6(socks[i],ip,port,0);
      else
	r=socket_bind4(socks[i],ip,port);
      if (r==-1) {
	buffer_puts(buffer_2,"bind() failed: ");
	buffer_puterror(buffer_2);
	buffer_putnlflush(buffer_2);
	exit(1);
      }
#ifdef __i386__
      rdtscl(c);
      buffer_putulong(buffer_1,b-a);
#else
      gettimeofday(&c,0);
      d=(b.tv_sec-a.tv_sec)*1000000;
      d=d+b.tv_usec-a.tv_usec;
      buffer_putulong(buffer_1,d);
#endif
      buffer_putspace(buffer_1);
#ifdef __i386__
      buffer_putulong(buffer_1,c-b);
#else
      d=(c.tv_sec-b.tv_sec)*1000000;
      d=d+c.tv_usec-b.tv_usec;
      buffer_putulong(buffer_1,d);
#endif
      buffer_puts(buffer_1,"\n");
      if (i>50) {
	close(socks[10]);
	socks[10]=v6?socket_tcp6():socket_tcp4();
      }
    }
  }

  buffer_flush(buffer_1);
  return 0;
}
Beispiel #18
0
int main(int argc,char* argv[]) {
  unsigned long count=10000;

  for (;;) {
    int i;
    int c=getopt(argc,argv,"hc:");
    if (c==-1) break;
    switch (c) {
    case 'c':
      i=scan_ulong(optarg,&count);
      if (i==0 || optarg[i]) {
	buffer_puts(buffer_2,"mktestdata: warning: could not parse count: ");
	buffer_puts(buffer_2,optarg+i+1);
	buffer_putsflush(buffer_2,"\n");
      }
      break;
    case 'h':
      buffer_putsflush(buffer_2,
		  "usage: mktestdata [-h] [-c count]\n"
		  "\n"
		  "\t-h\tprint this help\n"
		  "\t-c n\tcreate n small files (default: 10000)\n");
      return 0;
    }
  }

  {
    unsigned long i,j;
    char buf[4096];
    char filename[256];
    if (mkdir("data",0700)==-1) panic("mkdir");
    for (i=0; i<(count+99)/100; ++i) {
      j=fmt_str(filename,"data/");
      j+=fmt_ulong(filename+j,i);
      filename[j]=0;
      if (mkdir(filename,0700)==-1) panic("mkdir");
    }
    for (i=0; i<count; ++i) {
      int64 fd;
      j=fmt_str(filename,"data/");
      j+=fmt_ulong(filename+j,i/100);
      j+=fmt_str(filename+j,"/");
      j+=fmt_ulong(filename+j,i);
      j+=fmt_str(filename+j,".html");
      filename[j]=0;
      if (!io_createfile(&fd,filename))
	panic("creat");
      j=fmt_str(buf,"<title>Page ");
      j+=fmt_ulong(buf+j,i);
      j+=fmt_str(buf+j,"</title><h1>Page ");
      j+=fmt_ulong(buf+j,i);
      j+=fmt_str(buf+j,"</h1>\nThis is a nice, small and clean web page.<p>\n");
      if (i==count)
	j+=fmt_str(buf+j,"This is the last page.\n");
      else {
	j+=fmt_str(buf+j,"And <a href=../");
	j+=fmt_ulong(buf+j,(i+1)/100);
	j+=fmt_str(buf+j,"/");
	j+=fmt_ulong(buf+j,i+1);
	j+=fmt_str(buf+j,"html>here</a> is the next one.");
      }
      write(fd,buf,j);
      close(fd);
    }
  }
  buffer_flush(buffer_1);
  return 0;
}
Beispiel #19
0
static int readanswer(int s,int measurethroughput) {
  char buf[8192];
  int i,j,body=-1,r;
  unsigned long rest;
  unsigned long done=0;
  struct timeval a,b;
  i=0;
  while ((r=read(s,buf+i,sizeof(buf)-i)) > 0) {
    i+=r;
    for (j=0; j+3<i; ++j) {
      if (buf[j]=='\r' && buf[j+1]=='\n' && buf[j+2]=='\r' && buf[j+3]=='\n') {
	body=j+4;
	break;
      }
    }
    if (body!=-1) {
      if (byte_diff(buf,7,"HTTP/1.")) {
	buffer_putsflush(buffer_2,"invalid HTTP response!\n");
	return -1;
      }
      break;
    }
  }
  if (r==-1) return -1;
  rest=-1;
  for (j=0; j<r; j+=str_chr(buf+j,'\n')) {
    if (byte_equal(buf+j,17,"\nContent-Length: ")) {
      char* c=buf+j+17;
      if (c[scan_ulong(c,&rest)]!='\r') {
	buffer_putsflush(buffer_2,"invalid Content-Length header!\n");
	return -1;
      }
      break;
    }
    ++j;
  }
  if (measurethroughput) {
    gettimeofday(&a,0);
    done=r-body;
  }
  rest-=(r-body);
  while (rest) {
    r=read(s,buf,rest>sizeof(buf)?sizeof(buf):rest);
    if (r<1) {
      if (r==-1)
	carp("read from HTTP socket");
      else {
	buffer_puts(buffer_2,"early HTTP EOF; expected ");
	buffer_putulong(buffer_2,rest);
	buffer_putsflush(buffer_2,"more bytes!\n");
	return -1;
      }
    } else {
      rest-=r;
      if (measurethroughput) {
	unsigned long x=done/1000000;
	unsigned long y;
	done+=r;
	y=done/1000000;
	if (x!=y) {
	  unsigned long d;
	  unsigned long long z;
	  gettimeofday(&b,0);
	  d=(b.tv_sec-a.tv_sec)*1000000;
	  d=d+b.tv_usec-a.tv_usec;
	  buffer_puts(buffer_1,"tput ");
	  z=1000000000ull/d;
	  buffer_putulong(buffer_1,z);
	  buffer_putnlflush(buffer_1);

	  byte_copy(&a,sizeof(a),&b);
	}
      }
    }
  }
  return 0;
}
Beispiel #20
0
void mailq()
{
  execv(*qreadarg,qreadarg);
  buffer_putsflush(buffer_0,"sendmail: fatal: unable to run qmail-qread\n");
  _exit(111);
}
Beispiel #21
0
int
main(int argc, char* argv[]) {
  int s = socket_tcp6();
  uint32 scope_id;
  char ip[16];
  uint16 port;
  char hisip[16];
  uint16 hisport;
  uint32 hisscope_id;
  static char seed[128];
  static stralloc fqdn;
  static stralloc out;

  if(argc != 4) {
  usage:
    buffer_putsflush(buffer_2,
                     "usage: proxy myip myport hisip hisport\n"
                     "\n"
                     "e.g.: proxy 0 119 news.fu-berlin.de 119\n");
    return 0;
  }

  if(argv[1][scan_ip6if(argv[1], ip, &scope_id)]) {
    if(str_equal(argv[1], "0")) {
      byte_zero(ip, 16);
      scope_id = 0;
    } else
      goto usage;
  }
  if(argv[2][scan_ushort(argv[2], &port)])
    goto usage;
  if(argv[3][scan_ip6if(argv[3], hisip, &hisscope_id)]) {
    dns_random_init(seed);
    if(!stralloc_copys(&fqdn, argv[3]))
      goto nomem;
    if(dns_ip4(&out, &fqdn) == -1) {
      buffer_puts(buffer_2, "unable to find IP address for ");
      buffer_puts(buffer_2, argv[3]);
      buffer_puts(buffer_2, ": ");
      buffer_puterror(buffer_2);
      buffer_putnlflush(buffer_2);
      return 111;
    }
  } else if(!stralloc_catb(&out, hisip, 16)) {
  nomem:
    buffer_putsflush(buffer_2, "out of memory\n");
    return 111;
  }
  if(argv[4][scan_ushort(argv[4], &hisport)])
    goto usage;

  if(socket_bind6_reuse(s, ip, port, scope_id) == -1) {
    buffer_puts(buffer_2, "socket_bind6_reuse: ");
    buffer_puterror(buffer_2);
    buffer_putnlflush(buffer_2);
    return 111;
  }
  if(socket_listen(s, 16) == -1) {
    buffer_puts(buffer_2, "socket_listen: ");
    buffer_puterror(buffer_2);
    buffer_putnlflush(buffer_2);
    return 111;
  }
  if(!io_fd(s)) {
    buffer_puts(buffer_2, "io_fd: ");
    buffer_puterror(buffer_2);
    buffer_putnlflush(buffer_2);
    return 111;
  }
  io_wantread(s);
  for(;;) {
    int64 i;
    io_wait();
    while((i = io_canread()) != -1) {
      if(i == s) {
        /* the read event is on the server socket */
        /* that means it's an incoming connection */
        int n;
        while((n = socket_accept6(s, ip, &port, &scope_id)) != -1) {
          int x = socket_tcp6();
          if(x == -1) {
            buffer_puts(buffer_2, "socket_tcp6 failed: ");
          fail:
            buffer_puterror(buffer_2);
            buffer_putnlflush(buffer_2);
            io_close(n);
          } else {
            struct state* s = malloc(sizeof(struct state));
            if(!s)
              goto closefail;
            s->a = n;
            s->b = x;
            s->connected = 0;
            s->done = s->todo = 0;
            s->dir = UNDECIDED;
            io_nonblock(x);
            socket_connect6(x, out.s, hisport, hisscope_id);
            if(!io_fd(x) || !io_fd(n)) {
              buffer_puts(buffer_2, "io_fd failed: ");
            closefail:
              free(s);
              io_close(x);
              goto fail;
            }
            io_setcookie(x, s);
            io_setcookie(n, s);
            io_wantwrite(x);
          }
        }
        if(errno != EAGAIN) {
          buffer_puts(buffer_2, "socket_accept6 failed: ");
          buffer_puterror(buffer_2);
          buffer_putnlflush(buffer_2);
        }
      } else {
        /* read event on an established connection */
        struct state* s = io_getcookie(i);
        int l = io_tryread(i, s->buf, sizeof(s->buf));
        if(l == -1) {
          buffer_puts(buffer_2, "io_tryread(");
          buffer_putulong(buffer_2, i);
          buffer_puts(buffer_2, "): ");
          buffer_puterror(buffer_2);
          buffer_putnlflush(buffer_2);
          io_close(s->a);
          io_close(s->b);
        } else if(l == 0) {
          buffer_puts(buffer_2, "eof on fd #");
          buffer_putulong(buffer_2, i);
          buffer_putnlflush(buffer_2);
          io_close(i);
        } else {
          int r;
          switch(r = io_trywrite(i, s->buf, l)) {
            case -1:
              buffer_puts(buffer_2, "io_tryread(");
              buffer_putulong(buffer_2, i);
              buffer_puts(buffer_2, "): ");
              buffer_puterror(buffer_2);
              buffer_putnlflush(buffer_2);
              io_close(i);
              break;
            case 0:
              buffer_puts(buffer_2, "write eof on fd #");
              buffer_putulong(buffer_2, i);
              buffer_putnlflush(buffer_2);
              io_close(i);
            default:
              if(r != l) {
                buffer_puts(buffer_2, "short write on fd #");
                buffer_putulong(buffer_2, i);
                buffer_puts(buffer_2, ": wrote ");
                buffer_putulong(buffer_2, r);
                buffer_puts(buffer_2, ", wanted to write ");
                buffer_putulong(buffer_2, l);
                buffer_putsflush(buffer_2, ").\n");
              }
          }
        }
      }
    }
  }
  return 0;
}
Beispiel #22
0
void die_usage()
{
  buffer_putsflush(buffer_2,"sendmail: usage: sendmail [ -t ] [ -fsender ] [ -Fname ] [ -bp ] [ -bs ] [ arg ... ]\n");
  _exit(100);
}
Beispiel #23
0
void nomem()
{
  buffer_putsflush(buffer_2,"sendmail: fatal: out of memory\n");
  _exit(111);
}
Beispiel #24
0
void doit(int t)
{
  int j;
  uint32 scope_id;

  if (ip6_isv4mapped(remoteip))
    remoteipstr[ip4_fmt(remoteipstr,remoteip+12)] = 0;
  else
    remoteipstr[ip6_fmt(remoteipstr,remoteip)] = 0;

  if (verbosity >= 2) {
    strnum[fmt_ulong(strnum,getpid())] = 0;
    log(B("pid ",strnum," from ",remoteipstr,0));
  }

  if (flagkillopts)
    socket_ipoptionskill(t);
  if (!flagdelay)
    socket_tcpnodelay(t);

  if (*banner) {
    buffer_init(&b,write,t,bspace,sizeof bspace);
    if (buffer_putsflush(&b,banner) == -1)
      errint(EHARD,"unable to print banner: ");
  }

  if (socket_local(t,localip,&localport,&scope_id) == -1)
    errint(EHARD,"unable to get local address: ");

  if (ip6_isv4mapped(localip))
    localipstr[ip4_fmt(localipstr,localip+12)] = 0;
  else
    localipstr[ip6_fmt(localipstr,localip)] = 0;
  remoteportstr[fmt_ulong(remoteportstr,remoteport)] = 0;

  if (!localhost)
    if (dns_name(&localhostsa,localip) == 0)
      if (localhostsa.len) {
    if (!stralloc_0(&localhostsa)) errmem;
    localhost = localhostsa.s;
      }
  env("PROTO","TCP");

  env("TCPLOCALIP",localipstr);
  env("TCPLOCALPORT",localportstr);
  env("TCPLOCALHOST",localhost);

  if (flagremotehost)
    if (dns_name(&remotehostsa,remoteip) == 0)
      if (remotehostsa.len) {
    if (flagparanoid)
      if (dns_ip6(&tmp,&remotehostsa) == 0)
        for (j = 0;j + 16 <= tmp.len;j += 16)
          if (byte_equal(remoteip,16,tmp.s + j)) {
        flagparanoid = 0;
        break;
          }
    if (!flagparanoid) {
      if (!stralloc_0(&remotehostsa)) errmem;
      remotehost = remotehostsa.s;
    }
      }
  env("TCPREMOTEIP",remoteipstr);
  env("TCPREMOTEPORT",remoteportstr);
  env("TCPREMOTEHOST",remotehost);

  if (flagremoteinfo) {
    if (remoteinfo6(&tcpremoteinfo,remoteip,remoteport,localip,localport,timeout,netif) == -1)
      flagremoteinfo = 0;
    if (!stralloc_0(&tcpremoteinfo)) errmem;
  }
  env("TCPREMOTEINFO",flagremoteinfo ? tcpremoteinfo.s : 0);

  if (fnrules) {
    int fdrules;
    fdrules = open_read(fnrules);
    if (fdrules == -1) {
      if (errno != error_noent) drop_rules();
      if (!flagallownorules) drop_rules();
    }
    else {
      char* temp;
	  if (ip6_isv4mapped(remoteip))
        temp=remoteipstr+7;
      else
        temp=remoteipstr;
      if (rules(found,fdrules,temp,remotehost,flagremoteinfo ? tcpremoteinfo.s : 0) == -1) drop_rules();
      close(fdrules);
//      log(B("checking tcp rules for ",remotehost,": pass"));
    }
  }

  if (verbosity >= 2) {
    strnum[fmt_ulong(strnum,getpid())] = 0;
    if (!stralloc_copys(&tmp,"qmail-tcpsrv: ")) errmem;
    safecats(flagdeny ? "deny" : "ok");
    cats(" "); safecats(strnum);
    cats(" "); if (localhost) safecats(localhost);
    cats(":"); safecats(localipstr);
    cats(":"); safecats(localportstr);
    cats(" "); if (remotehost) safecats(remotehost);
    cats(":"); safecats(remoteipstr);
//    cats(":<remote info>"); if (flagremoteinfo) safecats(tcpremoteinfo.s);
    cats(":"); if (flagremoteinfo) safecats(tcpremoteinfo.s);
    cats(":"); safecats(remoteportstr);
    cats("\n");
    buffer_putflush(buffer_2,tmp.s,tmp.len);
  }

  if (flagdeny) _exit(100);
}
int main(int argc,char* argv[]) {
  unsigned long count=1000;
  struct timeval a,b;
  unsigned long d;

#ifdef RLIMIT_NPROC
  {
    struct rlimit rl;
    rl.rlim_cur=RLIM_INFINITY; rl.rlim_max=RLIM_INFINITY;
    setrlimit(RLIMIT_NPROC,&rl);
  }
#endif

  for (;;) {
    int i;
    int c=getopt(argc,argv,"hc:");
    if (c==-1) break;
    switch (c) {
    case 'c':
      i=scan_ulong(optarg,&count);
      if (i==0 || optarg[i]) {
	buffer_puts(buffer_2,"pthreadbench: warning: could not parse count: ");
	buffer_puts(buffer_2,optarg+i+1);
	buffer_putsflush(buffer_2,"\n");
      }
      break;
    case 'h':
      buffer_putsflush(buffer_2,
		  "usage: pthreadbench [-h] [-c count]\n"
		  "\n"
		  "\t-h\tprint this help\n"
		  "\t-c n\tfork off n children (default: 1000)\n");
      return 0;
    }
  }

  {
    unsigned long i;
    int pfd[2];
    char buf[100];
    pthread_t *p=malloc(count*sizeof(pthread_t));
    if (!p) {
      buffer_puts(buffer_2,"out of memory!\n");
      exit(1);
    }
    if (pipe(pfd)==-1) {
      buffer_puts(buffer_2,"pipe failed: ");
      buffer_puterror(buffer_2);
      buffer_putnlflush(buffer_2);
    }
    for (i=0; i<count; ++i) {
      int r;
      gettimeofday(&a,0);
      switch ((r=pthread_create(p+i,0,(void*(*)(void*))mythread,pfd))) {
      case 0: /* ok */
	break;
      default:
	buffer_puts(buffer_2,"could not create thread: ");
	buffer_puterror(buffer_2);
	buffer_putsflush(buffer_2,".\n");
	exit(1);
      }
      if (read(pfd[0],buf,1)!=1) {
	buffer_putsflush(buffer_2,"thread did not write into pipe?!\n");
	exit(1);
      }
      gettimeofday(&b,0);
      d=(b.tv_sec-a.tv_sec)*1000000;
      d=d+b.tv_usec-a.tv_usec;
      buffer_putulong(buffer_1,d);
      buffer_puts(buffer_1,"\n");
    }
    buffer_flush(buffer_1);
  }

  return 0;
}
Beispiel #26
0
int main(int argc,char **argv)
{
  int opt;
  char **qiargv;
  char **arg;
  int i;

  if (chdir(auto_qmail) == -1) {
    buffer_putsflush(buffer_2,"sendmail: fatal: unable to switch to qmail home directory\n");
    _exit(111);
  }

  flagh = 0;
  sender = 0;
  while ((opt = getopt(argc,argv,"vimte:f:p:o:B:F:EJxb:")) != opteof)
    switch(opt) {
      case 'B': break;
      case 't': flagh = 1; break;
      case 'f': sender = optarg; break;
      case 'F': if (!env_put("MAILNAME",optarg)) nomem(); break;
      case 'p': break; /* could generate a Received line from optarg */
      case 'v': break;
      case 'i': break; /* what an absurd concept */
      case 'x': break; /* SVR4 stupidity */
      case 'm': break; /* twisted-paper-path blindness, incompetent design */
      case 'e': break; /* qmail has only one error mode */
      case 'o':
        switch(optarg[0]) {
      case 'd': break; /* qmail has only one delivery mode */
      case 'e': break; /* see 'e' above */
      case 'i': break; /* see 'i' above */
      case 'm': break; /* see 'm' above */
    }
        break;
      case 'E': case 'J': /* Sony NEWS-OS */
        while (argv[optind][optpos]) ++optpos; /* skip optional argument */
        break;
      case 'b':
    switch(optarg[0]) {
      case 'm': break;
      case 'p': mailq();
      case 's': smtpd();
      default: die_usage();
    }
    break;
      default:
    die_usage();
    }
  argc -= optind;
  argv += optind;

  if (str_equal(optprogname,"mailq"))
    mailq();

  if (str_equal(optprogname,"newaliases")) {
    buffer_putsflush(buffer_2,"sendmail: fatal: please use fastforward/newaliases instead\n");
    _exit(100);
  }

  qiargv = (char **) alloc((argc + 10) * sizeof(char *));
  if (!qiargv) nomem();

  arg = qiargv;
  *arg++ = "bin/qmail-inject";
  *arg++ = (flagh ? "-H" : "-a");
  if (sender) {
    *arg++ = "-f";
    *arg++ = sender;
    do_sender(sender);
  }
  *arg++ = "--";
  for (i = 0;i < argc;++i) *arg++ = argv[i];
  *arg = 0;

  execv(*qiargv,qiargv);
  buffer_putsflush(buffer_2,"sendmail: fatal: unable to run qmail-inject\n");
  _exit(111);
//  return(0);  /* never reached */
}
Beispiel #27
0
int main(int argc,char* argv[]) {
  static size_t x;
  x=23;
  atomic_add(&x,3);
  printf("%u\n",x);
  printf("%u\n",atomic_add_return(&x,-3));
  printf("%u\n",compare_and_swap(&x,26,17));
  printf("%u\n",compare_and_swap(&x,23,17));

#if 0
  atomic_add(&x,3); printf("%u\n",x);
  x=23;
  atomic_add(&x,3); assert(x==26);
  atomic_or(&x,1); assert(x==27);
  atomic_and(&x,-2); assert(x==26);
#endif

#if 0
  iarray a;
  char* c;
  iarray_init(&a,sizeof(io_entry));
  printf("15 -> %p\n",c=iarray_allocate(&a,15));
  printf("23 -> %p\n",c=iarray_allocate(&a,23));
  printf("1234567 -> %p\n",c=iarray_allocate(&a,1234567));
  printf("23 -> %p\n",iarray_get(&a,23));
#endif
#if 0
  io_batch* b=iob_new(1234);
  int64 fd=open("t.c",0);
  iob_addbuf(b,"fnord",5);
  iob_addfile_close(b,fd,0,7365);
  iob_write(1,b,writecb);
#endif
#if 0
  char dest[1024];
  unsigned long len;
  scan_urlencoded2("libstdc++.tar.gz",dest,&len);
  buffer_putmflush(buffer_1,dest,"\n");
#endif
#if 0
  static stralloc sa;
  stralloc_copym(&sa,"foo ","bar ","baz.\n");
  write(1,sa.s,sa.len);
#endif
#if 0
  buffer_putmflush(buffer_1,"foo ","bar ","baz.\n");
#endif
#if 0
  char* c="fnord";
  int fd=open_read(c);
  errmsg_iam(argv[0]);
  carp("could not open file `",c,"'");
  diesys(23,"could not open file `",c,"'");
#endif
#if 0
  errmsg_warn("could not open file `",c,"'",0);
  errmsg_warnsys("could not open file `",c,"'",0);
#endif
#if 0
  char buf[100]="/usr/bin/sh";
  int len=str_len(buf);
  assert(byte_rchr(buf,len,'/')==8);
  assert(byte_rchr(buf,len,'@')==len);
  assert(byte_rchr(buf,len,'h')==len-1);
  printf("%d\n",byte_rchr("x",1,'x'));
#endif
#if 0
  char buf[IP6_FMT+100];
  int i;
  char ip[16];
  uint32 scope_id;
  char* s="fec0::1:220:e0ff:fe69:ad92%eth0/64";
  char blubip[16]="\0\0\0\0\0\0\0\0\0\0\xff\xff\x7f\0\0\001";
  i=scan_ip6if(s,ip,&scope_id);
  assert(s[i]=='/');
  buffer_put(buffer_1,buf,fmt_ip6if(buf,ip,scope_id));
  buffer_putnlflush(buffer_1);
  buffer_put(buffer_1,buf,fmt_ip6ifc(buf,blubip,scope_id));
  buffer_putnlflush(buffer_1);
  scan_ip6("2001:7d0:0:f015:0:0:0:1",ip);
  buffer_put(buffer_1,buf,fmt_ip6(buf,ip));
  buffer_putnlflush(buffer_1);
#endif
#if 0
  char buf[100];
  int i;
  printf("%d\n",i=fmt_pad(buf,"fnord",5,7,10));
  buf[i]=0;
  puts(buf);
#endif
#if 0
  char ip[16];
  char buf[32];
  printf("%d (expect 2)\n",scan_ip6("::",ip));
  printf("%d (expect 3)\n",scan_ip6("::1",ip));
  printf("%d (expect 16)\n",scan_ip6("fec0:0:0:ffff::1/0",ip));
  printf("%.*s\n",fmt_ip6(buf,ip),buf);
#endif
#if 0
  static stralloc s,t;
  stralloc_copys(&s,"fnord");
  stralloc_copys(&t,"abc"); printf("%d\n",stralloc_diff(&s,&t));
  stralloc_copys(&t,"fnor"); printf("%d\n",stralloc_diff(&s,&t));
  stralloc_copys(&t,"fnord"); printf("%d\n",stralloc_diff(&s,&t));
  stralloc_copys(&t,"fnordh"); printf("%d\n",stralloc_diff(&s,&t));
  stralloc_copys(&t,"hausen"); printf("%d\n",stralloc_diff(&s,&t));
#endif
#if 0
  static stralloc s;
  stralloc_copys(&s,"fnord");
  printf("%d\n",stralloc_diffs(&s,"abc"));
  printf("%d\n",stralloc_diffs(&s,"fnor"));
  printf("%d\n",stralloc_diffs(&s,"fnord"));
  printf("%d\n",stralloc_diffs(&s,"fnordh"));
  printf("%d\n",stralloc_diffs(&s,"hausen"));
#endif
#if 0
  printf("%d\n",case_starts("fnordhausen","FnOrD"));
  printf("%d\n",case_starts("fnordhausen","blah"));
#endif
#if 0
  char buf[]="FnOrD";
  case_lowers(buf);
  puts(buf);
#endif
#if 0
  char buf[100]="foo bar baz";
  printf("%d (expect 7)\n",byte_rchr(buf,11,' '));
#endif
#if 0
  unsigned long size;
  char* buf=mmap_read(argv[1],&size);
  if (buf) {
    unsigned int x=fmt_yenc(0,buf,size);
    unsigned int y;
    char* tmp=malloc(x+1);
    y=fmt_yenc(tmp,buf,size);
    write(1,tmp,x);
  }
#endif
#if 0
  char buf[100];
  char buf2[100];
  unsigned int len,len2;
  buf[fmt_yenc(buf,"http://localhost/~fefe",22)]=0;
  buffer_puts(buffer_1,buf);
  buffer_putsflush(buffer_1,"\n");
  if ((buf[len2=scan_yenc(buf,buf2,&len)])!='\n') {
    buffer_putsflush(buffer_2,"parse error!\n");
    return 1;
  }
  buffer_put(buffer_1,buf2,len2);
  buffer_putsflush(buffer_1,"\n");
  return 0;
#endif
#if 0
  char buf[100];
  char buf2[100];
  unsigned int len,len2;
  buf[fmt_base64(buf,"foo:bar",7)]=0;
  buffer_puts(buffer_1,buf);
  buffer_putsflush(buffer_1,"\n");
  if ((buf[len2=scan_base64(buf,buf2,&len)])!=0) {
    buffer_putsflush(buffer_2,"parse error!\n");
    return 1;
  }
  buffer_put(buffer_1,buf2,len2);
  buffer_putsflush(buffer_1,"\n");
  return 0;
#endif
#if 0
  unsigned long size;
  char* buf=mmap_read(argv[1],&size);
  if (buf) {
    unsigned int x=fmt_uuencoded(0,buf,size);
    unsigned int y;
    char* tmp=malloc(x+1);
    y=fmt_uuencoded(tmp,buf,size);
    write(1,tmp,x);
  }
#endif
#if 0
  char buf[]="00000000000000000000000000000001";
  char ip[16];
  if (scan_ip6_flat(buf,ip) != str_len(buf))
    buffer_putsflush(buffer_2,"parse error!\n");
#endif
#if 0
  int fd=open_read("t.c");
  buffer b;
  char buf[1024];
  char line[20];
  int i;
  buffer_init(&b,read,fd,buf,1024);
  i=buffer_getline(&b,line,19);
  buffer_puts(buffer_1,"getline returned ");
  buffer_putulong(buffer_1,i);
  buffer_puts(buffer_1,"\n");
  buffer_puts(buffer_1,line);
  buffer_flush(buffer_1);
#endif
#if 0
  buffer_putulong(buffer_1,23);
//  buffer_putspace(buffer_1);
  buffer_putsflush(buffer_1,"\n");
//  buffer_flush(buffer_1);
#endif
#if 0
  long a,b,c;
  char buf[4096];
  char buf2[4096];
  memcpy(buf,buf2,4096);
  byte_copy(buf,4096,buf2);
  rdtscl(a);
  memcpy(buf,buf2,4096);
  rdtscl(b);
  byte_copy(buf,4096,buf2);
  rdtscl(c);
  printf("memcpy: %d - byte_copy: %d\n",b-a,c-b);
#endif
#if 0
  char ip[16];
  int i;
  if ((i=scan_ip6(argv[1],ip))) {
    char buf[128];
    buf[fmt_ip6(buf,ip)]=0;
    puts(buf);
  }
#endif
#if 0
  char buf[100];
  strcpy(buf,"foobarbaz");
  buf[fmt_fill(buf,3,5,100)]=0;
  printf("\"%s\"\n",buf);
#endif
#if 0
  unsigned long len;
  char *c=mmap_read("/etc/passwd",&len);
  printf("got map %p of len %lu\n",c,len);
#endif
#if 0
  char c;
  printf("%d\n",buffer_getc(buffer_0,&c));
  printf("%c\n",c);
#endif
#if 0
  char buf[100]="01234567890123456789012345678901234567890123456789";
  long a,b,c;
#endif
#if 0
  buf[ip4_fmt(buf,ip4loopback)]=0;
  buffer_puts(buffer_1small,buf);
  buffer_flush(buffer_1small);
#endif

#if 0
  buf[0]=0;
  buf[fmt_8long(buf,0)]=0;
  puts(buf);
  rdtscl(a);
  c=str_len(buf);
  rdtscl(b);
  /*byte_zero_djb(buf,j); */
//  printf("\n%lu %d\n",b-a,c);
#endif
#if 0
  buffer_puts(buffer_1small,"hello, world\n");
  buffer_flush(buffer_1small);
#endif
#if 0
  int s=socket_tcp4();
  char ip[4]={127,0,0,1};
  int t=socket_connect4(s,ip,80);
#endif
#if 0
  char buf[100]="foo bar baz fnord   ";
  char buf2[100]="foo braz fnord";
  long a,b,c;
  long i=0,j=0,k=0;
  double d;
  uint32 l,m,n;
  stralloc sa={0};
  stralloc_copys(&sa,"fnord");
  stralloc_catlong0(&sa,-23,5);
  stralloc_append(&sa,"\n");
  printf("%d %d\n",str_equal("fnord","fnord1"),str_equal("fnord1","fnord"));
  write(1,sa.s,sa.len);
  printf("%d %d\n",stralloc_starts(&sa,"fnord"),stralloc_starts(&sa,"fnord\na"));

  l=0xdeadbeef;
  uint32_pack_big((char*)&m,l);
  uint32_unpack_big((char*)&m,&n);
  printf("%x %x %x\n",l,m,n);

  rdtscl(a);
/*  i=scan_double("3.1415",&d); */
  rdtscl(b);
  /*byte_zero_djb(buf,j); */
  rdtscl(c);
  printf("%lu %lu\n",b-a,c-b);
#endif
#if 0
  size_t size;
  char* buf=mmap_read(argv[1],&size);
  if (buf) {
    unsigned int x=fmt_urlencoded2(0,buf,size,"x");
    unsigned int y;
    char* tmp=malloc(x+1);
    y=fmt_urlencoded2(tmp,buf,size,"x");
    write(1,tmp,x);
  }
#endif
#if 0
  printf("%d %d\n",strcmp("foo","bar"),str_diff("foo","bar"));
  printf("%d %d\n",strcmp("foo","üar"),str_diff("foo","üar"));
#endif
#if 0
  {
    int16 a;
    int32 b;
    int64 c;
    assert(imult16(4,10000,&a)==0);
    assert(imult16(-4,10000,&a)==0);
    assert(imult16(5,10,&a)==1 && a==50);
    assert(imult16(-3,10000,&a)==1 && a==-30000);

    assert(imult32(0x40000000,2,&b)==0);
    assert(imult32(0x3fffffff,2,&b)==1 && b==0x7ffffffe);

    assert(imult64(0x4000000000000000ll,2,&c)==0);
    assert(imult64(0x3fffffffffffffffll,2,&c)==1 && c==0x7ffffffffffffffell);
  }
#endif
#if 0
  stralloc a;
  printf("%d\n",stralloc_copym(&a,"fnord",", ","foo"));
#endif

  return 0;
}
Beispiel #28
0
int main(int argc,char* argv[]) {
  unsigned long count=1000;
  unsigned long interval=10;
  unsigned long sample=5;
  int keepalive=0;
  char ip[16];
  uint16 port=80;
  uint32 scope_id=0;
  stralloc ips={0};
  int s;
  char* request;
  int rlen;

  signal(SIGPIPE,SIG_IGN);

  if (!geteuid()) {
    struct rlimit rl;
    long l;
#ifdef RLIMIT_NPROC
    rl.rlim_cur=RLIM_INFINITY; rl.rlim_max=RLIM_INFINITY;
    setrlimit(RLIMIT_NPROC,&rl);
#endif
    for (l=0; l<20000; l+=500) {
      rl.rlim_cur=l; rl.rlim_max=l;
      if (setrlimit(RLIMIT_NOFILE,&rl)==-1) break;
    }
  }

  for (;;) {
    int i;
    int c=getopt(argc,argv,"c:i:s:kb");
    if (c==-1) break;
    switch (c) {
    case 'k':
      keepalive=1;
      break;
    case 'i':
      i=scan_ulong(optarg,&interval);
      if (i==0 || optarg[i]) {
	buffer_puts(buffer_2,"httpbench: warning: could not parse interval: ");
	buffer_puts(buffer_2,optarg+i+1);
	buffer_putsflush(buffer_2,"\n");
      }
      break;
    case 'c':
      i=scan_ulong(optarg,&count);
      if (i==0 || optarg[i]) {
	buffer_puts(buffer_2,"httpbench: warning: could not parse count: ");
	buffer_puts(buffer_2,optarg+i+1);
	buffer_putsflush(buffer_2,"\n");
      }
      break;
    case 's':
      i=scan_ulong(optarg,&sample);
      if (i==0 || optarg[i]) {
	buffer_puts(buffer_2,"httpbench: warning: could not parse sample size: ");
	buffer_puts(buffer_2,optarg+i+1);
	buffer_putsflush(buffer_2,"\n");
      }
      break;
    case 'b':
      bindport=10000;
      break;
    case '?':
usage:
      buffer_putsflush(buffer_2,
		  "usage: httpbench [-hb] [-c count] [-i interval] [-s sample] url\n"
		  "\n"
		  "\t-h\tprint this help\n"
		  "\t-c n\topen n connections total (default: 1000)\n"
		  "\t-i n\tevery n connections, measure the latency (default: 10)\n"
		  "\t-s n\tlatency == average of time to fetch an URL n times (default: 5)\n"
		  "\t-k\tenable HTTP keep-alive\n"
		  "\t-b\tbind the sockets ourselves, so the OS doesn't choose the ports\n"
		  "Setting the number of connections to 1 measures the throughput\n"
		  "instead of the latency (give URL to a large file).\n");
      return 0;
    }
  }

  if (!argv[optind]) goto usage;
  if (byte_diff(argv[optind],7,"http://")) goto usage;
  {
    char* host=argv[optind]+7;
    int colon=str_chr(host,':');
    int slash=str_chr(host,'/');
    char* c;
    if (host[0]=='[') {	/* ipv6 IP notation */
      int tmp;
      ++host;
      --colon; --slash;
      tmp=str_chr(host,']');
      if (host[tmp]==']') host[tmp]=0;
      if (host[tmp+1]==':') colon=tmp+1;
      if (colon<tmp+1) colon=tmp+1+str_len(host+tmp+1);
    }
    if (colon<slash) {
      host[colon]=0;
      c=host+colon+1;
      if (c[scan_ushort(c,&port)]!='/') goto usage;
      *c=0;
    }
    host[colon]=0;
    c=host+slash;
    *c=0;
    {
      char* tmp=alloca(str_len(host)+1);
      tmp[fmt_str(tmp,host)]=0;
      host=tmp;
    }
    *c='/';
    {
      int tmp=str_chr(host,'%');
      if (host[tmp]) {
	host[tmp]=0;
	scope_id=socket_getifidx(host+tmp+1);
	if (scope_id==0) {
	  buffer_puts(buffer_2,"httpbench: warning: network interface ");
	  buffer_puts(buffer_2,host+tmp+1);
	  buffer_putsflush(buffer_2," not found.\n");
	}
      }
    }

    {
      stralloc a={0};
      stralloc_copys(&a,host);
      if (dns_ip6(&ips,&a)==-1) {
	buffer_puts(buffer_2,"httpbench: could not resolve IP: ");
	buffer_puts(buffer_2,host);
	buffer_putnlflush(buffer_2);
	return 1;
      }
    }

    request=malloc(300+str_len(host)+str_len(c)*3);
    if (!request) panic("malloc");
    {
      int i;
      i=fmt_str(request,"GET ");
      i+=fmt_urlencoded(request+i,c,str_len(c));
      i+=fmt_str(request+i," HTTP/1.0\r\nHost: ");
      i+=fmt_str(request+i,host);
      i+=fmt_str(request+i,":");
      i+=fmt_ulong(request+i,port);
      i+=fmt_str(request+i,"\r\nUser-Agent: httpbench/1.0\r\nConnection: ");
      i+=fmt_str(request+i,keepalive?"keep-alive":"close");
      i+=fmt_str(request+i,"\r\n\r\n");
      rlen=i; request[rlen]=0;
    }

  }

  {
    int i;
    s=-1;
    for (i=0; i+16<=ips.len; i+=16) {
      char buf[IP6_FMT];
      int v6=byte_diff(ips.s+i,12,V4mappedprefix);
      buffer_puts(buffer_1,"connecting to ");
      buffer_put(buffer_1,buf,
		 v6?
		 fmt_ip6(buf,ips.s+i):
		 fmt_ip4(buf,ips.s+i+12));
      buffer_puts(buffer_1," port ");
      buffer_putulong(buffer_1,port);
      buffer_putnlflush(buffer_1);
      s=make_connection(ips.s+i,port,scope_id);
      if (s!=-1) {
	byte_copy(ip,16,ips.s+i);
	break;
      }
    }
    if (s==-1)
      return 1;
  }
  if (write(s,request,rlen)!=rlen) panic("write");
  if (readanswer(s,count==1)==-1) exit(1);
  close(s);
  if (count==1)
    return 0;

  {
    long i;
    long j;
    long err=0;
    int *socks;
    socks=malloc(sizeof(int)*count);
    if (!socks) panic("malloc");
    for (i=j=0; i<count; ++i) {
      struct timeval a,b;
      long d;
      if (j==0) {
	int k,s=0;
	long x=0,y=0;
	for (k=0; k<sample; ++k) {
	  if (!keepalive || !k) {
	    gettimeofday(&a,0);
	    s=make_connection(ip,port,scope_id);
	    if (s==-1)
	      panic("make_connection");
	    gettimeofday(&b,0);
	    d=(b.tv_sec-a.tv_sec)*1000000;
	    d=d+b.tv_usec-a.tv_usec;
	    x+=d;
	  }
	  gettimeofday(&a,0);
	  write(s,request,rlen);
	  if (readanswer(s,0)==-1) {
	    ++err;
	    keepalive=0;
	  }
	  gettimeofday(&b,0);
	  d=(b.tv_sec-a.tv_sec)*1000000;
	  d=d+b.tv_usec-a.tv_usec;
	  y+=d;
	  if (!keepalive) close(s);
	}
	if (keepalive) close(s);
	buffer_puts(buffer_1,"sample ");
	buffer_putulong(buffer_1,x);
	buffer_puts(buffer_1," ");
	buffer_putulong(buffer_1,y/sample);
	buffer_putnlflush(buffer_1);
      }
      ++j; if (j==interval) j=0;

      gettimeofday(&a,0);
      socks[i]=make_connection(ip,port,scope_id);
      if (socks[i]==-1)
	panic("make_connection");
      gettimeofday(&b,0);
      d=(b.tv_sec-a.tv_sec)*1000000;
      d=d+b.tv_usec-a.tv_usec;
      buffer_puts(buffer_1,"clat ");
      buffer_putulong(buffer_1,d);
      buffer_putnlflush(buffer_1);
    }
  }
  buffer_flush(buffer_1);
  return 0;
}