By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,277 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,277 IT Pros & Developers. It's quick & easy.

server client application troubles

P: 1
Hello,

I have developed a client server application using C.

i have a list of usernames and a list of passwords in a text file. i need to be able to do 2 loops so i can send the usernames and the passwords to the server and get a response back. basically its a ftp force attack to find the correct username and password. i have written most of the code but i'm kind of strugling to finish it off and sort out the errors. this is a task we have to do for university. i have tried different books and websites and still can't sort out the errors. i really need your help.

Any advice would be most helpfull

this is the client part
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>                                                  /* for printf() and fprintf() */
  2. #include <sys/socket.h>                                             /* for socket(), connect(), send(), and recv() */
  3. #include <arpa/inet.h>                                              /* for sockaddr_in and inet_addr() */
  4. #include <stdlib.h>                                                 /* for atoi() and exit() */
  5. #include <string.h>                                                 /* for memset() */
  6. #include <unistd.h>                                                 /* for close() */
  7.  
  8. #define BUFSIZE 4096                                                   /* Size of receive buffer */
  9.  
  10. void DieWithError(char *errorMessage);                              /* Error handling function */
  11. void handeltcpclient(int sock, char *argv[]);                       /* Server handling function */
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.     int sock;                                                        /* Socket descriptor */
  16.     struct sockaddr_in echoServAddr;                                  /* Echo server address */
  17.     unsigned short echoServPort;                                      /* Echo server port */
  18.     char *servIP;                                                     /* Server IP address (dotted quad) */
  19.  
  20.     if ((argc < 2) || (argc > 3))                                     /* Test for correct number of arguments */
  21.     {
  22.        fprintf(stderr, "Usage: %s <Server IP> [<Port>]\n",argv[0]);
  23.        exit(1);
  24.     }
  25.  
  26.     servIP = argv[1];                                              /* First arg: server IP address (dotted quad) */
  27.  
  28.     if (argc == 3)
  29.         echoServPort = atoi(argv[2]);                             /* Use given port, if any */
  30.     else
  31.         echoServPort = 21;                                         /* 7 is the well-known port for the echo service */
  32.  
  33.     /* Create a reliable, stream socket using TCP */
  34.     if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  35.         DieWithError("socket() failed");
  36.  
  37.     /* Construct the server address structure */
  38.     memset(&echoServAddr, 0, sizeof(echoServAddr));               /* Zero out structure */
  39.     echoServAddr.sin_family      = AF_INET;                       /* Internet address family */
  40.     echoServAddr.sin_addr.s_addr = inet_addr(servIP);             /* Server IP address */
  41.     echoServAddr.sin_port        = htons(echoServPort);           /* Server port */
  42.  
  43.     /* Establish the connection to the echo server */
  44.     if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
  45.         DieWithError("connect() failed");
  46.  
  47.     handeltcpclient(sock, argv);
  48.  
  49.     close(sock);
  50. }
  51.  
