LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2002, 01:14 PM   #1
The Bag
LQ Newbie
 
Registered: Dec 2001
Distribution: Mandrake, Red Hat, Windows XP Pro
Posts: 14

Rep: Reputation: 0
Exclamation More C help pls


Hi I still writing my proxy server. I'm having trouble with the send commmand but I think its the way I'm trying to split the string thats causing the problems, because when I run the entire request string through it works.

Currently I'm trying


fname = "GET ";
temp = "\n\n";


sscanf(client_buffer,"GET http:// % [ ^/ ] % [ ^\n ] ", host, filename);

strcat(request, fname);
strcat(request, filename);
strcat(request, temp);




Where client_buffer is a char[1024] got with recv from my client, it holds the requested web page. I am trying to split it so host holds the web address & request holds the requested file (after catination, ie GET /index.html HTML V1 \n\n).

But when I pass request to the host (after getting a connection) the program hangs. This does not happen when client_buffer is passed (which defeats the purpose of a proxy).

Basically I think I need a different or better way to spilt the string. Please help & also point out any really obvious mistakes I've made. (pls igone the random variable defined for debug purposes that haven't been talken out).


The rest of the program:
PHP Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define BACKLOG 10

main(){

  
struct sockaddr_in client_add;
  
struct sockaddr_in my_add;
  
struct sockaddr_in host_add;
  
struct hostent server;


  
int portnum 8080;
  
int port 80;
  
char myname[50];
  
int sockclient_sockhost_sock;
  
unsigned int c_add_len;
  
unsigned int flag;
  
int length 50;

  
int buffer_size 1024;
  
char client_buffer[1024];
  
char data_buffer[1024];

  
char host = (char *)malloc(buffer_size sizeof(char));
  
char request = (char *)malloc(buffer_size sizeof(char));
  
char filename = (char *)malloc(buffer_size sizeof(char));
  
char fname = (char *)malloc(buffer_size sizeof(char));
  
char temp = (char *)malloc(buffer_size sizeof(char));

  
int sizething;
  
int testnmpti;
  
unsigned int sin_size;
  
int count 0;
  
int req_size;
  
int fetch_loop;
  
int host_starthost_end;
  
int bytesread;

  
fname "GET ";
  
temp  "\n\n";

    
gethostname(myname50);
    
printf("started\n");

  
my_add.sin_family AF_INET;
  
my_add.sin_port htons(portnum);
  
my_add.sin_addr.s_addr INADDR_ANY;
  
memset(&(my_add.sin_zero), '\0',8);

  
sock socket(AF_INETSOCK_STREAM,0);

  if(
sock 0){
    
printf("QWERTY");
    
close(sock);
    return -
1;
  }else{
    
printf("ok\n");
  }


  if((
bind(sock, (struct sockaddr *)&my_addsizeof(struct sockaddr))) == -){
    
printf("dead\n");
    
close(sock);
  }else{
    
printf("bound\n");
  }

  
/* loop infinitely */

  
bytesread 1;

  while(
bytesread != 0){

  if((
listen(sock3)) < 0){
    
printf("dies on listen");
    
close(sock);
    return 
0;
  }else{
    
printf("listening\n");
  }

  
sin_size sizeof(struct sockaddr_in);
  
client_sock accept(sock, (struct sockaddr *)&client_add, &sin_size);

  if(
client_sock == -1){
    
printf("DAMN IT\n");
    
close(client_sock);
    
close(sock);
    return 
0;
  }else{
    
printf("accepted\n");
  }

  
/* Get address from client */

  
printf("about to read");
  
bytesread recv(client_sockclient_buffer10240);
  
printf("finshed recv");

  
size bytesread;
  
printf("%d"size);    

  if(
size 0){
    
printf("Didn't read it");
    
close(client_sock);
    
close(sock);
    return 
0;
  }else{
    
printf("We are recieving cap'in: ");
  }

  
printf("%s\n"client_buffer);


  
sscanf(client_buffer,"GET [url]http://%[/url][/url][^/]%[^\n]"hostfilename);
  
printf("%s\n"filename);

  
strcat(requestfname);
  
strcat(requestfilename);
  
strcat(requesttemp);


  
printf("%s\n"host);
  
printf("--------------------------------------\n");
  
printf("%s\n"request);

  
printf("got host name\n");


  
/* trying to connect to host*/

  
host_sock socket(AF_INETSOCK_STREAM0);

  if(
host_sock 0){
    
printf("%s\n",strerror(errno));
    
close(client_sock);
    
close(host_sock);
    
close(sock);
    return -
1;
  }

  
printf("trying to get host\n");
  
server gethostbyname(host);

  if(
server == NULL){
    
printf("%s\n",strerror(errno));
    
close(client_sock);
    
close(host_sock);
    
close(sock);
    return -
1;
  }

  
printf("looked up host\n");


  
host_add.sin_family AF_INET;
  
host_add.sin_addr.s_addr inet_addr(inet_ntoa(* ((struct in_addr *)server->h_addr)));
  
host_add.sin_port htons(port);
  
memset(&(my_add.sin_zero), '\0',8);  

  
printf("trying to setup connection\n");


  
/*Connnect to host*/

  
thing connect(host_sock, (struct sockaddr *)&host_addsizeof(struct sockaddr));

  if(
thing  == -1){
    
perror ("connect");
    
close(client_sock);
    
close(host_sock);
    
close(sock);
    return -
1;
  }else{
    
printf("Connection set up\n");
  }

  
/* requested file*/
  
  
printf("%d\n",size);

  
host_end 0;





  
host_end send(host_sockclient_buffersizeof(client_buffer), 0);
  
printf("%s\n",request);
/*    host_end = send(host_sock, request, sizeof(request),0); */

  
if(host_end == -1){
    
printf("connection lost\n");
    
close(client_sock);
    
close(host_sock);
    
close(sock);
    return 
0;
  }else{
    
printf("request sent to server\n");
  }

  
/* Loop until fetch_loop is set to 0 indicatating the end of the file */

  
printf("testing testing\n");

  
fetch_loop 1;

  
printf("%d\n",fetch_loop);


  while(
fetch_loop != 0){

    
printf("in da loop\n");
    
/* Get data from host */
    
    
fetch_loop recv(host_sockdata_buffer10240);
    
printf("%d\n",fetch_loop);
    if(
fetch_loop 0){
      
printf("transfer failed\n");
      
close(client_sock);
      
close(host_sock);
      
close(sock);
      return 
0;
    }else{
      
printf("read from host\n");
    }

    
/* send data to client */
    
if(send(client_sockdata_bufferfetch_loop0) < 0){
      
printf("TF\n");
      
close(client_sock);
      
close(host_sock);
      
close(sock);
      return 
0;
    }else{
      
printf("sent buffer to client\n");
    }


  }

    
fetch_loop 1;
    
0;
    
close(host_sock);
    
close(client_sock);
    
sin_size 0;
    
size 0;

    
free(host);
    
free(request);
    
free(filename);

    
host = (char *)malloc(buffer_size sizeof(char));
    
request = (char *)malloc(buffer_size sizeof(char));
    
filename = (char *)malloc(buffer_size sizeof(char));
    
  }

  
close(host_sock);
  
close(client_sock);
  
close(sock);


 
Old 03-11-2002, 01:41 PM   #2
nitr0gen
LQ Newbie
 
Registered: Feb 2002
Distribution: Homemade Slakware
Posts: 14

Rep: Reputation: 0
Lightbulb Error

Hi dude...

If you take a closer look at your format string you will see that it's wrong...

look:

sscanf(client_buffer,"GET <a href="http://%" target="_blank">http://%</a>[/url][^/]%[^\n]", host, filename);

a format string should absolutly contain conversions character
%s string
%d integer
etc...
you only specify %
that's why it fucks up
and you close " as well few times in your html code... you cant do it as is...

and hummm... If I were you I would rather consider using strtok() function to parse your string in tokens instead...

so Good Luck
 
Old 03-11-2002, 01:56 PM   #3
nitr0gen
LQ Newbie
 
Registered: Feb 2002
Distribution: Homemade Slakware
Posts: 14

Rep: Reputation: 0
Lightbulb tips and tricks

and humm as matter of optimizing your code

do not malloc a buffer to 1024 directly hardcoded
use fstat() to read the higher block size for better I/O efficiency
declare a struct stat using st_blksize member

I recommand using read() and write() instead of recv(), send() respectivly.

I had experience writing a TCPIP tunnel software and it decreases overhead time..

nitr0gen
 
Old 03-11-2002, 03:22 PM   #4
The Bag
LQ Newbie
 
Registered: Dec 2001
Distribution: Mandrake, Red Hat, Windows XP Pro
Posts: 14

Original Poster
Rep: Reputation: 0
How would you use strtok, I can only find sketchy details on it.
 
Old 03-11-2002, 03:31 PM   #5
nitr0gen
LQ Newbie
 
Registered: Feb 2002
Distribution: Homemade Slakware
Posts: 14

Rep: Reputation: 0
Use the man Luke

man strtok
pretty easy prototype
char *strtok(char *s, const char *delim)

you specify s as your string
and seperate it using delimiter delim
i.e.
I have a string

char buf[] = "this is an example";
char *ptr;

ptr = strtok(buf, " ");
printf("first token: %s\n", ptr);
ptr = strtok(NULL, " ");
printf("second token: %s\n", ptr);

this following piece of code seperate the string in token with space as delimiter.
this would print like
first token: this
second token: is

at the first call you issue your string as first parameter and further calls you specify NULL...

hope you got it.. take a look at the man anyways its pretty easy to handle tokens

nitr0gen
 
Old 03-11-2002, 03:49 PM   #6
The Bag
LQ Newbie
 
Registered: Dec 2001
Distribution: Mandrake, Red Hat, Windows XP Pro
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks, it all makes sense now. As for the man pages I'm currently sitting at a windows machine, unfortunately.
 
Old 03-11-2002, 03:51 PM   #7
nitr0gen
LQ Newbie
 
Registered: Feb 2002
Distribution: Homemade Slakware
Posts: 14

Rep: Reputation: 0
Smile your welcome

no problem
if you have any other question feel free to ask me

nitr0gen
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help! Pls! Yull29m Linux - General 2 02-03-2005 12:57 PM
Can someone pls help me pancake Linux - Newbie 3 04-13-2004 12:22 AM
pls pls pls help me ! i'm tired with httpd config on fedora apache 2.0.48 AngelOfTheDamn Fedora 0 01-24-2004 05:12 PM
hi pls help TAAN Linux - Software 2 12-14-2003 12:57 PM
.pls Chazb Linux - Software 1 07-02-2003 07:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration