tcp/tcp_listen.c

Go to the documentation of this file.
00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00016 
00017 #include <unistd.h>
00018 #include <errno.h>
00019 #include <netinet/in.h>
00020 #include <sys/socket.h>
00021 
00022 #include "addr.h"
00023 #include "mem.h"
00024 #include "tcp.h"
00025 
00026 int tcp_listen(const char *ip, int port, int backlog)
00027 {
00028         int fd;
00029         struct sockaddr_in inaddr;
00030 
00031         if (port < 1)
00032                 return errno = EINVAL, -1;
00033 
00034         mem_set(&inaddr, 0, sizeof(inaddr));
00035         inaddr.sin_family = AF_INET;
00036         inaddr.sin_port   = addr_htos(port);
00037 
00038         if (addr_from_str(ip, &inaddr.sin_addr.s_addr, 0) == 0)
00039                 return errno = EINVAL, -1;
00040 
00041         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
00042                 return -1;
00043 
00044         if (bind(fd, (struct sockaddr *) &inaddr, sizeof(struct sockaddr_in)) == -1) {
00045                 close(fd);
00046                 return -1;
00047         }
00048 
00049         if (listen(fd, backlog) == -1) {
00050                 close(fd);
00051                 return -1;
00052         }
00053 
00054         return fd;
00055 }

Generated on Tue Jun 19 20:38:27 2007 for lucid by  doxygen 1.5.2