LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   More C help pls (https://www.linuxquestions.org/questions/programming-9/more-c-help-pls-16026/)

The Bag 03-11-2002 01:14 PM

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);




nitr0gen 03-11-2002 01:41 PM

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

nitr0gen 03-11-2002 01:56 PM

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

The Bag 03-11-2002 03:22 PM

How would you use strtok, I can only find sketchy details on it.

nitr0gen 03-11-2002 03:31 PM

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

The Bag 03-11-2002 03:49 PM

Thanks, it all makes sense now. As for the man pages I'm currently sitting at a windows machine, unfortunately.

nitr0gen 03-11-2002 03:51 PM

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

nitr0gen


All times are GMT -5. The time now is 08:22 AM.