Back to index

avfs  1.0.1
Functions
socket.c File Reference
#include "socket.h"
#include "avfs.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>

Go to the source code of this file.

Functions

static int sock_connect_host (const char *hostname, int port)
int av_sock_connect (const char *name, int defaultport)

Function Documentation

int av_sock_connect ( const char *  name,
int  defaultport 
)

Definition at line 57 of file socket.c.

{
    char *hostname;
    char *s;
    int port = defaultport;
    int sock;

    hostname = av_strdup(name);
    s = strrchr(hostname, ':');
    if(s != NULL) {
        char *end;

        *s = '\0';
        s++;
        port = strtol(s, &end, 10);
        if(end == s || *end != '\0') {
            av_free(hostname);
            av_log(AVLOG_ERROR, "Bad port: %s", name);
            return -ENOENT;
        }
    }
    else if(defaultport == -1) {
        av_free(hostname);
        av_log(AVLOG_ERROR, "Missing port: %s", name);
        return -ENOENT;
    }
    
    sock = sock_connect_host(hostname, port);
    av_free(hostname);
    
    return sock;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int sock_connect_host ( const char *  hostname,
int  port 
) [static]

Definition at line 19 of file socket.c.

{
    struct hostent *host;
    struct sockaddr_in sn;
    int sock;
    int res;

    host = gethostbyname(hostname);
    if(host == NULL) {
        av_log(AVLOG_ERROR, "Could not resolve host %s", hostname);
        return -ENOENT;
    }
    
    if(host->h_addrtype != AF_INET) {
        av_log(AVLOG_ERROR, "Cannot handle non-inet address %s", hostname);
        return -ENOENT;
    }

    sn.sin_family = AF_INET;
    sn.sin_port = htons(port);
    memcpy(&sn.sin_addr, host->h_addr_list[0], host->h_length);

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock == -1) {
        av_log(AVLOG_ERROR, "socket(): %s", strerror(errno));
        return -errno;
    }

    res = connect(sock, (struct sockaddr *) &sn, sizeof(sn));
    if(res == -1) {
        av_log(AVLOG_ERROR, "connect(): %s", strerror(errno));
        close(sock);
        return -errno;
    }

    return sock;
}

Here is the call graph for this function:

Here is the caller graph for this function: