Code Dump

Dumps . The ultimate Dumped Coded zone.

Tuesday, June 20, 2006

Milliseconds in C

http://www.thescripts.com/forum/thread212375.html

int main()

{

clock_t t1 = clock(), t2;

while ((t2 = clock()) == t1) ;

printf("CLOCKS_PER_SEC: %.0f resolution: %f sec\n",

(double)CLOCKS_PER_SEC, (t2 - t1) / (double)CLOCKS_PER_SEC);

return 0;

}

fangorn:~/tmp 2299> gcc clockres.c

fangorn:~/tmp 2300> ./a.out

CLOCKS_PER_SEC: 1000000 resolution: 0.010000 sec



As you can see, CLOCKS_PER_SEC is merely a conversion factor, it provides

no indication WRT the resolution of the clock() function. The resolution

can only be determined at run time, as shown above.

milliseconds of a program in c, time.h

http://www.math.uic.edu/~jan/MCS275/lec41.html



Below is the listing of the main program (stored in mytimer.c ),
that contains all routines in one file :


/* L-41 MCS 275 Fri 20 Apr 2001 : timing C programs */

/* The program below compares the time for multiplying two floats
(single precision) with multiplying two doubles (double precision).
Because one multplication happens extremely fast (unnoticable),
we execute many multiplications. We can test the correctness
of the timing by executing the program on the prompt like
prompt> time ./a.out
The UNIX command time displays after a.out terminates the
user time (among other things). */

#include <stdio.h>
#include <time.h>
#define N 100000000

typedef struct timedata timer;
struct timedata
{
clock_t user_time;
time_t real_time;
};

void tstart ( timer *tmr );
/* initializes the timer */

void tstop ( timer *tmr );
/* stops the timer */

long usersec ( timer tmr );
/* returns elapsed seconds user cpu time */

long realsec ( timer tmr );
/* returns elapsed seconds real time */

long usermsc ( timer tmr );
/* returns elapsed milliseconds user cpu time */

long realmsc ( timer tmr );
/* returns elapsed milliseconds real time */

void tprint ( timer tmr );
/* formatted print of user cpu time, seconds and milliseconds */

