Note that there are some explanatory texts on larger screens.

plurals
  1. POC Linux Bandwidth Throttling of Application
    primarykey
    data
    text
    <p>What are some ways I can try to throttle back a <code>send</code>/<code>sendto()</code> function inside a loop. I am creating a port scanner for my network and I tried two methods but they only seem to work locally (they work when I test them on my home machine but when I try to test them on another machine it doesn't want to create appropriate throttles).</p> <h2>method 1</h2> <p>I was originally parsing <code>/proc/net/dev</code> and reading in the "bytes sent" attribute and basing my sleep time off that. That worked locally (the sleep delay was adjusting to adjust the flow of bandwidth) but as soon as I tried it on another server also with <code>/proc/net/dev</code> it didn't seem to be adjusting data right. I ran <code>dstat</code> on a machine I was locally scanning and it was outputting to much data to fast. </p> <h2>method 2</h2> <p>I then tried to keep track of how many bytes total I was sending and adding it to a <code>total_sent</code> variable which my bandwidth thread would read and compute a sleep timer for. This also worked on my local machine but when I tried it on a server it was saying that it was only sending 1-2 packets each time my bandwidth thread would check <code>total_sent</code> making my bandwidth thread reduce sleep to 0, but even at 0 the <code>total_sent</code> variable did not increase due to the reduced sleep time but instead stayed the same.</p> <p>Overall I am wanting a way to monitor bandwidth of the Linux computer and calculate a sleep time I can pass into <code>usleep()</code> before or after each of my <code>send</code>/<code>sendto()</code> socket calls to throttle back the bandwidth.</p> <p><strong>Edit:</strong> some other things I forgot to mention is that I do have a speedtest function that calculates upload speed of the machine and I have 2 threads. 1 thread adjusts a global sleep timer based on bandwidth usage and thread 2 sends the packets to the ports on a remote machine to test if they are open and to fingerprint them (right now I am just using udp packets with a <code>sendto()</code> to test this all).</p> <p>How can I implement bandwidth throttling for a <code>send</code>/<code>sendto()</code> call using <code>usleep()</code>.</p> <p><strong>Edit:</strong> Here is the code for my bandwidth monitoring thread. Don't concern yourself about the structure stuff, its just my way of passing data to a thread.</p> <pre><code>void *bandwidthmonitor_cmd(void *param) { int i = 0; double prevbytes = 0, elapsedbytes = 0, byteusage = 0, maxthrottle = 0; //recreating my param struct i passed to the thread command_struct bandwidth = *((command_struct *)param); free(param); //set SLEEP (global variable) to a base time in case it was edited and not reset SLEEP = 5000; //find the maximum throttle speed in kb/s (takes the global var UPLOAD_SPEED //which is in kb/s and times it by how much bandwidth % you want to use //and devides by 100 to find the maximum in kb/s //ex: UPLOAD_SPEED = 60, throttle = 90, maxthrottle = 54 maxthrottle = (UPLOAD_SPEED * bandwidth.throttle) / 100; printf("max throttle: %.1f\n", maxthrottle); while(1) { //find out how many bytes elapsed since last polling of the thread elapsedbytes = TOTAL_BYTES_SEND - prevbytes; printf("elapsedbytes: %.1f\n", elapsedbytes); //set prevbytes to our current bytes so we can have results next loop prevbytes = TOTAL_BYTES_SEND; //convert our bytes to kb/s byteusage = 8 * (elapsedbytes / 1024); //throttle control to make it adjust sleep 20 times every 30~ //iterations of the loop if(i &amp; 0x40) { //adjust SLEEP by 1.1 gain SLEEP += (maxthrottle - byteusage) * -1.1;//; if(SLEEP &lt; 0){ SLEEP = 0; } printf("sleep:%.1f\n\n", SLEEP); } //sleep the thread for a short bit then start the process over usleep(25000); //increment variable i for our iteration throttling i++; } } </code></pre> <p>My sending thread is just a simple <code>sendto()</code> routine in a <code>while(1)</code> loop sending udp packets for testing. <code>sock</code> is my <code>sockfd</code>, <code>buff</code> is a 64 byte character array filled with "A" and <code>sin</code> is <code>my sockaddr_in</code>.</p> <pre><code> while(1) { TOTAL_BYTES_SEND += 64; sendto(sock, buff, strlen(buff), 0, (struct sockaddr *) &amp;sin, sizeof(sin)) usleep(SLEEP); } </code></pre> <p>I know my socket functions work because I can see the usage in <code>dstat</code> on my local machine and the remote machine. This bandwidth code works on my local system (all the variables change as they should) but on the server I tried testing on elapsed bytes does not change (is always 64/128 per iteration of the thread) and results in <code>SLEEP</code> throttling down to 0 which should in theory make the machine send packets faster but even with <code>SLEEP</code> equating to 0 elapsedbytes remain 64/128. I've also encoded the <code>sendto()</code> function inside a if statement checking for the function returning -1 and alerting me by <code>printf</code>-ing the error code but there hasn't been one in the tests I've done.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload