4 * Network support functions
6 * Copyright (c) 2009 Nathan Keynes.
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.
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.
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
27 int io_create_server_socket(const char *interface, int port )
29 struct sockaddr_in sin;
30 int fd = socket(AF_INET, SOCK_STREAM, 0);
32 ERROR( "Failed to create TCP socket!" );
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 */
46 if( bind( fd, (struct sockaddr *)&sin, sizeof(sin) ) != 0 ||
47 listen(fd, 5) != 0 ) {
49 ERROR( "Failed to bind port %d (%s)", port, strerror(errno) );
.