summaryrefslogtreecommitdiff
path: root/libs/networking/src/AbstractSocket.cc
blob: 64dc39b34ecbdf3fe5f59305ab039171743a3bb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
 * ======================== legal notice ======================
 * 
 * File:      AbstractSocket.cc
 * Created:   4. Juli 2012, 07
 * Author:    <a href="mailto:geronimo013@gmx.de">Geronimo</a>
 * Project:   libnetworking: classes for tcp/ip sockets and http-protocol handling
 * 
 * CMP - compound media player
 * 
 * is a client/server mediaplayer intended to play any media from any workstation
 * without the need to export or mount shares. cmps is an easy to use backend
 * with a (ready to use) HTML-interface. Additionally the backend supports
 * authentication via HTTP-digest authorization.
 * cmpc is a client with vdr-like osd-menues.
 * 
 * Copyright (c) 2012 Reinhard Mantey, some rights reserved!
 * published under Creative Commons by-sa
 * For details see http://creativecommons.org/licenses/by-sa/3.0/
 * 
 * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp
 * 
 * --------------------------------------------------------------
 */
#include <AbstractSocket.h>
#include <Logging.h>
#include <stddef.h>
#include <arpa/inet.h>
#include <resolv.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <poll.h>

cAbstractSocket::cAbstractSocket(int Port, int Queue)
 : sock(-1)
 , queue(Queue)
 , blocking(true)
 , thisSide(NULL)
{
}

cAbstractSocket::cAbstractSocket(const char *ServerName, int Port)
 : sock(-1)
 , queue(0)
 , blocking(true)
 , thisSide(NULL)
{
   others.push_back(new cConnectionPoint(ServerName, Port));
}

cAbstractSocket::~cAbstractSocket()
{
  Close();
  if (thisSide) delete thisSide;
  cConnectionPoint *p;

  for (size_t i=0; i < others.size(); ++i) {
      p = others[i];
      delete p;
      }
}

void cAbstractSocket::Close(void)
{
  if (sock >= 0) {
     close(sock);
     sock = -1;
     }
}

// the client side
bool cAbstractSocket::Connect(void)
{
  if (sock < 0) {
     struct addrinfo hints, *result, *rp;
     char buf[20];
     int s;

     /* Obtain address(es) matching host/port */
     memset(buf, 0, sizeof(buf));
     memset(&hints, 0, sizeof(struct addrinfo));
     hints.ai_family = AF_UNSPEC;   // Allow IPv4 or IPv6
     hints.ai_socktype = SOCK_STREAM;
     snprintf(buf, sizeof(buf), "%d", OtherSide()->Port());

     if ((s = getaddrinfo(OtherSide()->HostName(), buf, &hints, &result))) {
        esyslog("getaddrinfo: %s", gai_strerror(s));

        return -1;
        }

     // getaddrinfo potentially returns a list of addresses, that may fit,
     // so try each address until we successfully connect
     for (rp = result; rp; rp = rp->ai_next) {
         sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
         if (sock == -1) continue;

         if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1)
            break; // we got a connection
         close(sock);
         }
     if (rp == NULL) { // no address could be connected to
        esyslog("Could not connect!");
        Close();

        return false;
        }
     // here we have a valid connection, so lets gather some information
     thisSide = GetNameAndAddress(sock, rp->ai_addr, rp->ai_addrlen);
     OtherSide()->SetSocket(sock);
     char nameBuf[512];

     memset(nameBuf, 0, sizeof(nameBuf));
     if ((s = getnameinfo(rp->ai_addr, rp->ai_addrlen, nameBuf, sizeof(nameBuf), NULL, 0, NI_NAMEREQD))) {
        esyslog("failed to determine servers hostname %s", gai_strerror(s));
        }
     else {
        OtherSide()->SetRealName(nameBuf);
        }
     freeaddrinfo(result);
     }
  return sock > 0;
}

