Example #1
0
Octstr *udp_create_address(Octstr *host_or_ip, int port)
{
    struct sockaddr_in sa;
    struct hostent h;
    char *buff = NULL;
    Octstr *ret;

    sa = empty_sockaddr_in;
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);

    if (strcmp(octstr_get_cstr(host_or_ip), "*") == 0) {
        sa.sin_addr.s_addr = INADDR_ANY;
    } else {
        if (gw_gethostbyname(&h, octstr_get_cstr(host_or_ip), &buff) == -1) {
            error(0, "Couldn't find the IP number of `%s'",
                  octstr_get_cstr(host_or_ip));
            gw_free(buff);
            return NULL;
        }
        sa.sin_addr = *(struct in_addr *) h.h_addr;
    }

    ret = octstr_create_from_data((char *) &sa, sizeof(sa));
    gw_free(buff);

    return ret;
}
Example #2
0
int make_server_socket(int port, const char *interface_name )
{
    struct sockaddr_in addr;
    int s;
    int reuse;
    struct hostent hostinfo;
    char *buff = NULL;

    s = socket(PF_INET, SOCK_STREAM, 0);
    if (s == -1) {
        error(errno, "socket failed");
        goto error;
    }

    addr = empty_sockaddr_in;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    if (interface_name == NULL || strcmp(interface_name, "*") == 0)
        addr.sin_addr.s_addr = htonl(INADDR_ANY);
    else {
        if (gw_gethostbyname(&hostinfo, interface_name, &buff) == -1) {
            error(errno, "gethostbyname failed");
            goto error;
        }
        addr.sin_addr = *(struct in_addr *) hostinfo.h_addr;
    }

    reuse = 1;
    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse,
                   sizeof(reuse)) == -1) {
        error(errno, "setsockopt failed for server address");
        goto error;
    }

    if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
        error(errno, "bind failed");
        goto error;
    }

    if (listen(s, 10) == -1) {
        error(errno, "listen failed");
        goto error;
    }

    gw_free(buff);

    return s;

error:
    if (s >= 0)
        (void) close(s);
    gw_free(buff);
    return -1;
}
Example #3
0
static void setup_official_name(void)
{
    struct utsname u;
    struct hostent h;
    char *buff = NULL;

    gw_assert(official_name == NULL);
    if (uname(&u) == -1)
        panic(0, "uname failed - can't happen, unless " GW_NAME " is buggy.");
    if (gw_gethostbyname(&h, u.nodename, &buff) == -1) {
        error(0, "Can't find out official hostname for this host, "
              "using `%s' instead.", u.nodename);
        official_name = octstr_create(u.nodename);
        official_ip = octstr_create("127.0.0.1");
    } else {
        official_name = octstr_create(h.h_name);
        official_ip = gw_netaddr_to_octstr(AF_INET, h.h_addr);
    }
    gw_free(buff);
}
Example #4
0
int udp_bind(int port, const char *interface_name)
{
    int s;
    struct sockaddr_in sa;
    struct hostent hostinfo;
    char *buff = NULL;

    s = socket(PF_INET, SOCK_DGRAM, 0);
    if (s == -1) {
        error(errno, "Couldn't create a UDP socket");
        return -1;
    }

    sa = empty_sockaddr_in;
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    if (strcmp(interface_name, "*") == 0)
        sa.sin_addr.s_addr = htonl(INADDR_ANY);
    else {
        if (gw_gethostbyname(&hostinfo, interface_name, &buff) == -1) {
            error(errno, "gethostbyname failed");
            gw_free(buff);
            return -1;
        }
        sa.sin_addr = *(struct in_addr *) hostinfo.h_addr;
    }

    if (bind(s, (struct sockaddr *) &sa, (int) sizeof(sa)) == -1) {
        error(errno, "Couldn't bind a UDP socket to port %d", port);
        (void) close(s);
        return -1;
    }

    gw_free(buff);

    return s;
}
Example #5
0
int tcpip_connect_nb_to_server_with_port(char *hostname, int port, int our_port, const char *interface_name, int *done)
{
    struct sockaddr_in addr;
    struct sockaddr_in o_addr;
    struct hostent hostinfo;
    struct hostent o_hostinfo;
    int s, flags, rc = -1, i;
    char *buff, *buff1;

    *done = 1;
    buff = buff1 = NULL;

    s = socket(PF_INET, SOCK_STREAM, 0);
    if (s == -1) {
        error(errno, "Couldn't create new socket.");
        goto error;
    }

    if (gw_gethostbyname(&hostinfo, hostname, &buff) == -1) {
        error(errno, "gethostbyname failed");
        goto error;
    }

    if (our_port > 0 || (interface_name != NULL && strcmp(interface_name, "*") != 0)) {
        int reuse;

        o_addr = empty_sockaddr_in;
        o_addr.sin_family = AF_INET;
        o_addr.sin_port = htons(our_port);
        if (interface_name == NULL || strcmp(interface_name, "*") == 0)
            o_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        else {
            if (gw_gethostbyname(&o_hostinfo, interface_name, &buff1) == -1) {
                error(errno, "gethostbyname failed");
                goto error;
            }
            o_addr.sin_addr = *(struct in_addr *) o_hostinfo.h_addr;
        }

        reuse = 1;
        if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse, sizeof(reuse)) == -1) {
            error(errno, "setsockopt failed before bind");
            goto error;
        }
        if (bind(s, (struct sockaddr *) &o_addr, sizeof(o_addr)) == -1) {
            error(errno, "bind to local port %d failed", our_port);
            goto error;
        }
    }

    flags = fcntl(s, F_GETFL, 0);
    fcntl(s, F_SETFL, flags | O_NONBLOCK);

    i = 0;
    do {
        Octstr *ip2;

        addr = empty_sockaddr_in;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr = *(struct in_addr *) hostinfo.h_addr_list[i];

        ip2 = gw_netaddr_to_octstr(AF_INET, &addr.sin_addr);

        debug("gwlib.socket", 0, "Connecting nonblocking to <%s>", octstr_get_cstr(ip2));

        if ((rc = connect(s, (struct sockaddr *) &addr, sizeof(addr))) < 0) {
            if (errno != EINPROGRESS) {
                error(errno, "nonblocking connect to <%s> failed", octstr_get_cstr(ip2));
            }
        }
        octstr_destroy(ip2);
    } while (rc == -1 && errno != EINPROGRESS && hostinfo.h_addr_list[++i] != NULL);

    if (rc == -1 && errno != EINPROGRESS)
        goto error;

    /* May be connected immediatly
     * (if we connecting to localhost for example)
     */
    if (rc == 0) {
        *done = 0;
    }

    gw_free(buff);
    gw_free(buff1);

    return s;

