#include #include #include #include #include #include #include #include #include #define PORT 80 #define MAXDATASIZE 100 int main(int argc, char *argv[]){ char mylink[100], mydomain[100], *mydomainf[100], *p, *p2, buf[MAXDATASIZE],tmpbuff[100]; int sockfd, numbytes; struct hostent *he; struct sockaddr_in dest_addr; FILE *tmpfile; 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"); } // OPEN TEMP FILE if ((tmpfile = fopen("/tmp/rss-get.tmp", "w+")) == NULL ){ perror("fopen"); exit(1); } // FETCH DATA while(numbytes != 0){ if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1){ perror("recv"); exit(1); } else { buf[numbytes] = '\0'; fprintf(tmpfile, buf); // WRITE DATA TO TEMP FILE } } close(sockfd); // CLOSE SOCKET // PARSE TEMP FILE // 1. use fgets() to read line by line // 2. use strtok() to get location of tags // 3. print tags if ((fgets(tmpbuff, 100, tmpfile)) == NULL){ perror("fgets"); exit(1); } printf("%s", tmpbuff); fclose(tmpfile); // CLOSE TEMP FILE } // MAIN