PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
socket.h
Go to the documentation of this file.
1 /*
2 * socket.h
3 *
4 * Berkley Socket channel ancestor class.
5 *
6 * Portable Tools Library
7 *
8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9 *
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
14 *
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * The Original Code is Portable Windows Library.
21 *
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23 *
24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25 * All Rights Reserved.
26 *
27 * Contributor(s): ______________________________________.
28 *
29 * $Revision: 31050 $
30 * $Author: rjongbloed $
31 * $Date: 2013-12-10 09:44:29 +1100 (Tue, 10 Dec 2013) $
32 */
33
34 #ifndef PTLIB_SOCKET_H
35 #define PTLIB_SOCKET_H
36
37 #ifdef P_USE_PRAGMA
38 #pragma interface
39 #endif
40
41 #include <ptlib/channel.h>
42
43 #ifdef P_OPENBSD
44 #include <sys/uio.h>
45 #endif
46
47 #ifdef __NUCLEUS_PLUS__
48 #include <sys/socket.h>
49 #endif
50
51
52 class PSocket;
53
54 PLIST(PSocketList, PSocket);
55
56
63 class PSocket : public PChannel
64 {
65 PCLASSINFO(PSocket, PChannel);
66
67 protected:
68 PSocket();
69
70 public:
83 virtual PBoolean Connect(
84 const PString & address
85 );
86
87
89 enum Reusability {
90 CanReuseAddress,
91 AddressIsExclusive
92 };
93
107 virtual PBoolean Listen(
108 unsigned queueSize = 5,
109 WORD port = 0,
110 Reusability reuse = AddressIsExclusive
111 );
112
113
135 virtual PBoolean Accept(
136 PSocket & socket
137 );
138
144 virtual PBoolean Shutdown(
145 ShutdownValue option
146 );
148
157 PBoolean SetOption(
158 int option,
159 int value,
160 int level = SOL_SOCKET
161 );
162
169 PBoolean SetOption(
170 int option,
171 const void * valuePtr,
172 PINDEX valueSize,
173 int level = SOL_SOCKET
174 );
175
182 PBoolean GetOption(
183 int option,
184 int & value,
185 int level = SOL_SOCKET
186 );
187
194 PBoolean GetOption(
195 int option,
196 void * valuePtr,
197 PINDEX valueSize,
198 int level = SOL_SOCKET
199 );
201
209 static WORD GetProtocolByName(
210 const PString & name
211 );
212
218 static PString GetNameByProtocol(
219 WORD proto
220 );
221
222
224 virtual WORD GetPortByService(
225 const PString & service
226 ) const;
244 static WORD GetPortByService(
245 const char * protocol,
246 const PString & service
247 );
248
250 virtual PString GetServiceByPort(
251 WORD port
252 ) const;
270 static PString GetServiceByPort(
271 const char * protocol,
272 WORD port
273 );
274
275
277 void SetPort(
278 WORD port
279 );
292 void SetPort(
293 const PString & service
294 );
295
301 WORD GetPort() const;
302
310 PString GetService() const;
312
315
316 class SelectList : public PSocketList
317 {
318 PCLASSINFO(SelectList, PSocketList)
319 public:
320 SelectList()
321 { DisallowDeleteObjects(); }
323 void operator+=(PSocket & sock )
324 { Append(&sock); }
326 void operator-=(PSocket & sock )
327 { Remove(&sock); }
328 };
329
331 static int Select(
332 PSocket & sock1,
333 PSocket & sock2
334 );
336 static int Select(
337 PSocket & sock1,
338 PSocket & sock2,
339 const PTimeInterval & timeout
340 );
342 static Errors Select(
343 SelectList & read
344 );
346 static Errors Select(
347 SelectList & read,
348 const PTimeInterval & timeout
349 );
351 static Errors Select(
352 SelectList & read,
353 SelectList & write
354 );
356 static Errors Select(
357 SelectList & read,
358 SelectList & write,
359 const PTimeInterval & timeout
360 );
362 static Errors Select(
363 SelectList & read,
364 SelectList & write,
365 SelectList & except
366 );
388 static Errors Select(
389 SelectList & read,
390 SelectList & write,
391 SelectList & except,
392 const PTimeInterval & timeout
393 );
395
398
399 inline static WORD Host2Net(WORD v) { return htons(v); }
401 inline static DWORD Host2Net(DWORD v) { return htonl(v); }
402
404 inline static WORD Net2Host(WORD v) { return ntohs(v); }
406 inline static DWORD Net2Host(DWORD v) { return ntohl(v); }
408
409
414 #if _WIN32
415 struct Slice : public WSABUF
416 {
417 Slice()
418 { buf = (char *)0; len = 0; }
419
420 Slice(void * v, const size_t len)
421 { SetBase(v); SetLength(len); }
422
423 Slice(const void * v, const size_t len)
424 { SetBase(v); SetLength(len); }
425
426 void SetBase(const void * v) { buf = (char *)v; }
427 void SetBase(void * v) { buf = (char *)v; }
428 void * GetBase() const { return buf; }
429
430 void SetLength(size_t v) { len = (ULONG)v; }
431 size_t GetLength() const { return len; }
432 };
433 #else
434 #if P_HAS_RECVMSG
435 struct Slice : public iovec
436 {
437 #else
438 struct Slice
439 {
440 protected:
441 void * iov_base;
442 size_t iov_len;
443 public:
444 #endif
445 Slice()
446 { SetBase(NULL); SetLength(0); }
447
448 Slice(void * v, size_t len)
449 { SetBase(v); SetLength(len); }
450
451 void SetBase(void * v) { iov_base = v; }
452 void * GetBase() const { return iov_base; }
453 void SetLength(size_t v) { iov_len = v; }
454 size_t GetLength() const { return iov_len; }
455 };
456 #endif
457
467 virtual bool Read(
468 Slice * slices, // slices to read to
469 size_t sliceCount
470 );
471
481 virtual bool Write(
482 const Slice * slices, // slices to write from
483 size_t sliceCount
484 );
486
487 protected:
488 /*This function calls os_socket() with the correct parameters for the
489 socket protocol type.
490 */
491 virtual PBoolean OpenSocket() = 0;
492
495 virtual const char * GetProtocolName() const = 0;
496
497
498 int os_close();
499 int os_socket(int af, int type, int proto);
500 PBoolean os_connect(
501 struct sockaddr * sin,
502 socklen_t size
503 );
504
505 PBoolean os_vread(
506 Slice * slices,
507 size_t sliceCount,
508 int flags,
509 struct sockaddr * from,
510 socklen_t * fromlen
511 );
512
513 PBoolean os_vwrite(
514 const Slice * slices,
515 size_t sliceCount,
516 int flags,
517 struct sockaddr * to,
518 socklen_t tolen
519 );
520
521 PBoolean os_accept(
522 PSocket & listener,
523 struct sockaddr * addr,
524 socklen_t * size
525 );
526
527
528 // Member variables
530 WORD port;
531
532 // Include platform dependent part of class
533 #ifdef _WIN32
534 #include "msos/ptlib/socket.h"
535 #else
536 #include "unix/ptlib/socket.h"
537 #endif
538 };
539
540
541 // Utility classes
542
543 class P_fd_set {
544 public:
545 P_fd_set();
546 P_fd_set(SOCKET fd);
547 ~P_fd_set()
548 {
549 free(set);
550 }
551
552 P_fd_set & operator=(SOCKET fd);
553 P_fd_set & operator+=(SOCKET fd);
554 P_fd_set & operator-=(SOCKET fd);
555
556 void Zero();
557
558 PBoolean IsPresent(SOCKET fd) const;
559
560 operator fd_set*() const
561 {
562 return set;
563 }
564
565 protected:
566 void Construct();
567
568 SOCKET max_fd;
569 fd_set * set;
570
571 private:
572 #ifdef __MINGW32__
573 // avoid compile error when this declaration is private
574 // this could be a gcc bug
575 public:
576 P_fd_set(const P_fd_set &) {}
577 private:
578 #else
579 P_fd_set(const P_fd_set &) {}
580 #endif
581 void operator=(const P_fd_set &) {}
582 };
583
584
585 class P_timeval {
586 public:
587 P_timeval();
588 P_timeval(const PTimeInterval & time)
589 {
590 operator=(time);
591 }
592
593 P_timeval & operator=(const PTimeInterval & time);
594
595 operator timeval*()
596 {
597 return infinite ? NULL : &tval;
598 }
599
600 timeval * operator->()
601 {
602 return &tval;
603 }
604
605 timeval & operator*()
606 {
607 return tval;
608 }
609
610 private:
611 struct timeval tval;
612 PBoolean infinite;
613 };
614
615
616 #endif // PTLIB_SOCKET_H
617
618
619 // End Of File ///////////////////////////////////////////////////////////////