Shammer's Philosophy

My private adversaria

HTTP Client ver 0.21

HTTP Client ver 0.2 - Shammerismをちょっとだけ改良した。旧実装では、is_valid_ipaddr が条件によっては何も返さない箇所があった。そのため、コンパイルオプション次第では

warning: control may reach end of non-void function [-Wreturn-type]

の警告が出ることがあった。あと、

bytes_read = recv(sock, response, sizeof(response), 0);

の部分で処理が停滞してしまうことが判明。この部分はまだ修正していないけれど、変更しないといけない。現状はこんな感じ。

#include <ctype.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int is_valid_ipaddr(char v[]) {
    regex_t preg;
    int regcompresult, regexecresult;
    regcompresult = regcomp(&preg, "^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$", REG_NOSUB | REG_EXTENDED | REG_NEWLINE);
    if( regcompresult == 0 ){
	size_t nmatch = 0;
	regmatch_t pmatch[nmatch];
	regexecresult = regexec(&preg, v, nmatch, pmatch, 0);
	if( regexecresult == 0 ){
	    printf("%s is valid.\n", v);
	} else {
	    printf("%s is not valid.\n", v);
	}
    }
    return regexecresult;
}

int main(int argc, char* args[]){
    if( argc == 2 ){
	int is_valid_ipaddr_result = is_valid_ipaddr(args[1]);
	if( is_valid_ipaddr_result == 0 ){
	    // HTTP Client
	    int sock, bytes_read;
	    struct sockaddr_in destination_addr;
	    unsigned short destination_port = 80;
	    char response[256];
	    sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	    if( sock < 0 ){
		printf("Socket open error.\n");
		exit(1);
	    }
	    memset(&destination_addr, 0, sizeof(destination_addr));
	    destination_addr.sin_family = AF_INET;
	    destination_addr.sin_addr.s_addr = inet_addr(args[1]);
	    destination_addr.sin_port = htons(destination_port);
	    if( connect(sock, (struct sockaddr *)&destination_addr, sizeof(destination_addr)) < 0 ){
		printf("Connect error.\n");
		exit(2);
	    }
	    send( sock, "GET / HTTP/1.1\r\n", strlen("GET / HTTP/1.1\r\n"), 0);
	    send( sock, "Host: ", strlen("Host: "), 0);
	    send( sock, args[1], strlen(args[1]), 0);
	    send( sock, "\r\n\r\n", strlen("\r\n\r\n"), 0);
	    while(1) {
		bzero(response, sizeof(response));
		bytes_read = recv(sock, response, sizeof(response), 0);
		if( bytes_read > 0 ){
		    printf("%s", response);
		}
		else {
		    break;
		}
	    }
	    close(sock);
	}
	else {
	    printf("Usage: %s %s\n", args[0], "Destination_IP");
	}
    }
    else {
	printf("Usage: %s $Destination_IP\n", args[0]);
    }
    return 0;
}