#include #include #include #include #include #include #include #include #include #include #include #include #include #define CHAR_CODE_MAX 128 #define PROG_DNS "/export/home1/johnkoji/bin/dns" /* "/usr/sbin/nslookup" */ const char enc_map[CHAR_CODE_MAX] = { '\0','\0','\0','\0','\0','\0','\0','\0', /* 00 - 07 */ '\0','\0','\0','\0','\0','\0','\0','\0', /* 08 - 0f */ '\0','\0','\0','\0','\0','\0','\0','\0', /* 10 - 17 */ '\0','\0','\0','\0','\0','\0','\0','\0', /* 18 - 1f */ '+' ,'\0','\0','\0','\0','\0','\0','\0', /* 20 - 27 */ '\0','\0','*' ,'\0','\0','-' ,'.' ,'\0', /* 28 - 2f */ '0' ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' , /* 30 - 37 */ '8' ,'9' ,'\0','\0','\0','\0','\0','\0', /* 38 - 3f */ '@' ,'A' ,'B' ,'C' ,'D' ,'E' ,'F' ,'G' , /* 40 - 47 */ 'H' ,'I' ,'J' ,'K' ,'L' ,'M' ,'N' ,'O' , /* 48 - 4f */ 'P' ,'Q' ,'R' ,'S' ,'T' ,'U' ,'V' ,'W' , /* 50 - 57 */ 'X' ,'Y' ,'Z' ,'\0','\0','\0','\0','_' , /* 58 - 5f */ '\0','a' ,'b' ,'c' ,'d' ,'e' ,'f' ,'g' , /* 60 - 67 */ 'h' ,'i' ,'j' ,'k' ,'l' ,'m' ,'n' ,'o' , /* 68 - 6f */ 'p' ,'q' ,'r' ,'s' ,'t' ,'u' ,'v' ,'w' , /* 70 - 77 */ 'x' ,'y' ,'z' ,'\0','\0','\0','\0','\0' /* 78 - 7f */ } ; int char_encode( int c ) { c &= 0xff ; if( 0 <= c && c < CHAR_CODE_MAX ) return (int)enc_map[c] ; else return 0 ; } void www_encode( char *result, char *source ) { int c ; while( *source != '\0') { if( (c = char_encode( *source ))!= 0 ) { *result = (char)c ; result ++ ; } else { sprintf( result,"%%%02X", (unsigned char)*source ) ; result += 3 ; } source ++ ; } *result = '\0'; } #define C_BUF_SIZE 256 #define POST_SIZE 32768 #define N_CONNECT_AGAIN 3 #define SERVER "www.big3.or.jp" /* "210.156.254.2" 210.128.152.2 */ #define WWW_PORT 80 #define BROWSER "Mozilla/3.01 (Win95; I)" #ifdef INPUT_FILE_NAME #define N_IN_FILE 2 static char *INPUT_FILE[N_IN_FILE] = {"jichiku4.txt","jichiku5.txt"} ; #endif /* if [Return] letter is added to string, delete it and return 1. */ int i_ret( char *str ) { char *cp ; int ret = 0 ; if( (cp = strchr( str,'\r'))!= NULL ) { *cp = '\0'; ret = 1 ; } if( (cp = strchr( str,'\n'))!= NULL ) { *cp = '\0'; ret = 1 ; } return ret ; } int www_post( FILE *fp, int soc, char *url ) { char i_buf[C_BUF_SIZE], o_buf[C_BUF_SIZE*3], post_buf[POST_SIZE] ; char *field[4] = {"num","id","name","body"} ; char *cp, *ip ; int i, ret = -1, sep = 0 ; #ifdef SPLIT_URL if( fgets( i_buf, C_BUF_SIZE, fp )== NULL || /* 2nd row */ (cp = strtok( i_buf,"\n\r\t "))== NULL ) return -1 ; sprintf( o_buf,"POST %s HTTP/1.0\n", cp ) ; /* HTTP file name */ write( soc, o_buf, strlen( o_buf ) ) ; fputs( o_buf, stdout ) ; /* Debug */ #endif if( fgets( i_buf, C_BUF_SIZE, fp )== NULL || /* 3rd row */ (cp = strtok( i_buf,"\n\r\t "))== NULL ) return -1 ; sprintf( o_buf,"Referer: %s\n", cp ) ; /* Refer from ... */ write( soc, o_buf, strlen( o_buf ) ) ; fputs( o_buf, stdout ) ; /* Debug */ sprintf( o_buf,"User-Agent: %s\n", BROWSER ) ; write( soc, o_buf, strlen( o_buf ) ) ; fputs( o_buf, stdout ) ; /* Debug */ if( strncmp( url,"http://",7 )== 0 ){ cp = url+7 ; }else{ cp = url ; } i = strcspn( cp,"/" ) ; strncpy( i_buf, cp, i ) ; i_buf[i] ='\0' ; sprintf( o_buf,"Host: %s\n", i_buf ) ; write( soc, o_buf, strlen( o_buf ) ) ; fputs( o_buf, stdout ) ; /* Debug */ sprintf( o_buf,"Content-type: application/x-www-form-urlencoded\n") ; write( soc, o_buf, strlen( o_buf ) ) ; fputs( o_buf, stdout ) ; /* Debug */ /* Make Post Data */ post_buf[0] = '\0'; while( fgets( i_buf, C_BUF_SIZE, fp )!= NULL ) { if( (cp = strchr( i_buf,'='))!= NULL && ret != 0 ) { for( ip=i_buf ; ip %s", PROG_DNS, dn_text, tmp_fn ) ; if( system( line_buf )!= 0 ) return -1 ; if( (fp = fopen( tmp_fn,"rt"))== NULL ) return -2 ; while( fgets( line_buf, 128, fp )!= NULL ) { if( strncmp( line_buf,"Name:", 5 )== 0 && fgets( line_buf, 128, fp )!= NULL && ( strncmp( line_buf,"Address:", 8 )== 0 || strncmp( line_buf,"Addresses:", 10 )== 0 )&& sscanf(line_buf,"%*s %u.%u.%u.%u",n_ip,n_ip+1,n_ip+2,n_ip+3)==4) { for( i=0 ; i<4 ; i++ ) sin_addr[i] = n_ip[i] & 0xff ; fclose( fp ) ; remove( tmp_fn ) ; return 0 ; /* OK */ } } fclose( fp ) ; remove( tmp_fn ) ; return -3 ; } int open_http( char *url, char *proxy, struct sockaddr_in *remote_s ) { int i, socket_id, serv_port=WWW_PORT, use_proxy=0 ; char hostname[128], send_message[256] ; char *filename="" ; u_char *cp ; FILE *fp ; /* No proxy: hostname = WWW server */ if( proxy == NULL || *proxy =='\0'){ if( strncmp( url,"http://",7 )== 0 ){ cp = url+7 ; }else{ cp = url ; } /* file name part of URL after '/' */ if( (filename = strchr( cp,'/'))==NULL ){ fprintf( stderr,"URL Error:\n%s\n", url ) ; return -1 ; } /* host:port part of URL */ i = strcspn( cp,"/" ) ; strncpy( hostname, cp, i ) ; hostname[i] ='\0' ; use_proxy=0 ; /* Use proxy: hostname = proxy server */ }else{ strcpy( hostname, proxy ) ; filename = "" ; use_proxy=1 ; } /* Extract port number */ if( (cp = strrchr( hostname,':'))!= NULL && sscanf( cp+1,"%u", &i )==1 ) { serv_port = i ; *cp = '\0' ; } /* Create a socket */ if( (socket_id = socket(PF_INET, SOCK_STREAM, 0)) <0 ) { Err("socket"); return -3 ; } bzero( (char *)remote_s, sizeof( struct sockaddr_in ) ) ; remote_s->sin_family = PF_INET ; remote_s->sin_port = htons( serv_port ) ; /* Set IP address */ cp = (char *)&(remote_s->sin_addr) ; if( nslookup( cp, hostname )!= 0 && set_ip_addr( cp, hostname )!= 0 ) { fprintf( stdout,"Unknown host: %s\n", hostname ) ; return -2 ; } /* Connect: Debug */ fprintf( stdout,"Connecting %s [%u.%u.%u.%u] ...\n", hostname, cp[0], cp[1], cp[2], cp[3] ) ; for( i=0 ; i 0 ) { c_buf[bytes] = '\0'; fputs( c_buf, stdout ) ; } close( soc ) ; } }