Hell Oh Entropy!

Life, Code and everything in between

[Solved (I think)] MSEB Online Payment

Posted: Apr 28, 2009, 19:12

Finally! Here's how I went about it:

First, I wrote a simple program that downloads a page from http://billing.mahadiscom.in

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define BUF_SIZE 2048

int main(int argc, char *argv[])
{
    char *host="billing.mahadiscom.in";
    char *port="80";

    int n=0, iter=0;

    struct addrinfo hints;
    struct addrinfo *result, *rp;
    int sfd, s;
    char buf[BUF_SIZE];

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0;
    hints.ai_protocol = 0;

    s = getaddrinfo(host, port, &hints, &result);
    if (s != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
        exit(EXIT_FAILURE);
    }


    for (rp = result; rp != NULL; rp = rp->ai_next) {
        sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (sfd == -1)
            continue;

        if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
            break;  /* Success */
        else
            perror("connect");

        close(sfd);
    }

    if (rp == NULL) {   /* No address succeeded */
        fprintf(stderr, "Could not connect\n");
        exit(EXIT_FAILURE);
    }

    freeaddrinfo(result);   /* No longer needed */

    if(write(sfd, "GET /billinfo.php HTTP/1.1\r\n", 
            strlen("GET /billinfo.php HTTP/1.1\r\n") ) < 0 )
        perror("write1");

    if (write(sfd, "Host: billing.mahadiscom.in\r\n", 
            strlen("Host: billing.mahadiscom.in\r\n") )<0)
        perror("write2");

    if(write(sfd, "\r\n", strlen("\r\n") )<0)
        perror("write3");

    memset(buf, 0, BUF_SIZE);

    while((n = read(sfd, buf, BUF_SIZE-1)) >0) {
        printf("%s\n\n==========================\n\n"
            "%d bytes received (Iter %d)\n\n"
            "===============================\n\n", 
            buf, n, ++iter);
        memset(buf, 0, BUF_SIZE);
    }

    if (n<0)
        perror("error");

    return 0;
}

On first run, data came in at 1448 bytes per burst and then hung up in about 3 bursts. Then I connected to the UK based VPN and tried again. This time data came in at 1360 bytes per burst and the entire page got downloaded. I discussed this with Ranjith Rajaram at work and he told me about MTU, which affects this window size.

Sure enough, tun0 was configured with an MTU of 1412 while eth0 had an MTU of 1500. I modified the MTU for eth0 and presto! It worked! I'm still wondering how this works for Windows systems without any such interference.

PS: Yes, the code's really dirty... but that's not the point.

Comments

The making of libnet? libaynet? libnetwork?

Posted: Mar 09, 2009, 23:43

I was finally able to commit what I started thinking about and working on during FOSS.in back in November last year. Ayttm now starts its connections in a different thread. This eliminates the hang-ups that one may experience when the application is looking up a hostname. The proxy code was also not really with it. I wonder if anyone has tested to see if it works in the first place. For that matter, after I'm done with it, I hope someone tests it as well ;)

So here's what I did. Every time a connection is requested, The connector creates a gthread for the connection and then adds an idle function source (see g_idle_add() in glib) to see if that connection has completed or not. once the connection thread returns, the idle handler continues and calls the connection callback, thus returning control to the connection requestor.

Like that made a lot of sense ;)

The bigger goal is to make this into a more serious networking wrapper library. Next step now is to add SSL support into the library. Then instead of linking it statically, I wonder if it would be a good idea to make it a separate module altogether. It might be necessary since nothing in the core uses SSL, so statically linking libproxy into the core won't work at all.

Oh, and the name libproxy just doesn't cut it anymore. I guess it should be called libnet or something once I'm through with it :)

Comments