this is the handeltcpclient.c
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>                              /* for printf() and fprintf() */
  2. #include <string.h>                              /* for printf() and fprintf() */
  3. #include <sys/socket.h>                         /* for recv() and send() */
  4. #include <unistd.h>                             /* for close() */
  5.  
  6. #define BUFSIZE 4096                            /* Size of receive buffer */
  7.  
  8. void DieWithError(char *errorMessage);          /* Error handling function */
  9. char sendBuf[BUFSIZE];                          /* Send Buffer */
  10. char rcvBuf[BUFSIZE];                       
  11. char rcvMsgSize[BUFSIZE];
  12. char srvBuf[BUFSIZE];
  13. char clntBuf[BUFSIZE];            
  14. char USER[15];
  15. char PASS[15];
  16. FILE *fileuser;
  17. FILE *filepassword;
  18. int MsgSize;
  19. int result;
  20. char response[3];
  21.  
  22. void handeltcpclient(int clntSocket)
  23. {
  24.  
  25.  
  26.                     /* Recieve message from client */
  27.             memset(&rcvBuf,0,sizeof(rcvBuf));
  28.             rcvMsgSize = RecieveMessage(clntSocket, &rcvBuf, sizeof(rcvBuf));
  29.  
  30.                     /* Recieve Server Responses */
  31.             memset (&clntBuf,0,sizeof(clntBuf));
  32.             memset (&srvBuf,0,sizeof(srvBuf));
  33.             MsgSize = RecieveMessage(clntSocket,&srvBuf, sizeof(srvBuf));
  34.  
  35.             printf("Recieved: %s" &srvBuf,rcvMsgSize); 
  36.  
  37.            response = strncpy(&srvBuf, 3);
  38.          result = strcmp( result, "230");
  39. if( result == 0)
  40.         {
  41. sprintf(&clntBuf, "USER %s", USER);
  42. send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
  43.      DieWithError("send() sent a different number of bytes than expected");
  44.  
  45.  
  46. printf("Recieved: %s" &srvBuf,messageSize); 
  47.  
  48.  
  49.  /* Opening username.txt to selecting USER */
  50.  
  51. if (fileuser = (fopen ("usernames.txt", "r")) ==NULL)
  52.     {
  53.     printf("Error opening file.\n");
  54.     return 1;
  55.     }
  56.  
  57. else     {
  58.     printf ("File opened.\n");
  59.     while (fgets(USER, 15, file != NULL))
  60.         {
  61.         printf("%s",USER);
  62.         }
  63.  
  64.      /*send USER to server*/
  65.     sprintf(clntBuf, "USER %s", USER);
  66.  
  67.  if (    send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf)))
  68.      DieWithError("send() sent a different number of bytes than expected");
  69.  
  70. printf("The attemped username was: %s\n");
  71. return 0;
  72.  
  73. /* Recieve message from client */
  74.     memset(&rcvBuf,0,sizeof(rcvBuf));
  75.     rcvMsgSize = RecieveMessage(clntSocket, &rcvBuf, sizeof(rcvBuf));
  76.  
  77.    response = strncpy(&srvBuf, 3);
  78.    result = strcmp( result, "331");
  79. if( result == 0)
  80.         {
  81. sprintf(&clntBuf, "PASS %s", PASS);
  82. send(clntSocket, &clntBuf, strlen(clntBuf), 0)!= (strlen(clntBuf));
  83.      DieWithError("send() sent a different number of bytes than expected");
  84.  
  85. result = strncpy(&srvBuf, 3);
  86. result = strcmp(result, "230");
  87.  
  88. if (result == 0)
  89.         {
  90. printf("successfully connected.\n");
  91.         {
  92. sucess ++;
  93. }
  94.  
  95.  
  96. /* Opening passwords.txt to selecting PASS */
  97.     (
  98. if (filepassword = (fopen ("passwords.txt", "r"))==NULL)
  99.     {
  100.     printf("Error opening file.\n");
  101.     return 1;
  102.     }
  103.     (
  104. else     {
  105.     printf ("File opened.\n");
  106.     while (fgets(PASS, 15,file)!=NULL)
  107.         {
  108.         printf("%s",PASS);
  109.         }
  110.     }
  111.     /*send PASS to server*/
  112.  sprintf(clntBuf, "PASS %s", PASS);
  113.  
  114.  if (    send(clntSocket, &clntBuf, strlen(clntBuf), 0) != (strlen(clntBuf)))
  115.      DieWithError("send() sent a different number of bytes than expected");
  116.  
  117. printf("Closing file.\n");
  118. fclose(file);
  119. printf("The attempted password was: %s\n");
  120. return 0;
  121.  
  122. }
  123.  
  124. int RecieveMessage (int s,char *buf, int maxLen)
  125.         {    
  126.         int received = 0;
  127.         int rv = 0;
  128.         rv = recv(s, buf+received, 5, 0);
  129.         while ((received < maxLen) && (rv > 0) && *(buf+received) != '\n')
  130.         {
  131.                     received += rv;
  132.                     rv = recv(s, buf+received, 5, 0);
  133.         }
  134.         if (rv < 0)
  135.         {
  136.                     DieWithError("revc() failed");
  137.         }
  138.         return received;
  139.         }
  140.  
  141. /*end*/
this is the diewitherror.c (error handling file)
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>  /* for perror() */
  2. #include <stdlib.h> /* for exit() */
  3.  
  4. void DieWithError(char *errorMessage)
  5. {
  6.     perror(errorMessage);
  7.     exit(1);
  8. }
Mar 1 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.