97 lines
2.1 KiB
C
97 lines
2.1 KiB
C
/*
|
|
spb - a very simple toy spartan client
|
|
*/
|
|
#include <stdio.h> //printf
|
|
#include <string.h> //strlen
|
|
#include <sys/socket.h> //socket
|
|
#include <arpa/inet.h> //inet_addr
|
|
#include <unistd.h>
|
|
#include <netdb.h>
|
|
#include "yuarel.h"
|
|
|
|
char req[1000];
|
|
char reply[0x10000];
|
|
|
|
int process(int sock,struct sockaddr* server){
|
|
//Connect to remote server
|
|
if (connect(sock , server , sizeof(struct sockaddr_in)) < 0) {
|
|
perror("connect failed. Error");
|
|
return 1;
|
|
}
|
|
|
|
//Send some data
|
|
if( send(sock , req , strlen(req) , 0) < 0) {
|
|
puts("Send failed");
|
|
return 1;
|
|
}
|
|
|
|
//Receive a reply from the server
|
|
while( recv(sock , reply , sizeof(reply) , 0) > 0){
|
|
// puts("[");
|
|
puts(reply);
|
|
//puts("]");
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
long hostname_to_ip(char * hostname){
|
|
struct hostent *he;
|
|
struct in_addr **addr_list;
|
|
|
|
|
|
if ( (he = gethostbyname( hostname ) ) == NULL) {
|
|
// get the host info
|
|
herror("gethostbyname");
|
|
return 0;
|
|
}
|
|
|
|
addr_list = (struct in_addr **) he->h_addr_list;
|
|
return *(long*)addr_list[0];
|
|
|
|
}
|
|
|
|
|
|
int main(int argc , char *argv[]){
|
|
int sock;
|
|
struct sockaddr_in server;
|
|
|
|
struct yuarel url;
|
|
if(-1==yuarel_parse(&url,argv[1])){
|
|
fprintf(stderr,"Could not parse url\n");
|
|
return 1;
|
|
}
|
|
/*
|
|
printf("Struct values:\n");
|
|
printf("\tscheme:\t\t%s\n", url.scheme);
|
|
printf("\thost:\t\t%s\n", url.host);
|
|
printf("\tport:\t\t%d\n", url.port);
|
|
printf("\tpath:\t\t%s\n", url.path);
|
|
printf("\tquery:\t\t%s\n", url.query);
|
|
printf("\tfragment:\t%s\n", url.fragment);
|
|
*/
|
|
sprintf(req,"%s %s 0\r\n",url.host, url.path&&*url.path?url.path:"/");
|
|
long q = hostname_to_ip(url.host);
|
|
if(!q) return 1;
|
|
|
|
//Create socket
|
|
sock = socket(AF_INET , SOCK_STREAM , 0);
|
|
if (sock == -1){
|
|
printf("Could not create socket");
|
|
}
|
|
|
|
//struct in_addr *address;
|
|
// struct hostent *host_info;
|
|
// host_info = gethostbyname(argv[1]);
|
|
|
|
server.sin_addr.s_addr = q;//inet_addr(argv[1]);
|
|
server.sin_family = AF_INET;
|
|
server.sin_port = htons(url.port?url.port:300);
|
|
|
|
// printf("addr: %x",server.sin_addr.s_addr);
|
|
|
|
process(sock,(struct sockaddr*)&server);
|
|
|
|
close(sock);
|
|
return 0;
|
|
}
|