Ejemplo n.º 1
0
int               netbios_session_connect(struct in_addr *addr,
        netbios_session *s,
        const char *name,
        int direct_tcp)
{
    ssize_t                   recv_size;
    char                      *encoded_name = NULL;

    assert(s != NULL && s->packet != NULL);

    if (direct_tcp)
        s->remote_addr.sin_port       = htons(NETBIOS_PORT_DIRECT);
    else
        s->remote_addr.sin_port       = htons(NETBIOS_PORT_SESSION);
    s->remote_addr.sin_family       = AF_INET;
    s->remote_addr.sin_addr.s_addr  = addr->s_addr;
    if (!open_socket_and_connect(s))
        goto error;

    if (!direct_tcp)
    {
        // Send the Session Request message
        netbios_session_packet_init(s);
        s->packet->opcode = NETBIOS_OP_SESSION_REQ;
        encoded_name = netbios_name_encode(name, 0, NETBIOS_FILESERVER);
        if (!netbios_session_packet_append(s, encoded_name, strlen(encoded_name) + 1))
            goto error;
        free(encoded_name);
        encoded_name = netbios_name_encode("LIBDSM", 0, NETBIOS_WORKSTATION);
        if (!netbios_session_packet_append(s, encoded_name, strlen(encoded_name) + 1))
            goto error;
        free(encoded_name);
        encoded_name = NULL;

        s->state = NETBIOS_SESSION_CONNECTING;
        if (!netbios_session_packet_send(s))
            goto error;

        // Now receiving the reply from the server.
        recv_size = netbios_session_packet_recv(s, NULL);
        if (recv_size < 0)
            goto error;

        // Reply was negative, we are not connected :(
        if (s->packet->opcode != NETBIOS_OP_SESSION_REQ_OK)
        {
            s->state = NETBIOS_SESSION_REFUSED;
            return 0;
        }
    }

    // Reply was OK or DirectTCP, a session has been established
    s->state = NETBIOS_SESSION_CONNECTED;
    return 1;

error:
    free(encoded_name);
    s->state = NETBIOS_SESSION_ERROR;
    return 0;
}
Ejemplo n.º 2
0
int netbios_session_connect(uint32_t ip, netbios_session *s,
                            const char *name, int direct_tcp)
{
    ssize_t                   recv_size;
    char                      *encoded_name = NULL;
    uint16_t                  ports[2];
    unsigned int              nb_ports;
    bool                      opened = false;

    assert(s != NULL && s->packet != NULL);

    if(s != NULL && s->packet != NULL){
    
        if (direct_tcp)
        {
            ports[0] = htons(NETBIOS_PORT_DIRECT);
            ports[1] = htons(NETBIOS_PORT_DIRECT_SECONDARY);
            nb_ports = 2;
        }
        else
        {
            ports[0] = htons(NETBIOS_PORT_SESSION);
            nb_ports = 1;
        }
        for (unsigned int i = 0; i < nb_ports && !opened; ++i)
        {
            s->remote_addr.sin_port         = ports[i];
            s->remote_addr.sin_family       = AF_INET;
            s->remote_addr.sin_addr.s_addr  = ip;
            opened = open_socket_and_connect(s) == DSM_SUCCESS;
        }
        if (!opened)
            goto error;

        if (!direct_tcp)
        {
            // Send the Session Request message
            netbios_session_packet_init(s);
            s->packet->opcode = NETBIOS_OP_SESSION_REQ;
            encoded_name = netbios_name_encode(name, 0, NETBIOS_FILESERVER);
            if (!netbios_session_packet_append(s, encoded_name, strlen(encoded_name) + 1))
                goto error;
            free(encoded_name);
            encoded_name = netbios_name_encode("LIBDSM", 0, NETBIOS_WORKSTATION);
            if (!netbios_session_packet_append(s, encoded_name, strlen(encoded_name) + 1))
                goto error;
            free(encoded_name);
            encoded_name = NULL;

            s->state = NETBIOS_SESSION_CONNECTING;
            if (!netbios_session_packet_send(s))
                goto error;

            // Now receiving the reply from the server.
            recv_size = netbios_session_packet_recv(s, NULL);
            if (recv_size < 0)
                goto error;

            // Reply was negative, we are not connected :(
            if (s->packet->opcode != NETBIOS_OP_SESSION_REQ_OK)
            {
                s->state = NETBIOS_SESSION_REFUSED;
                return 0;
            }
        }

        // Reply was OK or DirectTCP, a session has been established
        s->state = NETBIOS_SESSION_CONNECTED;
        return 1;

    error:
        free(encoded_name);
        s->state = NETBIOS_SESSION_ERROR;
        return 0;
    }
    
    return 0;
}