1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/ioutil.c Wed Mar 30 21:51:54 2011 +1000
1.7 + * Network support functions
1.9 + * Copyright (c) 2009 Nathan Keynes.
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.22 +#include <string.h>
1.23 +#include <unistd.h>
1.24 +#include <sys/socket.h>
1.25 +#include <netinet/in.h>
1.26 +#include <arpa/inet.h>
1.28 +#include "ioutil.h"
1.30 +int io_create_server_socket(const char *interface, int port )
1.32 + struct sockaddr_in sin;
1.33 + int fd = socket(AF_INET, SOCK_STREAM, 0);
1.35 + ERROR( "Failed to create TCP socket!" );
1.39 + sin.sin_family = AF_INET;
1.40 + sin.sin_addr.s_addr = 0;
1.41 + sin.sin_port = htons(port);
1.43 + if( interface != NULL ) {
1.44 + if( !inet_aton(interface, &sin.sin_addr) ) {
1.45 + /* TODO: hostname lookup */
1.49 + if( bind( fd, (struct sockaddr *)&sin, sizeof(sin) ) != 0 ||
1.50 + listen(fd, 5) != 0 ) {
1.52 + ERROR( "Failed to bind port %d (%s)", port, strerror(errno) );