Esempio n. 1
0
int
sin_bind(void *s, const struct sockaddr *addr, socklen_t addrlen, int *e)
{
    struct sin_socket *ssp;
    struct sin_addr *saddr;

    ssp = (struct sin_socket *)s;
    if (ssp->src == NULL) {
        ssp->src = malloc(sizeof(struct sin_addr) + addrlen);
        if (ssp->src == NULL) {
            _SET_ERR(e, ENOMEM);
            return (-1);
        }
        SIN_TYPE_SET(ssp->src, _SIN_TYPE_ADDR);
        ssp->src->addr = (struct sockaddr *)((char *)ssp->src + 
          sizeof(struct sin_addr));
    } else if (addrlen > ssp->src->addrlen) {
        SIN_TYPE_ASSERT(ssp->src, _SIN_TYPE_ADDR);
        saddr = realloc(ssp->src, sizeof(struct sin_addr) + addrlen);
        if (saddr == NULL) {
            _SET_ERR(e, ENOMEM);
            return (-1);
        }
        ssp->src = saddr;
    }
    memcpy(ssp->src->addr, addr, addrlen);
    ssp->src->addrlen = addrlen;

    return (0);
}
Esempio n. 2
0
int
sin_wrk_thread_ctor(struct sin_type_wrk_thread *swtp, const char *tname,
  void *(*start_routine)(void *), int *e)
{
    int rval;

    swtp->sin_type = _SIN_TYPE_WRK_THREAD;
    swtp->pvt = malloc(sizeof(struct sin_wrk_thread_private));
    if (swtp->pvt == NULL) {
        _SET_ERR(e, ENOMEM);
        return (-1);
    }
    memset(swtp->pvt, '\0', sizeof(struct sin_wrk_thread_private));
    asprintf(&swtp->pvt->tname, "worker thread(%s)", tname);
    if (swtp->pvt->tname == NULL) {
        _SET_ERR(e, ENOMEM);
        goto er_undo_0;
    }
    swtp->pvt->ctrl_queue = sin_wi_queue_ctor(e, "%s control queue", tname);
    if (swtp->pvt->ctrl_queue == NULL) {
        goto er_undo_1;
    }
    swtp->pvt->sigterm = sin_signal_ctor(SIGTERM, e);
    if (swtp->pvt->sigterm == NULL) {
        goto er_undo_2;
    }
    swtp->pvt->runner = start_routine;
    rval = pthread_create(&swtp->pvt->tid, NULL, (void *(*)(void *))&sin_wrk_thread_runner, swtp);
    if (rval != 0) {
        _SET_ERR(e, rval);
        goto er_undo_3;
    }
    swtp->dtor = &sin_wrk_thread_dtor;
    swtp->check_ctrl = &sin_wrk_thread_check_ctrl;
    swtp->notify_on_ctrl = &sin_wrk_thread_notify_on_ctrl;
    swtp->get_tname = &sin_wrk_thread_get_tname;
    return (0);

er_undo_3:
    sin_signal_dtor(swtp->pvt->sigterm);
er_undo_2:
    sin_wi_queue_dtor(swtp->pvt->ctrl_queue);
er_undo_1:
    free(swtp->pvt->tname);
er_undo_0:
    free(swtp->pvt);
    return (-1);
}
Esempio n. 3
0
struct sin_signal *
sin_signal_ctor(int signum, int *e)
{
    struct sin_signal *ssign;

    ssign = malloc(sizeof(struct sin_signal));
    if (ssign == NULL) {
        _SET_ERR(e, ENOMEM);
        return (NULL);
    }
    memset(ssign, '\0', sizeof(struct sin_signal));
    SIN_TYPE_SET(ssign, _SIN_TYPE_SIGNAL);
    ssign->signum = signum;

    return (ssign);
}
Esempio n. 4
0
struct sin_pkt *
sin_pkt_ctor(struct sin_pkt_zone *my_zone, int zone_idx,
 struct netmap_ring *my_ring, int *e)
{
    struct sin_pkt *pkt;

    pkt = malloc(sizeof(struct sin_pkt));
    if (pkt == NULL) {
        _SET_ERR(e, ENOMEM);
        return (NULL);
    }
    memset(pkt, '\0', sizeof(struct sin_pkt));
    SIN_TYPE_SET(pkt, _SIN_TYPE_PKT);
    pkt->ts = malloc(sizeof(struct timeval));
    memset(pkt->ts, '\0', sizeof(struct timeval));
    pkt->my_zone = my_zone;
    pkt->my_ring = my_ring;
    pkt->zone_idx = zone_idx;
    pkt->my_slot = &(my_ring->slot[zone_idx]);
    pkt->buf = NETMAP_BUF(my_ring, pkt->my_slot->buf_idx);

    return (pkt);
}