udpconnection.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2000-2001 Nullsoft, Inc.
  4. ** Author: Justin Frankel
  5. ** File: udpconnection.cpp - JNL UDP connection implementation
  6. ** License: see jnetlib.h
  7. */
  8. #include "netinc.h"
  9. #include "util.h"
  10. #include "udpconnection.h"
  11. JNL_UDPConnection::JNL_UDPConnection(unsigned short incoming_port, JNL_AsyncDNS *dns, int sendbufsize, int recvbufsize)
  12. {
  13. init();
  14. open(dns, sendbufsize, recvbufsize);
  15. m_socket=::socket(PF_INET,SOCK_DGRAM,0);
  16. if (m_socket==-1)
  17. {
  18. m_errorstr="creating socket";
  19. m_state=STATE_ERROR;
  20. }
  21. SET_SOCK_BLOCK(m_socket,0);
  22. sockaddr_in m_iaddr;
  23. memset(&m_iaddr,0,sizeof(struct sockaddr_in));
  24. m_iaddr.sin_family=AF_INET;
  25. m_iaddr.sin_port=htons(incoming_port);
  26. m_iaddr.sin_addr.s_addr = htonl( INADDR_ANY );
  27. if(::bind(m_socket,(struct sockaddr *)&m_iaddr,sizeof(m_iaddr))==-1)
  28. {
  29. m_errorstr="binding socket";
  30. m_state=STATE_ERROR;
  31. }
  32. m_state=STATE_CONNECTED;
  33. }
  34. JNL_UDPConnection::JNL_UDPConnection()
  35. {
  36. init();
  37. }
  38. void JNL_UDPConnection::init()
  39. {
  40. m_errorstr="";
  41. address=0;
  42. address_len=0;
  43. m_dns=0;
  44. m_dns_owned=false;
  45. m_socket=-1;
  46. m_remote_port=0;
  47. m_state=STATE_NOCONNECTION;
  48. m_host=0;
  49. saddr=0;
  50. m_last_addr_len=0;
  51. ttl=0;
  52. }
  53. void JNL_UDPConnection::set_ttl(uint8_t new_ttl)
  54. {
  55. ttl=new_ttl;
  56. setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_TTL, (const char *)&ttl, sizeof(ttl));
  57. }
  58. void JNL_UDPConnection::open(int incoming_socket, JNL_AsyncDNS *dns, size_t sendbufsize, size_t recvbufsize)
  59. {
  60. if (dns != JNL_AUTODNS && dns)
  61. {
  62. m_dns=dns;
  63. m_dns_owned=false;
  64. }
  65. else if (!m_dns)
  66. {
  67. m_dns=new JNL_AsyncDNS;
  68. m_dns_owned=true;
  69. }
  70. recv_buffer.reserve(recvbufsize);
  71. send_buffer.reserve(sendbufsize);
  72. m_socket=incoming_socket;
  73. m_state=STATE_CONNECTED;
  74. }
  75. void JNL_UDPConnection::open(JNL_AsyncDNS *dns, size_t sendbufsize, size_t recvbufsize)
  76. {
  77. if (dns != JNL_AUTODNS && dns)
  78. {
  79. m_dns=dns;
  80. m_dns_owned=false;
  81. }
  82. else if (!m_dns)
  83. {
  84. m_dns=new JNL_AsyncDNS;
  85. m_dns_owned=true;
  86. }
  87. recv_buffer.reserve(recvbufsize);
  88. send_buffer.reserve(sendbufsize);
  89. }
  90. void JNL_UDPConnection::setpeer(const char *hostname, int port)
  91. {
  92. m_remote_port=(unsigned short)port;
  93. m_host = _strdup(hostname);
  94. if (!m_host || !m_host[0])
  95. {
  96. m_errorstr="empty hostname";
  97. m_state=STATE_ERROR;
  98. }
  99. else
  100. {
  101. m_state=STATE_RESOLVING;
  102. }
  103. }
  104. void JNL_UDPConnection::setpeer(sockaddr *addr, socklen_t length /* of addr */)
  105. {
  106. //memcpy(&m_saddr, addr, sizeof(sockaddr));
  107. free(address);
  108. address_len=length;
  109. address=(sockaddr *)malloc(length);
  110. memcpy(address, addr, length);
  111. }
  112. JNL_UDPConnection::~JNL_UDPConnection()
  113. {
  114. if (m_socket >= 0)
  115. {
  116. ::closesocket(m_socket);
  117. m_socket=-1;
  118. }
  119. if (!saddr) // free it if it was passed to us (by JNL_Listen, presumably)
  120. free(address); // TODO: change this if we ever do round-robin DNS connecting or in any way change how we handle 'address'
  121. if (m_dns_owned)
  122. {
  123. delete static_cast<JNL_AsyncDNS *>(m_dns);
  124. }
  125. free(m_host);
  126. }
  127. void JNL_UDPConnection::run(size_t max_send_bytes, size_t max_recv_bytes, size_t *bytes_sent, size_t *bytes_rcvd)
  128. {
  129. size_t bytes_allowed_to_send=(max_send_bytes<0)?send_buffer.size():max_send_bytes;
  130. size_t bytes_allowed_to_recv=(max_recv_bytes<0)?recv_buffer.avail():max_recv_bytes;
  131. if (bytes_sent) *bytes_sent=0;
  132. if (bytes_rcvd) *bytes_rcvd=0;
  133. switch (m_state)
  134. {
  135. case STATE_RESOLVING:
  136. if (saddr == 0)
  137. {
  138. int a=m_dns->resolve(m_host, m_remote_port, &saddr, SOCK_DGRAM);
  139. if (!a)
  140. {
  141. address=saddr->ai_addr;
  142. address_len=(socklen_t)saddr->ai_addrlen;
  143. m_state=STATE_CONNECTED;
  144. }
  145. else if (a == 1)
  146. {
  147. m_state=STATE_RESOLVING;
  148. break;
  149. }
  150. else
  151. {
  152. m_errorstr="resolving hostname";
  153. m_state=STATE_ERROR;
  154. return;
  155. }
  156. }
  157. break;
  158. case STATE_CONNECTED:
  159. case STATE_CLOSING:
  160. if (!send_buffer.empty() && bytes_allowed_to_send>0)
  161. {
  162. size_t sent = send_buffer.drain(this, bytes_allowed_to_send);
  163. if (bytes_sent)
  164. *bytes_sent+=sent;
  165. }
  166. /* only read from socket when buffer is empty
  167. * otherwise we risk data loss
  168. * see "man 2 recvfrom" for details
  169. */
  170. if (recv_buffer.empty() && bytes_allowed_to_recv)
  171. {
  172. /*
  173. * use LockBuffer()/UnlockBuffer() because
  174. * "wrap-around" reads can't be done
  175. * we might read data from two separate packets
  176. */
  177. size_t len = recv_buffer.avail();
  178. if (bytes_allowed_to_recv < len)
  179. len = bytes_allowed_to_recv;
  180. recv_buffer.clear();
  181. void *buffer = recv_buffer.LockBuffer();
  182. m_last_addr_len = (int)sizeof(m_last_addr);
  183. int res=::recvfrom(m_socket,(char *)buffer,(int)len,0,(sockaddr *)&m_last_addr,&m_last_addr_len);
  184. if (res == 0 || (res < 0 && ERRNO != JNL_EWOULDBLOCK))
  185. {
  186. recv_buffer.UnlockBuffer(0);
  187. m_state=STATE_CLOSED;
  188. break;
  189. }
  190. if (res > 0)
  191. {
  192. if (bytes_rcvd)
  193. *bytes_rcvd+=res;
  194. recv_buffer.UnlockBuffer(res);
  195. }
  196. else
  197. recv_buffer.UnlockBuffer(0);
  198. }
  199. if (m_state == STATE_CLOSING)
  200. {
  201. if (send_buffer.empty()) m_state = STATE_CLOSED;
  202. }
  203. break;
  204. default:
  205. break;
  206. }
  207. }
  208. /* RingBuffer client function */
  209. size_t JNL_UDPConnection::Read(void *dest, size_t len)
  210. {
  211. if (!len)
  212. return 0;
  213. m_last_addr_len = (int)sizeof(m_last_addr);
  214. int res=::recvfrom(m_socket, (char *)dest, (int)len, 0, (sockaddr *)&m_last_addr,&m_last_addr_len);
  215. if (res == 0 || (res < 0 && ERRNO != JNL_EWOULDBLOCK))
  216. {
  217. m_state=STATE_CLOSED;
  218. return 0;
  219. }
  220. if (res > 0)
  221. return res;
  222. else
  223. return 0;
  224. }
  225. /* RingBuffer client function */
  226. size_t JNL_UDPConnection::Write(const void *dest, size_t len)
  227. {
  228. if (!len)
  229. return 0;
  230. int res=::sendto(m_socket, (const char *)dest, (int)len, 0, address, address_len);
  231. if (res==-1 && ERRNO != JNL_EWOULDBLOCK)
  232. {
  233. return 0;
  234. // m_state=STATE_CLOSED;
  235. }
  236. if (res > 0)
  237. return res;
  238. else
  239. return 0;
  240. }
  241. void JNL_UDPConnection::close(int quick)
  242. {
  243. if (quick || m_state == STATE_RESOLVING)
  244. {
  245. m_state=STATE_CLOSED;
  246. if (m_socket >= 0)
  247. {
  248. ::closesocket(m_socket);
  249. }
  250. m_socket=-1;
  251. recv_buffer.clear();
  252. send_buffer.clear();
  253. m_remote_port=0;
  254. free(m_host);
  255. m_host=0;
  256. //memset(&m_saddr,0,sizeof(m_saddr));
  257. }
  258. else
  259. {
  260. if (m_state == STATE_CONNECTED) m_state=STATE_CLOSING;
  261. }
  262. }
  263. size_t JNL_UDPConnection::send_bytes_in_queue(void)
  264. {
  265. return send_buffer.size();
  266. }
  267. size_t JNL_UDPConnection::send_bytes_available(void)
  268. {
  269. return send_buffer.avail();
  270. }
  271. int JNL_UDPConnection::send(const void *data, size_t length)
  272. {
  273. if (length > send_bytes_available())
  274. {
  275. return -1;
  276. }
  277. send_buffer.write(data, length);
  278. return 0;
  279. }
  280. int JNL_UDPConnection::send_string(const char *line)
  281. {
  282. return send(line,strlen(line));
  283. }
  284. size_t JNL_UDPConnection::recv_bytes_available(void)
  285. {
  286. return recv_buffer.size();
  287. }
  288. size_t JNL_UDPConnection::peek_bytes(void *data, size_t maxlength)
  289. {
  290. return recv_buffer.peek(data, maxlength);
  291. }
  292. size_t JNL_UDPConnection::recv_bytes(void *data, size_t maxlength)
  293. {
  294. return recv_buffer.read(data, maxlength);
  295. }
  296. int JNL_UDPConnection::recv_lines_available(void)
  297. {
  298. int l=(int)recv_bytes_available();
  299. int lcount=0;
  300. int lastch=0;
  301. int pos;
  302. for (pos=0; pos < l; pos ++)
  303. {
  304. char t;
  305. if (recv_buffer.at(pos, &t, 1) != 1)
  306. return lcount;
  307. if ((t=='\r' || t=='\n') &&(
  308. (lastch != '\r' && lastch != '\n') || lastch==t
  309. )) lcount++;
  310. lastch=t;
  311. }
  312. return lcount;
  313. }
  314. int JNL_UDPConnection::recv_line(char *line, size_t maxlength)
  315. {
  316. if (maxlength > recv_buffer.size()) maxlength=recv_buffer.size();
  317. while (maxlength--)
  318. {
  319. char t;
  320. if (recv_buffer.read(&t, 1) == 0)
  321. {
  322. *line=0;
  323. return 0;
  324. }
  325. if (t == '\r' || t == '\n')
  326. {
  327. char r;
  328. if (recv_buffer.peek(&r, 1) != 0)
  329. {
  330. if ((r == '\r' || r == '\n') && r != t)
  331. recv_buffer.advance(1);
  332. }
  333. *line=0;
  334. return 0;
  335. }
  336. *line++=t;
  337. }
  338. return 1;
  339. }
  340. int JNL_UDPConnection::get_interface(sockaddr *sin, socklen_t *sin_length)
  341. {
  342. if (m_socket==-1) return 1;
  343. /*memset(sin,0,sizeof(sockaddr_storage));
  344. *sin_length =sizeof(sockaddr_storage);*/
  345. if (::getsockname(m_socket,(sockaddr *)sin,sin_length))
  346. return 1;
  347. return 0;
  348. }