summaryrefslogtreecommitdiffstats
path: root/tdecore/ksockaddr.h
blob: d3eb62d0de9f191e32d976064ccc4c196e70cff5 (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 2000-2003 Thiago Macieira <[email protected]>
 *
 *  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.
 */
#ifndef KSOCKADDR_H
#define KSOCKADDR_H

#include <tqobject.h>
#include <tqcstring.h>
#include <tqstring.h>
#include "tdelibs_export.h"

/*
 * This file defines a class that envelopes most, if not all, socket addresses
 */
typedef unsigned ksocklen_t;

struct sockaddr;

class KExtendedSocket;		// No need to define it fully

class TDESocketAddressPrivate;
/**
 * A socket address.
 *
 * This class envelopes almost if not all socket addresses.
 *
 * @author Thiago Macieira <[email protected]>
 * @short a socket address.
 */
class TDECORE_EXPORT TDESocketAddress: public TQObject
{
  Q_OBJECT
protected:
  /**
   * Creates an empty class
   */
  TDESocketAddress() { init(); }

  /**
   * Creates with given data
   * @param sa a sockaddr structure
   * @param size the size of @p sa
   */
  TDESocketAddress(const sockaddr* sa, ksocklen_t size);

public:
  /**
   * Destructor.
   */
  virtual ~TDESocketAddress();

  /**
   * Returns a string representation of this socket.
   * @return a pretty string representation
   */
  virtual TQString pretty() const;

  /**
   * Returns a sockaddr structure, for passing down to library functions.
   * @return the sockaddr structure, can be 0
   */
  const sockaddr* address() const
  { return data; }

  /**
   * Returns sockaddr structure size.
   * @return the size of the sockaddr structre, 0 if there is none.
   */
  virtual ksocklen_t size() const
  { return datasize; }

  /**
   * Returns a sockaddr structure, for passing down to library functions.
   * @return the sockaddr structure, can be 0.
   * @see address()
   */
  operator const sockaddr*() const
  { return data; }

  /**
   * Returns the family of this address.
   * @return the family of this address, AF_UNSPEC if it's undefined
   */
  int family() const;

  /**
   * Returns the IANA family number of this address.
   * @return the IANA family number of this address (1 for AF_INET.
   *         2 for AF_INET6, otherwise 0)
   */
  inline int ianaFamily() const
  { return ianaFamily(family()); }

  /**
   * Returns true if this equals the other socket.
   * @param other	the other socket
   * @return true if both sockets are equal
   */
  virtual bool isEqual(const TDESocketAddress& other) const;
  bool isEqual(const TDESocketAddress* other) const
  { return isEqual(*other); }

  /**
   * Overloaded == operator.
   * @see isEqual()
   */
  bool operator==(const TDESocketAddress& other) const
  { return isEqual(other); }

  /**
   * Some sockets may differ in such things as services or port numbers,
   * like Internet sockets. This function compares only the core part
   * of that, if possible.
   *
   * If not possible, like the default implementation, this returns
   * the same as isEqual.
   * @param other	the other socket
   * @return true if the code part is equal
   */
  bool isCoreEqual(const TDESocketAddress& other) const;

  /**
   * Some sockets may differ in such things as services or port numbers,
   * like Internet sockets. This function compares only the core part
   * of that, if possible.
   *
   * If not possible, like the default implementation, this returns
   * the same as isEqual.
   * @param other	the other socket
   * @return true if the code part is equal
   */
  bool isCoreEqual(const TDESocketAddress* other) const
  { return isCoreEqual(*other); }

  /**
   * Returns the node name of this socket, as KExtendedSocket::lookup expects
   * as the first argument.
   * In the case of Internet sockets, this is the hostname.
   * The default implementation returns TQString::null.
   * @return the node name, can be TQString::null
   */
  virtual TQString nodeName() const;

  /**
   * Returns the service name for this socket, as KExtendedSocket::lookup expects
   * as the service argument.
   * In the case of Internet sockets, this is the port number.
   * The default implementation returns TQString::null.
   * @return the service name, can be TQString::null
   */
  virtual TQString serviceName() const;

protected:
  sockaddr*	data;
  ksocklen_t	datasize;
  bool		owndata;

private:
  void init();
  /* No copy constructor */
  TDESocketAddress(TDESocketAddress&);
  TDESocketAddress& operator=(TDESocketAddress&);

public:
  /**
   * Creates a new TDESocketAddress or descendant class from given
   * raw socket address.
   * @param sa		new socket address
   * @param size	new socket address's length
   * @return the new TDESocketAddress, or 0 if the function failed
   */
  static TDESocketAddress* newAddress(const struct sockaddr *sa, ksocklen_t size);

  /**
   * Returns the IANA family number of the given address family.
   * Returns 0 if there is no corresponding IANA family number.
   * @param af		the address family, in AF_* constants
   * @return the IANA family number of this address (1 for AF_INET.
   *         2 for AF_INET6, otherwise 0)
   */
  static int ianaFamily(int af);

  /**
   * Returns the address family of the given IANA family number.
   * @return the address family, AF_UNSPEC for unknown IANA family numbers
   */
  static int fromIanaFamily(int iana);

  friend class KExtendedSocket;
protected:
  virtual void virtual_hook( int id, void* data );
private:
  TDESocketAddressPrivate* d;
};

/*
 * External definitions
 * We need these for KInetSocketAddress
 */
struct sockaddr_in;
struct sockaddr_in6;
struct in_addr;
struct in6_addr;

class KInetSocketAddressPrivate;
/**
 * An Inet (IPv4 or IPv6) socket address
 *
 * This is an IPv4 or IPv6 address of the Internet
 *
 * This class inherits most of the functionality from TDESocketAddress, but
 * is targeted specifically to Internet addresses
 *
 * @author Thiago Macieira <[email protected]>
 * @short an Internet socket address
 */
class TDECORE_EXPORT KInetSocketAddress: public ::TDESocketAddress
{
  Q_OBJECT
public:
  /**
   * Default constructor. Does nothing
   */
  KInetSocketAddress();

  /**
   * Copy constructor
   */
  KInetSocketAddress(const KInetSocketAddress&);

  /**
   * Creates an IPv4 socket from raw sockaddr_in.
   * @param sin		a sockaddr_in structure to copy from
   * @param len		the socket address length
   */
  KInetSocketAddress(const sockaddr_in* sin, ksocklen_t len);

  /**
   * Creates an IPv6 socket from raw sockaddr_in6.
   * @param sin6       	a sockaddr_in6 structure to copy from
   * @param len		the socket address length
   */
  KInetSocketAddress(const sockaddr_in6* sin6, ksocklen_t len);

  /**
   * Creates a socket from information.
   * @param addr	a binary address
   * @param port	a port number
   */
  KInetSocketAddress(const in_addr& addr, unsigned short port);

  /**
   * Creates a socket from information.
   * @param addr	a binary address
   * @param port	a port number
   */
  KInetSocketAddress(const in6_addr& addr, unsigned short port);

  /**
   * Creates a socket from text representation.
   *
   * @param addr	a text representation of the address
   * @param port	a port number
   * @param family	the family for this address. Use -1 to guess the
   *                    family type
   * @see setAddress
   */
  KInetSocketAddress(const TQString& addr, unsigned short port, int family = -1);

  /**
   * Destructor
   */
  virtual ~KInetSocketAddress();

  /**
   * Sets this socket to given socket.
   * @param ksa		the other socket
   * @return true if successful, false otherwise
   */
  bool setAddress(const KInetSocketAddress& ksa);

  /**
   * Sets this socket to given raw socket.
   * @param sin		the raw socket
   * @param len		the socket address length
   * @return true if successful, false otherwise
   */
  bool setAddress(const sockaddr_in* sin, ksocklen_t len);

  /**
   * Sets this socket to given raw socket.
   *
   * Note: this function does not clear the scope ID and flow info values
   * @param sin6	the raw socket
   * @param len		the socket address length
   * @return true if successful, false otherwise
   */
  bool setAddress(const sockaddr_in6* sin6, ksocklen_t len);

  /**
   * Sets this socket to raw address and port.
   * @param addr	the address
   * @param port	the port number
   * @return true if successful, false otherwise
   */
  bool setAddress(const in_addr& addr, unsigned short port);

  /**
   * Sets this socket to raw address and port.
   * @param addr	the address
   * @param port	the port number
   * @return true if successful, false otherwise
   */
  bool setAddress(const in6_addr& addr, unsigned short port);

  /**
   * Sets this socket to text address and port
   *
   * You can use the @p family parameter to specify what kind of socket
   * you want this to be. It could be AF_INET or AF_INET6 or -1.
   *
   * If the value is -1 (default), this function will make an effort to
   * discover what is the family. That isn't too hard, actually, and it
   * works in all cases. But, if you want to be sure that your socket
   * is of the type you want, use this parameter.
   *
   * This function returns false if the socket address was not valid.
   * @param addr	the address
   * @param port	the port number
   * @param family	the address family, -1 for any
   * @return true if successful, false otherwise
   */
  bool setAddress(const TQString& addr, unsigned short port, int family = -1);

  /**
   * Sets this socket's host address to given raw address.
   * @param addr	the address
   * @return true if successful, false otherwise
   */
  bool setHost(const in_addr& addr);

  /**
   * Sets this socket's host address to given raw address.
   * @param addr	the address
   * @return true if successful, false otherwise
   */
  bool setHost(const in6_addr& addr);

  /**
   * Sets this socket's host address to given text representation.
   * @param addr	the address
   * @param family	the address family, -1 to guess the family
   * @return true if successful, false otherwise
   */
  bool setHost(const TQString& addr, int family = -1);

  /**
   * Sets this socket's port number to given port number.
   * @param port	the port number
   * @return true if successful, false otherwise
   */
  bool setPort(unsigned short port);

  /**
   * Turns this into an IPv4 or IPv6 address.
   *
   * @param family the new address family
   * @return false if this is v6 and information was lost. That doesn't
   * mean the conversion was unsuccessful.
   */
  bool setFamily(int family);

  /**
   * Sets flowinfo information for this socket address if this is IPv6.
   * @param flowinfo	flowinfo
   * @return true if successful, false otherwise
   */
  bool setFlowinfo(TQ_UINT32 flowinfo);

  /**
   * Sets the scope id for this socket if this is IPv6.
   * @param scopeid	the scope id
   * @return true if successful, false otherwise
   */
  bool setScopeId(int scopeid);

  /**
   * Returns a pretty representation of this address.
   * @return a pretty representation
   */
  virtual TQString pretty() const;

  /**
   * Returns the text representation of the host address.
   * @return a text representation of the host address
   */
  virtual TQString nodeName() const;
  //  TQString prettyHost() const;

  /**
   * Returns the text representation of the port number.
   * @return the name of the service (a number)
   */
  virtual TQString serviceName() const;

  /**
   * Returns the socket address.
   *
   * This will be NULL if this is a non-convertible v6.
   * This function will return an IPv4 socket if this IPv6
   * socket is a v4-mapped address. That is, if it's really
   * an IPv4 address, but in v6 disguise.
   * @return the sockaddr_in struct, can be 0.
   */
  const sockaddr_in* addressV4() const;

  /**
   * Returns the socket address in IPv6
   * @return the sockaddr_in struct, can be 0 if IPv6 is unsupported.
   */
  const sockaddr_in6* addressV6() const;

  /**
   * Returns the host address.
   * Might be empty.
   * @return the host address
   */
  in_addr hostV4() const;

  /**
   * Returns the host address.
   *
   * WARNING: this function is not defined if there is no IPv6 support
   * @return the host address
   */
  in6_addr hostV6() const;

  /**
   * Returns the port number.
   * @return the port number
   */
  unsigned short port() const;

  /**
   * Returns flowinfo for IPv6 socket.
   * @return the flowinfo, 0 if unsupported
   */
  TQ_UINT32 flowinfo() const;

  /**
   * Returns the scope id for this IPv6 socket.
   * @return the scope id
   */
  int scopeId() const;

  /**
   * Returns the socket length.
   * Will be either sizeof(sockaddr_in) or sizeof(sockaddr_in6)
   * @return the length of the socket
   */
  virtual ksocklen_t size() const; // should be socklen_t

  /* comparation */
  /**
   * Compares two IPv4 addresses.
   * @param s1 the first address to compare
   * @param s2 the second address to compare
   * @param coreOnly true if only core parts should be compared (only
   *                 the address)
   * @return true if the given addresses are equal.
   * @see areEqualInet6()
   * @see TDESocketAddress::isEqual()
   * @see TDESocketAddress::isCoreEqual()
   */
  static bool areEqualInet(const TDESocketAddress &s1, const TDESocketAddress &s2, bool coreOnly);

  /**
   * Compares two IPv6 addresses.
   * @param s1 the first address to compare
   * @param s2 the second address to compare
   * @param coreOnly true if only core parts should be compared (only
   *                 the address)
   * @return true if the given addresses are equal.
   * @see areEqualInet()
   * @see TDESocketAddress::isEqual()
   * @see TDESocketAddress::isCoreEqual()
   */
  static bool areEqualInet6(const TDESocketAddress &s1, const TDESocketAddress &s2, bool coreOnly);

  /* operators */

  /**
   * Returns the socket address.
   * This will be NULL if this is a non-convertible v6.
   * @return the sockaddr_in structure, can be 0 if v6.
   * @see addressV4()
   */
  operator const sockaddr_in*() const
  { return addressV4(); }

  /**
   * Returns the socket address.
   * @return the sockaddr_in structure, can be 0 if v6 is unsupported.
   * @see addressV6()
   */
  operator const sockaddr_in6*() const
  { return addressV6(); }

  /**
   * Sets this object to be the same as the other.
   */
  KInetSocketAddress& operator=(const KInetSocketAddress &other)
  { setAddress(other); return *this; }

private:

  void fromV4();
  void fromV6();

public:
  /**
   * Convert s the given raw address into text form.
   * This function returns TQString::null if the address cannot be converted.
   * @param family	the family of the address
   * @param addr	the address, in raw form
   * @return the converted address, or TQString::null if not possible.
   */
  static TQString addrToString(int family, const void *addr);

  /**
   * Converts the address given in text form into raw form.
   * The size of the destination buffer @p dest is supposed to be
   * large enough to hold the address of the given family.
   * @param family	the family of the address
   * @param text	the text representation of the address
   * @param dest	the destination buffer of the address
   * @return true if convertion was successful.
   */
  static bool stringToAddr(int family, const char *text, void *dest);

  friend class KExtendedSocket;
protected:
  virtual void virtual_hook( int id, void* data );
private:
  KInetSocketAddressPrivate* d;
};

extern const ::KInetSocketAddress addressAny, addressLoopback;

/*
 * External definition KUnixSocketAddress
 */
struct sockaddr_un;

class KUnixSocketAddressPrivate;
/**
 * A Unix socket address
 *
 * This is a Unix socket address.
 *
 * This class expects TQCString instead of TQString values, which means the
 * filenames should be encoded in whatever form locale/system deems necessary
 * before passing down to the function
 *
 * @author Thiago Macieira <[email protected]>
 * @short a Unix socket address
 */
class TDECORE_EXPORT KUnixSocketAddress: public ::TDESocketAddress
{
  Q_OBJECT
public:
  /**
   * Default constructor
   */
  KUnixSocketAddress();

  /**
   * Constructor from raw data.
   * @param raw_data	raw data
   * @param size	data length
   */
  KUnixSocketAddress(const sockaddr_un* raw_data, ksocklen_t size);

  /**
   * Constructor from pathname.
   * @param pathname	pathname
   */
  KUnixSocketAddress(TQCString pathname);

  /**
   * Destructor
   */
  virtual ~KUnixSocketAddress();

  /**
   * Sets this to given sockaddr_un.
   * @param socket_address socket address
   * @param size	the socket length
   * @return true if successful, false otherwise
   */
  bool setAddress(const sockaddr_un* socket_address, ksocklen_t size);

  /**
   * Sets this to given pathname.
   * @param path	pathname
   * @return true if successful, false otherwise
   */
  bool setAddress(TQCString path);

  /**
   * Returns the pathname.
   * @return the pathname, can be TQCString::null if uninitialized, or
   *         "" if unknown
   */
  TQCString pathname() const;

  /**
   * Returns pretty representation of this socket.
   * @return a pretty text representation of the socket.
   */
  virtual TQString pretty() const;

  /*
   * Returns the path in the form of a TQString.
   * This can be fed into KExtendedSocket.
   * @return the path (can be TQString::null).
   * @see pathname()
   */
  virtual TQString serviceName() const;

  /**
   * Returns raw socket address.
   * @return the raw socket address (can be 0 if uninitialized)
   */
  const sockaddr_un* address() const;

  /**
   * Returns raw socket address.
   * @return the raw socket address (can be 0 if uninitialized)
   * @see address()
   */
  operator const sockaddr_un*() const
  { return address(); }

  /**
   * Compares two unix socket addresses.
   * @param s1 the first address to compare
   * @param s2 the second address to compare
   * @param coreOnly true if only core parts should be compared (currently
   *                 unused)
   * @return true if the given addresses are equal.
   * @see TDESocketAddress::isEqual()
   * @see TDESocketAddress::isCoreEqual()
   */
  static bool areEqualUnix(const TDESocketAddress &s1, const TDESocketAddress &s2, bool coreOnly);

private:
  void init();

  friend class KExtendedSocket;
protected:
  virtual void virtual_hook( int id, void* data );
private:
  KUnixSocketAddressPrivate* d;
};

#endif // KSOCKADDR_H