Thread: connecting to server with wrong port

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    13

    connecting to server with wrong port

    hey,

    I'm trying to mimic the process of a proxy.
    I setup a socket for the listening server first, do a bind and then listen, and
    on the while loop I do accept, after the accept I setup another socket and set the client sockaddr_in struct with sin_addr.s_addr being the http server that this app will connect
    to and sin_port being the http server port (80) and then I do a connect.

    what happens is that the app is listening on some port, say 1000, when connected to, it's suppose to connect itself to another http server on port 80 but instead it connects to that remote server with source address 1000 and some random destination port (instead of being the opposite). I've double checked that I didn't mix the server and client's structs for sockaddr_in.

    what can this be?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    how about you post some code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    13

    code...

    ofcourse.

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    
    const char MSG[] = 
    "GET / HTTP/1.1\r\n\
    User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.8)\r\n\
    Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/
    *;q=0.5\r\n\
    Accept-Language: en-us,en;q=0.5\r\n\
    Accept-Encoding: gzip,deflate\r\n\
    Accept-Charset: ISO-8859-1,utf-8;q=0.7\r\n\
    Keep-Alive: 300\r\n\
    Connection: keep-alive\r\n\
    ";
    
    
    int main(int argc, char *argv[]) {
    
            int simpleSocket = 0;
            int simplePort = 0;
            int returnStatus = 0;
    
            int clientSock = 0;
            int msgLength = 0;
            int clientConnect = 0;
            char buf[1024] = { 0 };
            char bufProxy[1024] = { 0 };
            struct sockaddr_in simpleServer;
            struct sockaddr_in simpleClient;
    
            if (4 != argc) {
                    fprintf(stderr, "usage: %s <listen-port> <remote-http-server> <remote-http-port>\n", argv[0]);
                    exit(1);
            }
    
            simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
            if (simpleSocket == -1) {
                    fprintf(stderr, "could not create socket\n");
                    exit(1);        } else {
                    fprintf(stderr, "socket created\n");
            }
    
            simplePort = atoi(argv[1]);
    
            bzero(&simpleServer, sizeof(simpleServer));
            simpleServer.sin_family = AF_INET;
            simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
            simpleServer.sin_port = htons(simplePort);
    
            returnStatus = bind(simpleSocket, (struct sockaddr*)&simpleServer, sizeof(simpleServer));
            if (returnStatus == 0) {
                    fprintf(stderr, "bind completed\n");
            } else {
                    fprintf(stderr, "could not bind\n");
                    close(simpleServer);
                    exit(1);
            }
    
    
            returnStatus = listen(simpleSocket, 5);
            if (returnStatus == -1) {
                    fprintf(stderr, "can not listen on socket\n");
                    close(simpleSocket);
                    exit(1);
            } 
    
            while (1) {
    
                    struct sockaddr_in clientName = { 0 };
                    int simpleChildSocket = 0;
                    int clientNameLength = sizeof(clientName);
    
                    simpleChildSocket = accept (simpleSocket, (struct sockaddr*)&clientName, &clientNameLength);
                    if (simpleChildSocket == -1) {
                            fprintf(stderr, "canot accept connection\n");
                            close(simpleServer);
                            exit (1);
                    } 
                    printf("accepted client\n");
                    
                    // let's receive the client's GET request
    
                    returnStatus = read(simpleChildSocket, buf, sizeof(buf));
                    printf("received from client %d bytes: %s \n", returnStatus, buf);
    
                    // connecting to the web server as a client:
                    clientSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    printf("clientSocket = socket() - %d\n", clientSock);
                    bzero(&simpleClient, sizeof(simpleClient));
    printf("bzero for simpleClient\n");
                    simpleClient.sin_family = AF_INET;
                    inet_addr(argv[2], &simpleClient.sin_addr.s_addr);
    printf("inet_addr copy\n");
                    simpleClient.sin_port = htons(atoi(argv[3]));
    printf("simpleclient port assignment\n");
                    clientConnect = connect(clientSock, (struct sockaddr*)&simpleClient, sizeof(simpleClient));
    printf("connecting to remote server - %d\n", clientConnect);
                    returnStatus = write(clientConnect, MSG, strlen(MSG));
    printf("writing to proxy server\n");
    /*
                    printf("wrote to proxied server %d bytes: %s\n", returnStatus, buf);
                    returnStatus = read(clientSock, bufProxy, sizeof(bufProxy));
                    printf("read from proxied server %d bytes: %s\n", returnStatus, bufProxy);
    */
                    close(clientSock);
    
                    write(simpleChildSocket, bufProxy, strlen(bufProxy));
                    close(simpleChildSocket);
    
            }
    
            close(simpleServer);
            return 0;
    
    }
    The code is a bit messy, I know, but it should be straight forward.
    The problem is with
    Code:
    clientConnect = connect(clientSock, (struct sockaddr*)&simpleClient, ...
    which returns -1 (because it tries to connect with listen-port as source port and some random port as destination)

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    13

    any ideas?

    or suggestions or this issue?
    it seems that the connect() call itself within the while(1) loop fails for some reason.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. connecting to ftp server (winsock2)
    By commissar in forum C++ Programming
    Replies: 1
    Last Post: 03-03-2005, 10:22 AM
  4. Connecting to COM port
    By Unimatrix139 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-25-2002, 11:00 AM
  5. What's wrong with my Multi-Thread Echo Server ?
    By toshiyuki555_99 in forum C Programming
    Replies: 1
    Last Post: 12-10-2001, 04:52 AM