00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __PJ_SOCK_H__
00021 #define __PJ_SOCK_H__
00022
00028 #include <pj/types.h>
00029
00030 PJ_BEGIN_DECL
00031
00032
00065 extern const pj_uint16_t PJ_AF_UNSPEC;
00066
00068 extern const pj_uint16_t PJ_AF_UNIX;
00069
00071 #define PJ_AF_LOCAL PJ_AF_UNIX;
00072
00074 extern const pj_uint16_t PJ_AF_INET;
00075
00077 extern const pj_uint16_t PJ_AF_INET6;
00078
00080 extern const pj_uint16_t PJ_AF_PACKET;
00081
00083 extern const pj_uint16_t PJ_AF_IRDA;
00084
00085
00086
00087
00088
00089
00090
00091 #if defined(PJ_DLL)
00092
00093 PJ_DECL(pj_uint16_t) pj_AF_UNSPEC(void);
00095 PJ_DECL(pj_uint16_t) pj_AF_UNIX(void);
00097 PJ_DECL(pj_uint16_t) pj_AF_INET(void);
00099 PJ_DECL(pj_uint16_t) pj_AF_INET6(void);
00101 PJ_DECL(pj_uint16_t) pj_AF_PACKET(void);
00103 PJ_DECL(pj_uint16_t) pj_AF_IRDA(void);
00104 #else
00105
00106
00107
00109 # define pj_AF_UNSPEC() PJ_AF_UNSPEC
00110
00111 # define pj_AF_UNIX() PJ_AF_UNIX
00112
00113 # define pj_AF_INET() PJ_AF_INET
00114
00115 # define pj_AF_INET6() PJ_AF_INET6
00116
00117 # define pj_AF_PACKET() PJ_AF_PACKET
00118
00119 # define pj_AF_IRDA() PJ_AF_IRDA
00120 #endif
00121
00122
00131 extern const pj_uint16_t PJ_SOCK_STREAM;
00132
00135 extern const pj_uint16_t PJ_SOCK_DGRAM;
00136
00138 extern const pj_uint16_t PJ_SOCK_RAW;
00139
00141 extern const pj_uint16_t PJ_SOCK_RDM;
00142
00143
00144
00145
00146
00147
00148
00149 #if defined(PJ_DLL)
00150
00151 PJ_DECL(int) pj_SOCK_STREAM(void);
00153 PJ_DECL(int) pj_SOCK_DGRAM(void);
00155 PJ_DECL(int) pj_SOCK_RAW(void);
00157 PJ_DECL(int) pj_SOCK_RDM(void);
00158 #else
00159
00160 # define pj_SOCK_STREAM() PJ_SOCK_STREAM
00161
00162 # define pj_SOCK_DGRAM() PJ_SOCK_DGRAM
00163
00164 # define pj_SOCK_RAW() PJ_SOCK_RAW
00165
00166 # define pj_SOCK_RDM() PJ_SOCK_RDM
00167 #endif
00168
00169
00176 extern const pj_uint16_t PJ_SOL_SOCKET;
00178 extern const pj_uint16_t PJ_SOL_IP;
00180 extern const pj_uint16_t PJ_SOL_TCP;
00182 extern const pj_uint16_t PJ_SOL_UDP;
00184 extern const pj_uint16_t PJ_SOL_IPV6;
00185
00186
00187
00188
00189
00190
00191 #if defined(PJ_DLL)
00192
00193 PJ_DECL(pj_uint16_t) pj_SOL_SOCKET(void);
00195 PJ_DECL(pj_uint16_t) pj_SOL_IP(void);
00197 PJ_DECL(pj_uint16_t) pj_SOL_TCP(void);
00199 PJ_DECL(pj_uint16_t) pj_SOL_UDP(void);
00201 PJ_DECL(pj_uint16_t) pj_SOL_IPV6(void);
00202 #else
00203
00204 # define pj_SOL_SOCKET() PJ_SOL_SOCKET
00205
00206 # define pj_SOL_IP() PJ_SOL_IP
00207
00208 # define pj_SOL_TCP() PJ_SOL_TCP
00209
00210 # define pj_SOL_UDP() PJ_SOL_UDP
00211
00212 # define pj_SOL_IPV6() PJ_SOL_IPV6
00213 #endif
00214
00215
00216
00217
00218
00219
00220
00221
00223 extern const pj_uint16_t PJ_IP_TOS;
00224
00225
00226
00227
00228
00229
00230
00231
00233 extern const pj_uint16_t PJ_IPTOS_LOWDELAY;
00234
00236 extern const pj_uint16_t PJ_IPTOS_THROUGHPUT;
00237
00239 extern const pj_uint16_t PJ_IPTOS_RELIABILITY;
00240
00243 extern const pj_uint16_t PJ_IPTOS_MINCOST;
00244
00245
00246 #if defined(PJ_DLL)
00247
00248 PJ_DECL(int) pj_IP_TOS(void);
00249
00251 PJ_DECL(int) pj_IPTOS_LOWDELAY(void);
00252
00254 PJ_DECL(int) pj_IPTOS_THROUGHPUT(void);
00255
00257 PJ_DECL(int) pj_IPTOS_RELIABILITY(void);
00258
00260 PJ_DECL(int) pj_IPTOS_MINCOST(void);
00261 #else
00262
00263 # define pj_IP_TOS() PJ_IP_TOS
00264
00266 # define pj_IPTOS_LOWDELAY() PJ_IP_TOS_LOWDELAY
00267
00269 # define pj_IPTOS_THROUGHPUT() PJ_IP_TOS_THROUGHPUT
00270
00272 # define pj_IPTOS_RELIABILITY() PJ_IP_TOS_RELIABILITY
00273
00275 # define pj_IPTOS_MINCOST() PJ_IP_TOS_MINCOST
00276 #endif
00277
00278
00285 extern const pj_uint16_t PJ_SO_TYPE;
00286
00288 extern const pj_uint16_t PJ_SO_RCVBUF;
00289
00291 extern const pj_uint16_t PJ_SO_SNDBUF;
00292
00294 extern const pj_uint16_t PJ_TCP_NODELAY;
00295
00298 extern const pj_uint16_t PJ_SO_REUSEADDR;
00299
00301 extern const pj_uint16_t PJ_SO_NOSIGPIPE;
00302
00305 extern const pj_uint16_t PJ_SO_PRIORITY;
00306
00308 extern const pj_uint16_t PJ_IP_MULTICAST_IF;
00309
00311 extern const pj_uint16_t PJ_IP_MULTICAST_TTL;
00312
00314 extern const pj_uint16_t PJ_IP_MULTICAST_LOOP;
00315
00317 extern const pj_uint16_t PJ_IP_ADD_MEMBERSHIP;
00318
00320 extern const pj_uint16_t PJ_IP_DROP_MEMBERSHIP;
00321
00322
00323 #if defined(PJ_DLL)
00324
00325 PJ_DECL(pj_uint16_t) pj_SO_TYPE(void);
00326
00328 PJ_DECL(pj_uint16_t) pj_SO_RCVBUF(void);
00329
00331 PJ_DECL(pj_uint16_t) pj_SO_SNDBUF(void);
00332
00334 PJ_DECL(pj_uint16_t) pj_TCP_NODELAY(void);
00335
00337 PJ_DECL(pj_uint16_t) pj_SO_REUSEADDR(void);
00338
00340 PJ_DECL(pj_uint16_t) pj_SO_NOSIGPIPE(void);
00341
00343 PJ_DECL(pj_uint16_t) pj_SO_PRIORITY(void);
00344
00346 PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_IF(void);
00347
00349 PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_TTL(void);
00350
00352 PJ_DECL(pj_uint16_t) pj_IP_MULTICAST_LOOP(void);
00353
00355 PJ_DECL(pj_uint16_t) pj_IP_ADD_MEMBERSHIP(void);
00356
00358 PJ_DECL(pj_uint16_t) pj_IP_DROP_MEMBERSHIP(void);
00359 #else
00360
00361 # define pj_SO_TYPE() PJ_SO_TYPE
00362
00364 # define pj_SO_RCVBUF() PJ_SO_RCVBUF
00365
00367 # define pj_SO_SNDBUF() PJ_SO_SNDBUF
00368
00370 # define pj_TCP_NODELAY() PJ_TCP_NODELAY
00371
00373 # define pj_SO_REUSEADDR() PJ_SO_REUSEADDR
00374
00376 # define pj_SO_NOSIGPIPE() PJ_SO_NOSIGPIPE
00377
00379 # define pj_SO_PRIORITY() PJ_SO_PRIORITY
00380
00382 # define pj_IP_MULTICAST_IF() PJ_IP_MULTICAST_IF
00383
00385 # define pj_IP_MULTICAST_TTL() PJ_IP_MULTICAST_TTL
00386
00388 # define pj_IP_MULTICAST_LOOP() PJ_IP_MULTICAST_LOOP
00389
00391 # define pj_IP_ADD_MEMBERSHIP() PJ_IP_ADD_MEMBERSHIP
00392
00394 # define pj_IP_DROP_MEMBERSHIP() PJ_IP_DROP_MEMBERSHIP
00395 #endif
00396
00397
00398
00399
00400
00401
00403 extern const int PJ_MSG_OOB;
00404
00406 extern const int PJ_MSG_PEEK;
00407
00409 extern const int PJ_MSG_DONTROUTE;
00410
00411
00412 #if defined(PJ_DLL)
00413
00414 PJ_DECL(int) pj_MSG_OOB(void);
00415
00417 PJ_DECL(int) pj_MSG_PEEK(void);
00418
00420 PJ_DECL(int) pj_MSG_DONTROUTE(void);
00421 #else
00422
00423 # define pj_MSG_OOB() PJ_MSG_OOB
00424
00426 # define pj_MSG_PEEK() PJ_MSG_PEEK
00427
00429 # define pj_MSG_DONTROUTE() PJ_MSG_DONTROUTE
00430 #endif
00431
00432
00436 typedef enum pj_socket_sd_type
00437 {
00438 PJ_SD_RECEIVE = 0,
00439 PJ_SHUT_RD = 0,
00440 PJ_SD_SEND = 1,
00441 PJ_SHUT_WR = 1,
00442 PJ_SD_BOTH = 2,
00443 PJ_SHUT_RDWR = 2
00444 } pj_socket_sd_type;
00445
00446
00447
00449 #define PJ_INADDR_ANY ((pj_uint32_t)0)
00450
00452 #define PJ_INADDR_NONE ((pj_uint32_t)0xffffffff)
00453
00455 #define PJ_INADDR_BROADCAST ((pj_uint32_t)0xffffffff)
00456
00457
00463 #if !defined(PJ_SOMAXCONN)
00464 # define PJ_SOMAXCONN 5
00465 #endif
00466
00467
00472 #define PJ_INVALID_SOCKET (-1)
00473
00474
00475 #undef s_addr
00476
00480 typedef struct pj_in_addr
00481 {
00482 pj_uint32_t s_addr;
00483 } pj_in_addr;
00484
00485
00489 #define PJ_INET_ADDRSTRLEN 16
00490
00494 #define PJ_INET6_ADDRSTRLEN 46
00495
00500 #ifndef PJ_SOCKADDR_IN_SIN_ZERO_LEN
00501 # define PJ_SOCKADDR_IN_SIN_ZERO_LEN 8
00502 #endif
00503
00512 struct pj_sockaddr_in
00513 {
00514 #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
00515 pj_uint8_t sin_zero_len;
00516 pj_uint8_t sin_family;
00517 #else
00518 pj_uint16_t sin_family;
00519 #endif
00520 pj_uint16_t sin_port;
00521 pj_in_addr sin_addr;
00522 char sin_zero[PJ_SOCKADDR_IN_SIN_ZERO_LEN];
00523 };
00524
00525
00526 #undef s6_addr
00527
00531 typedef union pj_in6_addr
00532 {
00533
00534 pj_uint8_t s6_addr[16];
00536
00537 pj_uint32_t u6_addr32[4];
00538
00539
00540
00541
00542
00543
00544 #if 0 && defined(PJ_HAS_INT64) && PJ_HAS_INT64!=0 && \
00545 (!defined(PJ_WIN32) || PJ_WIN32==0)
00546 pj_int64_t u6_addr64[2];
00547 #endif
00548
00549 } pj_in6_addr;
00550
00551
00553 #define PJ_IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
00554
00556 #define PJ_IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
00557
00566 typedef struct pj_sockaddr_in6
00567 {
00568 #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
00569 pj_uint8_t sin6_zero_len;
00570 pj_uint8_t sin6_family;
00571 #else
00572 pj_uint16_t sin6_family;
00573 #endif
00574 pj_uint16_t sin6_port;
00575 pj_uint32_t sin6_flowinfo;
00576 pj_in6_addr sin6_addr;
00577 pj_uint32_t sin6_scope_id;
00578 } pj_sockaddr_in6;
00579
00580
00589 typedef struct pj_addr_hdr
00590 {
00591 #if defined(PJ_SOCKADDR_HAS_LEN) && PJ_SOCKADDR_HAS_LEN!=0
00592 pj_uint8_t sa_zero_len;
00593 pj_uint8_t sa_family;
00594 #else
00595 pj_uint16_t sa_family;
00596 #endif
00597 } pj_addr_hdr;
00598
00599
00603 typedef union pj_sockaddr
00604 {
00605 pj_addr_hdr addr;
00606 pj_sockaddr_in ipv4;
00607 pj_sockaddr_in6 ipv6;
00608 } pj_sockaddr;
00609
00610
00614 typedef struct pj_ip_mreq {
00615 pj_in_addr imr_multiaddr;
00616 pj_in_addr imr_interface;
00617 } pj_ip_mreq;
00618
00619
00620
00621
00622
00623
00624
00625
00626
00633 PJ_DECL(pj_uint16_t) pj_ntohs(pj_uint16_t netshort);
00634
00641 PJ_DECL(pj_uint16_t) pj_htons(pj_uint16_t hostshort);
00642
00649 PJ_DECL(pj_uint32_t) pj_ntohl(pj_uint32_t netlong);
00650
00657 PJ_DECL(pj_uint32_t) pj_htonl(pj_uint32_t hostlong);
00658
00666 PJ_DECL(char*) pj_inet_ntoa(pj_in_addr inaddr);
00667
00678 PJ_DECL(int) pj_inet_aton(const pj_str_t *cp, struct pj_in_addr *inp);
00679
00695 PJ_DECL(pj_status_t) pj_inet_pton(int af, const pj_str_t *src, void *dst);
00696
00716 PJ_DECL(pj_status_t) pj_inet_ntop(int af, const void *src,
00717 char *dst, int size);
00718
00736 PJ_DECL(char*) pj_inet_ntop2(int af, const void *src,
00737 char *dst, int size);
00738
00751 PJ_DECL(char*) pj_sockaddr_print(const pj_sockaddr_t *addr,
00752 char *buf, int size,
00753 unsigned flags);
00754
00767 PJ_DECL(pj_in_addr) pj_inet_addr(const pj_str_t *cp);
00768
00781 PJ_DECL(pj_in_addr) pj_inet_addr2(const char *cp);
00782
00798 PJ_DECL(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
00799 const pj_str_t *cp,
00800 pj_uint16_t port);
00801
00818 PJ_DECL(pj_status_t) pj_sockaddr_init(int af,
00819 pj_sockaddr *addr,
00820 const pj_str_t *cp,
00821 pj_uint16_t port);
00822
00832 PJ_DECL(int) pj_sockaddr_cmp(const pj_sockaddr_t *addr1,
00833 const pj_sockaddr_t *addr2);
00834
00843 PJ_DECL(void*) pj_sockaddr_get_addr(const pj_sockaddr_t *addr);
00844
00852 PJ_DECL(pj_bool_t) pj_sockaddr_has_addr(const pj_sockaddr_t *addr);
00853
00863 PJ_DECL(unsigned) pj_sockaddr_get_addr_len(const pj_sockaddr_t *addr);
00864
00874 PJ_DECL(unsigned) pj_sockaddr_get_len(const pj_sockaddr_t *addr);
00875
00884 PJ_DECL(void) pj_sockaddr_copy_addr(pj_sockaddr *dst,
00885 const pj_sockaddr *src);
00895 PJ_DECL(void) pj_sockaddr_cp(pj_sockaddr_t *dst, const pj_sockaddr_t *src);
00896
00904 PJ_DECL(pj_in_addr) pj_sockaddr_in_get_addr(const pj_sockaddr_in *addr);
00905
00912 PJ_DECL(void) pj_sockaddr_in_set_addr(pj_sockaddr_in *addr,
00913 pj_uint32_t hostaddr);
00914
00930 PJ_DECL(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
00931 const pj_str_t *cp);
00932
00947 PJ_DECL(pj_status_t) pj_sockaddr_set_str_addr(int af,
00948 pj_sockaddr *addr,
00949 const pj_str_t *cp);
00950
00959 PJ_DECL(pj_uint16_t) pj_sockaddr_get_port(const pj_sockaddr_t *addr);
00960
00968 PJ_DECL(pj_uint16_t) pj_sockaddr_in_get_port(const pj_sockaddr_in *addr);
00969
00976 PJ_DECL(pj_status_t) pj_sockaddr_set_port(pj_sockaddr *addr,
00977 pj_uint16_t hostport);
00978
00987 PJ_DECL(void) pj_sockaddr_in_set_port(pj_sockaddr_in *addr,
00988 pj_uint16_t hostport);
00989
01040 PJ_DECL(pj_status_t) pj_sockaddr_parse(int af, unsigned options,
01041 const pj_str_t *str,
01042 pj_sockaddr *addr);
01043
01072 PJ_DECL(pj_status_t) pj_sockaddr_parse2(int af, unsigned options,
01073 const pj_str_t *str,
01074 pj_str_t *hostpart,
01075 pj_uint16_t *port,
01076 int *raf);
01077
01078
01079
01080
01081
01082
01083
01084
01091 PJ_DECL(const pj_str_t*) pj_gethostname(void);
01092
01100 PJ_DECL(pj_in_addr) pj_gethostaddr(void);
01101
01102
01103
01104
01105
01106
01107
01108
01109
01125 PJ_DECL(pj_status_t) pj_sock_socket(int family,
01126 int type,
01127 int protocol,
01128 pj_sock_t *sock);
01129
01137 PJ_DECL(pj_status_t) pj_sock_close(pj_sock_t sockfd);
01138
01139
01152 PJ_DECL(pj_status_t) pj_sock_bind( pj_sock_t sockfd,
01153 const pj_sockaddr_t *my_addr,
01154 int addrlen);
01155
01165 PJ_DECL(pj_status_t) pj_sock_bind_in( pj_sock_t sockfd,
01166 pj_uint32_t addr,
01167 pj_uint16_t port);
01168
01169 #if PJ_HAS_TCP
01170
01181 PJ_DECL(pj_status_t) pj_sock_listen( pj_sock_t sockfd,
01182 int backlog );
01183
01196 PJ_DECL(pj_status_t) pj_sock_accept( pj_sock_t serverfd,
01197 pj_sock_t *newsock,
01198 pj_sockaddr_t *addr,
01199 int *addrlen);
01200 #endif
01201
01218 PJ_DECL(pj_status_t) pj_sock_connect( pj_sock_t sockfd,
01219 const pj_sockaddr_t *serv_addr,
01220 int addrlen);
01221
01233 PJ_DECL(pj_status_t) pj_sock_getpeername(pj_sock_t sockfd,
01234 pj_sockaddr_t *addr,
01235 int *namelen);
01236
01248 PJ_DECL(pj_status_t) pj_sock_getsockname( pj_sock_t sockfd,
01249 pj_sockaddr_t *addr,
01250 int *namelen);
01251
01266 PJ_DECL(pj_status_t) pj_sock_getsockopt( pj_sock_t sockfd,
01267 pj_uint16_t level,
01268 pj_uint16_t optname,
01269 void *optval,
01270 int *optlen);
01284 PJ_DECL(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd,
01285 pj_uint16_t level,
01286 pj_uint16_t optname,
01287 const void *optval,
01288 int optlen);
01289
01290
01302 PJ_DECL(pj_status_t) pj_sock_recv(pj_sock_t sockfd,
01303 void *buf,
01304 pj_ssize_t *len,
01305 unsigned flags);
01306
01323 PJ_DECL(pj_status_t) pj_sock_recvfrom( pj_sock_t sockfd,
01324 void *buf,
01325 pj_ssize_t *len,
01326 unsigned flags,
01327 pj_sockaddr_t *from,
01328 int *fromlen);
01329
01342 PJ_DECL(pj_status_t) pj_sock_send(pj_sock_t sockfd,
01343 const void *buf,
01344 pj_ssize_t *len,
01345 unsigned flags);
01346
01361 PJ_DECL(pj_status_t) pj_sock_sendto(pj_sock_t sockfd,
01362 const void *buf,
01363 pj_ssize_t *len,
01364 unsigned flags,
01365 const pj_sockaddr_t *to,
01366 int tolen);
01367
01368 #if PJ_HAS_TCP
01369
01381 PJ_DECL(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd,
01382 int how);
01383 #endif
01384
01390 PJ_END_DECL
01391
01392 #endif
01393