sock.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* $Id$ */
  2. /*
  3. * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
  4. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #ifndef __PJPP_SOCK_HPP__
  21. #define __PJPP_SOCK_HPP__
  22. #include "pjsua/pj/sock.h"
  23. #include "pjsua/pj/string.h"
  24. class Pj_Event_Handler;
  25. //
  26. // Base class for address.
  27. //
  28. class Pj_Addr
  29. {
  30. };
  31. //
  32. // Internet address.
  33. //
  34. class Pj_Inet_Addr : public pj_sockaddr_in, public Pj_Addr
  35. {
  36. public:
  37. //
  38. // Get port number.
  39. //
  40. pj_uint16_t get_port_number() const
  41. {
  42. return pj_sockaddr_in_get_port(this);
  43. }
  44. //
  45. // Set port number.
  46. //
  47. void set_port_number(pj_uint16_t port)
  48. {
  49. sin_family = PJ_AF_INET;
  50. pj_sockaddr_in_set_port(this, port);
  51. }
  52. //
  53. // Get IP address.
  54. //
  55. pj_uint32_t get_ip_address() const
  56. {
  57. return pj_sockaddr_in_get_addr(this).s_addr;
  58. }
  59. //
  60. // Get address string.
  61. //
  62. const char *get_address() const
  63. {
  64. return pj_inet_ntoa(sin_addr);
  65. }
  66. //
  67. // Set IP address.
  68. //
  69. void set_ip_address(pj_uint32_t addr)
  70. {
  71. sin_family = PJ_AF_INET;
  72. pj_sockaddr_in_set_addr(this, addr);
  73. }
  74. //
  75. // Set address.
  76. //
  77. pj_status_t set_address(const pj_str_t *addr)
  78. {
  79. return pj_sockaddr_in_set_str_addr(this, addr);
  80. }
  81. //
  82. // Set address.
  83. //
  84. pj_status_t set_address(const char *addr)
  85. {
  86. pj_str_t s;
  87. return pj_sockaddr_in_set_str_addr(this, pj_cstr(&s, addr));
  88. }
  89. //
  90. // Compare for equality.
  91. //
  92. bool operator==(const Pj_Inet_Addr &rhs) const
  93. {
  94. return sin_family == rhs.sin_family &&
  95. sin_addr.s_addr == rhs.sin_addr.s_addr &&
  96. sin_port == rhs.sin_port;
  97. }
  98. private:
  99. //
  100. // Dummy length used in pj_ioqueue_recvfrom() etc
  101. //
  102. friend class Pj_Event_Handler;
  103. friend class Pj_Socket;
  104. friend class Pj_Sock_Stream;
  105. friend class Pj_Sock_Dgram;
  106. int addrlen_;
  107. };
  108. //
  109. // Socket base class.
  110. //
  111. // Note:
  112. // socket will not automatically be closed on destructor.
  113. //
  114. class Pj_Socket
  115. {
  116. public:
  117. //
  118. // Default constructor.
  119. //
  120. Pj_Socket()
  121. : sock_(PJ_INVALID_SOCKET)
  122. {
  123. }
  124. //
  125. // Initialize from a socket handle.
  126. //
  127. explicit Pj_Socket(pj_sock_t sock)
  128. : sock_(sock)
  129. {
  130. }
  131. //
  132. // Copy constructor.
  133. //
  134. Pj_Socket(const Pj_Socket &rhs)
  135. : sock_(rhs.sock_)
  136. {
  137. }
  138. //
  139. // Destructor will not close the socket.
  140. // You must call close() explicitly.
  141. //
  142. ~Pj_Socket()
  143. {
  144. }
  145. //
  146. // Set socket handle.
  147. //
  148. void set_handle(pj_sock_t sock)
  149. {
  150. sock_ = sock;
  151. }
  152. //
  153. // Get socket handle.
  154. //
  155. pj_sock_t get_handle() const
  156. {
  157. return sock_;
  158. }
  159. //
  160. // Get socket handle.
  161. //
  162. pj_sock_t& get_handle()
  163. {
  164. return sock_;
  165. }
  166. //
  167. // See if the socket is valid.
  168. //
  169. bool is_valid() const
  170. {
  171. return sock_ != PJ_INVALID_SOCKET;
  172. }
  173. //
  174. // Create the socket.
  175. //
  176. pj_status_t create(int af, int type, int proto)
  177. {
  178. return pj_sock_socket(af, type, proto, &sock_);
  179. }
  180. //
  181. // Bind socket.
  182. //
  183. pj_status_t bind(const Pj_Inet_Addr &addr)
  184. {
  185. return pj_sock_bind(sock_, &addr, sizeof(Pj_Inet_Addr));
  186. }
  187. //
  188. // Close socket.
  189. //
  190. pj_status_t close()
  191. {
  192. pj_sock_close(sock_);
  193. }
  194. //
  195. // Get peer socket name.
  196. //
  197. pj_status_t getpeername(Pj_Inet_Addr *addr)
  198. {
  199. return pj_sock_getpeername(sock_, addr, &addr->addrlen_);
  200. }
  201. //
  202. // getsockname
  203. //
  204. pj_status_t getsockname(Pj_Inet_Addr *addr)
  205. {
  206. return pj_sock_getsockname(sock_, addr, &addr->addrlen_);
  207. }
  208. //
  209. // getsockopt.
  210. //
  211. pj_status_t getsockopt(pj_uint16_t level, pj_uint16_t optname,
  212. void *optval, int *optlen)
  213. {
  214. return pj_sock_getsockopt(sock_, level, optname, optval, optlen);
  215. }
  216. //
  217. // setsockopt
  218. //
  219. pj_status_t setsockopt(pj_uint16_t level, pj_uint16_t optname,
  220. const void *optval, int optlen)
  221. {
  222. return pj_sock_setsockopt(sock_, level, optname, optval, optlen);
  223. }
  224. //
  225. // receive data.
  226. //
  227. pj_ssize_t recv(void *buf, pj_size_t len, int flag = 0)
  228. {
  229. pj_ssize_t bytes = len;
  230. if (pj_sock_recv(sock_, buf, &bytes, flag) != PJ_SUCCESS)
  231. return -1;
  232. return bytes;
  233. }
  234. //
  235. // send data.
  236. //
  237. pj_ssize_t send(const void *buf, pj_ssize_t len, int flag = 0)
  238. {
  239. pj_ssize_t bytes = len;
  240. if (pj_sock_send(sock_, buf, &bytes, flag) != PJ_SUCCESS)
  241. return -1;
  242. return bytes;
  243. }
  244. //
  245. // connect.
  246. //
  247. pj_status_t connect(const Pj_Inet_Addr &addr)
  248. {
  249. return pj_sock_connect(sock_, &addr, sizeof(Pj_Inet_Addr));
  250. }
  251. //
  252. // assignment.
  253. //
  254. Pj_Socket &operator=(const Pj_Socket &rhs)
  255. {
  256. sock_ = rhs.sock_;
  257. return *this;
  258. }
  259. protected:
  260. friend class Pj_Event_Handler;
  261. pj_sock_t sock_;
  262. };
  263. #if PJ_HAS_TCP
  264. //
  265. // Stream socket.
  266. //
  267. class Pj_Sock_Stream : public Pj_Socket
  268. {
  269. public:
  270. //
  271. // Default constructor.
  272. //
  273. Pj_Sock_Stream()
  274. {
  275. }
  276. //
  277. // Initialize from a socket handle.
  278. //
  279. explicit Pj_Sock_Stream(pj_sock_t sock)
  280. : Pj_Socket(sock)
  281. {
  282. }
  283. //
  284. // Copy constructor.
  285. //
  286. Pj_Sock_Stream(const Pj_Sock_Stream &rhs) : Pj_Socket(rhs)
  287. {
  288. }
  289. //
  290. // Assignment.
  291. //
  292. Pj_Sock_Stream &operator=(const Pj_Sock_Stream &rhs)
  293. {
  294. sock_ = rhs.sock_;
  295. return *this;
  296. }
  297. //
  298. // listen()
  299. //
  300. pj_status_t listen(int backlog = 5)
  301. {
  302. return pj_sock_listen(sock_, backlog);
  303. }
  304. //
  305. // blocking accept()
  306. //
  307. Pj_Sock_Stream accept(Pj_Inet_Addr *remote_addr = NULL)
  308. {
  309. pj_sock_t newsock;
  310. int *addrlen = remote_addr ? &remote_addr->addrlen_ : NULL;
  311. pj_status_t status;
  312. status = pj_sock_accept(sock_, &newsock, remote_addr, addrlen);
  313. if (status != PJ_SUCCESS)
  314. return Pj_Sock_Stream(-1);
  315. return Pj_Sock_Stream(newsock);
  316. }
  317. //
  318. // shutdown()
  319. //
  320. pj_status_t shutdown(int how = PJ_SHUT_RDWR)
  321. {
  322. return pj_sock_shutdown(sock_, how);
  323. }
  324. };
  325. #endif
  326. //
  327. // Datagram socket.
  328. //
  329. class Pj_Sock_Dgram : public Pj_Socket
  330. {
  331. public:
  332. //
  333. // Default constructor.
  334. //
  335. Pj_Sock_Dgram()
  336. {
  337. }
  338. //
  339. // Initialize from a socket handle.
  340. //
  341. explicit Pj_Sock_Dgram(pj_sock_t sock)
  342. : Pj_Socket(sock)
  343. {
  344. }
  345. //
  346. // Copy constructor.
  347. //
  348. Pj_Sock_Dgram(const Pj_Sock_Dgram &rhs)
  349. : Pj_Socket(rhs)
  350. {
  351. }
  352. //
  353. // Assignment.
  354. //
  355. Pj_Sock_Dgram &operator=(const Pj_Sock_Dgram &rhs)
  356. {
  357. Pj_Socket::operator =(rhs);
  358. return *this;
  359. }
  360. //
  361. // recvfrom()
  362. //
  363. pj_ssize_t recvfrom( void *buf, pj_size_t len, int flag = 0,
  364. Pj_Inet_Addr *fromaddr = NULL)
  365. {
  366. pj_ssize_t bytes = len;
  367. int *addrlen = fromaddr ? &fromaddr->addrlen_ : NULL;
  368. if (pj_sock_recvfrom( sock_, buf, &bytes, flag,
  369. fromaddr, addrlen) != PJ_SUCCESS)
  370. {
  371. return -1;
  372. }
  373. return bytes;
  374. }
  375. //
  376. // sendto()
  377. //
  378. pj_ssize_t sendto( const void *buf, pj_size_t len, int flag,
  379. const Pj_Inet_Addr &addr)
  380. {
  381. pj_ssize_t bytes = len;
  382. if (pj_sock_sendto( sock_, buf, &bytes, flag,
  383. &addr, sizeof(pj_sockaddr_in)) != PJ_SUCCESS)
  384. {
  385. return -1;
  386. }
  387. return bytes;
  388. }
  389. };
  390. #endif /* __PJPP_SOCK_HPP__ */