Unix Programming - traffic counter in C

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2006 > traffic counter in C





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author traffic counter in C
guilherme.oliveira@gmail.com

2006-05-31, 7:18 am

Hi !

I want to do a traffic counter in C for FreeBSD.
I have already done the daemon with forks but don't know where to get
the value of transfered data becayse these value isn't in '/proc'
(empty) or 'sysctl -a'

Any ideas ?

guilherme.oliveira@gmail.com

2006-05-31, 1:18 pm


Hi again.

I'm trying to do something with netstat but it's giving segmentation
fault (core dumped):


#include <stdio.h>
#include <unistd.h>

void recv()
{
#define LINE_LENGTH 500
char output_recv[LINE_LENGTH];
FILE *grab_recv;

/* exec netstat -s */

execl("/usr/bin/netstat -s -p tcp", "", grab_recv);

/* grab the result */

fgets(output_recv, LINE_LENGTH, grab_recv);
pclose(grab_recv);

/* print the result */

sprintf (output_recv, "Recebidos %s bytes ", output_recv);
puts(output_recv);
}

main ()
{
/* exec recv() to know received traffic */

recv();

}

/* EOF */


guilherme.oliveira@gmail.com escreveu:
> Hi !
>
> I want to do a traffic counter in C for FreeBSD.
> I have already done the daemon with forks but don't know where to get
> the value of transfered data becayse these value isn't in '/proc'
> (empty) or 'sysctl -a'
>
> Any ideas ?


moi

2006-05-31, 1:18 pm

guilherme.oliveira@gmail.com wrote:
> Hi again.
>
> I'm trying to do something with netstat but it's giving segmentation
> fault (core dumped):
>
>
> #include <stdio.h>
> #include <unistd.h>
>
> void recv()
> {
> #define LINE_LENGTH 500
> char output_recv[LINE_LENGTH];
> FILE *grab_recv;
>
> /* exec netstat -s */
>
> execl("/usr/bin/netstat -s -p tcp", "", grab_recv);


You are confusing popen/system/exec**

> /* grab the result */


Your program does not exist any more.
Unless execl() failed.
In that case grab_recv will be uninitialised.
Either way you lose.

> fgets(output_recv, LINE_LENGTH, grab_recv);
> pclose(grab_recv);
>
> /* print the result */
>
> sprintf (output_recv, "Recebidos %s bytes ", output_recv);
> puts(output_recv);
> }
>
> main ()
> {
> /* exec recv() to know received traffic */
>
> recv();


recv() conflicts with a systemcall/library function.
Choose another name.
[vbcol=seagreen]
> }
>
> /* EOF */
>
>
> guilherme.oliveira@gmail.com escreveu:
>

Don't toppost. Even when you are replying to yourself :-)

HTH,
AvK


ed

2006-05-31, 1:18 pm

On 31 May 2006 02:42:09 -0700
guilherme.oliveira@gmail.com wrote:

> I want to do a traffic counter in C for FreeBSD.
> I have already done the daemon with forks but don't know where to get
> the value of transfered data becayse these value isn't in '/proc'
> (empty) or 'sysctl -a'


getifdata is your friend

--
Regards, Ed :: http://www.keyra.co.uk
proud bash hacker
:%s/Open Source/Free Software/g :: Free DNS available

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com