int timeoutconn6(int s,const char ip[16],uint16 port,unsigned int scope_id,unsigned int timeout)
{
  struct taia now;
  struct taia deadline;
  iopause_fd x;

  if (socket_connect6(s,ip,port,scope_id) == -1) {
    if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) return -1;
    x.fd = s;
    x.events = IOPAUSE_WRITE;
    taia_now(&now);
    taia_uint(&deadline,timeout);
    taia_add(&deadline,&now,&deadline);
    for (;;) {
      taia_now(&now);
      iopause(&x,1,&deadline,&now);
      if (x.revents) break;
      if (taia_less(&deadline,&now)) {
	errno = ETIMEDOUT; /* note that connect attempt is continuing */
	return -1;
      }
    }
    if (!socket_connected(s)) return -1;
  }

  if (ndelay_off(s) == -1) return -1;
  return 0;
}
int timeoutconn(int s,char ip[4],uint16 port,unsigned int timeout)
{
  struct taia now;
  struct taia deadline;
  iopause_fd x;

  if (socket_connect4(s,ip,port) == -1) {
    if ((errno != error_wouldblock) && (errno != error_inprogress)) return -1;
    x.fd = s;
    x.events = IOPAUSE_WRITE;
    taia_now(&now);
    taia_uint(&deadline,timeout);
    taia_add(&deadline,&now,&deadline);
    for (;;) {
      taia_now(&now);
      iopause(&x,1,&deadline,&now);
      if (x.revents) break;
      if (taia_less(&deadline,&now)) {
	    errno = error_timeout; /* note that connect attempt is continuing */
	    return -1;
      }
    }
    if (!socket_connected(s)) return -1;
  }

  if (ndelay_off(s) == -1) return -1;
  return 0;
}
Example #3
0
ssize_t curve25519_encode(struct curve25519_struct *curve, struct curve25519_proto *proto,
			  unsigned char *plaintext, size_t size, unsigned char **chipertext)
{
	int ret, i;
	ssize_t done = size;
	struct taia packet_taia;

	spinlock_lock(&curve->enc_lock);

	if (unlikely(size > curve->enc_buf_size)) {
		done = -ENOMEM;
		goto out;
	}

	taia_now(&packet_taia);
	taia_pack(proto->enonce + NONCE_OFFSET, &packet_taia);

	memset(curve->enc_buf, 0, curve->enc_buf_size);
	ret = crypto_box_afternm(curve->enc_buf, plaintext, size, proto->enonce, proto->key);
	if (unlikely(ret)) {
		done = -EIO;
		goto out;
	}

	fmemcpy(curve->enc_buf + crypto_box_boxzerobytes - NONCE_LENGTH,
	       proto->enonce + NONCE_OFFSET, NONCE_LENGTH);

	for (i = 0; i < crypto_box_boxzerobytes - NONCE_LENGTH; ++i)
		curve->enc_buf[i] = (uint8_t) secrand();

	(*chipertext) = curve->enc_buf;
out:
	spinlock_unlock(&curve->enc_lock);
	return done;
}
Example #4
0
int main()
{
  if (leapsecs_init() == -1) {
    fprintf(stderr,"utcnow: fatal: unable to init leapsecs\n");
    exit(111);
  }
    
  taia_now(&now);
  x[taia_fmtfrac(x,&now)] = 0;

  taia_tai(&now,&sec);
  caltime_utc(&ct,&sec,(int *) 0,(int *) 0);

  printf("%ld-%02d-%02d %02d:%02d:%02d.%s\n"
    ,ct.date.year
    ,ct.date.month
    ,ct.date.day
    ,ct.hour
    ,ct.minute
    ,ct.second
    ,x
    );

  exit(0);
}
Example #5
0
int
ud_init (struct udoc *ud)
{
  bin_zero (ud, sizeof (*ud));
  if (!ud_oht_init (&ud->ud_parts, sizeof (struct ud_part))) goto FAIL;
  if (!ud_oht_init (&ud->ud_link_exts, sizeof (struct ud_ref))) goto FAIL;
  if (!ud_oht_init (&ud->ud_refs, sizeof (struct ud_ref))) goto FAIL;
  if (!ud_oht_init (&ud->ud_ref_names, sizeof (struct ud_ref))) goto FAIL;
  if (!ud_oht_init (&ud->ud_footnotes, sizeof (struct ud_ref))) goto FAIL;
  if (!ud_oht_init (&ud->ud_styles, sizeof (struct ud_ref))) goto FAIL;
  if (!dstack_init (&ud->ud_errors, 16, sizeof (struct ud_err))) goto FAIL;
  if (!token_init (&ud->ud_tok)) goto FAIL;

  ud->ud_dirfd_pwd = open_ro (".");
  if (ud->ud_dirfd_pwd == -1) goto FAIL;
  ud->ud_dirfd_src = -1;
  ud->ud_dirfd_out = -1;
  ud->ud_main_doc = ud;
  ud->ud_cur_doc = ud;

  if (!ht_init (&ud->ud_loopchecks)) goto FAIL;
  if (!ht_init (&ud->ud_documents)) goto FAIL;

  taia_now (&ud->ud_time_start);

  return 1;
  FAIL:
  ud_free (ud);
  return 0;
}
Example #6
0
int main(void)
{
  struct taia taia;
  char stamp[TAIA_TAI64N + 1];
  char ch;

  stamp[0] = '@';
  for (;;) {
    if (buffer_get(buffer0, &ch, 1) != 1) { done(); goto FINISH; }
    taia_now(&taia);
    taia_tai64n(stamp + 1, &taia);

    stamp[sizeof(stamp) - 1] = ' ';
    buffer_put(buffer1, stamp, sizeof(stamp));

    for (;;) {
      buffer_put(buffer1, &ch, 1);
      if (ch == '\n') break;
      if (buffer_get(buffer0, &ch, 1) != 1) { done(); goto FINISH; }
    }
  }

  FINISH:
  return 0;
}
Example #7
0
static int proto_encode(sigma_proto *instance, uint8_t* input, uint8_t* output, size_t len)
{
    sigma_proto_nacl* inst = (sigma_proto_nacl*) instance;

    uint8_t tempbufferinput[len + crypto_box_ZEROBYTES];

    bzero(tempbufferinput, crypto_box_ZEROBYTES);
    memcpy(tempbufferinput + crypto_box_ZEROBYTES, input, len);

    len += crypto_box_ZEROBYTES;

    taia_now(&inst->cdtaie);
    taia_pack(inst->encnonce + nonceoffset, &(inst->cdtaie));

    int result = crypto_box_afternm(
        output,
        tempbufferinput,
        len,
        inst->encnonce,
        ((sigma_proto_nacl*) instance)->precomp
    );

    if (result)
    {
        fprintf(stderr, "Encryption failed (length %u, given result %i)\n", (unsigned) len, result);
        errno = EINVAL;
        return -1;
    }

    memcpy(output, inst->encnonce + nonceoffset, noncelength);

    return len;
}
int socket_timeoutconn (int s, char const *ip, uint16 port, unsigned int timeout)
{
  struct taia stamp, deadline ;
  taia_now(&stamp) ;
  taia_addsec(&deadline, &stamp, timeout) ;
  return socket_deadlineconnstamp4(s, ip, port, &deadline, &stamp) ;
}
Example #9
0
int setup_log()
{
    if ((rploglen =str_len(rplog)) < 7)
    {
        warn3x("log must have at least seven characters.", 0, 0);
        return(0);
    }
    if (pipe(logpipe) == -1)
    {
        warn3x("unable to create pipe for log.", 0, 0);
        return(-1);
    }
    coe(logpipe[1]);
    coe(logpipe[0]);
    ndelay_on(logpipe[0]);
    ndelay_on(logpipe[1]);
    if (fd_copy(2, logpipe[1]) == -1)
    {
        warn3x("unable to set filedescriptor for log.", 0, 0);
        return(-1);
    }
    io[0].fd =logpipe[0];
    io[0].events =IOPAUSE_READ;
    taia_now(&stamplog);
    return(1);
}
Example #10
0
int
dns_resolve (const char *q, const char qtype[2])
{
    int r = 0;
    char servers[64];
    iopause_fd x[1];

    struct taia stamp;
    struct taia deadline;

    if (dns_resolvconfip (servers) == -1)
        return -1;

    memset (&dns_resolve_tx, 0, sizeof (dns_resolve_tx));
    r = dns_transmit_start (&dns_resolve_tx, servers, 1, q, qtype, "\0\0\0\0");
    if (r == -1)
        return -1;

    for (;;)
    {
        taia_now (&stamp);
        taia_uint (&deadline, 120);
        taia_add (&deadline, &deadline, &stamp);

        dns_transmit_io (&dns_resolve_tx, x, &deadline);
        iopause (x, 1, &deadline, &stamp);

        r = dns_transmit_get (&dns_resolve_tx, x, &stamp);
        if (r == -1)
            return -1;
        if (r == 1)
            return 0;
    }
}
Example #11
0
void
dns_transmit_io (struct dns_transmit *d, iopause_fd *x, struct taia *deadline)
{
    x->fd = d->s1 - 1;

    switch (d->tcpstate)
    {
    case 0:
        if (d->master)
            return;
        if (d->packet)
        {
            taia_now (deadline);
            return;
        }
        /* otherwise, fall through */
    case 3:
    case 4:
    case 5:
        x->events = IOPAUSE_READ;
        break;

    case 1:
    case 2:
        x->events = IOPAUSE_WRITE;
    }

    if (taia_less (&d->deadline, deadline))
        *deadline = d->deadline;
}
Example #12
0
void t_timeout(int j)
{
  struct taia now;
  if (!t[j].active) return;
  taia_now(&now);
  taia_uint(&t[j].timeout,10);
  taia_add(&t[j].timeout,&t[j].timeout,&now);
}
Example #13
0
PyObject *TaiaNow(PyObject *self, PyObject *args) {
  TaiaObject *taia;
  
  taia = PyObject_New(TaiaObject, &TaiaType);
  taia_now(&taia->t);
  
  return (PyObject *)taia;
}
Example #14
0
static void blog_modified(struct cdbb *a)
{
	char pk[TAIA_PACK];
	struct taia t;

	taia_now(&t);
	taia_pack(pk, &t);
	cdbb_rep(a, DB_LAST_MODIFIED, 13, pk, TAIA_PACK);
}
Example #15
0
int ssl_timeoutaccept(SSL *ssl,unsigned int timeout)
{
  struct taia now;
  struct taia deadline;
  iopause_fd x;
  int r;
  int rfd;
  int wfd;

  taia_now(&now);
  taia_uint(&deadline,timeout);
  taia_add(&deadline,&now,&deadline);

  rfd = SSL_get_fd(ssl); /* XXX */
  wfd = SSL_get_fd(ssl); /* XXX */

  SSL_set_accept_state(ssl);

  for (;;) {
    r = SSL_accept(ssl);
    if (r == 1) return 0;
    ssl_errno = SSL_get_error(ssl,r);
    errno = error_proto;
    if ((ssl_errno != SSL_ERROR_WANT_READ) && (ssl_errno != SSL_ERROR_WANT_WRITE))
      return -1;
    if (ssl_errno == SSL_ERROR_WANT_READ) {
      x.events = IOPAUSE_READ;
      x.fd = rfd;
      if (x.fd == -1) return -1;
    }
    else {
      x.events = IOPAUSE_WRITE;
      x.fd = wfd;
      if (x.fd == -1) return -1;
    }
    for (;;) {
      taia_now(&now);
      iopause(&x,1,&deadline,&now);
      if (x.revents) break;
      if (taia_less(&deadline,&now))
	return -1;
    }
  }
}
Example #16
0
int
timestr_parse(const char * input, struct taia * res, struct taia * b)
{
    struct taia base, * pbase;
    const char * p;
    uint32_t w, d, h, m, s;
    unsigned int n;

    if (!input || !res) return -1;
    if (b) pbase = b;
    else {
        taia_now(&base);
        pbase = &base;
    }
    p = input;
    w = d = h = m = s = 0;

next:
    n = 0;
    while (*p && (*p >= '0' && *p <= '9')) {
        n *= 10;
        n += (*p-'0');
        p++;
    }
    if (!*p) return -1;

    switch (*p) {
    case 'w':
        if (d*h*m*s) return -1;
        w = n*604800;
        break;
    case 'd':
        if (h*m*s) return -1;
        d = n*86400;
        break;
    case 'h':
        if (m*s) return -1;
        h = n*3600;
        break;
    case 'm':
        if (s) return -1;
        m = n*60;
        break;
    case 's':
        s = n;
        break;
    default:
        return -1;
    }
    if (*++p) goto next;

    res->sec.x = pbase->sec.x - (w+d+h+m+s);
    res->nano = pbase->nano;

    return 0;
}
Example #17
0
void
u_new (void)
{
    int i = 0, j = 0, len = 0;
    struct udpclient *x = NULL;

    static char *q = 0;
    char qtype[2], qclass[2];

    for (j = 0; j < MAXUDP; j++)
        if (!u[j].active)
            break;

    if (j >= MAXUDP)
    {
        j = 0;
        for (i = 1; i < MAXUDP; i++)
            if (taia_less (&u[i].start, &u[j].start))
                j = i;
        errno = error_timeout;
        u_drop (j);
    }

    x = u + j;
    taia_now (&x->start);

    len = socket_recv4 (udp53, buf, sizeof (buf), x->ip, &x->port, &odst);
    if (len == -1)
        return;
    if ((unsigned)len >= sizeof buf)
        return;
    if (x->port < 1024 && x->port != 53)
        return;
    if (!okclient (x->ip))
        return;
    if (!packetquery (buf, len, &q, qtype, qclass, x->id))
        return;

    x->active = ++numqueries;
    ++uactive;

    if (debug_level)
        log_query (x->active, x->ip, x->port, x->id, q, qtype);

    switch (query_start (&x->q, q, qtype, qclass, myipoutgoing))
    {
    case -1:
        u_drop (j);
        return;

    case 1:
        u_respond (j);
    }
}
Example #18
0
void
t_new (void)
{
    int i = 0, j = 0;
    struct tcpclient *x = NULL;

    for (j = 0; j < MAXTCP; ++j)
        if (!t[j].active)
          break;

    if (j >= MAXTCP)
    {
        j = 0;
        for (i = 1; i < MAXTCP; ++i)
            if (taia_less (&t[i].start, &t[j].start))
                j = i;
        errno = error_timeout;
        if (t[j].state == 0)
          t_drop (j);
        else
          t_close (j);
    }

    x = t + j;
    taia_now (&x->start);

    x->tcp = socket_accept4 (tcp53, x->ip, &x->port);
    if (x->tcp == -1)
        return;
    if (x->port < 1024 && x->port != 53)
    {
        close (x->tcp);
        return;
    }
    if (!okclient (x->ip))
    {
        close (x->tcp);
        return;
    }
    if (ndelay_on (x->tcp) == -1)
    {
        close(x->tcp);
        return;
    } /* Linux bug */

    x->active = 1;
    ++tactive;
    x->state = 1;
    t_timeout (j);

    if (debug_level > 2)
        log_tcpopen (x->ip, x->port);
}
Example #19
0
ssize_t http_sendiovecdata( const int64 sock, struct ot_workstruct *ws, int iovec_entries, struct iovec *iovector ) {
  struct http_data *cookie = io_getcookie( sock );
  char *header;
  int i;
  size_t header_size, size = iovec_length( &iovec_entries, &iovector );
  tai6464 t;

  /* No cookie? Bad socket. Leave. */
  if( !cookie ) {
    iovec_free( &iovec_entries, &iovector );
    HTTPERROR_500;
  }

  /* If this socket collected request in a buffer, free it now */
  array_reset( &cookie->request );

  /* If we came here, wait for the answer is over */
  cookie->flag &= ~STRUCT_HTTP_FLAG_WAITINGFORTASK;

  /* Our answers never are 0 vectors. Return an error. */
  if( !iovec_entries ) {
    HTTPERROR_500;
  }

  /* Prepare space for http header */
  header = malloc( SUCCESS_HTTP_HEADER_LENGTH + SUCCESS_HTTP_HEADER_LENGTH_CONTENT_ENCODING );
  if( !header ) {
    iovec_free( &iovec_entries, &iovector );
    HTTPERROR_500;
  }

  if( cookie->flag & STRUCT_HTTP_FLAG_GZIP )
    header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Encoding: gzip\r\nContent-Length: %zd\r\n\r\n", size );
  else if( cookie->flag & STRUCT_HTTP_FLAG_BZIP2 )
    header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Encoding: bzip2\r\nContent-Length: %zd\r\n\r\n", size );
  else
    header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %zd\r\n\r\n", size );

  iob_reset( &cookie->batch );
  iob_addbuf_free( &cookie->batch, header, header_size );

  /* Will move to ot_iovec.c */
  for( i=0; i<iovec_entries; ++i )
    iob_addbuf_munmap( &cookie->batch, iovector[i].iov_base, iovector[i].iov_len );
  free( iovector );

  /* writeable sockets timeout after 10 minutes */
  taia_now( &t ); taia_addsec( &t, &t, OT_CLIENT_TIMEOUT_SEND );
  io_timeout( sock, t );
  io_dontwantread( sock );
  io_wantwrite( sock );
  return 0;
}
Example #20
0
void
profile(const char *s)
{
#ifdef ENABLE_PROFILE
	char buf[TAIA_PACK];
	struct taia t;

	taia_now(&t);
	taia_pack(buf,&t);
	logit(LOG_PROFILE, "PROFILE: %s @%s\n", s, buf); 
#endif
}
Example #21
0
int fmt_accustamp(char s[TIMESTAMP])
{
  struct taia now;
  int len;

  taia_now(&now);

  len = fmt_ulong(s,(unsigned long)tai_tounix(&now.sec));
  s[len++] = '.';
  len += fmt_uint0(s+len,now.nano / 1000,6);
  return len;
}
Example #22
0
unsigned int
px_fmt_timestamp(char *ptr)
{
  struct taia tai;

  if (ptr) {
    taia_now(&tai);
    taia_tai64n(ptr + 1, &tai);
    ptr[0] = '@';
  }
  return TAIA_TAI64N;
}
Example #23
0
ssize_t curve25519_decode(struct curve25519_struct *c, struct curve25519_proto *p,
			  unsigned char *chipertext, size_t size,
			  unsigned char **plaintext, struct taia *arrival_taia)
{
	int ret;
	ssize_t done = size;
	struct taia packet_taia, __arrival_taia;

	spinlock_lock(&c->dec_lock);

	if (unlikely(size > c->dec_buf_size)) {
		spinlock_unlock(&c->dec_lock);
		return -ENOMEM;
	}

	if (unlikely(size < crypto_box_boxzerobytes + NONCE_LENGTH)) {
		spinlock_unlock(&c->dec_lock);
		return 0;
	}
	if (arrival_taia == NULL) {
		taia_now(&__arrival_taia);
		arrival_taia = &__arrival_taia;
	}

	taia_unpack(chipertext + crypto_box_boxzerobytes - NONCE_LENGTH,
		    &packet_taia);
        if (is_good_taia(arrival_taia, &packet_taia) == 0) {
		/* Ignoring packet */
		spinlock_unlock(&c->dec_lock);
		syslog(LOG_ERR, "Bad packet time! Dropping connection!\n");
		return 0;
	}

	memcpy(p->dnonce + NONCE_OFFSET,
	       chipertext + crypto_box_boxzerobytes - NONCE_LENGTH,
	       NONCE_LENGTH);

	memset(c->dec_buf, 0, c->dec_buf_size);

	ret = crypto_box_open_afternm(c->dec_buf, chipertext, size,
				      p->dnonce, p->key);
	if (unlikely(ret)) {
		spinlock_unlock(&c->dec_lock);
		return -EIO;
	}

	(*plaintext) = c->dec_buf;

	spinlock_unlock(&c->dec_lock);

	return done;
}
Example #24
0
int64 io_trywritetimeout(int64 d,const char* buf,int64 len) {
    int64 r=io_trywrite(d,buf,len);
    if (r==-1) {
        tai6464 x;
        io_entry* e=array_get(&io_fds,sizeof(io_entry),d);
        taia_now(&x);
        if (!taia_less(&x,&e->timeout)) {
            errno=ETIMEDOUT;
            r=-2;
        }
    }
    return r;
}
Example #25
0
int timeoutwrite(int t,int fd,const char *buf,int len)
{
  struct taia now;
  struct taia deadline;
  iopause_fd x;

  taia_now(&now);
  taia_uint(&deadline,t);
  taia_add(&deadline,&now,&deadline);

  x.fd = fd;
  x.events = IOPAUSE_WRITE;
  for (;;) {
    taia_now(&now);
    iopause(&x,1,&deadline,&now);
    if (x.revents) break;
    if (taia_less(&deadline,&now)) {
      errno = error_timeout;
      return -1;
    }
  }
  return write(fd,buf,len);
}
Example #26
0
static void pidchange()
{
	struct taia now;
	unsigned long u;

	taia_now(&now);
	taia_pack(status, &now);

	u = (unsigned long) pid;
	status[16] = u; u >>= 8;
	status[17] = u; u >>= 8;
	status[18] = u; u >>= 8;
	status[19] = u;
}
Example #27
0
PyObject *pytaia_now_pack(PyObject *self){
  PyObject *ret;
  unsigned char *tpad;
  tpad = PyMem_Malloc(16);

  if (!tpad)
    return PyErr_NoMemory();

  taia_now(tpad);
  taia_pack(tpad,tpad);

  ret = PyBytes_FromStringAndSize((char *)tpad,16);
  PyMem_Free(tpad);
  return ret;}
