summaryrefslogtreecommitdiffstats
path: root/lskat/lskat/KRSocket.cpp
blob: 9b904ac350708f00ad3373196c96b4694d37e8e1 (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/***************************************************************************
                          KRSocket.cpp  -  description
                             -------------------
    begin                : Tue May 2 2000
    copyright            : (C) 2000 by Martin Heni
    email                : martin@heni-online.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/***************************************************************************
                          FILENAME|  -  description
                             -------------------
    begin                : Tue Apr 4 2000
    copyright            : (C) |1995-2000 by Martin Heni
    email                : martin@heni-online.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 1997 Torben Weis (weis@kde.org)
 *
 *  $Id$
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include <config.h>

#include <sys/types.h>
#include <sys/stat.h>
// on Linux/libc5, this includes linux/socket.h where SOMAXCONN is defined
#include <sys/socket.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/un.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include "KRSocket.h"

#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
// defines MAXDNAME under Solaris
#include <arpa/nameser.h>
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>

#ifdef HAVE_SYSENT_H
#include <sysent.h>
#endif

#ifdef TIME_WITH_SYS_TIME
#include <time.h>
#endif

// Play it safe, use a reasonable default, if SOMAXCONN was nowhere defined.
#ifndef SOMAXCONN
#warning Your header files do not seem to support SOMAXCONN
#define SOMAXCONN 5
#endif

#include <tqsocketnotifier.h>

#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108 // this is the value, I found under Linux
#endif

#include <kdebug.h>

KRServerSocket::KRServerSocket( const char *_path, int optname, int value, int level) :
  notifier( 0L ), sock( -1 )
{
  domain = PF_UNIX;

  if ( !init ( _path,optname,value,level ) )
  {
    tqFatal("Error constructing PF_UNIX domain server socket\n");
    return;
  }

  notifier = new TQSocketNotifier( sock, TQSocketNotifier::Read );
  connect( notifier, TQT_SIGNAL( activated(int) ), this, TQT_SLOT( slotAccept(int) ) );
}

KRServerSocket::KRServerSocket( const char *_path ) :
  notifier( 0L ), sock( -1 )
{
  domain = PF_UNIX;

  if ( !init ( _path ) )
  {
    tqFatal("Error constructing PF_UNIX domain server socket\n");
    return;
  }

  notifier = new TQSocketNotifier( sock, TQSocketNotifier::Read );
  connect( notifier, TQT_SIGNAL( activated(int) ), this, TQT_SLOT( slotAccept(int) ) );
}

KRServerSocket::KRServerSocket( unsigned short int _port ) :
  notifier( 0L ), sock( -1 )
{
  domain = PF_INET;

  if ( !init ( _port ) )
  {
    // fatal("Error constructing\n");
    return;
  }

  notifier = new TQSocketNotifier( sock, TQSocketNotifier::Read );
  connect( notifier, TQT_SIGNAL( activated(int) ), this, TQT_SLOT( slotAccept(int) ) );
}

KRServerSocket::KRServerSocket( unsigned short int _port,int optname,int value,int level ) :
  notifier( 0L ), sock( -1 )
{
  domain = PF_INET;

  if ( !init ( _port,optname,value,level ) )
  {
    // fatal("Error constructing\n");
    return;
  }

  notifier = new TQSocketNotifier( sock, TQSocketNotifier::Read );
  connect( notifier, TQT_SIGNAL( activated(int) ), this, TQT_SLOT( slotAccept(int) ) );
}

bool KRServerSocket::init( const char *_path )
{
  return init(_path,0);
}

bool KRServerSocket::init( const char *_path,int optname,int value,int level )
{
  if ( domain != PF_UNIX )
    return false;

  int l = strlen( _path );
  if ( l > UNIX_PATH_MAX - 1 )
  {
    kdWarning() << "Too long PF_UNIX domain name: " << _path << endl;
    return false;
  }

  sock = ::socket( PF_UNIX, SOCK_STREAM, 0 );
  if (sock < 0)
  {
    kdWarning() << "Could not create socket" << endl;
    return false;
  }
  // Heni - 05042000
  if (optname>0)
  {
   kde_socklen_t len=sizeof(value);
   if (-1==setsockopt(sock,level,optname,(char*)&value,len ))
   {
	   kdWarning() << "Could not set socket options." << endl;
   }
  }
  // end Heni

  unlink(_path );

  struct sockaddr_un name;
  name.sun_family = AF_UNIX;
  strcpy( name.sun_path, _path );

  if ( bind( sock, (struct sockaddr*) &name,sizeof( name ) ) < 0 )
  {
    kdWarning() << "Could not bind to socket." << endl;
    ::close( sock );
    sock = -1;
    return false;
  }

  if ( chmod( _path, 0600) < 0 )
  {
    kdWarning() << "Could not setupt premissions for server socket." << endl;
    ::close( sock );
    sock = -1;
    return false;
  }

  if ( listen( sock, SOMAXCONN ) < 0 )
  {
    kdWarning() << "Error listening on socket." << endl;
    ::close( sock );
    sock = -1;
    return false;
  }

  return true;
}

bool KRServerSocket::init( unsigned short int _port )
{
  return init(_port,0);
}
bool KRServerSocket::init( unsigned short int _port,int optname,int value,int level )
{
  if (
#ifdef INET6
      ( domain != PF_INET6 ) &&
#endif
      ( domain != PF_INET  ) )
    return false;

  sock = ::socket( domain, SOCK_STREAM, 0 );
  if (sock < 0)
  {
    kdWarning() << "Could not create socket" << endl;
    return false;
  }
  // Heni - 05042000
  if (optname>0)
  {
   kde_socklen_t len=sizeof(value);
   if (-1==setsockopt(sock,level,optname,(char*)&value,len ))
   {
	   kdWarning() << "Could not set socket options." << endl;
   }
  }
  // end Heni

  if (domain == AF_INET) {

    sockaddr_in name;

    name.sin_family = domain;
    name.sin_port = htons( _port );
    name.sin_addr.s_addr = htonl(INADDR_ANY);

    if ( bind( sock, (struct sockaddr*) &name,sizeof( name ) ) < 0 ) {
	  kdWarning() << "Could not bind to socket." << endl;
	  ::close( sock );
	  sock = -1;
	  return false;
    }
  }
#ifdef INET6
  else if (domain == AF_INET6) {
    sockaddr_in6 name;

    name.sin6_family = domain;
    name.sin6_flowinfo = 0;
    name.sin6_port = htons(_port);
    memcpy(&name.sin6_addr, &in6addr_any, sizeof(in6addr_any));

    if ( bind( sock, (struct sockaddr*) &name,sizeof( name ) ) < 0 ) {
	  kdWarning() << "Could not bind to socket!" << endl;
	  ::close( sock );
	  sock = -1;
	  return false;
    }
  }
#endif

  if ( listen( sock, SOMAXCONN ) < 0 )
    {
	  kdWarning() << "Error listening on socket" << endl;
	  ::close( sock );
	  sock = -1;
	  return false;
    }

  return true;
}

unsigned short int KRServerSocket::port()
{
  if ( domain != PF_INET )
    return false;

  ksockaddr_in name; kde_socklen_t len = sizeof(name);
  getsockname(sock, (struct sockaddr *) &name, &len);
  return ntohs(get_sin_port(name));
}

unsigned long KRServerSocket::ipv4_addr()
{
  if ( domain != PF_INET )
    return 0;

  sockaddr_in name; kde_socklen_t len = sizeof(name);
  getsockname(sock, (struct sockaddr *) &name, &len);
  if (name.sin_family == AF_INET) // It's IPv4
    return ntohl(name.sin_addr.s_addr);
#ifdef INET6
  else if (name.sin_family == AF_INET6) // It's IPv6 Ah.
    return 0;
#endif
  else // We dunno what it is
    return 0;
}

void KRServerSocket::slotAccept( int )
{
  if ( domain == PF_INET )
  {
    ksockaddr_in clientname;
    int new_sock;

    kde_socklen_t size = sizeof(clientname);

    if ((new_sock = accept (sock, (struct sockaddr *) &clientname, &size)) < 0)
    {
      kdWarning() << "Error accepting" << endl;
      return;
    }

    emit accepted( new TDESocket( new_sock ) );
  }
  else if ( domain == PF_UNIX )
  {
    struct sockaddr_un clientname;
    int new_sock;

    kde_socklen_t size = sizeof(clientname);

    if ((new_sock = accept (sock, (struct sockaddr *) &clientname, &size)) < 0)
    {
      kdWarning() << "Error accepting" << endl;
      return;
    }

    emit accepted( new TDESocket( new_sock ) );
  }
}

KRServerSocket::~KRServerSocket()
{
    delete notifier;

  close( sock );
}

#include "KRSocket.moc"