Search
lxdream.org :: lxdream/src/ioutil.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/ioutil.c
changeset 1077:136fc24d17ef
prev1021:848db285a184
author nkeynes
date Fri May 29 18:47:05 2015 +1000 (7 years ago)
permissions -rw-r--r--
last change Fix test case
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/ioutil.c Fri May 29 18:47:05 2015 +1000
1.3 @@ -0,0 +1,54 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * Network support functions
1.8 + *
1.9 + * Copyright (c) 2009 Nathan Keynes.
1.10 + *
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.15 + *
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.20 + */
1.21 +
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.27 +#include <errno.h>
1.28 +#include "ioutil.h"
1.29 +
1.30 +int io_create_server_socket(const char *interface, int port )
1.31 +{
1.32 + struct sockaddr_in sin;
1.33 + int fd = socket(AF_INET, SOCK_STREAM, 0);
1.34 + if( fd == -1 ) {
1.35 + ERROR( "Failed to create TCP socket!" );
1.36 + return -1;
1.37 + }
1.38 +
1.39 + sin.sin_family = AF_INET;
1.40 + sin.sin_addr.s_addr = 0;
1.41 + sin.sin_port = htons(port);
1.42 +
1.43 + if( interface != NULL ) {
1.44 + if( !inet_aton(interface, &sin.sin_addr) ) {
1.45 + /* TODO: hostname lookup */
1.46 + }
1.47 + }
1.48 +
1.49 + if( bind( fd, (struct sockaddr *)&sin, sizeof(sin) ) != 0 ||
1.50 + listen(fd, 5) != 0 ) {
1.51 + close(fd);
1.52 + ERROR( "Failed to bind port %d (%s)", port, strerror(errno) );
1.53 + return -1;
1.54 + }
1.55 + return fd;
1.56 +}
1.57 +
.