| 1 |
// Copyright © 2002 Rob Kaper <[email protected]>. |
| 2 |
// 2010-2015 Sylvain Rochet <[email protected]> |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// version 2 as published by the Free Software Foundation. |
| 7 |
// |
| 8 |
// This program is distributed in the hope that it will be useful, |
| 9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
// General Public License for more details. |
| 12 |
// |
| 13 |
// You should have received a copy of the GNU General Public License |
| 14 |
// along with this program; see the file COPYING. If not, see |
| 15 |
// <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
#include "config.h" |
| 18 |
|
| 19 |
#include <errno.h> |
| 20 |
#include <sys/types.h> |
| 21 |
#include <sys/socket.h> |
| 22 |
#include <arpa/inet.h> |
| 23 |
#include <netinet/in.h> |
| 24 |
|
| 25 |
#include <fcntl.h> |
| 26 |
#include <netdb.h> |
| 27 |
#include <unistd.h> |
| 28 |
#include <string.h> |
| 29 |
|
| 30 |
#include "listenport.h" |
| 31 |
|
| 32 |
#define MAXLINE 1024 |
| 33 |
|
| 34 |
#ifdef USE_INET_ATON |
| 35 |
#define inet_pton(a, b, c) inet_aton(b, c) |
| 36 |
#endif |
| 37 |
|
| 38 |
ListenPort::ListenPort(sa_family_t family, const std::string ip, const int port) |
| 39 |
{ |
| 40 |
int reuse; |
| 41 |
|
| 42 |
m_ipAddr = ip; |
| 43 |
m_port = port; |
| 44 |
m_fd = socket(family, SOCK_STREAM, 0); |
| 45 |
if(m_fd < 0) { |
| 46 |
goto fail_socket; |
| 47 |
} |
| 48 |
|
| 49 |
struct sockaddr_storage servaddr; |
| 50 |
memset(&servaddr, 0, sizeof(servaddr)); |
| 51 |
servaddr.ss_family = family; |
| 52 |
socklen_t addrlen; |
| 53 |
if(family == AF_INET) { |
| 54 |
struct sockaddr_in *servaddr_in = (struct sockaddr_in *)&servaddr; |
| 55 |
inet_pton(AF_INET, m_ipAddr.c_str(), &(servaddr_in->sin_addr)); |
| 56 |
servaddr_in->sin_port = htons(m_port); |
| 57 |
addrlen = sizeof(*servaddr_in); |
| 58 |
} else if (family == AF_INET6) { |
| 59 |
struct sockaddr_in6 *servaddr_in6 = (struct sockaddr_in6 *)&servaddr; |
| 60 |
inet_pton(AF_INET6, m_ipAddr.c_str(), &(servaddr_in6->sin6_addr)); |
| 61 |
servaddr_in6->sin6_port = htons(m_port); |
| 62 |
addrlen = sizeof(*servaddr_in6); |
| 63 |
|
| 64 |
// bind on IPv6 only if possible |
| 65 |
int v6only = 1; |
| 66 |
setsockopt(m_fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only)); |
| 67 |
} else { |
| 68 |
goto fail_bind; |
| 69 |
} |
| 70 |
|
| 71 |
// release the socket after program crash, avoid TIME_WAIT |
| 72 |
reuse = 1; |
| 73 |
if(setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) |
| 74 |
{ |
| 75 |
goto fail_bind; |
| 76 |
} |
| 77 |
|
| 78 |
if( (bind(m_fd, (struct sockaddr *) &servaddr, addrlen)) == -1) |
| 79 |
{ |
| 80 |
goto fail_bind; |
| 81 |
} |
| 82 |
|
| 83 |
if(listen(m_fd, LISTENQ) == -1) |
| 84 |
{ |
| 85 |
goto fail_bind; |
| 86 |
} |
| 87 |
|
| 88 |
// get current socket flags |
| 89 |
int flags; |
| 90 |
if ((flags=fcntl(m_fd, F_GETFL)) == -1) { |
| 91 |
goto fail_bind; |
| 92 |
} |
| 93 |
|
| 94 |
// set socket to non-blocking |
| 95 |
flags |= O_NDELAY; |
| 96 |
if (fcntl(m_fd, F_SETFL, flags) == -1) { |
| 97 |
goto fail_bind; |
| 98 |
} |
| 99 |
|
| 100 |
return; |
| 101 |
|
| 102 |
fail_bind: |
| 103 |
close(m_fd); |
| 104 |
fail_socket: |
| 105 |
m_error = errno ? errno : 255; |
| 106 |
} |
| 107 |
|
| 108 |
ListenPort::ListenPort(int fd) { |
| 109 |
m_ipAddr = ""; |
| 110 |
m_port = 0; |
| 111 |
m_fd = fd; |
| 112 |
|
| 113 |
// get current socket flags |
| 114 |
int flags; |
| 115 |
if ((flags=fcntl(m_fd, F_GETFL)) == -1) |
| 116 |
goto fail; |
| 117 |
|
| 118 |
// set socket to non-blocking |
| 119 |
flags |= O_NDELAY; |
| 120 |
if (fcntl(m_fd, F_SETFL, flags) == -1) |
| 121 |
goto fail; |
| 122 |
|
| 123 |
return; |
| 124 |
|
| 125 |
fail: |
| 126 |
m_error = errno ? errno : 255; |
| 127 |
} |