char *getNextSteppingStone() { FILE *ifp = fopen("chainlist.txt", "r"); if (ifp == NULL) { perror("Can't open file chainlist.txt"); exit(-1); } char c = NULL; int num_ss; c = fgetc(ifp); num_ss = c - '0'; fclose(ifp); if (num_ss == 1) return NULL; num_ss--; removeCurrentHost(num_ss); return getRandomSS(); }
int main (int argc, char **argv) { struct chainData cd; char urlValue[255] = ""; char *argValue = NULL; char *SSaddr; int SSport; char chainfileName[255] = "./chaingang.txt"; int c; int i; int chosenLinkNum; int iMaxPortNumber = 65535; opterr = 0; srand(time(NULL)); while ((c = getopt (argc, argv, "c:")) != -1) { argValue = optarg; switch (c) { case 'c': strcpy(chainfileName,argValue); break; default: fprintf (stderr, "Unknown command line option: -%c\n", optopt); printUsage (); exit (1); } } for (i=optind; i < argc; i++) strcpy(urlValue,argv[i]); if (urlValue == "") { fprintf (stderr, "Command line argument '<URL>' is required.\n"); printUsage (); exit (1); } FILE *fd = fopen(chainfileName,"r"); if (fd == NULL) { fprintf (stderr, "Error opening chainfile specified ('%s').\n",chainfileName); exit (1); } if ((i = sanityCheckFile(chainfileName)) != 0) { fprintf (stderr, "Error in line number %d of Chainfile specified ('%s').\n",i,chainfileName); exit (1); } if (readChainFile(fd,&cd)) { fclose(fd); fprintf (stderr, "Error reading chainfile ('%s').\n",chainfileName); exit (1); } fclose(fd); dbgPrintChainData(&cd); printf("\n"); for (i=0; i< 10; i++) { getRandomSS(&cd,&SSaddr,&SSport,&chosenLinkNum); printf("%s,%d,%d\n",SSaddr,SSport,chosenLinkNum); } sendURLandChainData (&cd, urlValue); // if (goGetFile(urlValue)) { // DieWithError("Error retreiving URL.\n"); // } return 0; }
void sendURLandChainData (struct chainData *cd, char *urlValue) { int sock; /* Socket descriptor */ struct sockaddr_in myServAddr; /* server address */ unsigned short myServPort; /* server port */ char rcvBuffer; /* Buffer for reply string */ int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv() and total bytes read */ struct hostent *hostentStruct; /* helper struct for gethostbyname() */ char *serverName; char *SSaddr; int SSport,portNumber; int i; char *buf; int chosenLinkNum; int ipAddressEntered = 1; /* flag to indicate an IP address was entered as -s arg */ struct chainData cdSend; getRandomSS(cd,&SSaddr,&SSport,&chosenLinkNum); /* if all digits and periods, then IP address was entered */ for (i = 0; i < strlen (SSaddr); i++) { if (SSaddr[i] != '.') { if (isdigit (SSaddr[i]) == 0) { ipAddressEntered = 0; } } } serverName = SSaddr; portNumber = SSport; /* Create a stream socket using TCP */ if ((sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError ("socket() failed"); /* establish 3 seconds as the timeout value */ struct timeval tv; tv.tv_sec = 3; tv.tv_usec = 0; if (setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof tv)) DieWithError ("setsockopt() failed"); /* Construct the server address structure */ memset (&myServAddr, 0, sizeof (myServAddr)); /* Zero out structure */ myServAddr.sin_family = AF_INET; /* Internet address family */ if (ipAddressEntered) { /* load the address */ myServAddr.sin_addr.s_addr = inet_addr (serverName); } else { /* if a hostname was entered, we need to look for IP address */ hostentStruct = gethostbyname (serverName); if (hostentStruct == NULL) DieWithError ("gethostbyname() failed"); /* get the correct "format" of the IP address */ serverName = inet_ntoa (*(struct in_addr *) (hostentStruct->h_addr_list[0])); if (serverName == NULL) { fprintf (stderr, "inet_ntoa() failed.\n"); exit (1); } /* load the address */ memcpy (&myServAddr.sin_addr, hostentStruct->h_addr_list[0], hostentStruct->h_length); } /* load the port */ myServAddr.sin_port = htons (portNumber); /* Server port */ /* Establish the connection to the server */ if (connect (sock, (struct sockaddr *) &myServAddr, sizeof (myServAddr)) < 0) DieWithError ("connect() failed"); /* Send the struct to the server */ chainDataAndURLToString(cd,urlValue,&buf); if (send (sock, buf, strlen(buf), 0) != strlen(buf)) DieWithError ("send() sent a different number of bytes than expected"); /* let everyone know the URL was sent */ fprintf (stdout, "send buffer size = %d\n", strlen(buf)); fprintf (stdout, "Sent chainfile contents to server %s:%d via TCP\n", serverName, portNumber); fprintf (stdout, "(sent)*****\n%s\n*****\n", buf); free(buf); /* sent the bytes to server */ /* so close things up */ if (close (sock) != 0) DieWithError ("close() failed"); fprintf (stderr, "Send was a Success - exiting.\n"); exit (0); }