Hi guys, I really have been trying to fix this one myself but I am just learning C so I my skill limits me, naturally.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define BUFFER 20
int i;
int iPort;
void display_usage(void);
char *target_ip,the_buffer[BUFFER],return_data[BUFFER];
char input_Command[BUFFER];
char jumble_data(char temp_data);
int main( int argc, char *argv[] ) {
if( argc < 2 )
{
display_usage();
exit(1);
}
target_ip = argv[1]; // TARGETIP had to be a pointer
iPort = atoi(argv[2]);
// Prompt user for command
printf("command>");
if ( fgets(input_Command, sizeof input_Command, stdin) )
{
char *newline = strchr(input_Command, '\n');
if ( newline )
{
*newline = '\0'; /* strip trailing \n */
}
}
// send input to jumble_data
*input_Command = jumble_data(input_Command);
//setup sockets
int testsocket;
struct sockaddr_in thetarget;
testsocket = socket(AF_INET, SOCK_STREAM, 0);
thetarget.sin_family = AF_INET; //always AF_INET
thetarget.sin_port = htons(iPort);
thetarget.sin_addr.s_addr = inet_addr(target_ip); // TARGETIP is argv[1]
connect(testsocket, (struct sockaddr *)&thetarget, sizeof(struct sockaddr)); // make the connection
send(testsocket, input_Command, strlen(input_Command), 0); // send the data
/*while (1) {
recv(testsocket, return_data, strlen(the_buffer), 0);
printf("%s\n", return_data);
}*/
perror("connect");
return(0);
}
void display_usage(void)
{
fprintf(stderr, "Usage: ./connect 127.0.0.1 9999\n" );
}
/* Fucntion Summary:
Take in user supplied command and inserts 4 random characters inbetween each character of the origianl command
*/
char jumble_data(char temp_Data) /* take in input_Command and put it in temp_Data */
{
int i, k, j;
char jumbled_data[sizeof temp_Data * 4]; /* multiply temp_Data's array size by 4 to hold random chars */
for ( i = 0, k = 0; temp_Data[i] != '\0'; ++i ) /* until temp_data[i] reaches newline char, increment and loop */
{
jumbled_data[k++] = temp_Data[i]; /* assign temp_Data[i] to jumbled_Data[k] then increment (jumbled_Data[k++]) */
/* At this point if the user submitted command would have been "destroy", jumbled_Data[0] would contain the "d" and jumbled_D
ta[k] == jumbled_Data[1]. */
for ( j = 0; j < 4; ++j ) /* until j == 4, increment */
{
jumbled_data[k++] = rand() % 26 + 'a'; /* fill next 4 char cells with a random character */
}
/* Now restart the loop */
}
return(jumbled_data[BUFFER]);
}
Please ignore my horrible style.