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 #ifndef PTLIB_SOCKET_H
00035 #define PTLIB_SOCKET_H
00036
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040
00041 #include <ptlib/channel.h>
00042
00043 #ifdef __NUCLEUS_PLUS__
00044 #include <sys/socket.h>
00045 #endif
00046
00047 class PSocket;
00048
00049 PLIST(PSocketList, PSocket);
00050
00051
00058 class PSocket : public PChannel
00059 {
00060 PCLASSINFO(PSocket, PChannel);
00061
00062 protected:
00063 PSocket();
00064
00065 public:
00078 virtual PBoolean Connect(
00079 const PString & address
00080 );
00081
00082
00084 enum Reusability {
00085 CanReuseAddress,
00086 AddressIsExclusive
00087 };
00088
00102 virtual PBoolean Listen(
00103 unsigned queueSize = 5,
00104 WORD port = 0,
00105 Reusability reuse = AddressIsExclusive
00106 );
00107
00108
00130 virtual PBoolean Accept(
00131 PSocket & socket
00132 );
00133
00139 virtual PBoolean Shutdown(
00140 ShutdownValue option
00141 );
00143
00152 PBoolean SetOption(
00153 int option,
00154 int value,
00155 int level = SOL_SOCKET
00156 );
00157
00164 PBoolean SetOption(
00165 int option,
00166 const void * valuePtr,
00167 PINDEX valueSize,
00168 int level = SOL_SOCKET
00169 );
00170
00177 PBoolean GetOption(
00178 int option,
00179 int & value,
00180 int level = SOL_SOCKET
00181 );
00182
00189 PBoolean GetOption(
00190 int option,
00191 void * valuePtr,
00192 PINDEX valueSize,
00193 int level = SOL_SOCKET
00194 );
00196
00204 static WORD GetProtocolByName(
00205 const PString & name
00206 );
00207
00213 static PString GetNameByProtocol(
00214 WORD proto
00215 );
00216
00217
00219 virtual WORD GetPortByService(
00220 const PString & service
00221 ) const;
00239 static WORD GetPortByService(
00240 const char * protocol,
00241 const PString & service
00242 );
00243
00245 virtual PString GetServiceByPort(
00246 WORD port
00247 ) const;
00265 static PString GetServiceByPort(
00266 const char * protocol,
00267 WORD port
00268 );
00269
00270
00272 void SetPort(
00273 WORD port
00274 );
00287 void SetPort(
00288 const PString & service
00289 );
00290
00296 WORD GetPort() const;
00297
00305 PString GetService() const;
00307
00310
00311 class SelectList : public PSocketList
00312 {
00313 PCLASSINFO(SelectList, PSocketList)
00314 public:
00315 SelectList()
00316 { DisallowDeleteObjects(); }
00318 void operator+=(PSocket & sock )
00319 { Append(&sock); }
00321 void operator-=(PSocket & sock )
00322 { Remove(&sock); }
00323 };
00324
00326 static int Select(
00327 PSocket & sock1,
00328 PSocket & sock2
00329 );
00331 static int Select(
00332 PSocket & sock1,
00333 PSocket & sock2,
00334 const PTimeInterval & timeout
00335 );
00337 static Errors Select(
00338 SelectList & read
00339 );
00341 static Errors Select(
00342 SelectList & read,
00343 const PTimeInterval & timeout
00344 );
00346 static Errors Select(
00347 SelectList & read,
00348 SelectList & write
00349 );
00351 static Errors Select(
00352 SelectList & read,
00353 SelectList & write,
00354 const PTimeInterval & timeout
00355 );
00357 static Errors Select(
00358 SelectList & read,
00359 SelectList & write,
00360 SelectList & except
00361 );
00383 static Errors Select(
00384 SelectList & read,
00385 SelectList & write,
00386 SelectList & except,
00387 const PTimeInterval & timeout
00388 );
00390
00393
00394 inline static WORD Host2Net(WORD v) { return htons(v); }
00396 inline static DWORD Host2Net(DWORD v) { return htonl(v); }
00397
00399 inline static WORD Net2Host(WORD v) { return ntohs(v); }
00401 inline static DWORD Net2Host(DWORD v) { return ntohl(v); }
00403
00404 protected:
00405
00406
00407
00408 virtual PBoolean OpenSocket() = 0;
00409
00412 virtual const char * GetProtocolName() const = 0;
00413
00414
00415 int os_close();
00416 int os_socket(int af, int type, int proto);
00417 PBoolean os_connect(
00418 struct sockaddr * sin,
00419 PINDEX size
00420 );
00421 PBoolean os_recvfrom(
00422 void * buf,
00423 PINDEX len,
00424 int flags,
00425 struct sockaddr * from,
00426 PINDEX * fromlen
00427 );
00428 PBoolean os_sendto(
00429 const void * buf,
00430 PINDEX len,
00431 int flags,
00432 struct sockaddr * to,
00433 PINDEX tolen
00434 );
00435 PBoolean os_accept(
00436 PSocket & listener,
00437 struct sockaddr * addr,
00438 PINDEX * size
00439 );
00440
00441
00442
00444 WORD port;
00445
00446 #if P_HAS_RECVMSG
00447 PBoolean catchReceiveToAddr;
00448 virtual void SetLastReceiveAddr(void * , int )
00449 { }
00450 #endif
00451
00452
00453 #ifdef _WIN32
00454 #include "msos/ptlib/socket.h"
00455 #else
00456 #include "unix/ptlib/socket.h"
00457 #endif
00458 };
00459
00460
00461
00462
00463 class P_fd_set {
00464 public:
00465 P_fd_set();
00466 P_fd_set(SOCKET fd);
00467 ~P_fd_set()
00468 {
00469 free(set);
00470 }
00471
00472 P_fd_set & operator=(SOCKET fd);
00473 P_fd_set & operator+=(SOCKET fd);
00474 P_fd_set & operator-=(SOCKET fd);
00475
00476 void Zero();
00477
00478 PBoolean IsPresent(SOCKET fd) const
00479 {
00480 return FD_ISSET(fd, set);
00481 }
00482
00483 operator fd_set*() const
00484 {
00485 return set;
00486 }
00487
00488 private:
00489 void Construct();
00490
00491 SOCKET max_fd;
00492 fd_set * set;
00493 };
00494
00495
00496 class P_timeval {
00497 public:
00498 P_timeval();
00499 P_timeval(const PTimeInterval & time)
00500 {
00501 operator=(time);
00502 }
00503
00504 P_timeval & operator=(const PTimeInterval & time);
00505
00506 operator timeval*()
00507 {
00508 return infinite ? NULL : &tval;
00509 }
00510
00511 timeval * operator->()
00512 {
00513 return &tval;
00514 }
00515
00516 timeval & operator*()
00517 {
00518 return tval;
00519 }
00520
00521 private:
00522 struct timeval tval;
00523 PBoolean infinite;
00524 };
00525
00526 #ifdef _WIN32
00527 class PWinSock : public PSocket
00528 {
00529 PCLASSINFO(PWinSock, PSocket)
00530
00531 public:
00532 PWinSock();
00533 ~PWinSock();
00534 private:
00535 virtual PBoolean OpenSocket();
00536 virtual const char * GetProtocolName() const;
00537 };
00538 #endif
00539
00540
00541 #endif // PTLIB_SOCKET_H
00542
00543
00544