// httpget.c written by Caesum 13/07/2003 #include #include #include #include #define HTTPBUFFSIZE 100000 #define HTTP_PORT 80 struct sockaddr_in A; WSADATA wsaData; int i; struct hostent *s; char R[HTTPBUFFSIZE+1]; int init_winsock(WORD version,BOOL verbose) { int err; if(verbose) printf("Initialising Winsock\n"); err = WSAStartup(version, &wsaData); if(err) { printf("Could not initialise Winsock\n"); if(verbose) printf("Error code:%d\n",WSAGetLastError()); } return err; } void exit_winsock(void) { WSACleanup(); } unsigned long hostlookupaddr(char *target,BOOL verbose) { unsigned long iadr; s=NULL; iadr=inet_addr(target); if(iadr==INADDR_NONE) { if(verbose) printf("Trying gethostbyname\n"); s=gethostbyname(target); if(s==NULL) { printf("No entry for IP...(Error Code:%d)\n",WSAGetLastError()); return 0; } return ((unsigned long *)(s->h_addr_list[0]))[0]; } return iadr; } void helpscreen(void) { printf("\nHTTPGet by Caesum - help\n"); printf("-------------------------\n"); printf("HTTPGet attempts to request a webpage directly from a given http server with full control of headers given to the user.\n"); printf("Options:\n"); printf("-h These help options\n"); printf("-v Verbose mode (more messages)\n"); printf("Example: httpget -v www.httpsvr.com myfile.txt\n"); printf("Example: httpget 128.200.34.45 headers.txt\n"); } int getsocket(int *sockn,BOOL verbose) { if(verbose) printf("Getting socket\n"); if ((*sockn = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { printf("Failed to get socket\n"); if(verbose) printf("Errorcode: %d\n", WSAGetLastError()); return -1; } return 0; } int opensocket(int sock, unsigned long server, short port,BOOL verbose) { struct sockaddr_in blah; memset((char *)&blah,'0', sizeof(blah)); blah.sin_family=AF_INET; blah.sin_addr.s_addr=server; blah.sin_port=htons(port); if(verbose) { printf("HTTPserver resolved to : %s\n",inet_ntoa(blah.sin_addr)); printf("Connecting....\n"); } if (connect(sock,(struct sockaddr *)&blah,sizeof(blah))==SOCKET_ERROR) { printf("Error:unable to connect(no http service ?)\n"); closesocket(sock); return(-4); } if(verbose) printf("Connected to [%s:%d].\n",inet_ntoa(blah.sin_addr),port); return 0; } int getresponse(int sockn,char *buff,int buffsize,BOOL verbose) { int i; i=recv(sockn,buff,buffsize,0); if(!i) { if(verbose) printf("\nServer closed connection\n"); return 0; } if(i==SOCKET_ERROR) { printf("\nSocket Error returned:%d\n",WSAGetLastError()); return 0; } buff[i]=0; printf("%s",buff); return 1; } int sendline(int sockn, char *buff) { int i; printf("%s",buff); i=send(sockn,buff,strlen(buff),0); if(i==SOCKET_ERROR) return -1; return 0; } int sendfile(int sockn, char *fname) { int i,nz; FILE *infile; nz=0; if ((infile=fopen(fname,"rt"))==NULL) { printf("Can't Open : %s\n",fname); return -1; } printf("Sending file\n"); while(!feof(infile)) { if((fgets(R,HTTPBUFFSIZE-2,infile))==NULL) break; i=sendline(sockn,R); if(i==-1) { fclose(infile); return -1; } if(R[0]=='\n') break; } if(R[0]!='\n') { nz=strlen(R); if(R[nz-1]!='\n') sendline(sockn,"\n\n"); else sendline(sockn,"\n"); } fclose(infile); return 0; } int main(int argc,char *argv[]) { char *errv,*arv,*opts; int argn,httpnum,oc,textnum,httpsock; BOOL verb,hp; unsigned long padr; if (argc < 2) { errv=argv[0]; arv=errv; while(*errv) { if(*errv=='\\') arv=errv+1; errv++; } printf("Usage: %s -[hv] \n",arv); exit(-1); } verb=FALSE; hp=FALSE; argn=1; httpnum=0; textnum=0; while(argn