Search
lxdream.org :: lxdream/src/ioutil.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/ioutil.c
changeset 1077:136fc24d17ef
prev1021:848db285a184
author nkeynes
date Thu Jan 26 20:16:51 2012 +1000 (12 years ago)
permissions -rw-r--r--
last change Fix SL function signatures
view annotate diff log raw
     1 /**
     2  * $Id$
     3  * 
     4  * Network support functions
     5  *
     6  * Copyright (c) 2009 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <string.h>
    20 #include <unistd.h>
    21 #include <sys/socket.h>
    22 #include <netinet/in.h>
    23 #include <arpa/inet.h>
    24 #include <errno.h>
    25 #include "ioutil.h"
    27 int io_create_server_socket(const char *interface, int port )
    28 {
    29     struct sockaddr_in sin;
    30     int fd = socket(AF_INET, SOCK_STREAM, 0);
    31     if( fd == -1 ) {
    32         ERROR( "Failed to create TCP socket!" );
    33         return -1;
    34     }
    36     sin.sin_family = AF_INET;
    37     sin.sin_addr.s_addr = 0;
    38     sin.sin_port = htons(port);
    40     if( interface != NULL ) {
    41         if( !inet_aton(interface, &sin.sin_addr) ) {
    42             /* TODO: hostname lookup */
    43         }
    44     }
    46     if( bind( fd, (struct sockaddr *)&sin, sizeof(sin) ) != 0 ||
    47             listen(fd, 5) != 0 ) {
    48         close(fd);
    49         ERROR( "Failed to bind port %d (%s)", port, strerror(errno) );
    50         return -1;
    51     }
    52     return fd;
    53 }
.