// as each address may be IP4 or IP6, cut this off
static void * getAddr(struct sockaddr *sa)
{
  if (sa->sa_family == AF_INET)
     return &(((struct sockaddr_in*)sa)->sin_addr);
  return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

// same is true for port access
static int getPort(struct sockaddr *sa)
{
  if (sa->sa_family == AF_INET)
     return ((struct sockaddr_in*)sa)->sin_port;
  return ((struct sockaddr_in6*)sa)->sin6_port;
}

cConnectionPoint *cAbstractSocket::GetNameAndAddress(int Socket, struct sockaddr *sa, socklen_t sa_size)
{
  cConnectionPoint *rv = NULL;
  char nameBuf[512], addrBuf[INET6_ADDRSTRLEN];

  if (getnameinfo(sa, sa_size, nameBuf, sizeof(nameBuf), NULL, 0, NI_NAMEREQD)) {
     // ok, just ip address has to suffice to start work
     rv = new cConnectionPoint(inet_ntop(sa->sa_family, getAddr(sa), addrBuf, sizeof(addrBuf))
                                  , ntohs(getPort(sa))
                                  , inet_ntop(sa->sa_family, getAddr(sa), addrBuf, sizeof(addrBuf)));
     }
  else {
     rv = new cConnectionPoint(inet_ntop(sa->sa_family, getAddr(sa), addrBuf, sizeof(addrBuf))
                                  , ntohs(getPort(sa))
                                  , nameBuf);
     }
  if (rv) rv->SetSocket(Socket);

  return rv;
}

void cAbstractSocket::SetBlockingIO(bool ForceBlockingIO)
{
  blocking = ForceBlockingIO;
}

// server side first part
bool cAbstractSocket::Open(int Port)
{
  if (sock < 0) {
     isyslog("socket is < 0 - so start a new connection ...");
     struct addrinfo hints, *result, *rp;
     char buf[20];
     int s;

     memset(&hints, 0, sizeof(hints));
     memset(buf, 0, sizeof(buf));
     hints.ai_family = AF_UNSPEC;     // allow IPv4 or IPv6
     hints.ai_socktype = SOCK_STREAM;
     hints.ai_flags = AI_PASSIVE;
     snprintf(buf, sizeof(buf), "%d", Port);

     if ((s = getaddrinfo(NULL, buf, &hints, &result))) {
        esyslog("getaddrinfo: %s\n", gai_strerror(s));

        return false;
        }
     int yes=1;

     // getaddrinfo potentially returns a list of addresses, that may fit,
     // so try each address until we successfully bound
     for (rp = result; rp; rp = rp->ai_next) {
         sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
         if (sock == -1) continue;

         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

         if (bind(sock, rp->ai_addr, rp->ai_addrlen) != -1)
            break; // ok, we succeeded to bind
         close(sock);
         }
     if (!rp) {
        esyslog("could not bind");
        Close();

        return false;
        }
     ConfigureSocket(sock);
     freeaddrinfo(result);

     // listen to the socket:
     if (listen(sock, queue) < 0) {
        esyslog("failed to listen to bound socket!");
        Close();
        return (false);
        }
     }
  return (true);
}

// server side second part
cConnectionPoint *cAbstractSocket::Accept(int Port, int TimeoutMs)
{
  cConnectionPoint *client = NULL;

  if (Open(Port)) {
     struct sockaddr_storage clientname;
     uint cl_size = sizeof(clientname);
     struct pollfd pf[1];

     pf[0].fd = sock;
     pf[0].events = POLLIN;

     if (poll(pf, 1, TimeoutMs) < 1) return NULL;
     int  newsock = accept(sock, (struct sockaddr *) &clientname, &cl_size);

     if (newsock > 0) {
        isyslog("ok, got client connection request ...");

        // if server address has not been restricted at bind-time, its possible that we don't
        // have a local address yet. If server is bound to 0 (any address), we can determine
        // the real server address in use now. But only do it once.
        if (!thisSide) {
           struct sockaddr_storage server;
           uint sa_size = sizeof(server);

           if (getpeername(newsock, (struct sockaddr *) &server, &sa_size)) {
              esyslog("getpeername failed: #%d", errno);
              }
           else thisSide = GetNameAndAddress(newsock, (struct sockaddr *) &server, sa_size);
           }

        // At server side, multiple clients can have open connections at the same time,
        // so get rid of that here. We always fetch connections informations
        // before any access decisions, so those could be extended to use names
        // some day ...
        client = GetNameAndAddress(newsock, (struct sockaddr *) &clientname, cl_size);
        if (client) {
           bool accepted = true;
           //FIXME: change determination of accepting client access!
           // accepted = SVDRPhosts.Acceptable(clientname.sin_addr.s_addr);
           // change to:
           // accepted = SVDRPHosts.Acceptable(cpi);
           // so in SVDRPHosts you have IP-Address and real name (if accessible)

           if (!accepted) {
              const char *s = "Access denied!\n";

              if (write(newsock, s, strlen(s)) < 0) {
                 esyslog(s);
                 close(newsock);
                 }
              newsock = -1;
              client = NULL;
              }
           else {
              others.push_back(client);
              }
           isyslog("connect from %s, port %hu - %s", client->HostName(), client->Port(), accepted ? "accepted" : "DENIED");
           }
        }
     else if (errno != EINTR && errno != EAGAIN) {
        esyslog("failed to accept client");
        }
     }
  return (client);
}