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
#ifndef _PSOCKETS
00183
#define _PSOCKETS
00184
00185
#ifdef P_USE_PRAGMA
00186
#pragma interface
00187
#endif
00188
00189
#include <ptlib/channel.h>
00190
00191
#ifdef __NUCLEUS_PLUS__
00192
#include <sys/socket.h>
00193
#endif
00194
00195
class PSocket;
00196
00197
PLIST(PSocketList,
PSocket);
00198
00199
00206 class PSocket :
public PChannel
00207 {
00208
PCLASSINFO(
PSocket,
PChannel);
00209
00210
protected:
00211
PSocket();
00212
00213
public:
00226
virtual BOOL
Connect(
00227
const PString & address
00228 );
00229
00230
00232 enum Reusability {
00233
CanReuseAddress,
00234
AddressIsExclusive
00235 };
00236
00250
virtual BOOL Listen(
00251
unsigned queueSize = 5,
00252 WORD port = 0,
00253 Reusability reuse = AddressIsExclusive
00254 );
00255
00256
00278
virtual BOOL Accept(
00279
PSocket & socket
00280 );
00281
00287
virtual BOOL Shutdown(
00288 ShutdownValue option
00289 );
00291
00300 BOOL SetOption(
00301
int option,
00302
int value,
00303
int level = SOL_SOCKET
00304 );
00305
00312 BOOL SetOption(
00313
int option,
00314
const void * valuePtr,
00315 PINDEX valueSize,
00316
int level = SOL_SOCKET
00317 );
00318
00325 BOOL GetOption(
00326
int option,
00327
int & value,
00328
int level = SOL_SOCKET
00329 );
00330
00337 BOOL GetOption(
00338
int option,
00339
void * valuePtr,
00340 PINDEX valueSize,
00341
int level = SOL_SOCKET
00342 );
00344
00352
static WORD GetProtocolByName(
00353
const PString & name
00354 );
00355
00361
static PString GetNameByProtocol(
00362 WORD proto
00363 );
00364
00365
00367
virtual WORD GetPortByService(
00368
const PString & service
00369 )
const;
00387
static WORD GetPortByService(
00388
const char * protocol,
00389
const PString & service
00390 );
00391
00393
virtual PString GetServiceByPort(
00394 WORD port
00395 )
const;
00413
static PString GetServiceByPort(
00414
const char * protocol,
00415 WORD port
00416 );
00417
00418
00420
void SetPort(
00421 WORD port
00422 );
00435
void SetPort(
00436
const PString & service
00437 );
00438
00444 WORD
GetPort() const;
00445
00453
PString GetService() const;
00455
00458
00459 class
SelectList : public PSocketList
00460 {
00461
PCLASSINFO(
SelectList, PSocketList)
00462
public:
00463 SelectList()
00464 { DisallowDeleteObjects(); }
00466 void operator+=(
PSocket & sock )
00467 { Append(&sock); }
00469 void operator-=(
PSocket & sock )
00470 { Remove(&sock); }
00471 };
00472
00474
static int Select(
00475
PSocket & sock1,
00476
PSocket & sock2
00477 );
00479
static int Select(
00480
PSocket & sock1,
00481
PSocket & sock2,
00482
const PTimeInterval & timeout
00483 );
00485
static Errors Select(
00486 SelectList & read
00487 );
00489
static Errors Select(
00490 SelectList & read,
00491
const PTimeInterval & timeout
00492 );
00494
static Errors Select(
00495 SelectList & read,
00496 SelectList & write
00497 );
00499
static Errors Select(
00500 SelectList & read,
00501 SelectList & write,
00502
const PTimeInterval & timeout
00503 );
00505
static Errors Select(
00506 SelectList & read,
00507 SelectList & write,
00508 SelectList & except
00509 );
00531
static Errors Select(
00532 SelectList & read,
00533 SelectList & write,
00534 SelectList & except,
00535
const PTimeInterval & timeout
00536 );
00538
00541
00542 inline static WORD
Host2Net(WORD v) {
return htons(v); }
00544 inline static DWORD Host2Net(DWORD v) {
return htonl(v); }
00545
00547 inline static WORD
Net2Host(WORD v) {
return ntohs(v); }
00549 inline static DWORD Net2Host(DWORD v) {
return ntohl(v); }
00551
00552
protected:
00553
00554
00555
00556
virtual BOOL
OpenSocket() = 0;
00557
00560
virtual const char *
GetProtocolName() const = 0;
00561
00562
00563
int os_close();
00564
int os_socket(
int af,
int type,
int proto);
00565 BOOL os_connect(
00566 struct sockaddr * sin,
00567 PINDEX size
00568 );
00569 BOOL os_recvfrom(
00570
void * buf,
00571 PINDEX len,
00572
int flags,
00573 struct sockaddr * from,
00574 PINDEX * fromlen
00575 );
00576 BOOL os_sendto(
00577 const
void * buf,
00578 PINDEX len,
00579
int flags,
00580 struct sockaddr * to,
00581 PINDEX tolen
00582 );
00583 BOOL os_accept(
00584
PSocket & listener,
00585 struct sockaddr * addr,
00586 PINDEX * size
00587 );
00588
00589
00590
00592 WORD port;
00593
00594
00595 #ifdef _WIN32
00596 #include "msos/ptlib/socket.h"
00597 #else
00598 #include "unix/ptlib/socket.h"
00599 #endif
00600 };
00601
00602
00603
00604
00605 class
P_fd_set {
00606
public:
00607 P_fd_set();
00608 P_fd_set(SOCKET fd);
00609 ~P_fd_set()
00610 {
00611 free(set);
00612 }
00613
00614 P_fd_set & operator=(SOCKET fd);
00615 P_fd_set & operator+=(SOCKET fd);
00616 P_fd_set & operator-=(SOCKET fd);
00617
00618
void Zero();
00619
00620 BOOL IsPresent(SOCKET fd)
const
00621
{
00622
return FD_ISSET(fd, set);
00623 }
00624
00625 operator fd_set*()
const
00626
{
00627
return set;
00628 }
00629
00630
private:
00631
void Construct();
00632
00633 SOCKET max_fd;
00634 fd_set * set;
00635 };
00636
00637
00638 class P_timeval {
00639
public:
00640
P_timeval();
00641 P_timeval(
const PTimeInterval & time)
00642 {
00643 operator=(time);
00644 }
00645
00646
P_timeval & operator=(
const PTimeInterval & time);
00647
00648 operator timeval*()
00649 {
00650
return infinite ? NULL : &tval;
00651 }
00652
00653 timeval * operator->()
00654 {
00655
return &tval;
00656 }
00657
00658 timeval & operator*()
00659 {
00660
return tval;
00661 }
00662
00663
private:
00664
struct timeval tval;
00665 BOOL infinite;
00666 };
00667
00668
#ifdef _WIN32
00669
class PWinSock :
public PSocket
00670 {
00671
PCLASSINFO(PWinSock,
PSocket)
00672
00673 public:
00674 PWinSock();
00675 ~PWinSock();
00676 private:
00677 virtual BOOL OpenSocket();
00678 virtual const
char * GetProtocolName() const;
00679 };
00680 #endif
00681
00682 #endif
00683
00684