error:
    error(0, "error connecting to server `%s' at port `%d'", hostname, port);
    if (s >= 0)
        close(s);
    gw_free(buff);
    gw_free(buff1);
    return -1;
}
Example #6
0
int tcpip_connect_to_server_with_port(char *hostname, int port, int our_port, const char *source_addr)
{
    struct sockaddr_in addr;
    struct sockaddr_in o_addr;
    struct hostent hostinfo;
    struct hostent o_hostinfo;
    int s, rc = -1, i;
    char *buff, *buff1;

    buff = buff1 = NULL;

    s = socket(PF_INET, SOCK_STREAM, 0);
    if (s == -1) {
        error(errno, "Couldn't create new socket.");
        goto error;
    }

    if (gw_gethostbyname(&hostinfo, hostname, &buff) == -1) {
        error(errno, "gethostbyname failed");
        goto error;
    }

    if (our_port > 0 || (source_addr != NULL && strcmp(source_addr, "*") != 0))  {
        int reuse;

        o_addr = empty_sockaddr_in;
        o_addr.sin_family = AF_INET;
        o_addr.sin_port = htons(our_port);
        if (source_addr == NULL || strcmp(source_addr, "*") == 0)
            o_addr.sin_addr.s_addr = htonl(INADDR_ANY);
        else {
            if (gw_gethostbyname(&o_hostinfo, source_addr, &buff1) == -1) {
                error(errno, "gethostbyname failed");
                goto error;
            }
            o_addr.sin_addr = *(struct in_addr *) o_hostinfo.h_addr;
        }

        reuse = 1;
        if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse, sizeof(reuse)) == -1) {
            error(errno, "setsockopt failed before bind");
            goto error;
        }
        if (bind(s, (struct sockaddr *) &o_addr, sizeof(o_addr)) == -1) {
            error(errno, "bind to local port %d failed", our_port);
            goto error;
        }
    }

    i = 0;
    do {
        Octstr *ip2;

        addr = empty_sockaddr_in;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr = *(struct in_addr *) hostinfo.h_addr_list[i];

        ip2 = gw_netaddr_to_octstr(AF_INET, &addr.sin_addr);

        debug("gwlib.socket", 0, "Connecting to <%s>", octstr_get_cstr(ip2));

        rc = connect(s, (struct sockaddr *) &addr, sizeof(addr));
        if (rc == -1) {
            error(errno, "connect to <%s> failed", octstr_get_cstr(ip2));
        }
        octstr_destroy(ip2);
    } while (rc == -1 && hostinfo.h_addr_list[++i] != NULL);

    if (rc == -1)
        goto error;

    gw_free(buff);
    gw_free(buff1);
    return s;

error:
    error(0, "error connecting to server `%s' at port `%d'", hostname, port);
    if (s >= 0)
        close(s);
    gw_free(buff);
    gw_free(buff1);
    return -1;
}