static mrp_transport_t *create_transport(pdp_t *pdp, const char *address)
{
    static mrp_transport_evt_t msg_evt, wrt_evt;

    mrp_transport_evt_t *e;
    mrp_transport_t     *t;
    mrp_sockaddr_t       addr;
    socklen_t            alen;
    int                  flags;
    const char          *type;

    t    = NULL;
    alen = mrp_transport_resolve(NULL, address, &addr, sizeof(addr), &type);

    if (alen <= 0) {
        mrp_log_error("Failed to resolve transport address '%s'.", address);
        return NULL;
    }

    flags = MRP_TRANSPORT_REUSEADDR;

    if (strncmp(address, "wsck", 4) != 0) {
        e = &msg_evt;

        e->connection  = msg_connect_cb;
        e->closed      = msg_closed_cb;
        e->recvmsg     = msg_recv_cb;
        e->recvmsgfrom = NULL;
    }
    else {
        e = &wrt_evt;

        e->connection     = wrt_connect_cb;
        e->closed         = wrt_closed_cb;
        e->recvcustom     = wrt_recv_cb;
        e->recvcustomfrom = NULL;

        flags |= MRP_TRANSPORT_MODE_CUSTOM;
    }

    t = mrp_transport_create(pdp->ctx->ml, type, e, pdp, flags);

    if (t != NULL) {
        if (mrp_transport_bind(t, &addr, alen) && mrp_transport_listen(t, 4))
            return t;
        else {
            mrp_log_error("Failed to bind to transport address '%s'.", address);
            mrp_transport_destroy(t);
        }
    }
    else
        mrp_log_error("Failed to create transport '%s'.", address);

    return NULL;
}
Beispiel #2
0
static int set_address(transport_lua_t *t, const char *address,
                       char *err, size_t elen, int overwrite)
{
    MRP_LUA_ERRUSE(err, elen);

    if (t->address != NULL) {
        if (t->address == address) {
            if (t->alen > 0 && t->atype != NULL)
                return 1;
        }
        else {
            if (!overwrite)
                return mrp_lua_error(-1, t->L,
                                     "address already set ('%s')", t->address);
        }
    }

    if (t->address != address) {
        mrp_free(t->address);
        t->address = NULL;
    }

    t->atype = NULL;
    t->alen  = 0;

    if (address == NULL)
        return 1;

    if ((t->address = mrp_strdup(address)) == NULL)
        return mrp_lua_error(-1, t->L,
                             "failed to store address '%s'", address);

    t->alen = mrp_transport_resolve(NULL, t->address, &t->addr,
                                    sizeof(t->addr), &t->atype);

    if (t->alen <= 0) {
        if (address != t->address)
            mrp_free(t->address);

        t->atype = NULL;
        t->alen  = 0;

        return mrp_lua_error(-1, t->L, "failed to resolve '%s'", address);
    }

    return 0;
}
Beispiel #3
0
{
    static mrp_transport_evt_t evt = {
        { .recvnative     = recv_evt },
        { .recvnativefrom = NULL     },
        .connection       = connection_evt,
        .closed           = closed_evt,
    };

    srs_context_t  *srs  = s->self->srs;
    mrp_sockaddr_t  addr;
    socklen_t       alen;
    const char     *type;
    int             flags, state, sock;
    void           *typemap;

    alen = mrp_transport_resolve(NULL, s->address, &addr, sizeof(addr), &type);

    if (alen == 0) {
        mrp_log_error("Failed to resolve transport address '%s'.",
                      s->address);
        goto fail;
    }

    flags = MRP_TRANSPORT_REUSEADDR | MRP_TRANSPORT_NONBLOCK |  \
        MRP_TRANSPORT_MODE_NATIVE;

    if (s->sock < 0)
        s->lt = mrp_transport_create(srs->ml, type, &evt, s, flags);
    else {
        state = MRP_TRANSPORT_LISTENED;
        sock  = s->sock;