Thread: C Proxy Write problems

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    25

    Question C Proxy Write problems

    I wrote an HTTP server and simple single threaded proxy for a class awhile back, and I'm now trying to fix an issue that came up then that only happened when testing it using an external browser. What happens is that when using lynx in the UNIX test environment, it works fine, no issues. But when tested using another computer, and the external ip-address, the proxy program quits, but the server works fine. After some debugging, I realized it's quitting due to a SIGPIPE being raised by one of the write functions. If I set a handle in gdb to not stop on sigpipe, it continues to work, and sends the html code to the browser just fine. I'm trying to find where the pipe is getting closed early, but I haven't been able to figure it out, I was wondering if the problems is more obvious to anyone on here?

    Web Proxy Code: (Single Threaded)

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h> 
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    #include <err.h>
    #include <string.h>
    
    
    // /********************************************************           
    // * HTTP Web Proxy -- C, simple Web Proxy    Server        *   
    // *                                                      *   
    // * Authors: Alexander Hollis, Isaias Delgado              *   
    // *                                                      *   
    // * Purpose:  Demonstration of a simple web proxy in C   *   
    // *                                                      *   
    // * Usage:                                               *   
    // *      Refer to included manual for proper usage.      *   
    // ********************************************************/
    /*
    void signal_callback_handler(int signum){  // Catch Signal Handler function // 
    
    
        printf("Caught signal SIGPIPE %d\n",signum);
    }*/
    
    
    
    
    
    
    int main(int argc, char *argv[])
    {
    
    
        char get[] = "GET /index.html HTTP/1.1\r\n\r\n";
        char response[] = "HTTP/1.1 200 OK\r\n"
        "Content-Type: text/html; charset=UTF-8\r\n\r\n"
        "<doctype !html>\r\n";
    
    
    
    
        char page[10000];
        
    
    
        int one = 1, client_fd;
        struct sockaddr_in svr_addr, cli_addr, out_addr;
        socklen_t sin_len = sizeof(cli_addr);
        
        out_addr.sin_family = AF_INET;
        out_addr.sin_port = htons(8085);
        out_addr.sin_addr.s_addr=inet_addr("129.120.151.94");
        
        int sock1 = socket(AF_INET, SOCK_STREAM, 0);
        int sock2 = socket(AF_INET, SOCK_STREAM, 0);
        
    
    
        setsockopt(sock1, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
        setsockopt(sock2, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
        int port = 8084;
        svr_addr.sin_family = AF_INET;
        svr_addr.sin_addr.s_addr = INADDR_ANY;
        svr_addr.sin_port = htons(port);
    
    
        if (bind(sock1, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) {
            close(sock1);
            err(1, "Can't bind listening");
        }
    
    
        listen(sock1, 5);
    
    
        int bytesRec;
        char buf[1024];
        char* request;
        int x=0;
        
        
        
        int connreturn;
        
    
    
        while (1) {
            
            //signal(SIGPIPE, signal_callback_handler); /* Catch Signal Handler SIGPIPE */ 
            client_fd = accept(sock1, (struct sockaddr *) &cli_addr, &sin_len);
            printf("got connection\n");
            printf("Requesting index from local server...\n");
            
            connreturn = connect(sock2, (struct sockaddr *) &out_addr,(socklen_t)sizeof(out_addr));
            printf("Connect returns: %d\n",connreturn);
            send(sock2, &get, sizeof(get),0);
            recv(sock2, page, sizeof(page)-1, 0);
            //page[sizeof(page)-1] = '/0';
            write(client_fd, response, sizeof(response) - 1);
            write(client_fd, page, sizeof(page)-1);
            //send(sock2, &page, sizeof(page),0);
            close(client_fd);
            close(sock2);
            memset(page, 0, sizeof(page[0]) * 10000);
            continue;
            
            
        }
        
    }







    Thank You very much!


    - Alex

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You're closing sock2 in your loop, but the next iteration, you try to use it again. That will cause problems.

    You're also failing to capture the return value of recv(), which tells you how many bytes were received. Instead of using that information, you're just sending your entire buffer, including the junk data beyond the end of what you read. You should only send as much data as you actually have.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Write-to-file problems
    By dcdude in forum C Programming
    Replies: 3
    Last Post: 04-25-2011, 01:55 PM
  2. Replies: 1
    Last Post: 04-13-2011, 01:15 PM
  3. Problems with fstream, one variabile for write and read
    By Smjert in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2009, 10:19 PM
  4. proxy.pac (proxy auto config) question
    By boreder in forum C Programming
    Replies: 2
    Last Post: 01-20-2009, 03:13 AM
  5. newbie problems with write()
    By lastninja in forum C Programming
    Replies: 5
    Last Post: 01-20-2002, 01:18 PM

Tags for this Thread