Example #28
0
void pidchange(void)
{
  struct taia now;
  unsigned long u;

  taia_now(&now);
  taia_pack(status,&now);

  u = (unsigned long) pid;
  status[12] = u; u >>= 8;
  status[13] = u; u >>= 8;
  status[14] = u; u >>= 8;
  status[15] = u;
}
int timeoutread(int t,int fd,char *buf,int len)
{
  struct taia now;
  struct taia deadline;
  iopause_fd x;

  taia_now(&now);
  taia_uint(&deadline,t);
  taia_add(&deadline,&now,&deadline);

  x.fd = fd;
  x.events = IOPAUSE_READ;
  for (;;) {
    taia_now(&now);
    iopause(&x,1,&deadline,&now);
    if (x.revents) break;
    if (taia_less(&deadline,&now)) {
      errno = ETIMEDOUT;
      return -1;
    }
  }
  return read(fd,buf,len);
}
Example #30
0
static int
thistcp (struct dns_transmit *d)
{
    struct taia now;
    const char *ip = NULL;

    socketfree (d);
    packetfree (d);

    for (; d->curserver < 16; ++d->curserver)
    {
        ip = d->servers + 4 * d->curserver;
        if (byte_diff (ip, 4, "\0\0\0\0"))
        {
            d->query[2] = dns_random (256);
            d->query[3] = dns_random (256);

            d->s1 = 1 + socket_tcp ();
            if (!d->s1)
            {
                dns_transmit_free (d);
                return -1;
            }
            if (randombind (d) == -1)
            {
                dns_transmit_free (d);
                return -1;
            }

            taia_now (&now);
            taia_uint (&d->deadline, 10);
            taia_add (&d->deadline, &d->deadline, &now);
            if (socket_connect4 (d->s1 - 1, ip, 53) == 0)
            {
                d->pos = 0;
                d->tcpstate = 2;
                return 0;
            }
            if (errno == error_inprogress || errno == error_wouldblock)
            {
                d->tcpstate = 1;
                return 0;
            }
            socketfree(d);
        }
    }

    dns_transmit_free(d);
    return -1;
}