00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 #ifndef _PSOCKETS
00189 #define _PSOCKETS
00190
00191 #ifdef P_USE_PRAGMA
00192 #pragma interface
00193 #endif
00194
00195 #include <ptlib/channel.h>
00196
00197 #ifdef __NUCLEUS_PLUS__
00198 #include <sys/socket.h>
00199 #endif
00200
00201 class PSocket;
00202
00203 PLIST(PSocketList, PSocket);
00204
00205
00212 class PSocket : public PChannel
00213 {
00214 PCLASSINFO(PSocket, PChannel);
00215
00216 protected:
00217 PSocket();
00218
00219 public:
00232 virtual BOOL Connect(
00233 const PString & address
00234 );
00235
00236
00238 enum Reusability {
00239 CanReuseAddress,
00240 AddressIsExclusive
00241 };
00242
00256 virtual BOOL Listen(
00257 unsigned queueSize = 5,
00258 WORD port = 0,
00259 Reusability reuse = AddressIsExclusive
00260 );
00261
00262
00284 virtual BOOL Accept(
00285 PSocket & socket
00286 );
00287
00293 virtual BOOL Shutdown(
00294 ShutdownValue option
00295 );
00297
00306 BOOL SetOption(
00307 int option,
00308 int value,
00309 int level = SOL_SOCKET
00310 );
00311
00318 BOOL SetOption(
00319 int option,
00320 const void * valuePtr,
00321 PINDEX valueSize,
00322 int level = SOL_SOCKET
00323 );
00324
00331 BOOL GetOption(
00332 int option,
00333 int & value,
00334 int level = SOL_SOCKET
00335 );
00336
00343 BOOL GetOption(
00344 int option,
00345 void * valuePtr,
00346 PINDEX valueSize,
00347 int level = SOL_SOCKET
00348 );
00350
00358 static WORD GetProtocolByName(
00359 const PString & name
00360 );
00361
00367 static PString GetNameByProtocol(
00368 WORD proto
00369 );
00370
00371
00373 virtual WORD GetPortByService(
00374 const PString & service
00375 ) const;
00393 static WORD GetPortByService(
00394 const char * protocol,
00395 const PString & service
00396 );
00397
00399 virtual PString GetServiceByPort(
00400 WORD port
00401 ) const;
00419 static PString GetServiceByPort(
00420 const char * protocol,
00421 WORD port
00422 );
00423
00424
00426 void SetPort(
00427 WORD port
00428 );
00441 void SetPort(
00442 const PString & service
00443 );
00444
00450 WORD GetPort() const;
00451
00459 PString GetService() const;
00461
00464
00465 class SelectList : public PSocketList
00466 {
00467 PCLASSINFO(SelectList, PSocketList)
00468 public:
00469 SelectList()
00470 { DisallowDeleteObjects(); }
00472 void operator+=(PSocket & sock )
00473 { Append(&sock); }
00475 void operator-=(PSocket & sock )
00476 { Remove(&sock); }
00477 };
00478
00480 static int Select(
00481 PSocket & sock1,
00482 PSocket & sock2
00483 );
00485 static int Select(
00486 PSocket & sock1,
00487 PSocket & sock2,
00488 const PTimeInterval & timeout
00489 );
00491 static Errors Select(
00492 SelectList & read
00493 );
00495 static Errors Select(
00496 SelectList & read,
00497 const PTimeInterval & timeout
00498 );
00500 static Errors Select(
00501 SelectList & read,
00502 SelectList & write
00503 );
00505 static Errors Select(
00506 SelectList & read,
00507 SelectList & write,
00508 const PTimeInterval & timeout
00509 );
00511 static Errors Select(
00512 SelectList & read,
00513 SelectList & write,
00514 SelectList & except
00515 );
00537 static Errors Select(
00538 SelectList & read,
00539 SelectList & write,
00540 SelectList & except,
00541 const PTimeInterval & timeout
00542 );
00544
00547
00548 inline static WORD Host2Net(WORD v) { return htons(v); }
00550 inline static DWORD Host2Net(DWORD v) { return htonl(v); }
00551
00553 inline static WORD Net2Host(WORD v) { return ntohs(v); }
00555 inline static DWORD Net2Host(DWORD v) { return ntohl(v); }
00557
00558 protected:
00559
00560
00561
00562 virtual BOOL OpenSocket() = 0;
00563
00566 virtual const char * GetProtocolName() const = 0;
00567
00568
00569 int os_close();
00570 int os_socket(int af, int type, int proto);
00571 BOOL os_connect(
00572 struct sockaddr * sin,
00573 PINDEX size
00574 );
00575 BOOL os_recvfrom(
00576 void * buf,
00577 PINDEX len,
00578 int flags,
00579 struct sockaddr * from,
00580 PINDEX * fromlen
00581 );
00582 BOOL os_sendto(
00583 const void * buf,
00584 PINDEX len,
00585 int flags,
00586 struct sockaddr * to,
00587 PINDEX tolen
00588 );
00589 BOOL os_accept(
00590 PSocket & listener,
00591 struct sockaddr * addr,
00592 PINDEX * size
00593 );
00594
00595
00596
00598 WORD port;
00599
00600 #if P_HAS_RECVMSG
00601 BOOL catchReceiveToAddr;
00602 virtual void SetLastReceiveAddr(void * , int )
00603 { }
00604 #endif
00605
00606
00607 #ifdef _WIN32
00608 #include "msos/ptlib/socket.h"
00609 #else
00610 #include "unix/ptlib/socket.h"
00611 #endif
00612 };
00613
00614
00615
00616
00617 class P_fd_set {
00618 public:
00619 P_fd_set();
00620 P_fd_set(SOCKET fd);
00621 ~P_fd_set()
00622 {
00623 free(set);
00624 }
00625
00626 P_fd_set & operator=(SOCKET fd);
00627 P_fd_set & operator+=(SOCKET fd);
00628 P_fd_set & operator-=(SOCKET fd);
00629
00630 void Zero();
00631
00632 BOOL IsPresent(SOCKET fd) const
00633 {
00634 return FD_ISSET(fd, set);
00635 }
00636
00637 operator fd_set*() const
00638 {
00639 return set;
00640 }
00641
00642 private:
00643 void Construct();
00644
00645 SOCKET max_fd;
00646 fd_set * set;
00647 };
00648
00649
00650 class P_timeval {
00651 public:
00652 P_timeval();
00653 P_timeval(const PTimeInterval & time)
00654 {
00655 operator=(time);
00656 }
00657
00658 P_timeval & operator=(const PTimeInterval & time);
00659
00660 operator timeval*()
00661 {
00662 return infinite ? NULL : &tval;
00663 }
00664
00665 timeval * operator->()
00666 {
00667 return &tval;
00668 }
00669
00670 timeval & operator*()
00671 {
00672 return tval;
00673 }
00674
00675 private:
00676 struct timeval tval;
00677 BOOL infinite;
00678 };
00679
00680 #ifdef _WIN32
00681 class PWinSock : public PSocket
00682 {
00683 PCLASSINFO(PWinSock, PSocket)
00684
00685 public:
00686 PWinSock();
00687 ~PWinSock();
00688 private:
00689 virtual BOOL OpenSocket();
00690 virtual const char * GetProtocolName() const;
00691 };
00692 #endif
00693
00694 #endif
00695
00696