nkeynes@998: /** nkeynes@1021: * $Id$ nkeynes@998: * nkeynes@998: * Network support functions nkeynes@998: * nkeynes@998: * Copyright (c) 2009 Nathan Keynes. nkeynes@998: * nkeynes@998: * This program is free software; you can redistribute it and/or modify nkeynes@998: * it under the terms of the GNU General Public License as published by nkeynes@998: * the Free Software Foundation; either version 2 of the License, or nkeynes@998: * (at your option) any later version. nkeynes@998: * nkeynes@998: * This program is distributed in the hope that it will be useful, nkeynes@998: * but WITHOUT ANY WARRANTY; without even the implied warranty of nkeynes@998: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nkeynes@998: * GNU General Public License for more details. nkeynes@998: */ nkeynes@998: nkeynes@998: #include nkeynes@998: #include nkeynes@998: #include nkeynes@998: #include nkeynes@998: #include nkeynes@998: #include nkeynes@1077: #include "ioutil.h" nkeynes@998: nkeynes@1077: int io_create_server_socket(const char *interface, int port ) nkeynes@998: { nkeynes@998: struct sockaddr_in sin; nkeynes@998: int fd = socket(AF_INET, SOCK_STREAM, 0); nkeynes@998: if( fd == -1 ) { nkeynes@998: ERROR( "Failed to create TCP socket!" ); nkeynes@998: return -1; nkeynes@998: } nkeynes@998: nkeynes@998: sin.sin_family = AF_INET; nkeynes@998: sin.sin_addr.s_addr = 0; nkeynes@998: sin.sin_port = htons(port); nkeynes@998: nkeynes@998: if( interface != NULL ) { nkeynes@998: if( !inet_aton(interface, &sin.sin_addr) ) { nkeynes@998: /* TODO: hostname lookup */ nkeynes@998: } nkeynes@998: } nkeynes@998: nkeynes@998: if( bind( fd, (struct sockaddr *)&sin, sizeof(sin) ) != 0 || nkeynes@998: listen(fd, 5) != 0 ) { nkeynes@998: close(fd); nkeynes@998: ERROR( "Failed to bind port %d (%s)", port, strerror(errno) ); nkeynes@998: return -1; nkeynes@998: } nkeynes@998: return fd; nkeynes@998: } nkeynes@998: