All pastes #565031 Raw Edit

Untitled

public text v1 · immutable
·published 2007-06-13 20:55 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>

#define PORT 80
#define MAXDATASIZE 100

int main(int argc, char *argv[]){
        char mylink[100], mydomain[100], *mydomainf[100], *p, *p2, *token, buf[MAXDATASIZE];
        int sockfd, numbytes;
        struct hostent *he;
        struct sockaddr_in dest_addr;

        if(argc < 2){
        printf("usage: ./rss-get feed-url\n");
        exit(1);
        }

        strcpy(mydomain, argv[1]);

        p = strchr(mydomain, '.');      // get addr of first '.'
        if (!p) error();                // empty p?
        p2 = strchr(p, '/');            // get addr of following '/'
        if (p2 != NULL) *p2 = 0;        // empty? no, end string there

if ( !strncmp( mydomain, "http://", 7 )){       // find string
        *mydomainf = mydomain + 7;              // set new start addr
        } else {                                // or
        *mydomainf = mydomain;                  // leave the same
        }

        printf("Domain: %s\n", *mydomainf);     // print domain
        printf("Feed URL: %s\n", argv[1]);      // print feed url

// BEGIN TRANSMISSION PHASE

        if ((he = gethostbyname(*mydomainf)) == NULL){
                herror("gethostbyname");
                exit(1);
        }

        if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1){
                perror("socket");
                exit(1);
        }

        dest_addr.sin_family = AF_INET;
        dest_addr.sin_port = htons(PORT);
        dest_addr.sin_addr = *((struct in_addr *)he->h_addr);
        memset(&(dest_addr.sin_zero), '\0', 8);

        if (connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr)) == -1){
                perror("connect");
                exit(1);
        }

// SEND GET REQUEST - 'GET *mydomainf HTTP/1.0\n\n'

        sprintf(buf, "GET %s HTTP/1.0\n\n", argv[1]);
        printf("Request: %s", buf);
        if ((numbytes = send(sockfd, buf, MAXDATASIZE-1, 0)) == -1){
                perror("send");
                exit(1);
        } else {
        printf("Successfully sent request, awaiting response...\n");
        }

// FETCH DATA

while(numbytes != 0){
        if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1){
                perror("recv");
                exit(1);
        } else {
        buf[numbytes] = '\0';
        printf("%s", buf);              // WRITE DATA TO STDOUT
        }
}
        close(sockfd);                  // CLOSE SOCKET

} // MAIN