10 November 2009

wget

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

#include <fcntl.h>
#include <libgen.h>

char *gethostname(char *url)
{
if(strstr(url, "http://"))
{
url += strlen("http://");
}
if(strstr(url, "https://"))
{
url += strlen("https://");
}
if(strchr(url, '/'))
{
strchr(url, '/')[0] = '\0';
}

return url;
}

char *getfilename(char *file)
{
if(file[0] == '\0')
strcpy(file, "test.txt");
if(file[0] == '/' && file[1] == '\0')
strcpy(file, "test.txt");

return file;
}


#define PORT 80

int connect_to_ip(char *host)
{
int sock;
int sockfd;
struct hostent *he;
struct sockaddr_in their_addr;


if((he = gethostbyname(host)) == NULL)
{
perror("gethostbyname(): fail: ");
exit(1);
}
else
printf("Client-The remote host is: %s\n", host);

if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket()");
exit(1);
}
else
printf("Client-The socket() sockfd is OK...\n");

their_addr.sin_family = AF_INET;
printf("Server-Using %s and port %d...\n", host, PORT);
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8);

if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)
{
perror("connect()");
exit(1);
}
else
printf("Client-The connect() is OK...\n");

return sockfd;
}

char *gethdr(char *buff, int size, FILE *sfp)
{
char *s;
memset((char*)buff, 0, size);

fgets(buff, size, sfp);

for(s=buff; *s == '\r'; ++s) continue;
if(*s == '\n') return NULL;

/* locate the end of header */
while (*s && *s != '\r' && *s != '\n')
++s;

*s = '\0';

return buff;
}


#define isspace(s) (*s == ' ')

char *skip_whitespace(char *str)
{
while(isspace(str)) ++str;

return str;
}

char *skip_non_whitespace(char *str)
{
while(*str && !isspace(str)) ++str;

return str;
}


void getfile(int sockfd, char *host, char *file)
{
char cmd[1024] = {};
char buff[512] = {};
char *fname;
int fd, len=0;
int hack = 0;
FILE *sfp;
char *str;

/* open outpt file */
fname = basename(file);
fd = open(fname, O_CREAT|O_RDWR, 0755);

sprintf(cmd, "GET %s HTTP/1.1\r\n", file);
sprintf(cmd, "%sHost: %s\r\n", cmd, host);
sprintf(cmd, "%s\r\n", cmd);

write(sockfd, cmd, strlen(cmd));
printf("cmd: %s\n", cmd);

/* skip the HEADER */
/* START */
sfp = fdopen(sockfd, "r+");
gethdr(buff, 20, sfp);

str = buff;
str = skip_non_whitespace(str);
str = skip_whitespace(str);
printf("%s, status: %d\n", buff, atoi(str));

while(gethdr(buff, sizeof(buff), sfp) != NULL)
printf("%s\n", buff);

fflush(sfp);
/* END */

while(1)
{
memset((char*)buff, 0, sizeof(buff));
len=read(sockfd, buff, sizeof(buff));

write(fd, buff, len);

if(len <= 0) break;
}

close(fd);
close(sockfd);

return;
}

int main(int argc, char *argv[])
{
char *host, *file;
char url[256] = {};
int sockfd;

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

strcpy(url, argv[1]);
host = gethostname(url);
file = argv[1];
file = getfilename(&file[host+strlen(host)-url]);
printf("url: %s, file: %s\n", url, file);

sockfd = connect_to_ip(host);
getfile(sockfd, host, file);

printf("\nsuccessfully written to \"%s\"\n\n", basename(file));

return 0;
}

gcc -o wget wget.c
./wget http://www.dhpc.adelaide.edu.au/education/dhpc/lab/MPI/hello.c
./wget www.physics.arizona.edu/~physreu/dox/cprogs/day1/hello.c

22 October 2009

Network Frames

ALL
Ethernet 802.11 ARP
IP ICMP
TCP UDP

















Reference:
http://jennifermadden.com/javascript/window1.html

21 October 2009

Flo Rida n Kesha - Right Round

(Flo Rida)
You spin my head right round, right round
When you go down, when you go down down...
(Kesha)
You spin my head right round, right round
When you go down, when you go down down...

(Flo Rida)
Heyy!
Hopped out of that house with my swagger
Hop in that with girl, I got places to go!
People to see, time is precious
I look at my crowd and they out of control
Just like my mind where I'm going
No women, no shorties, no nothin but clothes
No stoppin now, my parolees on role
I like my jewlrey, that's always on gold
I know the storm is comin
My pockets keep tellin me it's gonna shower
Call up my homies that's home
Then pop in the night cuz it's meant to be ours
We keep a fade away shot cuz we ballin
it's platinum patrone that be ours
Lil mama, I owe you just like the flowers
Girl you to drink with all that and power clubs

(Flo Rida)
You spin my head right round, right round
When you go down, when you go down down...
(Kesha)
You spin my head right round, right round
When you go down, when you go down down...

(Flo Rida)
From the top of the pole I watch her go down
She got me throwin my money around
Ain't nothin more beautiful to be found
It's goin down down...
From the top of the pole I watch her go down
She got me throwin my money around
Ain't nothin more beautiful to be found
It's goin down down...

(Flo Rida)
Heyy!
Shawty must know I'm not playin
My money love her like a numba one fan
Don't look at my mouth, let her talk to my fans
My Benjamin Franklins...
A couple of grands, I got rubberbands
My paper planes makin a dance
Get dirty all night, that's part of my thing
Keep building castles that's made out of sand
She's amazing, the fire blazing
Hotter than cajun
Girl won't you move a lil closer?
Time to get paid, it's maximum wage
That body belong on a poster
I'm in a daze, that bottom is wavin' at me
Like damnit I know you
You wanna show like a gun out of holster
Tell me whatever and I'll be your roper cuz...

(Flo Rida)
You spin my head right round, right round
When you go down, when you go down down...
(Kesha)
You spin my head right round, right round
When you go down, when you go down down...

(Flo Rida)
From the top of the pole I watch her go down
She got me throwin my money around
Ain't nothin more beautiful to be found
It's goin down down...
From the top of the pole I watch her go down
She got me throwin my money around
Ain't nothin more beautiful to be found
It's goin down down...

(Flo Rida)
Yeah!
I'm spendin my money
I'm out of control
Somebody help me
She's takin my bank roll.
But I'm king of the club
And I'm wearin the crown
Poppin these bottles
Touchin these models
Watchin they asses go down down....(echo)

(Flo Rida)
You spin my head right round, right round
When you go down, when you go down down...
(Kesha)
You spin my head right round, right round
When you go down, when you go down down...
(Flo Rida)
You spin my head right round, right round
When you go down, when you go down down...
(Kesha)
You spin my head right round, right round
When you go down, when you go down down...