bandy.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Bandwidth Tester Tester *
  23. * *
  24. * $Archive:: /Commando/Code/Tests/Bandy/bandy.cpp $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 1/05/02 10:22p $*
  29. * *
  30. * $Revision:: 4 $*
  31. * *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * *
  35. * *
  36. *---------------------------------------------------------------------------------------------*
  37. * *
  38. * Functions: *
  39. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  40. #include <winsock.h>
  41. #include <assert.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <bandtest\bandtest.h>
  45. char *ErrorList[13] = {
  46. "BANDTEST_OK",
  47. "BANDTEST_NO_WINSOCK2",
  48. "BANDTEST_NO_RAW_SOCKET_PERMISSION",
  49. "BANDTEST_NO_RAW_SOCKET_CREATE",
  50. "BANDTEST_NO_UDP_SOCKET_BIND",
  51. "BANDTEST_NO_TTL_SET",
  52. "BANDTEST_NO_PING_RESPONSE",
  53. "BANDTEST_NO_FINAL_PING_TIME",
  54. "BANDTEST_NO_EXTERNAL_ROUTER",
  55. "BANDTEST_NO_IP_DETECT",
  56. "BANDTEST_UNKNOWN_ERROR",
  57. "BANDTEST_WRONG_API_VERSION",
  58. "BANDTEST_BAD_PARAM"
  59. };
  60. #define NUM_BANDS 12
  61. unsigned long Bandwidths [NUM_BANDS * 2] = {
  62. 12000, 14400,
  63. 25000, 28800,
  64. 33600, 33600,
  65. 53000, 57600,
  66. 62000, 67200,
  67. 105000, 115200,
  68. 125000, 128000,
  69. 250000, 256000,
  70. 500000, 512000,
  71. 999999, 1024000,
  72. 1999999, 2048000,
  73. 3999999, 4096000
  74. };
  75. char *BandwidthNames [NUM_BANDS] = {
  76. "14400",
  77. "28800",
  78. "33600",
  79. "57600",
  80. "67200",
  81. "115200",
  82. "128k",
  83. "256k",
  84. "512k",
  85. "1M",
  86. "2M",
  87. "4M"
  88. };
  89. BandtestSettingsStruct DefaultSettings = {
  90. 0, //AlwaysICMP
  91. 0, //TTLScatter
  92. 50, //FastPingPackets
  93. 12, //SlowPingPackets
  94. 25, //def =
  95. 0, //Ping profile
  96. };
  97. ULONG Enumerate_Nics(ULONG * addresses, ULONG max_nics)
  98. {
  99. assert(addresses != NULL);
  100. assert(max_nics > 0);
  101. ULONG num_addresses = 0;
  102. //
  103. // Get the local hostname
  104. //
  105. char local_host_name[300];
  106. #ifdef _DEBUG
  107. int gethostname_rc =
  108. #endif //DEBUG
  109. gethostname(local_host_name, sizeof(local_host_name));
  110. assert(gethostname_rc != SOCKET_ERROR);
  111. //
  112. // Resolve hostname for local adapter addresses.
  113. // This does a DNS lookup (name resolution)
  114. //
  115. LPHOSTENT p_hostent = gethostbyname(local_host_name);
  116. if (p_hostent == NULL)
  117. {
  118. }
  119. while (num_addresses < max_nics && p_hostent->h_addr_list[num_addresses] != NULL)
  120. {
  121. IN_ADDR in_addr;
  122. memcpy(&in_addr, p_hostent->h_addr_list[num_addresses], sizeof(in_addr));
  123. addresses[num_addresses] = in_addr.s_addr;
  124. num_addresses++;
  125. }
  126. return num_addresses;
  127. }
  128. char * Addr_As_String(unsigned char *addr)
  129. {
  130. static char _string[128];
  131. sprintf(_string, "%d.%d.%d.%d", (int)(addr[0]), (int)(addr[1]), (int)(addr[2]), (int)(addr[3]));
  132. return(_string);
  133. }
  134. int main(int argc, char **argv)
  135. {
  136. unsigned long my_addresses[8];
  137. int use_addr = -1;
  138. int retries = 3;
  139. int failure_code = BANDTEST_OK;
  140. struct sockaddr_in address;
  141. BandtestSettingsStruct *settings = &DefaultSettings;
  142. WSADATA wsa_data;
  143. if (WSAStartup(MAKEWORD(1,1), &wsa_data) != 0) {
  144. printf("Bandy: WSAStartup failed: error code %d\n", GetLastError());
  145. return(0);
  146. }
  147. int nics = Enumerate_Nics(&my_addresses[0], 8);
  148. struct hostent *host = gethostbyname("www.ea.com");
  149. /*
  150. ** Usage.
  151. */
  152. printf("Bandwidth tester tester\n");
  153. printf("Programmer: Steve Tall\n");
  154. printf("V1.0\n");
  155. printf("Usage: bandy.exe <options>\n");
  156. printf("Options:\n");
  157. printf(" -s<server> - Use specified server (def = www.ea.com)\n");
  158. printf(" -i<ip index> - Use specified local ip index (def = auto discovery)\n");
  159. printf(" -r<retrys> - Retry attempts after failure (def = 3)\n");
  160. printf(" -l<packets> - Number of packets to send on low ping times (def = 50)\n");
  161. printf(" -h<packets> - Number of packets to send on high ping times (def = 12)\n");
  162. printf(" -p<time> - Fast ping threshold in ms (def = 25 ms)\n");
  163. printf(" -a - Send bulk data as ICMP packets (def = UDP)\n");
  164. printf(" -t - Scatter TTL on bulk data (def = no TTL scatter)\n");
  165. printf(" -x - Do ping profiling (def = no profiling)\n\n");
  166. printf("Available IPs : ");
  167. for (int i=0 ; i<nics ; i++) {
  168. printf("%d - %s\n ", i, Addr_As_String((unsigned char*)&my_addresses[i]));
  169. }
  170. printf("\n");
  171. //WSACleanup();
  172. //return(0);
  173. for (int a=1 ; a<argc ; a++) {
  174. if (argv[a][0] != '-' || strlen(&argv[a][0]) < 2) {
  175. printf("Bad parameter %d\n", a);
  176. WSACleanup();
  177. return(0);
  178. }
  179. switch (toupper(argv[a][1])) {
  180. case 'S':
  181. host = gethostbyname(&argv[a][2]);
  182. if (host == NULL) {
  183. printf("Unable to resolve host name %s\n", &argv[a][2]);
  184. WSACleanup();
  185. return(0);
  186. }
  187. break;
  188. case 'I':
  189. use_addr = atoi(&argv[a][2]);
  190. if (use_addr >= nics) {
  191. printf("Bad IP index\n");
  192. WSACleanup();
  193. return(0);
  194. }
  195. break;
  196. case 'R':
  197. retries = atoi(&argv[a][2]);
  198. break;
  199. case 'L':
  200. settings->FastPingPackets = atoi(&argv[a][2]);
  201. break;
  202. case 'H':
  203. settings->SlowPingPackets = atoi(&argv[a][2]);
  204. break;
  205. case 'P':
  206. settings->FastPingThreshold = atoi(&argv[a][2]);
  207. break;
  208. case 'A':
  209. settings->AlwaysICMP = 1;
  210. break;
  211. case 'T':
  212. settings->TTLScatter = 1;
  213. break;
  214. case 'X':
  215. settings->PingProfile = 1;
  216. break;
  217. case 0:
  218. default:
  219. printf("Bad parameter %d\n", a);
  220. WSACleanup();
  221. return(0);
  222. }
  223. }
  224. if (host == NULL) {
  225. printf("Unable to resolve host name\n");
  226. WSACleanup();
  227. return(0);
  228. }
  229. memcpy(&(address.sin_addr), host->h_addr, host->h_length);
  230. printf("Detecting bandwidth - please wait\n");
  231. unsigned long downstream = 0;
  232. unsigned long bw = Detect_Bandwidth(ntohl(address.sin_addr.s_addr), (use_addr == -1) ? 0 : ntohl(my_addresses[use_addr]), retries, failure_code, downstream, BANDTEST_API_VERSION, settings);
  233. if (bw == 0) {
  234. printf("Failed to get bandwidth - error code %s\n", ErrorList[failure_code]);
  235. } else {
  236. if (bw == 0xffffffff) {
  237. printf("Upstream bandwidth is huge :-)\n");
  238. } else {
  239. if (bw > 100000) {
  240. float floater = (float)bw / 1024;
  241. printf("Upstream bandwidth to external router is %.1f kilobits per second\n", floater);
  242. } else {
  243. printf("Upstream bandwidth to external router is %d bits per second\n", bw);
  244. }
  245. bool got_bw_str = false;
  246. for (int i=0 ; i<NUM_BANDS ; i++) {
  247. if (bw < Bandwidths[i*2]) {
  248. printf("Reported upstream connection bandwidth is %s bits per second\n", BandwidthNames[i]);
  249. got_bw_str = true;
  250. break;
  251. }
  252. }
  253. if (!got_bw_str) {
  254. printf("Reported upstream connection bandwidth is > 4M bits per second\n");
  255. }
  256. }
  257. printf("\n");
  258. if (downstream == 0xffffffff) {
  259. printf("Downstream bandwidth is huge :-)\n");
  260. } else {
  261. if (downstream > 100000) {
  262. float floater = (float)downstream / 1024;
  263. printf("Downstream bandwidth to external router is %.1f kilobits per second\n", floater);
  264. } else {
  265. printf("Downstream bandwidth to external router is %d bits per second\n", downstream);
  266. }
  267. bool got_downstream_str = false;
  268. for (int i=0 ; i<NUM_BANDS ; i++) {
  269. if (downstream < Bandwidths[i*2]) {
  270. printf("Reported downstream connection bandwidth is %s bits per second\n", BandwidthNames[i]);
  271. got_downstream_str = true;
  272. break;
  273. }
  274. }
  275. if (!got_downstream_str) {
  276. printf("Reported downstream connection bandwidth is > 4M bits per second\n");
  277. }
  278. }
  279. }
  280. WSACleanup();
  281. return(0);
  282. }