22 September 2009

RJ45

10base-T straight cable
Pin No.strand colorName
1white and orangeTX+
2orangeTX-
3white and greenRX+
4NC*
5NC*
6greenRX-
7NC*
8NC*

10base-T cross cable
One end
RJ45 Male
Other end
RJ45 Male
1 (TX+)3 (RX+)
2 (TX-)6 (RX-)
3 (RX+)1 (TX+)
**
**
6 (RX-)2 (TX-)
**
**


100base-T straight cable
Pin No.conductor colorName
1white and orangeTX_D1+
2orangeTX_D1-
3white and greenRX_D2+
4blueBI_D3+
5white and blueBI_D3-
6greenRX_D2-
7white and brownBI_D4+
8brownBI_D4-

100base-T cross cable
One end
RJ45 Male
Other end
RJ45 Male
1 (TX_D1+)3 (RX_D2+)
2 (TX_D1-)6 (RX_D2-)
3 (RX_D2+)1 (TX_D1+)
4 (BI_D3+)7 (BI_D4+)
5 (BI_D3-)8 (BI_D4-)
6 (RX_D2-)2 (TX_D1-)
7 (BI_D4+)4 (BI_D3+)
8 (BI_D4-)5 (BI_D3-)



Reference:
http://konfigurasi-lan.blogspot.com/2008/07/lan.html
http://konfigurasi-lan.blogspot.com/2008_07_01_archive.html

18 September 2009

miscellaneous in C


Type int represents a machine's natural word size.

#define abs(x) ((x < 0)? -x : x)

#define offsetof(type, member) ((size_t)&((type *)0)->member)

#define roundup(x, y) (x + (x % y))

#define _THIS_IP_ ({ __here: (unsigned long)&&__here; })

#define swap(a, b) ({typeof(a) __tmp = a; a = b; b = __tmp;})

#define container_of(ptr, type, member) ({ \
(type *)((char *)ptr - offsetof(type, member)); })

#define dprintf(fmt, arg...) printf("%s:%s:%d " fmt "\n", \
__FILE__, __FUNCTION__, __LINE__, ##arg)

typedef unsigned char u8;
typedef unsigned long u32;

#define set(val, addr) (*(volatile u32 *)addr = val)

void wrap_printf(const char *fmt, ...) {
va_list args;

va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}

Reference:
http://www.linuxjournal.com/article/7269
http://c-faq.com
http://kernelnewbies.org/FAQ/DoWhile0
http://publications.gbdirect.co.uk/c_book/chapter9/stdarg.html

17 September 2009

doubly linked list in C

list *reverse(list *current)
{
list *prev = NULL;
list *next = NULL;

while(current)
{
next = current->next; //tricky: save the next node
current->next = prev; //point current to previous
current->prev = next; //point current to next
prev = current;
current = next;
}

return prev;
}

---------------------dlist.c---------------------
#include <stdio.h>
#include <stdlib.h>

typedef struct list{
int val;
struct list *next;
struct list *prev;
}list;

list *getfirst(list *head)
{
while(head && head->prev) head = head->prev;
return head;
}

list *getlast(list *head)
{
while(head && head->next) head = head->next;
return head;
}

void print(list *head1)
{
list *head = NULL;
list *end = NULL;

head = head1;
printf("to next: ");
while(head)
{
printf("%d->", head->val);
head = head->next;
}
printf("NULL\n");

head = getlast(head1);
printf("to prev: ");
while(head)
{
printf("%d->", head->val);
head = head->prev;
}
printf("NULL\n");
}


list *insert(list *head, int val)
{
list *new = (list*)malloc(sizeof(list));
new->val = val;
new->prev = NULL;
new->next = head;

if(head) head->prev = new;

return new;
}

list *reverse(list *current)
{
list *prev = NULL;
list *next = NULL;

while(current)
{
next = current->next; //tricky: save the next node
current->next = prev; //point current to previous
current->prev = next; //point current to next
prev = current;
current = next;
}

return prev;
}

int main()
{
list *head = NULL;

head = insert(head, 10);
head = insert(head, 20);
head = insert(head, 60);
head = insert(head, 51);
head = insert(head, 73);
head = insert(head, 19);
head = insert(head, 10);
head = insert(head, 81);
head = insert(head, 95);
printf("before reverse:\n");
print(head);

printf("after reverse:\n");
head = reverse(head);
print(head);

return 0;
}
-------------------------------------------------

Output:
before reverse:
to next: 95->81->10->19->73->51->60->20->10->NULL
to prev: 10->20->60->51->73->19->10->81->95->NULL
after reverse:
to next: 10->20->60->51->73->19->10->81->95->NULL
to prev: 95->81->10->19->73->51->60->20->10->NULL

reverse a string in C

void reverse(char *buf)
{
char *start = buf;
char *end = buf + strlen(buf) - 1;

while(start < end)
swap(start++, end--);

}
#include <stdio.h>
#include <string.h>

void swap(char *s1, char *s2)
{
char t = *s1;
*s1 = *s2;
*s2 = t;
}

void swap2(char *s1, char *s2)
{
*s1 ^= *s2;
*s2 ^= *s1;
*s1 ^= *s2;
}

void reverse(char *buf)
{
char *start = buf;
char *end = buf + strlen(buf) - 1;

while(start < end)
swap(start++, end--);
}

int main()
{
char buf[] = "program to reverse a string";

printf("before: %s\n", buf);
reverse(buf);
printf("after : %s\n", buf);

return 0;
}

output:
before: program to reverse a string
after : gnirts a esrever ot margorp

15 September 2009

single linked list in C

list *reverse(list *current)
{
list *prev = NULL;
list *next = NULL;

while(current)
{
next = current->next; //tricky: save the next node
current->next = prev; //point current to previous
prev = current;
current = next;
}

return prev;
}

--------------------llist.c--------------------
#include <stdio.h>
#include <stdlib.h>

typedef struct list{
int val;
struct list *next;
}list;


list *insert(list *head, int val)
{
list *new = (list*)malloc(sizeof(list));
new->val = val;
new->next = head;

return new;
}

void print(list *head)
{
while(head)
{
printf("%d->", head->val);
head = head->next;
}

printf("NULL\n");
}

list *reverse(list *current)
{
list *prev = NULL;
list *next = NULL;

while(current)
{
next = current->next; //tricky: save the next node
current->next = prev; //point current to previous
prev = current;
current = next;
}

return prev;
}

int main()
{
list *head = NULL;

head = insert(head, 10);
head = insert(head, 12);
head = insert(head, 20);
head = insert(head, 17);
head = insert(head, 25);
head = insert(head, 3);
head = insert(head, 75);
head = insert(head, 49);
head = insert(head, 32);
printf("before reverse: ");
print(head);

head = reverse(head);
printf("after reverse: ");
print(head);

return 0;
}
-----------------------------------------------

Output:
before reverse: 32->49->75->3->25->17->20->12->10->NULL
after reverse: 10->12->20->17->25->3->75->49->32->NULL

binary tree in C

int count(node *head)
{
if(head == NULL) return 0;

return count(head->left) + 1 + count(head->right);
}

int depth(node *head)
{
if(head == NULL)
return 0;
else
{
int ldepth = depth(head->left);
int rdepth = depth(head->right);

if(ldepth < rdepth) return rdepth+1;
else return ldepth+1;
}
}

int compare(node *n1, node *n2)
{
if(n1==NULL && n2==NULL)
return true;
else if(n1!=NULL && n2!=NULL)
{
return ((n1->val==n2->val) &&
compare(n1->left, n2->left) &&
compare(n1->right, n2->right));
}
else return false;
}

-----------------btree.c-----------------
#include <stdio.h>
#include <stdlib.h>

typedef struct node{
int val;
struct node *left;
struct node *right;
}node;


void print(node *head)
{
if(head == NULL) return;

print(head->left);
printf("%d->",head->val);
print(head->right);
}

node * insert(node *head, int val)
{
if(head == NULL)
{
head = (node*)malloc(sizeof(node));
head->left=head->right=NULL;
head->val = val;
}
else if(val < head->val)
head->left = insert(head->left, val);
else if(val > head->val)
head->right = insert(head->right, val);
else
printf("duplicate vale (%d)\n", val);

return head;
}

int count(node *head)
{
if(head == NULL) return 0;

return count(head->left) + 1 + count(head->right);
}

int count2(node *head)
{
int count = 0;

if(head == NULL) return 0;
count += count2(head->left);
count += count2(head->right);

return ++count;
}

void count3(node *head, int *count)
{
if(head == NULL) return;

count3(head->left, count);
count3(head->right, count);
(*count)++;
}

int depth(node *head)
{
if(head == NULL)
return 0;
else
{
int ldepth = depth(head->left);
int rdepth = depth(head->right);

if(ldepth < rdepth) return rdepth+1;
else return ldepth+1;
}
}

#define true 1
#define false 0
int compare(node *n1, node *n2)
{
if(n1==NULL && n2==NULL)
return true;
else if(n1!=NULL && n2!=NULL)
{
return ((n1->val==n2->val) &&
compare(n1->left, n2->left) &&
compare(n1->right, n2->right));
}
else return false;
}

int main()
{
node *head1 = NULL;
node *head2 = NULL;
int c1 = 0;

head1 = insert(head1, 50);
insert(head1, 40);
insert(head1, 70);
insert(head1, 30);
insert(head1, 25);
insert(head1, 90);
insert(head1, 45);
insert(head1, 10);
insert(head1, 55);
insert(head1, 85);
print(head1); printf("NULL\n");

head2 = insert(head2, 50);
insert(head2, 40);
insert(head2, 70);
insert(head2, 30);
insert(head2, 25);
insert(head2, 90);
insert(head2, 45);
insert(head2, 10);
insert(head2, 55);
insert(head2, 85);
print(head2); printf("NULL\n");

printf("number of nodes in the tree is %d\n", count(head1));
printf("Max depth of the tree is %d\n", depth(head1));
printf("comparison for both the tree is \"%s\"\n",
compare(head1, head2)?"true":"false");

return 0;
}
-----------------------------------------

Reference:
http://cslibrary.stanford.edu/110/BinaryTrees.html

parity in C

int oparity(int val)
{
int parity = 0;

while(val)
{
parity = !parity;
val &= val-1;
//val = val&(val-1);
//val &= ~(val&-val);
//val ^= val&(~val+1);
}

return parity;
}

int oparity(int val)
{
int nbit = 0;

while(val)
{
if(val&1) nbit++;
val >>= 1;
}

return nbit%2;
}

-----------------oddparity.c-----------------
#include <stdio.h>

int oparity(int val)
{
int nbit = 0;

while(val)
{
if(val&1) nbit++;
val >>= 1;
}

return nbit%2;
}

int main()
{
int val = 0xaF19;

printf("oddparity of 0x%X is %d\n", val, oparity(val));
printf("evenparity of 0x%X is %d\n", val, oparity(val)?0:1);

return 0;
}
---------------------------------------------

Reference:
http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html

pyramid in C

-----------------------pyramid1.c-----------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char*argv[])
{
int i, j, level;
char ch = '*';

if(argc < 2)
{
printf("usage: %s <level> <<character>>\n", argv[0]);
exit(0);
}
level = atoi(argv[1]);
if(argv[2]) ch = argv[2][0];

for(i=0; i<level; i++)
{
for(j=i; j<level-1; j++)
printf(" ");

for(j=0; j<2*i+1; j++)
printf("%c ", ch);

printf("\n");
}


return 0;
}
--------------------------------------------------------

-----------------------pyramid2.c-----------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char*argv[])
{
int i, j, level;
char ch = '*';

if(argc < 2)
{
printf("usage: %s <level> <<character>>\n", argv[0]);
exit(0);
}
level = atoi(argv[1]);
if(argv[2]) ch = argv[2][0];

for(i=0; i<level; i++)
{
for(j=0; j<2*level-1; j++)
printf("%c ", (abs(level-1-j)<=i)?ch:' ');

printf("\n");
}


return 0;
}
--------------------------------------------------------

factorial in C

int fact(int num)
{
if(num<=0) return 1;

return num * fact(num-1);
}


---------------------factorial.c---------------------
#include <stdio.h>
#include <stdlib.h>

int fact(int num)
{
if(num<=0) return 1;

return num * fact(num-1);
}

int main(int argc, char *argv[])
{
int num = 0;

if(argc < 2)
{
printf("usage: %s <number>\n", argv[0]);
exit(0);
}

num = atoi(argv[1]);

printf("factorial of %d is %d\n", num, fact(num));
return 0;
}
-----------------------------------------------------

yum

yum install createrepo
mkdir -p /mnt/iso
mount /dev/cdrom /mnt/iso
cd /mnt
createrepo .
cd /etc/yum.repos.d/
edit all repo files and make enabled=0
-------------------iso.repo-------------------
[localrepo]
name=My Repo
baseurl=file:///mnt/iso/
enabled=1
gpgcheck=0
----------------------------------------------

yum repolist
yum --disablerepo=updates install gcc*
yum --disablerepo=updates install xorg*

Traceback (most recent call last):
File "/usr/bin/yum", line 29, in
yummain.main(sys.argv[1:])
File "/usr/share/yum-cli/yummain.py", line 172, in main
base.doTransaction()
File "/usr/share/yum-cli/cli.py", line 302, in doTransaction
problems = self.downloadPkgs(downloadpkgs)
File "/usr/lib/python2.5/site-packages/yum/__init__.py", line 798, in downloadPkgs
remote_pkgs.sort(mediasort)
File "/usr/lib/python2.5/site-packages/yum/__init__.py", line 747, in mediasort
a = a.getDiscNum()
File "/usr/lib/python2.5/site-packages/yum/packages.py", line 485, in getDiscNum
return int(fragid)

ValueError: invalid literal for int() with base 10: ''

-----------------------------------------------
vi /usr/lib/python2.5/site-packages/yum/packages.py +485
return int(fragid) ==> return int()
-----------------------------------------------

Happy yumming...

Reference:
http://fedora-help.blogspot.com/2009/07/install-from-dvd-using-yum-installer.html

06 September 2009

Two New Technologies to check out!!!

1. One technology involves enabling users to gain instant access to a laptop's e-mail, browser and other basic functionality -- without booting Windows at all.
2. The second technology enables an Internet-based message to wake a Windows PC from sleep mode.

Dell Latitude On
Dell announced this week a new feature called Latitude On that enables the use of e-mail, Web surfing, basic personal information manager functionality and document reading -- all without booting Windows. The Latitude On feature uses a low-power Intel ARM processor, flash storage and Linux (SUSE Linux Enterprise Desktop 10) separate from the laptop's main CPU, hard drive and Windows operating system. If you use only Latitude On, battery life lasts not hours but days, according to Dell.

Intel Remote Wake
Intel introduced new technology yesterday called Remote Wake, a chip set and software development kit that enables a PC to be "awakened" over the Internet when in sleep mode. This will enable you to dump your land-line phone and use a PC-based VoIP phone without leaving your PC on all the time.

Reference:
http://www.computerworld.com/s/article/9112732/Elgan_While_Windows_sleeps

03 September 2009

Cross Site scripting

Setup the WampServer as explained here
------------------test.php------------------
<?php echo "Welcome to our site " . stripslashes($name); ?>
--------------------------------------------
-> http://localhost/test.php?name=John
-> http://localhost/test.php?name=<script language=javascript>alert('Hey, you are going to be hijacked!');</script>"
-> http://localhost/test.php?name=<script language=javascript>setInterval("window.open('http://www.google.com','innerName')",1000);</script>
--------------------------------------------

------------------test1.php------------------
<?php
function validateQueryString ( $queryString , $min=1, $max=32 ) {
if ( !preg_match( "/^[a-zA-Z0-9]{".$min.",".$max."}$/", $queryString ) ) {
return false;
}
return true;
}

if ( !validateQueryString ( $name ) ) {
echo "The cross site scripting is not allowed.";
}
else {
echo "Welcome to our site " . stripslashes($name);
}
?>
--------------------------------------------
-> http://localhost/test2.php?name=John
-> http://localhost/test1.php?name=<script language=javascript>alert('Hey, you are going to be hijacked!');</script>
--------------------------------------------

Reference:
Acunetix Web Vulnerability Scanner
Paros - for web application security assessment
Rational AppScan Standard Edition V7.8
A Quick Look at Cross Site Scripting