Пример #1
0
int dill_tcp_close(int s, int64_t deadline) {
    int err;
    /* Listener socket needs no special treatment. */
    if(dill_hquery(s, dill_tcp_listener_type)) {
        return dill_hclose(s);
    }
    struct dill_tcp_conn *self = dill_hquery(s, dill_tcp_type);
    if(dill_slow(!self)) return -1;
    if(dill_slow(self->inerr || self->outerr)) {err = ECONNRESET; goto error;}
    /* If not done already, flush the outbound data and start the terminal
       handshake. */
    if(!self->outdone) {
        int rc = dill_tcp_done(s, deadline);
        if(dill_slow(rc < 0)) {err = errno; goto error;}
    }
    /* Now we are going to read all the inbound data until we reach end of the
       stream. That way we can be sure that the peer either received all our
       data or consciously closed the connection without reading all of it. */
    int rc = dill_tcp_brecvl(&self->bvfs, NULL, NULL, deadline);
    dill_assert(rc < 0);
    if(dill_slow(errno != EPIPE)) {err = errno; goto error;}
    dill_tcp_hclose(&self->hvfs);
    return 0;
error:
    dill_tcp_hclose(&self->hvfs);
    errno = err;
    return -1;
}
Пример #2
0
int tcpaccept(int s, int64_t deadline) {
    int err;
    struct tcplistener *lst = hdata(s, tcplistener_type);
    if(dill_slow(!lst)) return -1;
    /* Try to get new connection in a non-blocking way. */
    ipaddr addr;
    socklen_t addrlen;
    int as = dsaccept(lst->fd, (struct sockaddr*)&addr, &addrlen, deadline);
    if(dill_slow(as < 0)) return -1;
    tcptune(as);
    /* Create the object. */
    struct tcpconn *conn = tcpconn_create();
    if(dill_slow(!conn)) {err = errno; goto error1;}
    conn->fd = as;
    conn->addr = addr;
    /* Bind the object to a handle. */
    int hndl = bsock(tcpconn_type, conn, &tcpconn_vfptrs);
    if(dill_slow(hndl < 0)) {err = errno; goto error2;}
    return hndl;
error2:
    tcpconn_destroy(conn);
error1:;
    int rc = dsclose(s);
    dill_assert(rc == 0);
    errno = err;
    return -1;
}
Пример #3
0
static int dill_tls_brecvl(struct dill_bsock_vfs *bvfs,
      struct dill_iolist *first, struct dill_iolist *last, int64_t deadline) {
    struct dill_tls_sock *self = dill_cont(bvfs, struct dill_tls_sock, bvfs);
    if(dill_slow(self->indone)) {errno = EPIPE; return -1;}
    if(dill_slow(self->inerr)) {errno = ECONNRESET; return -1;}
    self->deadline = deadline;
    struct dill_iolist *it = first;
    while(1) {
        uint8_t *base = it->iol_base;
        size_t len = it->iol_len;
        while(1) {
            ERR_clear_error();
            int rc = SSL_read(self->ssl, base, len);
            if(dill_tls_followup(self, rc)) {
                if(dill_slow(errno != 0)) {
                    if(errno == EPIPE) self->indone = 1;
                    else self->inerr = 1;
                    return -1;
                }
                if(rc == len) break;
                base += rc;
                len -= rc;
            }
        }
        if(it == last) break;
        it = it->iol_next;
    }
    return 0;
}
Пример #4
0
int dill_tls_detach(int s, int64_t deadline) {
    int err;
    struct dill_tls_sock *self = dill_hquery(s, dill_tls_type);
    if(dill_slow(!self)) return -1;
    if(dill_slow(self->inerr || self->outerr)) {err = ECONNRESET; goto error;}
    /* Start terminal TLS handshake. */
    if(!self->outdone) {
        int rc = dill_tls_done(s, deadline);
        if(dill_slow(rc < 0)) {err = errno; goto error;}
    }
    /* Wait for the handshake acknowledgement from the peer. */
    if(!self->indone) {
        while(1) {
            ERR_clear_error();
            int rc = SSL_shutdown(self->ssl);
            if(rc == 1) break;
            if(dill_tls_followup(self, rc)) {err = errno; goto error;}
        }
    }
    int u = self->u;
    self->u = -1;
    dill_tls_hclose(&self->hvfs);
    return u;
error:
    dill_tls_hclose(&self->hvfs);
    errno = err;
    return -1;
}
Пример #5
0
int msendl(int s, struct iolist *first, struct iolist *last, int64_t deadline) {
    struct msock_vfs *m = hquery(s, msock_type);
    if(dill_slow(!m)) return -1;
    if(dill_slow(!first || !last || last->iol_next)) {
        errno = EINVAL; return -1;}
    return m->msendl(m, first, last, deadline);
}
Пример #6
0
int dill_iolcheck(struct iolist *first, struct iolist *last,
      size_t *nbufs, size_t *nbytes) {
    if(!first && !last) {
        if(nbufs) *nbufs = 0;
        if(nbytes) *nbytes = 0;
        return 0;
    }
    if(dill_slow(!first || !last || last->iol_next)) {
        errno = EINVAL; return -1;}
    size_t nbf = 0, nbt = 0, res = 0;
    struct iolist *it;
    for(it = first; it; it = it->iol_next) {
        if(dill_slow(it->iol_rsvd || (!it->iol_next && it != last)))
            goto error;
        it->iol_rsvd = 1;
        nbf++;
        nbt += it->iol_len;
    }
    for(it = first; it; it = it->iol_next) it->iol_rsvd = 0;
    if(nbufs) *nbufs = nbf;
    if(nbytes) *nbytes = nbt;
    return 0;
error:;
    struct iolist *it2;
    for(it2 = first; it2 != it; it2 = it2->iol_next) it->iol_rsvd = 0;
    errno = EINVAL;
    return -1;
}
Пример #7
0
int dill_term_detach(int s, int64_t deadline) {
    int err;
    struct dill_term_sock *self = dill_hquery(s, dill_term_type);
    if(dill_slow(!self)) return -1;
    if(!self->outdone) {
        int rc = dill_term_done(s, deadline);
        if(dill_slow(rc < 0)) {err = errno; goto error;}
    }
    if(!self->indone) {
        while(1) {
            struct dill_iolist iol = {NULL, SIZE_MAX, NULL, 0};
            ssize_t sz = dill_term_mrecvl(&self->mvfs, &iol, &iol, deadline);
            if(sz < 0) {
                if(errno == EPIPE) break;
                err = errno;
                goto error;
            }
        }
    }
    int u = self->u;
    if(!self->mem) free(self);
    return u;
error:;
    int rc = dill_hclose(s);
    dill_assert(rc == 0);
    errno = err;
    return -1;
}
Пример #8
0
static int sf_send(int s, const void *buf, size_t len, int64_t deadline) {
    struct sf *conn = msockdata(s, sf_type);
    if(dill_slow(!conn)) return -1;
    if(dill_slow(conn->res != SF_ACTIVE)) {errno = ECONNRESET; return -1;}
    /* Create a message object. */
    struct msg msg;
    msg.buf = malloc(len);
    if(dill_slow(!msg.buf)) {errno = ENOMEM; return -1;}
    memcpy(msg.buf, buf, len);
    msg.len = len;
    /* Send it to the worker. */
    int rc = chsend(conn->ochan, &msg, sizeof(msg), deadline);
    if(dill_fast(rc >= 0)) return 0;
    /* Closed pipe means that the connection was terminated. */
    if(errno == EPIPE) {
        dill_assert(conn->ores == SF_RESET);
        conn->res = SF_RESET;
        errno = ECONNRESET;
    }
    /* Clean up. */
    int err = errno;
    free(msg.buf);
    errno = err;
    return -1;
}
Пример #9
0
int dill_tcp_accept_mem(int s, struct dill_ipaddr *addr,
        struct dill_tcp_storage *mem, int64_t deadline) {
    int err;
    if(dill_slow(!mem)) {err = EINVAL; goto error1;}
    /* Retrieve the listener object. */
    struct dill_tcp_listener *lst = dill_hquery(s, dill_tcp_listener_type);
    if(dill_slow(!lst)) {err = errno; goto error1;}
    /* Try to get new connection in a non-blocking way. */
    socklen_t addrlen = sizeof(struct dill_ipaddr);
    int as = dill_fd_accept(lst->fd, (struct sockaddr*)addr, &addrlen,
        deadline);
    if(dill_slow(as < 0)) {err = errno; goto error1;}
    /* Set it to non-blocking mode. */
    int rc = dill_fd_unblock(as);
    if(dill_slow(rc < 0)) {err = errno; goto error2;}
    /* Create the handle. */
    int h = dill_tcp_makeconn(as, mem);
    if(dill_slow(h < 0)) {err = errno; goto error2;}
    return h;
error2:
    dill_fd_close(as);
error1:
    errno = err;
    return -1;
}
Пример #10
0
int tcpconnect(const ipaddr *addr, int64_t deadline) {
    int err;
    /* Open a socket. */
    int s = socket(ipfamily(addr), SOCK_STREAM, 0);
    if(dill_slow(s < 0)) return -1;
    tcptune(s);
    /* Connect to the remote endpoint. */
    int rc = dsconnect(s, ipsockaddr(addr), iplen(addr), deadline);
    if(dill_slow(rc < 0)) return -1;
    /* Create the object. */
    struct tcpconn *conn = tcpconn_create();
    if(dill_slow(!conn)) {err = errno; goto error1;}
    conn->fd = s;
    conn->addr = *addr;
    /* Bind the object to a sock handle. */
    int bs = bsock(tcpconn_type, conn, &tcpconn_vfptrs);
    if(dill_slow(bs < 0)) {err = errno; goto error2;}
    return bs;
error2:
    tcpconn_destroy(conn);
error1:
    rc = dsclose(s);
    dill_assert(rc == 0);
    errno = err;
    return -1;
}
int dsrecv(int s, void *buf, size_t *len, int64_t deadline) {
    size_t received = 0;
    while(1) {
        ssize_t sz = recv(s, ((char*)buf) + received, *len - received, 0);
        if(dill_slow(sz == 0)) {
            *len = received;
            errno = ECONNRESET;
            return -1;
        }
        if(sz < 0) {
            if(dill_slow(errno != EWOULDBLOCK && errno != EAGAIN)) {
                *len = received;
                return -1;
            }
        }
        else {
            received += sz;
            if(received >= *len)
                return 0;
        }
        int rc = fdin(s, deadline);
        if(dill_slow(rc < 0)) {
            *len = received;
            return -1;
        }
    }
}
Пример #12
0
int udp_sendl_(struct msock_vfs *mvfs, const struct ipaddr *addr,
      struct iolist *first, struct iolist *last) {
    struct udp_sock *obj = dill_cont(mvfs, struct udp_sock, mvfs);
    /* If no destination IP address is provided, fall back to the stored one. */
    const struct ipaddr *dstaddr = addr;
    if(!dstaddr) {
        if(dill_slow(!obj->hasremote)) {errno = EINVAL; return -1;}
        dstaddr = &obj->remote;
    }
    struct msghdr hdr;
    memset(&hdr, 0, sizeof(hdr));
    hdr.msg_name = (void*)ipaddr_sockaddr(dstaddr);
    hdr.msg_namelen = ipaddr_len(dstaddr);
    /* Make a local iovec array. */
    /* TODO: This is dangerous, it may cause stack overflow.
       There should probably be a on-heap per-socket buffer for that. */
    size_t niov;
    int rc = iol_check(first, last, &niov, NULL);
    if(dill_slow(rc < 0)) return -1;
    struct iovec iov[niov];
    iol_toiov(first, iov);
    hdr.msg_iov = (struct iovec*)iov;
    hdr.msg_iovlen = niov;
    ssize_t sz = sendmsg(obj->fd, &hdr, 0);
    if(dill_fast(sz >= 0)) return 0;
    if(errno == EAGAIN || errno == EWOULDBLOCK) return 0;
    return -1;
}
Пример #13
0
int dill_prefix_attach_mem(int s, size_t hdrlen, int flags,
      struct dill_prefix_storage *mem) {
    int err;
    if(dill_slow(!mem || hdrlen == 0)) {err = EINVAL; goto error;}
    /* Take ownership of the underlying socket. */
    s = dill_hown(s);
    if(dill_slow(s < 0)) {err = errno; goto error;}
    /* Check whether underlying socket is a bytestream. */
    void *q = dill_hquery(s, dill_bsock_type);
    if(dill_slow(!q && errno == ENOTSUP)) {err = EPROTO; goto error;}
    if(dill_slow(!q)) {err = errno; goto error;}
    /* Create the object. */
    struct dill_prefix_sock *self = (struct dill_prefix_sock*)mem;
    self->hvfs.query = dill_prefix_hquery;
    self->hvfs.close = dill_prefix_hclose;
    self->mvfs.msendl = dill_prefix_msendl;
    self->mvfs.mrecvl = dill_prefix_mrecvl;
    self->u = s;
    self->hdrlen = hdrlen;
    self->bigendian = !(flags & DILL_PREFIX_LITTLE_ENDIAN);
    self->inerr = 0;
    self->outerr = 0;
    self->mem = 1;
    /* Create the handle. */
    int h = dill_hmake(&self->hvfs);
    if(dill_slow(h < 0)) {int err = errno; goto error;}
    return h;
error:
    if(s >= 0) dill_hclose(s);
    errno = err;
    return -1;
}
Пример #14
0
ssize_t udp_recvl_(struct msock_vfs *mvfs, struct ipaddr *addr,
      struct iolist *first, struct iolist *last, int64_t deadline) {
    struct udp_sock *obj = dill_cont(mvfs, struct udp_sock, mvfs);
    struct msghdr hdr;
    memset(&hdr, 0, sizeof(hdr));
    hdr.msg_name = (void*)addr;
    hdr.msg_namelen = sizeof(struct ipaddr);
    /* Make a local iovec array. */
    /* TODO: This is dangerous, it may cause stack overflow.
       There should probably be a on-heap per-socket buffer for that. */
    size_t niov;
    int rc = iol_check(first, last, &niov, NULL);
    if(dill_slow(rc < 0)) return -1;
    struct iovec iov[niov];
    iol_toiov(first, iov);
    hdr.msg_iov = (struct iovec*)iov;
    hdr.msg_iovlen = niov;
    while(1) {
        ssize_t sz = recvmsg(obj->fd, &hdr, 0);
        if(sz >= 0) return sz;
        if(errno != EAGAIN && errno != EWOULDBLOCK) return -1;
        rc = fdin(obj->fd, deadline);
        if(dill_slow(rc < 0)) return -1;
    }
}
Пример #15
0
static ssize_t dill_term_mrecvl(struct dill_msock_vfs *mvfs,
      struct dill_iolist *first, struct dill_iolist *last, int64_t deadline) {
    struct dill_term_sock *self = dill_cont(mvfs, struct dill_term_sock, mvfs);
    if(self->len == 0) {
        ssize_t sz = dill_mrecvl(self->u, first, last, deadline);
        if(dill_slow(sz < 0)) return -1;
        if(dill_slow(sz == 0)) {
            self->indone = 1;
            errno = EPIPE;
            return -1;
        }
        return sz;
    }
    struct dill_iolist trimmed = {0};
    int rc = dill_ioltrim(first, self->len, &trimmed);
    uint8_t buf[self->len];
    struct dill_iolist iol = {buf, self->len, rc < 0 ? NULL : &trimmed, 0}; 
    ssize_t sz = dill_mrecvl(self->u, &iol, rc < 0 ? &iol : last, deadline);
    if(dill_slow(sz < 0)) return -1;
    if(dill_slow(sz == self->len &&
          dill_slow(memcmp(self->buf, buf, self->len) == 0))) {
        self->indone = 1;
        errno = EPIPE;
        return -1;
    }
    dill_iolto(buf, self->len, first);
    return sz;
}
Пример #16
0
static int sf_finish(int s, int64_t deadline) {
    int u = sfdetach(s, deadline);
    if(dill_slow(u < 0)) return -1;
    int rc = hfinish(u, deadline);
    if(dill_slow(rc != 0)) return -1;
    return 0;
}
Пример #17
0
int dill_term_done(int s, int64_t deadline) {
    struct dill_term_sock *self = dill_hquery(s, dill_term_type);
    if(dill_slow(!self)) return -1;
    if(dill_slow(self->outdone)) {errno = EPIPE; return -1;}
    int rc = dill_msend(self->u, self->buf, self->len, deadline);
    if(dill_slow(rc < 0)) return -1;
    self->outdone = 1;
    return 0;
}
Пример #18
0
ssize_t mrecvl(int s, struct iolist *first, struct iolist *last,
      int64_t deadline) {
    struct msock_vfs *m = hquery(s, msock_type);
    if(dill_slow(!m)) return -1;
    if(dill_slow((last && last->iol_next) ||
          (!first && last) ||
          (first && !last))) {
        errno = EINVAL; return -1;}
    return m->mrecvl(m, first, last, deadline);
}
Пример #19
0
int dill_msleep(int64_t deadline) {
    /* Return ECANCELED if shutting down. */
    int rc = dill_canblock();
    if(dill_slow(rc < 0)) return -1;
    /* Actual waiting. */
    struct dill_tmclause tmcl;
    dill_timer(&tmcl, 1, deadline);
    int id = dill_wait();
    if(dill_slow(id < 0)) return -1;
    return 0;
}
Пример #20
0
static int tcpconn_recv(int s, void *buf, size_t len, int64_t deadline) {
    struct tcpconn *conn = bsockdata(s, tcpconn_type);
    if(dill_slow(!conn)) return -1;
    /* If there's enough data in the buffer use it. */
    if(dill_fast(len <= conn->ilen)) {
        memcpy(buf, conn->ibuf, len);
        memmove(conn->ibuf, conn->ibuf + len, conn->ilen - len);
        conn->ilen -= len;
        return 0;
    }
    /* If there's not enough data in the buffer, yet the message is small,
       read whole chunk of data to avoid excessive system calls. */
    if(len <= BATCHSIZE) {
        /* First try a non-blocking batch read. */
        size_t recvd = BATCHSIZE - conn->ilen;
        /* TODO: Make sure that deadline of 0 works as intended! */
        dsrecv(conn->fd, conn->ibuf + conn->ilen, &recvd, 0);
        conn->ilen += recvd;
        if(dill_fast(len <= conn->ilen)) {
            memcpy(buf, conn->ibuf, len);
            memmove(conn->ibuf, conn->ibuf + len, conn->ilen - len);
            conn->ilen -= len;
            return 0;
        }
    }
    /* Either message is big or we weren't able to read it in non-blocking
       fashion. In both cases let's try to read the missing data directly
       to the user's buffer. If the operation fails we'll copy the data that
       was already read to the input buffer. To make sure that the operation
       is atomic even in face of memory shortage let's resize the buffer first
       do receiving second. */
    if(dill_slow(len > conn->ilen)) {
        char *newbuf = realloc(conn->ibuf, len);
        if(dill_slow(!newbuf)) {errno = ENOMEM; return -1;}
        conn->ibuf = newbuf;
        conn->icap = len;
    }
    /* Read the missing part directly into user's buffer. */
    size_t recvd = len - conn->ilen;
    int rc = dsrecv(conn->fd, ((char*)buf) + conn->ilen, &recvd, deadline);
    /* If succesfull copy the first part of the message from input buffer. */
    if(dill_fast(rc == 0)) {
        memcpy(buf, conn->ibuf, conn->ilen);
        conn->ilen = 0;
        return 0;
    }
    /* If not successful, store the aready received data into input buffer. */
    int err = errno;
    memcpy(conn->ibuf + conn->ilen, ((char*)buf) + conn->ilen, recvd);
    conn->ibuf += recvd;
    errno = err;
    return -1;
}
Пример #21
0
static int dill_tcp_bsendl(struct dill_bsock_vfs *bvfs,
      struct dill_iolist *first, struct dill_iolist *last, int64_t deadline) {
    struct dill_tcp_conn *self = dill_cont(bvfs, struct dill_tcp_conn, bvfs);
    if(dill_slow(self->sbusy)) {errno = EBUSY; return -1;}
    if(dill_slow(self->outdone)) {errno = EPIPE; return -1;}
    if(dill_slow(self->outerr)) {errno = ECONNRESET; return -1;}
    self->sbusy = 1;
    ssize_t sz = dill_fd_send(self->fd, first, last, deadline);
    self->sbusy = 0;
    if(dill_fast(sz >= 0)) return sz;
    self->outerr = 1;
    return -1;
}
Пример #22
0
int dill_prefix_detach(int s) {
    int err;
    struct dill_prefix_sock *self = dill_hquery(s, dill_prefix_type);
    if(dill_slow(!self)) {err = errno; goto error;}
    if(dill_slow(self->inerr || self->outerr)) {err = ECONNRESET; goto error;}
    int u = self->u;
    if(!self->mem) free(self);
    return u;
error:
    if(s >= 0) dill_hclose(s);
    errno = err;
    return -1;
}
Пример #23
0
int udp_open(struct ipaddr *local, const struct ipaddr *remote) {
    int err;
    /* Sanity checking. */
    if(dill_slow(local && remote &&
          ipaddr_family(local) != ipaddr_family(remote))) {
        err = EINVAL; goto error1;}
    /* Open the listening socket. */
    int family = AF_INET;
    if(local) family = ipaddr_family(local);
    if(remote) family = ipaddr_family(remote);
    int s = socket(family, SOCK_DGRAM, 0);
    if(s < 0) {err = errno; goto error1;}
    /* Set it to non-blocking mode. */
    int rc = fd_unblock(s);
    if(dill_slow(rc < 0)) {err = errno; goto error2;}
    /* Start listening. */
    if(local) {
        rc = bind(s, ipaddr_sockaddr(local), ipaddr_len(local));
        if(s < 0) {err = errno; goto error2;}
        /* Get the ephemeral port number. */
        if(ipaddr_port(local) == 0) {
            struct ipaddr baddr;
            socklen_t len = sizeof(struct ipaddr);
            rc = getsockname(s, (struct sockaddr*)&baddr, &len);
            if(dill_slow(rc < 0)) {err = errno; goto error2;}
            ipaddr_setport(local, ipaddr_port(&baddr));
        }
    }
    /* Create the object. */
    struct udp_sock *obj = malloc(sizeof(struct udp_sock));
    if(dill_slow(!obj)) {err = ENOMEM; goto error2;}
    obj->hvfs.query = udp_hquery;
    obj->hvfs.close = udp_hclose;
    obj->hvfs.done = NULL; /* hdone() is not supported for UDP sockets. */
    obj->mvfs.msendl = udp_msendl;
    obj->mvfs.mrecvl = udp_mrecvl;
    obj->fd = s;
    obj->hasremote = remote ? 1 : 0;
    if(remote) obj->remote = *remote;
    /* Create the handle. */
    int h = hmake(&obj->hvfs);
    if(dill_slow(h < 0)) {err = errno; goto error3;}
    return h;
error3:
    free(obj);
error2:
    fd_close(s);
error1:
    errno = err;
    return -1;
}
Пример #24
0
static int dill_tcp_brecvl(struct dill_bsock_vfs *bvfs,
      struct dill_iolist *first, struct dill_iolist *last, int64_t deadline) {
    struct dill_tcp_conn *self = dill_cont(bvfs, struct dill_tcp_conn, bvfs);
    if(dill_slow(self->rbusy)) {errno = EBUSY; return -1;}
    if(dill_slow(self->indone)) {errno = EPIPE; return -1;}
    if(dill_slow(self->inerr)) {errno = ECONNRESET; return -1;}
    self->rbusy = 1;
    int rc = dill_fd_recv(self->fd, &self->rxbuf, first, last, deadline);
    self->rbusy = 0;
    if(dill_fast(rc == 0)) return 0;
    if(errno == EPIPE) self->indone = 1;
    else self->inerr = 1;
    return -1;
}
Пример #25
0
int dill_tcp_fromfd(int fd) {
    int err;
    struct dill_tcp_conn *obj = malloc(sizeof(struct dill_tcp_conn));
    if(dill_slow(!obj)) {err = ENOMEM; goto error1;}
    int s = dill_tcp_fromfd_mem(fd, (struct dill_tcp_storage*)obj);
    if (dill_slow(s < 0)) {err = errno; goto error2;}
    obj->mem = 0;
    return s;
error2:
    free(obj);
error1:
    errno = err;
    return -1;
}
Пример #26
0
int sfdetach(int s, int64_t deadline) {
    int err = 0;
    struct sf *conn = msockdata(s, sf_type);
    if(dill_slow(!conn)) return -1;
    /* If connection is broken don't even try to do termination handshake. */
    if(conn->res == SF_RESET) {err = ECONNRESET; goto dealloc;}
    /* Ask oworker to exit. */
    struct msg msg = {NULL, 0};
    int rc = chsend(conn->ochan, &msg, sizeof(msg), deadline);
    if(dill_slow(rc < 0 && errno == EPIPE)) {err = ECONNRESET; goto dealloc;}
    if(dill_slow(rc < 0)) {err = errno; goto dealloc;}
    /* Given that there's no way for oworker to receive this message,
       the function only exits when it closes the channel. */
    rc = chsend(conn->ochan, &msg, sizeof(msg), deadline);
    dill_assert(rc < 0);
    if(dill_slow(errno != EPIPE)) {err = errno; goto dealloc;}
    if(dill_slow(conn->ores == SF_RESET)) {err = ECONNRESET; goto dealloc;}
    dill_assert(conn->ores == SF_DONE);
    /* Now that oworker have exited send the termination sequence. */
    rc = bsend(conn->u, &sf_termsequence, sizeof(sf_termsequence), -1);
    if(dill_slow(rc < 0)) {err = errno; goto dealloc;}
    rc = bflush(conn->u, deadline);
    if(dill_slow(rc < 0)) {err = errno; goto dealloc;} 
    /* Read and drop any pending inbound messages. By doing this we'll ensure
       that reading on the underlying socket will continue from the first byte
       following the sf termination sequence. */
    if(conn->res == SF_ACTIVE) {
        while(1) {
            struct msg msg;
            rc = chrecv(conn->ichan, &msg, sizeof(msg), deadline);
            if(rc < 0) break;
            free(msg.buf);
        }
        if(dill_slow(errno != EPIPE)) {err = errno; goto dealloc;}
        if(dill_slow(conn->ires == SF_RESET)) {err = ECONNRESET; goto dealloc;}
        dill_assert(conn->ires == SF_DONE);
    }
dealloc:
    /* Deallocate the object. */
    rc = hclose(conn->iworker);
    dill_assert(rc == 0);
    rc = hclose(conn->oworker);
    dill_assert(rc == 0);
    rc = hclose(conn->ichan);
    dill_assert(rc == 0);
    rc = hclose(conn->ochan);
    dill_assert(rc == 0);
    int u = conn->u;
    free(conn);
    if(err == 0) return u;
    rc = hclose(u);
    dill_assert(rc == 0);
    errno = err;
    return -1;
}
Пример #27
0
int dill_bundle(void) {
    int err;
    struct dill_bundle *b = malloc(sizeof(struct dill_bundle));
    if(dill_slow(!b)) {err = ENOMEM; goto error1;}
    int h = dill_bundle_mem((struct dill_bundle_storage*)b);
    if(dill_slow(h < 0)) {err = errno; goto error2;}
    b->mem = 0;
    return h;
error2:
    free(b);
error1:
    errno = err;
    return -1;
}
Пример #28
0
int dill_tcp_accept(int s, struct dill_ipaddr *addr, int64_t deadline) {
    int err;
    struct dill_tcp_conn *obj = malloc(sizeof(struct dill_tcp_conn));
    if(dill_slow(!obj)) {err = ENOMEM; goto error1;}
    int as = dill_tcp_accept_mem(s, addr, (struct dill_tcp_storage*)obj,
        deadline);
    if(dill_slow(as < 0)) {err = errno; goto error2;}
    obj->mem = 0;
    return as;
error2:
    free(obj);
error1:
    errno = err;
    return -1;
}
Пример #29
0
int dill_tcp_listen(struct dill_ipaddr *addr, int backlog) {
    int err;
    struct dill_tcp_listener *obj = malloc(sizeof(struct dill_tcp_listener));
    if(dill_slow(!obj)) {err = ENOMEM; goto error1;}
    int ls = dill_tcp_listen_mem(addr, backlog,
        (struct dill_tcp_listener_storage*)obj);
    if(dill_slow(ls < 0)) {err = errno; goto error2;}
    obj->mem = 0;
    return ls;
error2:
    free(obj);
error1:
    errno = err;
    return -1;
}
Пример #30
0
int dill_msleep(int64_t deadline, const char *where) {
    /* Return ECANCELED if shutting down. */
    int rc = dill_canblock();
    if(dill_slow(rc < 0)) return -1;
    /* Trivial case. No waiting, but we do want a context switch. */
    if(dill_slow(deadline == 0)) return yield();
    /* Actual waiting. */
    struct dill_tmcl tmcl;
    if(deadline > 0)
        dill_timer(&tmcl, 1, deadline);
    int id = dill_wait(where);
    if(dill_slow(id < 0)) return -1;
    dill_assert(id == 1);
    return 0;
}