dostest-attacker.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /**
  2. * @file dostest-attacker.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <stdarg.h>
  32. #include <stdlib.h>
  33. #include <misc/debug.h>
  34. #include <misc/version.h>
  35. #include <misc/offset.h>
  36. #include <misc/open_standard_streams.h>
  37. #include <misc/balloc.h>
  38. #include <misc/loglevel.h>
  39. #include <misc/minmax.h>
  40. #include <structure/LinkedList1.h>
  41. #include <base/BLog.h>
  42. #include <system/BAddr.h>
  43. #include <system/BReactor.h>
  44. #include <system/BNetwork.h>
  45. #include <system/BConnection.h>
  46. #include <system/BSignal.h>
  47. #include <generated/blog_channel_dostest_attacker.h>
  48. #define PROGRAM_NAME "dostest-attacker"
  49. // connection structure
  50. struct connection {
  51. int connected;
  52. BConnector connector;
  53. BConnection con;
  54. StreamRecvInterface *recv_if;
  55. uint8_t buf[512];
  56. LinkedList1Node connections_list_node;
  57. };
  58. // command-line options
  59. static struct {
  60. int help;
  61. int version;
  62. char *connect_addr;
  63. int max_connections;
  64. int max_connecting;
  65. int loglevel;
  66. int loglevels[BLOG_NUM_CHANNELS];
  67. } options;
  68. // connect address
  69. static BAddr connect_addr;
  70. // reactor
  71. static BReactor reactor;
  72. // connections
  73. static LinkedList1 connections_list;
  74. static int num_connections;
  75. static int num_connecting;
  76. // timer for scheduling creation of more connections
  77. static BTimer make_connections_timer;
  78. static void print_help (const char *name);
  79. static void print_version (void);
  80. static int parse_arguments (int argc, char *argv[]);
  81. static int process_arguments (void);
  82. static void signal_handler (void *unused);
  83. static int connection_new (void);
  84. static void connection_free (struct connection *conn);
  85. static void connection_logfunc (struct connection *conn);
  86. static void connection_log (struct connection *conn, int level, const char *fmt, ...);
  87. static void connection_connector_handler (struct connection *conn, int is_error);
  88. static void connection_connection_handler (struct connection *conn, int event);
  89. static void connection_recv_handler_done (struct connection *conn, int data_len);
  90. static void make_connections_timer_handler (void *unused);
  91. int main (int argc, char **argv)
  92. {
  93. if (argc <= 0) {
  94. return 1;
  95. }
  96. // open standard streams
  97. open_standard_streams();
  98. // parse command-line arguments
  99. if (!parse_arguments(argc, argv)) {
  100. fprintf(stderr, "Failed to parse arguments\n");
  101. print_help(argv[0]);
  102. goto fail0;
  103. }
  104. // handle --help and --version
  105. if (options.help) {
  106. print_version();
  107. print_help(argv[0]);
  108. return 0;
  109. }
  110. if (options.version) {
  111. print_version();
  112. return 0;
  113. }
  114. // init loger
  115. BLog_InitStderr();
  116. // configure logger channels
  117. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  118. if (options.loglevels[i] >= 0) {
  119. BLog_SetChannelLoglevel(i, options.loglevels[i]);
  120. }
  121. else if (options.loglevel >= 0) {
  122. BLog_SetChannelLoglevel(i, options.loglevel);
  123. }
  124. }
  125. BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
  126. // initialize network
  127. if (!BNetwork_GlobalInit()) {
  128. BLog(BLOG_ERROR, "BNetwork_GlobalInit failed");
  129. goto fail1;
  130. }
  131. // process arguments
  132. if (!process_arguments()) {
  133. BLog(BLOG_ERROR, "Failed to process arguments");
  134. goto fail1;
  135. }
  136. // init time
  137. BTime_Init();
  138. // init reactor
  139. if (!BReactor_Init(&reactor)) {
  140. BLog(BLOG_ERROR, "BReactor_Init failed");
  141. goto fail1;
  142. }
  143. // setup signal handler
  144. if (!BSignal_Init(&reactor, signal_handler, NULL)) {
  145. BLog(BLOG_ERROR, "BSignal_Init failed");
  146. goto fail2;
  147. }
  148. // init connections list
  149. LinkedList1_Init(&connections_list);
  150. num_connections = 0;
  151. num_connecting = 0;
  152. // init make connections timer
  153. BTimer_Init(&make_connections_timer, 0, make_connections_timer_handler, NULL);
  154. BReactor_SetTimer(&reactor, &make_connections_timer);
  155. // enter event loop
  156. BLog(BLOG_NOTICE, "entering event loop");
  157. BReactor_Exec(&reactor);
  158. // free connections
  159. while (!LinkedList1_IsEmpty(&connections_list)) {
  160. struct connection *conn = UPPER_OBJECT(LinkedList1_GetFirst(&connections_list), struct connection, connections_list_node);
  161. connection_free(conn);
  162. }
  163. // free make connections timer
  164. BReactor_RemoveTimer(&reactor, &make_connections_timer);
  165. // free signal
  166. BSignal_Finish();
  167. fail2:
  168. // free reactor
  169. BReactor_Free(&reactor);
  170. fail1:
  171. // free logger
  172. BLog(BLOG_NOTICE, "exiting");
  173. BLog_Free();
  174. fail0:
  175. // finish debug objects
  176. DebugObjectGlobal_Finish();
  177. return 1;
  178. }
  179. void print_help (const char *name)
  180. {
  181. printf(
  182. "Usage:\n"
  183. " %s\n"
  184. " [--help]\n"
  185. " [--version]\n"
  186. " --connect-addr <addr>\n"
  187. " --max-connections <number>\n"
  188. " --max-connecting <number>\n"
  189. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  190. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  191. "Address format is a.b.c.d:port (IPv4) or [addr]:port (IPv6).\n",
  192. name
  193. );
  194. }
  195. void print_version (void)
  196. {
  197. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  198. }
  199. int parse_arguments (int argc, char *argv[])
  200. {
  201. options.help = 0;
  202. options.version = 0;
  203. options.connect_addr = NULL;
  204. options.max_connections = -1;
  205. options.max_connecting = -1;
  206. options.loglevel = -1;
  207. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  208. options.loglevels[i] = -1;
  209. }
  210. int i;
  211. for (i = 1; i < argc; i++) {
  212. char *arg = argv[i];
  213. if (!strcmp(arg, "--help")) {
  214. options.help = 1;
  215. }
  216. else if (!strcmp(arg, "--version")) {
  217. options.version = 1;
  218. }
  219. else if (!strcmp(arg, "--connect-addr")) {
  220. if (1 >= argc - i) {
  221. fprintf(stderr, "%s: requires an argument\n", arg);
  222. return 0;
  223. }
  224. options.connect_addr = argv[i + 1];
  225. i++;
  226. }
  227. else if (!strcmp(arg, "--max-connections")) {
  228. if (1 >= argc - i) {
  229. fprintf(stderr, "%s: requires an argument\n", arg);
  230. return 0;
  231. }
  232. if ((options.max_connections = atoi(argv[i + 1])) <= 0) {
  233. fprintf(stderr, "%s: wrong argument\n", arg);
  234. return 0;
  235. }
  236. i++;
  237. }
  238. else if (!strcmp(arg, "--max-connecting")) {
  239. if (1 >= argc - i) {
  240. fprintf(stderr, "%s: requires an argument\n", arg);
  241. return 0;
  242. }
  243. if ((options.max_connecting = atoi(argv[i + 1])) <= 0) {
  244. fprintf(stderr, "%s: wrong argument\n", arg);
  245. return 0;
  246. }
  247. i++;
  248. }
  249. else if (!strcmp(arg, "--loglevel")) {
  250. if (1 >= argc - i) {
  251. fprintf(stderr, "%s: requires an argument\n", arg);
  252. return 0;
  253. }
  254. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  255. fprintf(stderr, "%s: wrong argument\n", arg);
  256. return 0;
  257. }
  258. i++;
  259. }
  260. else if (!strcmp(arg, "--channel-loglevel")) {
  261. if (2 >= argc - i) {
  262. fprintf(stderr, "%s: requires two arguments\n", arg);
  263. return 0;
  264. }
  265. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  266. if (channel < 0) {
  267. fprintf(stderr, "%s: wrong channel argument\n", arg);
  268. return 0;
  269. }
  270. int loglevel = parse_loglevel(argv[i + 2]);
  271. if (loglevel < 0) {
  272. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  273. return 0;
  274. }
  275. options.loglevels[channel] = loglevel;
  276. i += 2;
  277. }
  278. else {
  279. fprintf(stderr, "unknown option: %s\n", arg);
  280. return 0;
  281. }
  282. }
  283. if (options.help || options.version) {
  284. return 1;
  285. }
  286. if (!options.connect_addr) {
  287. fprintf(stderr, "--connect-addr missing\n");
  288. return 0;
  289. }
  290. if (options.max_connections == -1) {
  291. fprintf(stderr, "--max-connections missing\n");
  292. return 0;
  293. }
  294. if (options.max_connecting == -1) {
  295. fprintf(stderr, "--max-connecting missing\n");
  296. return 0;
  297. }
  298. return 1;
  299. }
  300. int process_arguments (void)
  301. {
  302. // resolve listen address
  303. if (!BAddr_Parse(&connect_addr, options.connect_addr, NULL, 0)) {
  304. BLog(BLOG_ERROR, "connect addr: BAddr_Parse failed");
  305. return 0;
  306. }
  307. return 1;
  308. }
  309. void signal_handler (void *unused)
  310. {
  311. BLog(BLOG_NOTICE, "termination requested");
  312. // exit event loop
  313. BReactor_Quit(&reactor, 1);
  314. }
  315. int connection_new (void)
  316. {
  317. // allocate structure
  318. struct connection *conn = (struct connection *)malloc(sizeof(*conn));
  319. if (!conn) {
  320. BLog(BLOG_ERROR, "malloc failed");
  321. goto fail0;
  322. }
  323. // set not connected
  324. conn->connected = 0;
  325. // init connector
  326. if (!BConnector_Init(&conn->connector, connect_addr, &reactor, conn, (BConnector_handler)connection_connector_handler)) {
  327. BLog(BLOG_ERROR, "BConnector_Init failed");
  328. goto fail1;
  329. }
  330. // add to connections list
  331. LinkedList1_Append(&connections_list, &conn->connections_list_node);
  332. num_connections++;
  333. num_connecting++;
  334. return 1;
  335. fail1:
  336. free(conn);
  337. fail0:
  338. return 0;
  339. }
  340. void connection_free (struct connection *conn)
  341. {
  342. // remove from connections list
  343. LinkedList1_Remove(&connections_list, &conn->connections_list_node);
  344. num_connections--;
  345. if (!conn->connected) {
  346. num_connecting--;
  347. }
  348. if (conn->connected) {
  349. // free receive interface
  350. BConnection_RecvAsync_Free(&conn->con);
  351. // free connection
  352. BConnection_Free(&conn->con);
  353. }
  354. // free connector
  355. BConnector_Free(&conn->connector);
  356. // free structure
  357. free(conn);
  358. }
  359. void connection_logfunc (struct connection *conn)
  360. {
  361. BLog_Append("%d connection (%p): ", num_connecting, (void *)conn);
  362. }
  363. void connection_log (struct connection *conn, int level, const char *fmt, ...)
  364. {
  365. va_list vl;
  366. va_start(vl, fmt);
  367. BLog_LogViaFuncVarArg((BLog_logfunc)connection_logfunc, conn, BLOG_CURRENT_CHANNEL, level, fmt, vl);
  368. va_end(vl);
  369. }
  370. void connection_connector_handler (struct connection *conn, int is_error)
  371. {
  372. ASSERT(!conn->connected)
  373. // check for connection error
  374. if (is_error) {
  375. connection_log(conn, BLOG_INFO, "failed to connect");
  376. goto fail0;
  377. }
  378. // init connection from connector
  379. if (!BConnection_Init(&conn->con, BConnection_source_connector(&conn->connector), &reactor, conn, (BConnection_handler)connection_connection_handler)) {
  380. connection_log(conn, BLOG_INFO, "BConnection_Init failed");
  381. goto fail0;
  382. }
  383. // init receive interface
  384. BConnection_RecvAsync_Init(&conn->con);
  385. conn->recv_if = BConnection_RecvAsync_GetIf(&conn->con);
  386. StreamRecvInterface_Receiver_Init(conn->recv_if, (StreamRecvInterface_handler_done)connection_recv_handler_done, conn);
  387. // start receiving
  388. StreamRecvInterface_Receiver_Recv(conn->recv_if, conn->buf, sizeof(conn->buf));
  389. // no longer connecting
  390. conn->connected = 1;
  391. num_connecting--;
  392. connection_log(conn, BLOG_INFO, "connected");
  393. // schedule making connections (because of connecting limit)
  394. BReactor_SetTimer(&reactor, &make_connections_timer);
  395. return;
  396. fail0:
  397. // free connection
  398. connection_free(conn);
  399. // schedule making connections
  400. BReactor_SetTimer(&reactor, &make_connections_timer);
  401. }
  402. void connection_connection_handler (struct connection *conn, int event)
  403. {
  404. ASSERT(conn->connected)
  405. if (event == BCONNECTION_EVENT_RECVCLOSED) {
  406. connection_log(conn, BLOG_INFO, "connection closed");
  407. } else {
  408. connection_log(conn, BLOG_INFO, "connection error");
  409. }
  410. // free connection
  411. connection_free(conn);
  412. // schedule making connections
  413. BReactor_SetTimer(&reactor, &make_connections_timer);
  414. }
  415. void connection_recv_handler_done (struct connection *conn, int data_len)
  416. {
  417. ASSERT(conn->connected)
  418. // receive more
  419. StreamRecvInterface_Receiver_Recv(conn->recv_if, conn->buf, sizeof(conn->buf));
  420. connection_log(conn, BLOG_INFO, "received %d bytes", data_len);
  421. }
  422. void make_connections_timer_handler (void *unused)
  423. {
  424. int make_num = bmin_int(options.max_connections - num_connections, options.max_connecting - num_connecting);
  425. if (make_num <= 0) {
  426. return;
  427. }
  428. BLog(BLOG_INFO, "making %d connections", make_num);
  429. for (int i = 0; i < make_num; i++) {
  430. if (!connection_new()) {
  431. // can happen if fd limit is reached
  432. BLog(BLOG_ERROR, "failed to make connection, waiting");
  433. BReactor_SetTimerAfter(&reactor, &make_connections_timer, 10);
  434. return;
  435. }
  436. }
  437. }