~ubuntu-branches/debian/sid/monopd/sid

« back to all changes in this revision

Viewing changes to src/listenport.cpp

  • Committer: Package Import Robot
  • Author(s): Markus Koschany
  • Date: 2014-02-28 15:39:47 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20140228153947-1a5z05x3alj61wx1
Tags: 0.9.5-1
* Imported Upstream version 0.9.5.
  - Add support for IPv6 and systemd.
* Update watch file and point to the new upstream location
  at tuxfamily.org.
* Drop all patches. Merged upstream.
* debian/control:
  - Drop libcapsinetwork-dev from Build-Depends.
    Essential features of libcapsinetwork were merged into monopd and
    IPv6 support was added.
  - Add pkg-config to Build-Depends.
  - Add libsystemd-daemon-dev [linux-any] to Build-Depends.
  - Update homepage field and point to gtkatlantic.gradator.net.
* Add monopd.service and monopd.socket file and support systemd.
* Update monopd.6 man page.
* Use dh-systemd helper.
* Update debian/copyright. Add new BSD license.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2002 Rob Kaper <rob@unixcode.org>. All rights reserved.
 
2
//
 
3
// Redistribution and use in source and binary forms, with or without
 
4
// modification, are permitted provided that the following conditions
 
5
// are met:
 
6
//
 
7
// 1. Redistributions of source code must retain the above copyright
 
8
//    notice, this list of conditions and the following disclaimer.
 
9
// 2. Redistributions in binary form must reproduce the above copyright
 
10
//    notice, this list of conditions and the following disclaimer in the
 
11
//    documentation and/or other materials provided with the distribution.
 
12
//
 
13
// THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS'' AND
 
14
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
15
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
16
// ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 
17
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
18
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
19
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
20
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
21
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
22
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
23
// SUCH DAMAGE.
 
24
 
 
25
#include <sys/types.h>
 
26
#include <sys/socket.h>
 
27
#include <arpa/inet.h>
 
28
#include <netinet/in.h>
 
29
 
 
30
#include <fcntl.h>
 
31
#include <netdb.h>
 
32
#include <unistd.h>
 
33
#include <string.h>
 
34
 
 
35
#include "listenport.h"
 
36
 
 
37
#define MAXLINE 1024
 
38
 
 
39
#ifdef USE_INET_ATON
 
40
#define inet_pton(a, b, c) inet_aton(b, c)
 
41
#endif
 
42
 
 
43
extern int errno;
 
44
 
 
45
ListenPort::ListenPort(sa_family_t family, const std::string ip, const int port)
 
46
{
 
47
        m_ipAddr = ip;
 
48
        m_port = port;
 
49
        m_isBound = false;
 
50
        m_fd = socket(family, SOCK_STREAM, 0);
 
51
        if(m_fd < 0) {
 
52
                return;
 
53
        }
 
54
 
 
55
        struct sockaddr_storage servaddr;
 
56
        memset(&servaddr, 0, sizeof(servaddr));
 
57
        servaddr.ss_family = family;
 
58
        if(family == AF_INET) {
 
59
                struct sockaddr_in *servaddr_in = (struct sockaddr_in *)&servaddr;
 
60
                inet_pton(AF_INET, m_ipAddr.c_str(), &(servaddr_in->sin_addr));
 
61
                servaddr_in->sin_port = htons(m_port);
 
62
        } else if (family == AF_INET6) {
 
63
                struct sockaddr_in6 *servaddr_in6 = (struct sockaddr_in6 *)&servaddr;
 
64
                inet_pton(AF_INET6, m_ipAddr.c_str(), &(servaddr_in6->sin6_addr));
 
65
                servaddr_in6->sin6_port = htons(m_port);
 
66
 
 
67
                // bind on IPv6 only if possible
 
68
                int v6only = 1;
 
69
                setsockopt(m_fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only));
 
70
        } else {
 
71
                close(m_fd);
 
72
                return;
 
73
        }
 
74
 
 
75
        // release the socket after program crash, avoid TIME_WAIT
 
76
        int reuse = 1;
 
77
        if(setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
 
78
        {
 
79
                close(m_fd);
 
80
                return;
 
81
        }
 
82
 
 
83
        if( (bind(m_fd, (struct sockaddr *) &servaddr, sizeof(servaddr))) == -1)
 
84
        {
 
85
                close(m_fd);
 
86
                return;
 
87
        }
 
88
        m_isBound = true;
 
89
 
 
90
        if(listen(m_fd, LISTENQ) == -1)
 
91
        {
 
92
                close(m_fd);
 
93
                return;
 
94
        }
 
95
 
 
96
        // get current socket flags
 
97
        int flags;
 
98
        if ((flags=fcntl(m_fd, F_GETFL)) == -1)
 
99
                return;
 
100
 
 
101
        // set socket to non-blocking
 
102
        flags |= O_NDELAY;
 
103
        if (fcntl(m_fd, F_SETFL, flags) == -1)
 
104
                return;
 
105
}
 
106
 
 
107
ListenPort::ListenPort(int fd) {
 
108
        m_ipAddr = "";
 
109
        m_port = 0;
 
110
        m_isBound = true;
 
111
        m_fd = fd;
 
112
 
 
113
        // get current socket flags
 
114
        int flags;
 
115
        if ((flags=fcntl(m_fd, F_GETFL)) == -1)
 
116
                return;
 
117
 
 
118
        // set socket to non-blocking
 
119
        flags |= O_NDELAY;
 
120
        if (fcntl(m_fd, F_SETFL, flags) == -1)
 
121
                return;
 
122
}
 
123
 
 
124
bool ListenPort::isBound() const
 
125
{
 
126
        return m_isBound;
 
127
}