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 #ifndef _PSOCKETS
00186 #define _PSOCKETS
00187
00188 #ifdef P_USE_PRAGMA
00189 #pragma interface
00190 #endif
00191
00192 #include <ptlib/channel.h>
00193
00194 #ifdef __NUCLEUS_PLUS__
00195 #include <sys/socket.h>
00196 #endif
00197
00198 class PSocket;
00199
00200 PLIST(PSocketList, PSocket);
00201
00202
00209 class PSocket : public PChannel
00210 {
00211 PCLASSINFO(PSocket, PChannel);
00212
00213 protected:
00214 PSocket();
00215
00216 public:
00229 virtual BOOL Connect(
00230 const PString & address
00231 );
00232
00233
00235 enum Reusability {
00236 CanReuseAddress,
00237 AddressIsExclusive
00238 };
00239
00253 virtual BOOL Listen(
00254 unsigned queueSize = 5,
00255 WORD port = 0,
00256 Reusability reuse = AddressIsExclusive
00257 );
00258
00259
00281 virtual BOOL Accept(
00282 PSocket & socket
00283 );
00284
00290 virtual BOOL Shutdown(
00291 ShutdownValue option
00292 );
00294
00303 BOOL SetOption(
00304 int option,
00305 int value,
00306 int level = SOL_SOCKET
00307 );
00308
00315 BOOL SetOption(
00316 int option,
00317 const void * valuePtr,
00318 PINDEX valueSize,
00319 int level = SOL_SOCKET
00320 );
00321
00328 BOOL GetOption(
00329 int option,
00330 int & value,
00331 int level = SOL_SOCKET
00332 );
00333
00340 BOOL GetOption(
00341 int option,
00342 void * valuePtr,
00343 PINDEX valueSize,
00344 int level = SOL_SOCKET
00345 );
00347
00355 static WORD GetProtocolByName(
00356 const PString & name
00357 );
00358
00364 static PString GetNameByProtocol(
00365 WORD proto
00366 );
00367
00368
00370 virtual WORD GetPortByService(
00371 const PString & service
00372 ) const;
00390 static WORD GetPortByService(
00391 const char * protocol,
00392 const PString & service
00393 );
00394
00396 virtual PString GetServiceByPort(
00397 WORD port
00398 ) const;
00416 static PString GetServiceByPort(
00417 const char * protocol,
00418 WORD port
00419 );
00420
00421
00423 void SetPort(
00424 WORD port
00425 );
00438 void SetPort(
00439 const PString & service
00440 );
00441
00447 WORD GetPort() const;
00448
00456 PString GetService() const;
00458
00461
00462 class SelectList : public PSocketList
00463 {
00464 PCLASSINFO(SelectList, PSocketList)
00465 public:
00466 SelectList()
00467 { DisallowDeleteObjects(); }
00469 void operator+=(PSocket & sock )
00470 { Append(&sock); }
00472 void operator-=(PSocket & sock )
00473 { Remove(&sock); }
00474 };
00475
00477 static int Select(
00478 PSocket & sock1,
00479 PSocket & sock2
00480 );
00482 static int Select(
00483 PSocket & sock1,
00484 PSocket & sock2,
00485 const PTimeInterval & timeout
00486 );
00488 static Errors Select(
00489 SelectList & read
00490 );
00492 static Errors Select(
00493 SelectList & read,
00494 const PTimeInterval & timeout
00495 );
00497 static Errors Select(
00498 SelectList & read,
00499 SelectList & write
00500 );
00502 static Errors Select(
00503 SelectList & read,
00504 SelectList & write,
00505 const PTimeInterval & timeout
00506 );
00508 static Errors Select(
00509 SelectList & read,
00510 SelectList & write,
00511 SelectList & except
00512 );
00534 static Errors Select(
00535 SelectList & read,
00536 SelectList & write,
00537 SelectList & except,
00538 const PTimeInterval & timeout
00539 );
00541
00544
00545 inline static WORD Host2Net(WORD v) { return htons(v); }
00547 inline static DWORD Host2Net(DWORD v) { return htonl(v); }
00548
00550 inline static WORD Net2Host(WORD v) { return ntohs(v); }
00552 inline static DWORD Net2Host(DWORD v) { return ntohl(v); }
00554
00555 protected:
00556
00557
00558
00559 virtual BOOL OpenSocket() = 0;
00560
00563 virtual const char * GetProtocolName() const = 0;
00564
00565
00566 int os_close();
00567 int os_socket(int af, int type, int proto);
00568 BOOL os_connect(
00569 struct sockaddr * sin,
00570 PINDEX size
00571 );
00572 BOOL os_recvfrom(
00573 void * buf,
00574 PINDEX len,
00575 int flags,
00576 struct sockaddr * from,
00577 PINDEX * fromlen
00578 );
00579 BOOL os_sendto(
00580 const void * buf,
00581 PINDEX len,
00582 int flags,
00583 struct sockaddr * to,
00584 PINDEX tolen
00585 );
00586 BOOL os_accept(
00587 PSocket & listener,
00588 struct sockaddr * addr,
00589 PINDEX * size
00590 );
00591
00592
00593
00595 WORD port;
00596
00597 #if P_HAS_RECVMSG
00598 BOOL catchReceiveToAddr;
00599 virtual void SetLastReceiveAddr(void * , int )
00600 { }
00601 #endif
00602
00603
00604 #ifdef _WIN32
00605 #include "msos/ptlib/socket.h"
00606 #else
00607 #include "unix/ptlib/socket.h"
00608 #endif
00609 };
00610
00611
00612
00613
00614 class P_fd_set {
00615 public:
00616 P_fd_set();
00617 P_fd_set(SOCKET fd);
00618 ~P_fd_set()
00619 {
00620 free(set);
00621 }
00622
00623 P_fd_set & operator=(SOCKET fd);
00624 P_fd_set & operator+=(SOCKET fd);
00625 P_fd_set & operator-=(SOCKET fd);
00626
00627 void Zero();
00628
00629 BOOL IsPresent(SOCKET fd) const
00630 {
00631 return FD_ISSET(fd, set);
00632 }
00633
00634 operator fd_set*() const
00635 {
00636 return set;
00637 }
00638
00639 private:
00640 void Construct();
00641
00642 SOCKET max_fd;
00643 fd_set * set;
00644 };
00645
00646
00647 class P_timeval {
00648 public:
00649 P_timeval();
00650 P_timeval(const PTimeInterval & time)
00651 {
00652 operator=(time);
00653 }
00654
00655 P_timeval & operator=(const PTimeInterval & time);
00656
00657 operator timeval*()
00658 {
00659 return infinite ? NULL : &tval;
00660 }
00661
00662 timeval * operator->()
00663 {
00664 return &tval;
00665 }
00666
00667 timeval & operator*()
00668 {
00669 return tval;
00670 }
00671
00672 private:
00673 struct timeval tval;
00674 BOOL infinite;
00675 };
00676
00677 #ifdef _WIN32
00678 class PWinSock : public PSocket
00679 {
00680 PCLASSINFO(PWinSock, PSocket)
00681
00682 public:
00683 PWinSock();
00684 ~PWinSock();
00685 private:
00686 virtual BOOL OpenSocket();
00687 virtual const char * GetProtocolName() const;
00688 };
00689 #endif
00690
00691 #endif
00692
00693