int main(void)
{
long i;
timer tmr,total;
float a, b = 3.3333, c = 5.5555;
double x, y = 3.3333, z = 5.5555;

tstart(&total);
printf("\nTiming single precision float multiplication : \n");
tstart(&tmr);
for (i = 0; i < a =" b*c;" i =" 0;" x =" y*z;">user_time = clock();
tmr->real_time = time(NULL);
}

void tstop ( timer *tmr )
{
tmr->user_time = clock() - tmr->user_time;
tmr->real_time = time(NULL) - tmr->real_time;
}

long usersec ( timer tmr )
{
return tmr.user_time/CLOCKS_PER_SEC;
}

long realsec ( timer tmr )
{
return tmr.real_time;
}

long usermsc ( timer tmr )
{
return (tmr.user_time*1000)/CLOCKS_PER_SEC;
}

long realmsc ( timer tmr )
{
return (tmr.real_time*1000);
}

void tprint ( timer tmr )
{
long usec = usersec(tmr);
long umsc = usermsc(tmr);
long rsec = realsec(tmr);
long rmsc = realmsc(tmr);

printf("%s%2d%s%3d%s\n",
"Elapsed CPU user time : ", usec, " second(s) and ",
umsc - 1000*usec , " millisecond(s).");

printf("%s%2d%s%3d%s\n",
"Elapsed real time : ", rsec, " second(s) and ",
rmsc - 1000*rsec , " millisecond(s).");
}


If we put all the timing utilities in the library libtimer ,
then our main program usetimer.c looks like


/* L-41 MCS 275 Fri 20 Apr 2001 : timing C programs with library */

#include <stdio.h>
#include "libtimer.h"
#define N 100000000

int main(void)
{
long i;
timer tmr,total;
float a, b = 3.3333, c = 5.5555;
double x, y = 3.3333, z = 5.5555;

tstart(&total);
printf("\nTiming single precision float multiplication : \n");
tstart(&tmr);
for (i = 0; i < a =" b*c;" i =" 0;" x =" y*z;">

Then libtimer.h has to contain

/* L-41 MCS 275 Fri 20 Apr 2001 : creating a library for timing */

#include <time.h>

typedef struct timedata timer;
struct timedata
{
clock_t user_time;
time_t real_time;
};

void tstart ( timer *tmr );
/* initializes the timer */

void tstop ( timer *tmr );
/* stops the timer */

long usersec ( timer tmr );
/* returns elapsed seconds user cpu time */

long realsec ( timer tmr );
/* returns elapsed seconds real time */

long usermsc ( timer tmr );
/* returns elapsed milliseconds user cpu time */

long realmsc ( timer tmr );
/* returns elapsed milliseconds real time */

void tprint ( timer tmr );
/* formatted print of user cpu time, seconds and milliseconds */


and the file libtimer.c has as content


/* L-41 MCS 275 Fri 20 Apr 2001 : timing library */

/* If this file has the name libtimer.c, we can create the library
by typing after the prompt :
prompt> gcc -c libtimer.c
prompt> ar ruv libtimer.a libtimer
or with we place in makefile the following :

libtimer: libtimer.c
gcc -c libtimer.c
ar ruv libtimer.a libtimer.o
*/

#include <stdio.h>
#include <time.h>
#include "libtimer.h"

void tstart ( timer *tmr )
{
tmr->user_time = clock();
tmr->real_time = time(NULL);
}

void tstop ( timer *tmr )
{
tmr->user_time = clock() - tmr->user_time;
tmr->real_time = time(NULL) - tmr->real_time;
}

long usersec ( timer tmr )
{
return tmr.user_time/CLOCKS_PER_SEC;
}

long realsec ( timer tmr )
{
return tmr.real_time;
}

long usermsc ( timer tmr )
{
return (tmr.user_time*1000)/CLOCKS_PER_SEC;
}

long realmsc ( timer tmr )
{
return (tmr.real_time*1000);
}

void tprint ( timer tmr )
{
long usec = usersec(tmr);
long umsc = usermsc(tmr);
long rsec = realsec(tmr);
long rmsc = realmsc(tmr);

printf("%s%2d%s%3d%s\n",
"Elapsed CPU user time : ", usec, " second(s) and ",
umsc - 1000*usec , " millisecond(s).");

printf("%s%2d%s%3d%s\n",
"Elapsed real time : ", rsec, " second(s) and ",
rmsc - 1000*rsec , " millisecond(s).");
}


To create the library and the executables, use the makefile below :


libtimer.a: libtimer.h libtimer.c
gcc -c libtimer.c
ar ruv libtimer.a libtimer.o

mytimer: mytimer.c
gcc -o mytimer mytimer.c

usetimer: usetimer.c libtimer.a
gcc -o usetimer usetimer.c libtimer.a

clean:
/bin/rm -f -r *o libtimer.a mytimer usetimer

Friday, June 09, 2006

program for insertion sort

from http://www.cs.ucsc.edu/~pohl/CBDCourse/hw/kklenk_5.c
/**********************************************************/
/* */
/* Author: Kevin Klenk */
/* Date: May 25, 1998 */
/* Class: CMPS012A - Spring 1998 */
/* Professor: Ira Pohl */
/* Description: This program sorts randomly-filled */
/* arrays of four sizes (10, 100, 1000 and */
/* 10000) using insertion sort and then */
/* does several binary lookups on those */
/* arrays. Originally, the program was */
/* also supposed to print timing */
/* information, but the clock() function */
/* in time.h proved to be too unreliable. */
/* */
/**********************************************************/

/* Required libraries */
#include <stdio.h>
#include <time.h>
#include <assert.h>

/* Constants */
#define CLOCKS_PER_SEC 1000000
#define MAXSIZE 10000
#define RANGE 100
#define NUMLOOKUPSTOPRINT 20
#define NUMLOOKUPS 100

/* Function Prototypes */
void swap(int *a, int *b);
int GetRandNum();
void FillArray(int array[], int size);
void PrintArray(int array[], int size);
void Swap(int *a, int *b);
void InsertionSort(int data[], int size);
int BinaryLookup(int data[], int size, int key, int* position);

/* Returns a random integer from 0 to RANGE - 1. */
int GetRandNum()
{
return (rand() % RANGE);
}

/* Fills an array of size integers with random values. */
void FillArray(int array[], int size)
{
int i;

srand(time(NULL));
for(i = 0; i < size; i++)
array[i] = GetRandNum();
}

/* Prints an array of size integers to stdout. */
void PrintArray(int array[], int size)
{
int i;

printf("\t");
for (i = 0; i < size; i++)
printf("%5d", array[i]);
printf("\n");
}

/* Swaps two integers. */
void Swap(int *a, int *b)
{
int tmp = *a;

*a = *b;
*b = tmp;
}

/* Sorts an array of size integers in increasing order using */
/* the insertion sort algorithm. */
void InsertionSort(int data[], int size)
{
int i, k;

for(i = 0; i < size - 1; ++i)
{
for(k = i + 1; k > 0; --k)
{
if(data[k] < data[k-1])
Swap(&data[k], &data[k-1]);
}
}
}

/* Looks for an integer, "key", in an array of size integers */
/* and sets the integer pointed to by position to the index */
/* which was last checked by the function. Returns 0 if */
/* "key" is not found; returns 1, otherwise. "position" */
/* points to an integer storing the index at which "key" is */
/* found if it IS in fact found. Otherwise, the integer */
/* pointed to by "position" will contain an index close */
/* to the index where "key" would have been found if it */
/* were in the array. */
int BinaryLookup(int data[], int size, int key, int* position)
{
int leftIndex = 0;
int rightIndex = size - 1;

*position = (leftIndex + rightIndex)/2;

while ((leftIndex <= rightIndex) && (key != data[*position]))
{
if (key < data[*position])
{
leftIndex = leftIndex;
rightIndex = *position - 1;
*position = (leftIndex + rightIndex)/2;
}
else if (key > data[*position])
{
leftIndex = *position + 1;
rightIndex = rightIndex;
*position = (leftIndex + rightIndex)/2;
}
else
{
/* This will never occur because it is prevented by the loop */
/* condition. */
}
}

if (leftIndex > rightIndex)
return 0;
else
return 1;
}

/* Main */
int main(void)
{
int currSize, i, r, found, position, numLookups;
int data[MAXSIZE];
long before, after;

for (currSize = 10; currSize <= MAXSIZE; currSize *= 10)
{
printf("\nRUNNING WITH %d ELEMENTS \n\n", currSize);

FillArray(data, currSize);

if (currSize == 10)
{
printf(" Before sorting array is:\n");
PrintArray(data, currSize);
}

/* before = clock(); */
printf(" Sorting the array:\n");
InsertionSort(data, currSize);
/*
after = clock();
printf("\n Sorting takes %lf seconds. \n\n",
((double)(after - before))/CLOCKS_PER_SEC);
*/
if (currSize == 10)
{
printf(" After sorting array is:\n");
PrintArray(data, currSize);
}

/* before = clock(); */

if (currSize == 10)
numLookups = NUMLOOKUPSTOPRINT;
else
numLookups = NUMLOOKUPS;
printf(" Doing %d binary lookups:\n", numLookups);

for (i = 0; i < numLookups; i++)
{
r = GetRandNum();
found = BinaryLookup(data, currSize, r, &position);
if (currSize == 10) {
if (found)
printf(" element = %2d found at index = %d\n", r, position);
else
printf(" element = %2d not found; would be near index = %d\n",
r, position);
}
}
/*
after = clock();
printf("\n Doing %d lookups takes %lf seconds. \n\n", NUMLOOKUPS,
((double)(after - before))/CLOCKS_PER_SEC);
*/
}

return 0;
}

Saturday, June 03, 2006

Cron - Program Scheduler

Cron - Program Scheduler: "Some examples of complete cron table entries are show below, implementing the vnukelog command as an example.
# Any output generated by the cron entries below is sent to the e-mail
# address assigned to the MAILTO environment variable.
MAILTO='webmaster@mycompany.com'

# Execute the 'vnukelog' command at 1:15 (15 1) AM every day.
15 1 * * * /usr/local/bin/vnukelog

# Execute the 'vnukelog' command at 11:40 PM (40 23) on the first day (1)
# of each month.
40 23 1 * */usr/local/bin/vnukelog

# Execute the 'vnukelog' command every 10 minutes for for the first
# half-hour (0-30/10) of the 9:00 AM and 5:00 PM hours (9,17) on
# Monday-Friday (1-5).
0-30/10 9,17 * * 1-5/usr/local/bin/vnukelog

# Execute the 'vnukelog' command at 4:00 AM, 8:00 AM, 12:00 noon, 4:00 PM,
# and 8:00 PM (0 */4) on each Sunday (sun) every January (jan).
0 */4 * jan sun/usr/local/bin/vnukelog

# Execute the 'vnukelog' command at 4:30 AM (30 4) on the first, fifteenth
# (1,15), and each Friday (fri) of every month.
30 4 1,15 * fri/usr/local/bin/vnukelog

# Execute the 'vnukelog' command at 12:00 midnight (0 0) on August 19 (8)
# (aug).
0 0 19 8 */usr/local/bin/vnukelog
0 0 19 aug */usr/local/bin/vnukelog"

Friday, June 02, 2006

How i can refine my search? search history !! and success historyGoogle Search



"one user"+masquerade iptables - Google Search:


Guarddogiptables 1.2.6a - Is anyone using Guarddog with this version of
iptables? ... This means no Guarddog and NAT/IP masquerade on older kernel
2.2 systems. ...




www.simonzone.com/software/guarddog/
- 75k - Cached - Similar pages

iptables and firewall - LinuxQuestions.orgiptables and firewall Linux -
Security. ... how to set firewall to allow only one user (or several users)
from behind the masquerade use p2p programs ...www.linuxquestions.org/
questions/showthread.php?t=369895 - 57k - Cached - Similar pages"



search key words::


http://www.google.com/search?hl=en&lr=&q=%22one+user%22%2Bmasquerade+iptables



"one user"+masquerade iptables

found out what I needed


iptables and firewall - LinuxQuestions.org


iptables and firewall Linux - Security. ... how to set firewall to allow
only one

user (or several users) from behind the masquerade use p2p programs ...




http://www.google.com/search?hl=en&lr=&q=%22one+user%22%2Bmasquerade


"one user"+masquerade

top 5 results for google: did not satisfy my query

[PPT]

One Class Training for Masquerade Detection


[PDF]

One-Class Training for Masquerade Detection



Statistical Methods for Computer Intrusion Detection



IAWorkshop 2006 - Profiling Users in GUI Based Systems for ...






What I searched before:


http://www.google.com/search?hl=en&lr=&q=%22masquerade+one+user%22&btnG=Search



"masquerade one user"

Your search - "masquerade one user" - did not match any documents.




http://groups.google.com/groups/search?hl=en&q=%22masquerade+one+user%22



"masquerade one user"

Searched all groups

Your search - "masquerade one user" - did not match any documents




http://groups.google.com/group/comp.os.linux.networking/browse_thread/thread/3f153ac992873c71/450e5cfefed89aad?lnk=st&q=iptables+tutorial&rnum=1&hl=en#450e5cfefed89aad



keywords: iptables tutorial

got : Hello,

I was just taking a look over the iptables tutorial again (http://www.netfilter.org/documentation/tutorials/blueflux/iptables-
tutorial.html) and i noticed ....




http://groups.google.com/groups?hl=en&lr=&rls=GGLG,GGLG:2006-18,GGLG:en&q=iptables%20tutorial&sa=N&tab=wg



keywords:iptables tutorial




iptables tutorial - nat
Hello, I was just taking a look over the
iptables tutorial again
(http://www.netfilter.org/documentation/tutorials/blueflux/iptables-
tutorial.html) and i ...

comp.os.linux.networking






keywords by order:


http://www.google.com/search?sourceid=navclient&ie=UTF-8&rls=GGLG,GGLG:2006-18,GGLG:en&q=iptables+help



keywords: iptables help


http://www.gege.org/iptables/doc/faq.html#id2716868



http://www.google.com/search?hl=en&lr=&rls=GGLG%2CGGLG%3A2006-18%2CGGLG%3Aen&q=iptables+tutorial

keywords: iptables tutorial


http://groups.google.com/groups?hl=en&lr=&rls=GGLG,GGLG:2006-18,GGLG:en&q=iptables%20tutorial&sa=N&tab=wg

Groups.google.com , keywords: iptables tutorial


http://groups.google.com/group/comp.os.linux.networking/browse_thread/thread/3f153ac992873c71/450e5cfefed89aad?lnk=st&q=iptables+tutorial&rnum=1&hl=en#450e5cfefed89aad

keywords: iptables tutorial


http://groups.google.com/groups/search?hl=en&q=%22masquerade+one+user%22



keywords: "masquerade one user"

Your search - "masquerade one user" - did not match any documents.

"masquerade one user"


http://www.google.com/search?hl=en&lr=&q=%22masquerade+one+user%22&btnG=Search




http://www.google.com/search?hl=en&lr=&q=%22one+user%22%2Bone%2Buser



http://www.google.com/search?hl=en&lr=&q=%22one+user%22%2Bmasquerade



http://www.google.com/search?hl=en&lr=&q=%22one+user%22%2Bmasquerade+iptables

Linux 2.4 Packet Filtering HOWTO: Using iptables

Linux 2.4 Packet Filtering HOWTO: Using iptables: "Creating a New Chain
Let's create a new chain. Because I am such an imaginative fellow, I'll call it test. We use the `-N' or `--new-chain' options:
# iptables -N test
#

It's that simple. Now you can put rules in it as detailed above.
Deleting a Chain
Deleting a chain is simple as well, using the `-X' or `--delete-chain' options. Why `-X'? Well, all the good letters were taken.
# iptables -X test
#

There are a couple of restrictions to deleting chains: they must be empty (see Flushing a Chain below) and they must not be the target of any rule. You can't delete any of the three built-in chains.
If you don't specify a chain, then all user-defined chains will be deleted, if possible.
Flushing a Chain
There is a simple way of emptying all rules out of a chain, using the `-F' (or `--flush') commands.
# iptables -F FORWARD
#"

Resetting (Zeroing) Counters
It is useful to be able to reset the counters. This can be done with the `-Z' (or `--zero') option.
Consider the following:
# iptables -L FORWARD
# iptables -Z FORWARD
#
Setting Policy
The policy can be either ACCEPT or DROP, for example:
# iptables -P FORWARD DROP
#



Linux 2.4 Packet Filtering HOWTO


So What's A Packet Filter?
3.1 Why Would I Want to Packet Filter?
3.2 How Do I Packet Filter Under Linux?

4. Who the hell are you, and why are you playing with my kernel?

5. Rusty's Really Quick Guide To Packet Filtering

6. How Packets Traverse The Filters



Netfilter FAQ (Frequently Asked Questions)

Linux 2.4 Packet Filtering HOWTO: Mixing NAT and Packet Filtering

Linux 2.4 Packet Filtering HOWTO: Mixing NAT and Packet Filtering: "9. Mixing NAT and Packet Filtering
It's common to want to do Network Address Translation (see the NAT HOWTO) and packet filtering. The good news is that they mix extremely well.
You design your packet filtering completely ignoring any NAT you are doing. The sources and destinations seen by the packet filter will be the `real' sources and destinations. For example, if you are doing DNAT to send any connections to 1.2.3.4 port 80 through to 10.1.1.1 port 8080, the packet filter would see packets going to 10.1.1.1 port 8080 (the real destination), not 1.2.3.4 port 80. Similarly, you can ignore masquerading: packets will seem to come from their real internal IP addresses (say 10.1.1.1), and replies will seem to go back there.
You can use the `state' match extension without making the packet filter do any extra work, since NAT requires connection tracking anyway. To enhance the simple masquerading example in the NAT HOWTO to disallow any new connections from coming in the ppp0 interface, you would do this:
# Masquerade out ppp0
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

# Disallow NEW and INVALID incoming or forwarded packets from ppp0.
iptables -A INPUT -i ppp0 -m state --state NEW,INVALID -j DROP
iptables -A FORWARD -i ppp0 -m state --state NEW,INVALID -j DROP

# Turn on IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward"