Monday, October 29, 2007

UNIX Networking : Sockets UDP Transmitter and Reciever

Intro

Networking in UNIX or Linux is done with sockets. The following are 2 programs I made up.
1. Sends messages in UDP.
2. Listens and Recieves messages from UDP.

User Datagram Protocol or in short UDP is like a letter or telegram, its arrival is not anticipated and there is no connect and disconnect procedures to send and recieve UDP messages unlike TCP/IP. So the program is a little more easier and simpler to understand.


The Transmitter
Reads messages from input and sends it to localhost:34000.
To Setup a transmitter following must be done:
1. Setup a port address and open a socket and bind to it.
2. Send message to specified port and IP addresses.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>

/*Some are unused, But I read these are
general headers used in a network program
:)*/

int main(void)
{
struct sockaddr_in sin;
int s;

s = socket(AF_INET, SOCK_DGRAM, 0);
if(!s)
{
perror("socket()");
return 1;
}
sin.sin_family = AF_INET;
sin.sin_port = htons(9999);
sin.sin_addr.s_addr = INADDR_ANY;//Just any would do

if(bind(s, (struct sockaddr *)&sin, sizeof(sin)))
{
perror("bind()");
return 1;
}

struct sockaddr_in sout;

sout.sin_family = AF_INET;
sout.sin_port = htons(34000);//Destination PORT!
sout.sin_addr.s_addr = inet_addr("127.0.0.1");//Destination IP Address, here localhost.

char msg[1000];//Buffer

do
{
printf("Message: ");
scanf("%s",msg);
sendto(s, msg, strlen(msg)+1, 0, (struct sockaddr *)&sout, sizeof(sout));
//Sends the message
}while(*msg!='0'); /*Just an exit condition*/

close(s);//Close Socket

return 0;
}


The Reciever
This program listens to localhost:34000 and prints the messages whenever it recieves one.
To Setup a reciever following must be done:
1. Setup a port address and open a socket and bind to it.
2. Start Recieving...

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>

int main(void)
{

struct sockaddr_in sin;
char msg[10000];
int ret;
int sin_length;


int s;

s = socket(AF_INET, SOCK_DGRAM, 0);
if(!s)
{
perror("socket()");
return;
}
sin.sin_family = AF_INET;
sin.sin_port = htons(34000);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
if(bind(s, (struct sockaddr *)&sin, sizeof(sin)))
{
perror("bind()");
return 1;
}


do
{
sin_length = sizeof(sin);
ret = recvfrom(s, msg, 10000, 0, (struct sockaddr *)&sin, &sin_length);
//Waits until a message is recieved...
printf("Message[%s:%d] : %s\n",
inet_ntoa(sin.sin_addr), sin.sin_port,msg);
}
while(msg[0]!='0');

close(s);
return 0;
}


Thats all
Tested and proved working on Fedora Core 6.
Do let me know your results and comments.....
To know more about the functions check the Linux Manual for each function.

Check out... (TCP/IP Transciever)
:)

Saturday, October 20, 2007

Disable Autorun Windows XP

Do this very important!!

Most viruses uses the autorun.inf to get itself infected on your computer. Autorun.inf is a small file that instructs the windows os to do when the CD is inserted into the computer. In genuine cases , it runs a setup in case of a Software Installation Disc. In the other case it may run a virus and gets your system infected. All you got to do to get the virus is put the CD. Same is the case for Mass Storage Devices like Memory Sticks, Pen Drives, Flash Drives etc.

So Disable your Autorun now


HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\
Services\Cdrom\Autorun Change from 1 to 0


To disable Autoplay of all drives
Start > Run > gpedit.msc

Enable : Computer Configuration > Administrative Templates > System > Turn Off Autoplay

Friday, October 12, 2007

XPM File format

Intro
XPM or XPixMap is a bitmap format in unix based systems. It is a relatively easy format to understand, XPM files have a format similar to a C source code of a string array.


/* XPM */
static char * var name[] = {
"","","" //etc....
};


The first line is XPM in standard C comments. Second is an array declaration with a valid token as variable name.
The array index 0 [0] : contains the following %d %d %d %d [%d %d] [%d]
which are :
Width,Height,Number of colors, Characters Per Pixel, optional X & Y Hotspots, optional XPM Extension.
The array indexes 1-> number of colors indicates the color section.
The strings in these section take the following format :
%s %s %s
The first contains the characters that represent the pixels. The number of characters used to represent a pixel is given by Characters Per Pixel field in array[0].
The second string represents control characters that defines what the following field is about.
switch(control character)
{
case 'm' : Mono color
case 's' : Symbolic name
case 'c' : Color value
case 'g4': 4 Level Gray Scale
case 'g' : Grayscale
}

the third string is then
a color name eg: black
a # followed by RGB Hex
a % followed by HSV Hex
a symbolic name
a "None" indicating a transparent.

The rest of the section contains the actual pixels. With each (Character Per Pixel) representing a pixel on the screen. Each string is width pixels long and there will be height number of strings.

There might be an optional XPM Extension following the pixels.

Examples:

/* XPM */
static char * plaid[] =
{
/* plaid pixmap */
/* width height ncolors chars_per_pixel */
"22 22 4 2 0 0 XPMEXT",
/* colors */
" c red m white s light_color",
"Y c green m black s ines_in_mix",
"+ c yellow m white s lines_in_dark ",
"x m black s dark_color ",
/* pixels */
"x x x x x x x x x x x x + x x x x x ",
" x x x x x x x x x x x x x x x x ",
"x x x x x x x x x x x x + x x x x x ",
" x x x x x x x x x x x x x x x x ",
"x x x x x x x x x x x x + x x x x x ",
"Y Y Y Y Y x Y Y Y Y Y + x + x + x + x + x + ",
"x x x x x x x x x x x x + x x x x x ",
" x x x x x x x x x x x x x x x x ",
"x x x x x x x x x x x x + x x x x x ",
" x x x x x x x x x x x x x x x x ",
"x x x x x x x x x x x x + x x x x x ",
" x x x x Y x x x ",
" x x x Y x x ",
" x x x x Y x x x ",
" x x x Y x x ",
" x x x x Y x x x ",
"x x x x x x x x x x x x x x x x x x x x x x ",
" x x x x Y x x x ",
" x x x Y x x ",
" x x x x Y x x x ",
" x x x Y x x ",
" x x x x Y x x x ",
"XPMEXT ext1 data1",
"XPMEXT ext2",
"data2_1",
"data2_2",
"XPMEXT ext3",
"data3",
"XPMEXT",
"data4",
"XPMENDEXT"
};

Another one :

/* XPM */
static char * example_xpm[] = {
"24 20 3 1",
" c None",
". c #0000FF",
"+ c #FF0000",
" ",
" .. ",
" .... ",
" ......++++++++ ",
" .........+++++++ ",
" ..........+++++++ ",
" ............++++++ ",
" .............++++++ ",
" ..............++++ ",
" +.............+++ ",
" ++.............++ ",
" +++.............+ ",
" +++++............. ",
" ++++++.............. ",
" ++++++++............ ",
" +++++++++........... ",
" +++++++++......... ",
" ++++++++++....... ",
" ++++++++++..... ",
" +++++++++ ... "};


Thats all
for a Hi Fi version
refer this.

Monday, October 1, 2007

PERL Referencing and Dereferencing

Intro

PERL - Practical Extraction and Report Language.

Referencing a variable means creating a pointer to the variable.
Referencing in C is done by the & operator, but in PERL referencing is done by the \(backslash) operator.

Check out the following examples to understand better:

my $scalar = "http://digitalpbk.blogspot.com";
my $scalar_ptr = \$scalar; //Scalar reference

my $array= (1,2,4,3,5);
my $array_ptr = \@array; //Array reference
//or

my $array_ptr = [1,2,3,4,5];//Array reference
//Note the [Square brackets]


my %hash = ("one",1,"two",2);
my $hash_ptr = \%hash; //Hash reference
//or

my $hash_ptr = {"one" => 1,"two"=>2};//Hash reference
//Note the {Curly brackets}


my $sub_ref = \&a_subroutine;//Subroutine reference
//or

my $sub_ref = sub { print "somethings"; };//Sub reference




Dereferencing is the opposite of referencing i.e, to get the value pointed by the referencing/pointing variable. Dereferencing in C is done by the * operator, whereas in PERL it is done by prepending, adding an appropriate @,$ or % to the reference variable.



my $scalar_ptr = \$scalar; //Scalar reference
my $scalar = $$scalar_ptr; //Scalar Var

my $array_ptr = \@array; //Array reference
//or

my $array_ptr = [1,2,3,4,5];//Array reference
my $array = @$array; // Array Var


my $hash_ptr = \%hash; //Hash reference
my %hash = %$hash //Hash Var

my $sub_ref = \&a_subroutine;//Subroutine reference
my $sub_ref = sub { print "somethings"; };//Sub reference
&$sub_ref //Call Subroutine



Thats all about referencing and dereferencing...