NetBrowse.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // NetBrowse.cpp
  19. // Project: RSPiX
  20. //
  21. // History:
  22. // 09/01/97 MJR Nearing the end of a major overhaul.
  23. //
  24. // 09/02/97 MJR Changed so browse end now does the periodic broadcast
  25. // and hosts merely respond to them. This saves bandwidth
  26. // on the host end, and in fact cuts down overall network
  27. // traffic because we'll only be generating these messages
  28. // when we're browsing, as opposed to having the hosts
  29. // constantly spew out messages, regardless of whether
  30. // anyone is listening.
  31. //
  32. // 09/06/97 MJR Fixed so that it will properly drop hosts that no longer
  33. // exist.
  34. //
  35. ////////////////////////////////////////////////////////////////////////////////
  36. #include "RSPiX.h"
  37. #include "netbrowse.h"
  38. ////////////////////////////////////////////////////////////////////////////////
  39. // Constructor
  40. ////////////////////////////////////////////////////////////////////////////////
  41. CNetBrowse::CNetBrowse()
  42. {
  43. Reset();
  44. }
  45. ////////////////////////////////////////////////////////////////////////////////
  46. // Destructor
  47. ////////////////////////////////////////////////////////////////////////////////
  48. CNetBrowse::~CNetBrowse()
  49. {
  50. Reset();
  51. }
  52. ////////////////////////////////////////////////////////////////////////////////
  53. // Reset
  54. ////////////////////////////////////////////////////////////////////////////////
  55. void CNetBrowse::Reset(void)
  56. {
  57. m_socketBrowse.Reset();
  58. m_lLastBroadcast = 0;
  59. m_usBasePort = 0;
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////
  62. // Startup
  63. ////////////////////////////////////////////////////////////////////////////////
  64. short CNetBrowse::Startup( // Returns 0 if sucessfull, non-zero otherwise
  65. USHORT usPort, // In: Server's base port number
  66. RSocket::BLOCK_CALLBACK callback) // In: Blocking callback
  67. {
  68. short sResult = 0;
  69. // Make sure we start in a good state
  70. Reset();
  71. // Save base port
  72. m_usBasePort = usPort;
  73. // Create socket on which to broadcast
  74. sResult = m_socketBrowse.Open(m_usBasePort + Net::BroadcastPortOffset, RSocket::typDatagram, RSocket::optDontBlock, callback);
  75. if (sResult == 0)
  76. {
  77. // Set socket to broadcast mode
  78. sResult = m_socketBrowse.Broadcast();
  79. if (sResult == 0)
  80. {
  81. }
  82. else
  83. TRACE("CNetBrowse::StartBrowse(): Error putting socket into broadcast mode!\n");
  84. }
  85. else
  86. TRACE("CNetBrowse::StartBrowse(): Couldn't open broadcast socket!\n");
  87. return sResult;
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////
  90. // Shutdown
  91. ////////////////////////////////////////////////////////////////////////////////
  92. void CNetBrowse::Shutdown(void)
  93. {
  94. Reset();
  95. }
  96. ////////////////////////////////////////////////////////////////////////////////
  97. // Update (must be called regularly!)
  98. //
  99. // The lists are updated, if necessary. Note that only the phostsAll is
  100. // important to this function, as it uses that list as the basis of its
  101. // decisions to add or remove hosts. This function will simply add to the
  102. // other two lists as needed -- it does not care what they contain. It is up
  103. // to the caller to decide whether and when to clear those lists.
  104. ////////////////////////////////////////////////////////////////////////////////
  105. void CNetBrowse::Update(
  106. Hosts* phostsAll, // I/O: List of all hosts
  107. Hosts* phostsAdded, // I/O: List of hosts that were added
  108. Hosts* phostsRemoved) // I/O: List of hosts that were removed
  109. {
  110. // Check if it's time to broadcast
  111. long lTime = rspGetMilliseconds();
  112. if ((lTime - m_lLastBroadcast) > Net::BroadcastInterval)
  113. {
  114. // Create message
  115. U8 buf1[4];
  116. buf1[0] = Net::BroadcastMagic0;
  117. buf1[1] = Net::BroadcastMagic1;
  118. buf1[2] = Net::BroadcastMagic2;
  119. buf1[3] = Net::BroadcastMagic3;
  120. // Create destination address (the address on which others will receive this message)
  121. RSocket::Address address;
  122. RSocket::CreateBroadcastAddress(m_usBasePort + Net::AntennaPortOffset, &address);
  123. // Broadcast the message
  124. long lBytesSent;
  125. short serr = m_socketBrowse.SendTo(buf1, sizeof(buf1), &lBytesSent, &address);
  126. if (serr == 0)
  127. {
  128. if (lBytesSent != sizeof(buf1))
  129. TRACE("CNetBrowse::Update(): Error sending broadcast (wrong size)!\n");
  130. }
  131. else
  132. {
  133. if (serr != RSocket::errWouldBlock)
  134. TRACE("CNetBrowse::Update(): Error sending broadcast!\n");
  135. }
  136. // If there was no error, reset the timer. If there was an error, we want to
  137. // retry as soon as possible. If the error is a recurring one that won't go
  138. // away, we'll be retrying every time this is called, but what the hell -- if
  139. // it isn't working, what are we gonna do instead?
  140. if (serr == 0)
  141. m_lLastBroadcast = lTime;
  142. }
  143. // Check for a reply to our broadcast. If we get an incorrectly-sized message,
  144. // we simply ignore it -- this is a datagram socket, so if the message was larger
  145. // than we expected, the rest of it will be discarded, and if it was smaller, then
  146. // we can ignore it as well. Bad messages could come from a foreign app that is
  147. // using the same port as us. If we do get a message, the address of the sender
  148. // will be recorded -- this gives us the host's address!
  149. CHost host;
  150. long lReceived;
  151. U8 buf[sizeof(host.m_acName) + 4 + 4];
  152. short serr = m_socketBrowse.ReceiveFrom(buf, sizeof(buf), &lReceived, &host.m_address);
  153. if (serr == 0)
  154. {
  155. // Validate the message to make sure it was sent by another app of this
  156. // type, as opposed to some unknown app that happens to use the same port.
  157. if ((lReceived == sizeof(buf)) &&
  158. (buf[0] == Net::BroadcastMagic0) &&
  159. (buf[1] == Net::BroadcastMagic1) &&
  160. (buf[2] == Net::BroadcastMagic2) &&
  161. (buf[3] == Net::BroadcastMagic3))
  162. {
  163. // Copy the magic number. The endian nature will always be correct because
  164. // the only entitity that is meant to recognize this value is the one
  165. // that sent it, so as long as the encoding and decoding of the bytes
  166. // is the same, that entity will get the same value that it sent. All
  167. // other entities will see this as a meaningless value, which is fine.
  168. host.m_lMagic =
  169. ((long)buf[4] & 0x000000ff) +
  170. (((long)buf[5] << 8) & 0x0000ff00) +
  171. (((long)buf[6] << 16) & 0x00ff0000) +
  172. (((long)buf[7] << 24) & 0xff000000);
  173. // Copy the name
  174. strncpy(host.m_acName, (char*)&buf[8], sizeof(host.m_acName));
  175. host.m_acName[sizeof(host.m_acName)-1] = 0;
  176. // Init time we last heard from this host to "now"
  177. host.m_lLastHeardFrom = rspGetMilliseconds();
  178. // Change the host's port number from its antenna port to its base port
  179. unsigned short usHostBasePort = RSocket::GetAddressPort(&host.m_address) - Net::AntennaPortOffset;
  180. RSocket::SetAddressPort(usHostBasePort, &host.m_address);
  181. // Check if this host already exists in the list
  182. bool bExists = false;
  183. Hosts::Pointer p;
  184. for (p = phostsAll->GetHead(); p; p = phostsAll->GetNext(p))
  185. {
  186. if (host.IsSameHost(&phostsAll->GetData(p)))
  187. {
  188. // Update this host's time to "now"
  189. phostsAll->GetData(p).m_lLastHeardFrom = rspGetMilliseconds();
  190. // Set flag and stop
  191. bExists = true;
  192. break;
  193. }
  194. }
  195. // If host doesn't already exist, add it to the list
  196. if (!bExists)
  197. {
  198. phostsAll->InsertTail(host);
  199. phostsAdded->InsertTail(host);
  200. }
  201. }
  202. else
  203. TRACE("CNetBrowse::Update(): Validation failed -- another app may be sending crap to our port!\n");
  204. }
  205. else
  206. {
  207. if (serr != RSocket::errWouldBlock)
  208. TRACE("CNetBrowse::Update(): Error receiving broadcast!\n");
  209. }
  210. // Check for hosts that haven't been heard from in too long a time,
  211. // and should therefore be dropped.
  212. Hosts::Pointer p = phostsAll->GetHead();
  213. while (p)
  214. {
  215. Hosts::Pointer pNext = phostsAll->GetNext(p);
  216. if ((rspGetMilliseconds() - phostsAll->GetData(p).m_lLastHeardFrom) > Net::BroadcastDropTime)
  217. {
  218. // Drop this host by moving it from the "all" list to the "dropped" list
  219. phostsRemoved->InsertTail(phostsAll->GetData(p));
  220. phostsAll->Remove(p);
  221. }
  222. p = pNext;
  223. }
  224. }
  225. ////////////////////////////////////////////////////////////////////////////////
  226. // Lookup host by name or hardwired address (like a TCP/IP dotted address).
  227. // The specified port must be the host's "base port".
  228. ////////////////////////////////////////////////////////////////////////////////
  229. // static
  230. short CNetBrowse::LookupHost( // Returns 0 if successfull, non-zero otherwise
  231. char* pszName, // In: Server's name or dotted address (x.x.x.x)
  232. USHORT usPort, // In: Server's port number
  233. RSocket::Address* paddress) // Out: Addresss
  234. {
  235. // Try to get requested address
  236. short sResult = RSocket::GetAddress(pszName, usPort, paddress);
  237. if (sResult != 0)
  238. TRACE("CNetBrowse::LookupHost(): Error getting host address!\n");
  239. return sResult;
  240. }
  241. ////////////////////////////////////////////////////////////////////////////////
  242. // EOF
  243. ////////////////////////////////////////////////////////////////////////////////