Advertisement
Guest User

Untitled

a guest
Dec 20th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.29 KB | None | 0 0
  1. #include <errno.h>
  2. #include <string.h>
  3. #include <netdb.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7. #include <arpa/inet.h>
  8. #include <sys/types.h>
  9. #include <netinet/in.h>
  10. #include <sys/socket.h>
  11. #include <sys/stat.h>
  12. #include <sys/sendfile.h>
  13. #include <unistd.h>
  14.  
  15. void grumble(char* error) {
  16.   printf("%s", error);
  17.   exit(EXIT_FAILURE);
  18. }
  19.  
  20. int main(int argc, char * argv[]) {
  21.  
  22.     struct addrinfo * info;
  23.     struct addrinfo hints;
  24.     int sc, ai, sd, sca;
  25.     struct sockaddr_in server, client, peer;
  26.     char *cmd, *serveraddr, *user, *pwd, *str, *req, *res, * clientaddr;
  27.  
  28.     void * ptr;
  29.     printf("ftp> ");
  30.  
  31.     int bool_ = 1;
  32.  
  33. int port;
  34.     //char serveraddr[100];
  35.  
  36.     while(bool_) {
  37.  
  38.         cmd = malloc(sizeof(char)*10);
  39.         serveraddr = malloc(sizeof(char)*100);
  40.         clientaddr = malloc(sizeof(char)*100);
  41.        
  42.         scanf("%s %s", cmd, serveraddr);
  43.  
  44.         memset(&hints, 0, sizeof(struct addrinfo));
  45.         hints.ai_family = AF_INET;
  46.         hints.ai_socktype = SOCK_STREAM;
  47.         hints.ai_protocol = IPPROTO_TCP;
  48.  
  49.         req= malloc(sizeof(char)*100);
  50.         char res[1024];
  51.  
  52.  
  53.         if(strcmp(cmd, "open") == 0 || strcmp(cmd, "OPEN") == 0) {
  54.             if ((sc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
  55.                 grumble("socket");
  56.  
  57.             ai = getaddrinfo(serveraddr, NULL, &hints, &info);
  58.  
  59.             if(ai!=0)
  60.                 grumble("getaddrinfo\n");
  61.  
  62.  
  63.  
  64.             inet_ntop(info->ai_family, info->ai_addr->sa_data, serveraddr, 100);
  65.  
  66.             switch(info->ai_family) {
  67.                 case AF_INET:
  68.                     ptr = &((struct sockaddr_in *)info->ai_addr)->sin_addr;
  69.  
  70.                 //  ((struct sockaddr_in *)info->ai_addr)->sin_port ;
  71.                     //port = ((struct sockaddr_in *)info->ai_addr)->sin_port;
  72.                     break;
  73.                 case AF_INET6:
  74.                     ptr = &((struct sockaddr_in6 *)info->ai_addr)->sin6_addr;
  75.                 //  port = ((struct sockaddr_in *)info->ai_addr)->sin_port;
  76.  
  77.                     break;             
  78.             }
  79.  
  80.             inet_ntop(info->ai_family, ptr, serveraddr, 100);
  81.  
  82.            
  83.  
  84.             //printf("%s\n", serveraddr);
  85.  
  86.             server.sin_addr.s_addr = inet_addr(serveraddr);
  87.             server.sin_family = AF_INET;
  88.             server.sin_port =htons(21)
  89. ;
  90.            
  91.  
  92.  
  93.             if (connect(sc, (struct sockaddr *)&server, sizeof(server)) < 0) {
  94.                 grumble("Unable to connect\n");
  95.             }
  96.  
  97.         /*  if(bind(sc, (struct sockaddr *) &server, sizeof(server)) < 0) {
  98.                 grumble("bind");
  99.             }
  100.  
  101.  
  102.         //  printf("%d\n",port);
  103.  
  104.  
  105.         /*  while(read(sc, res, strlen(res))) {
  106.                 printf("%s\n", res);
  107.             }*/
  108.  
  109.             /*int n =0;
  110.             char * pt;
  111.  
  112.  
  113.             pt = res;
  114.  
  115.             n = recv(sc, res, strlen(res), 0);
  116.             if(n>0) {
  117.                 printf("%s\n", res);
  118.             }
  119.  
  120.             memset((char*)&res, 0, sizeof(res));
  121.             while((*pt++ = getchar()) != '\n');
  122.                 *pt = '\0';*/
  123.  
  124.             /*msg = ntohs(msg);
  125.             printf("%d\n", msg);*/
  126.  
  127.         //  recv(sc, res, 2000, 0);
  128.             //printf("%s\n", info->ai_addr);
  129.  
  130.             /*printf("Connected to %s\n", serveraddr);
  131.             //recup code serveur
  132.             printf("220--> Please login with user anonymous or ftp to access our archive <-\n");
  133.             printf("220\nUser : ");
  134.             //sscanf(str, "%s\n", user);*/
  135.  
  136.             int n=0;
  137.             char *com = malloc(50);
  138.  
  139.             read(sc, res, sizeof(res) - 1);
  140.             write(fileno(stdout), res, strlen(res)-1);
  141.  
  142.             if((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
  143.             {
  144.                grumble("socket");
  145.             }
  146.  
  147.             int len = sizeof(struct sockaddr_in);
  148.  
  149.             /*getsockname(sc, (struct sockaddr *) &server, &len);
  150.             inet_ntop(AF_INET, &client.sin_addr, clientaddr, sizeof(clientaddr));
  151.  
  152.  
  153.             client.sin_addr.s_addr = inet_addr(clientaddr);*/
  154.  
  155.             //client.sin_family = AF_INET;
  156.             client.sin_port =htons(12345);
  157.  
  158.             //printf("%s\n", inet_ntoa(client.sin_addr));
  159.  
  160.  
  161.             //printf("%s\n", serveraddr);
  162.             /* Bind a special Port */
  163.  
  164.            
  165.  
  166.             //getoscketname pour recup port
  167.  
  168.             getsockname(sc, (struct sockaddr *) &client, &len);
  169.             printf("%s\n", inet_ntoa(client.sin_addr));
  170.            
  171.            
  172.             if(bind(sd, (struct sockaddr *) &client, sizeof(client)) < 0) {
  173.                 grumble("bind");
  174.             }
  175.            
  176.             while (strncmp(req, "QUIT", 4) != 0) {
  177.  
  178.             /*scanf("%s", cmd);
  179.  
  180.  
  181.                 printf("%s\n", cmd);*/
  182.  
  183.                     memset(res, 0x00, strlen(res));
  184.                     memset(req, 0x00, strlen(req));
  185.  
  186.                     //write(fileno(stdout), "enter ftp cmd", 20);
  187.                     read(fileno(stdin), req, 1024);
  188.                     //printf("%s\n", req);
  189.                     send(sc, req, strlen(req),0);
  190.  
  191.                     /*if(strcmp(req, "port 12345\n")==0) {
  192.                         printf("bla\n");
  193.                         b=0;
  194.                         break;
  195.                     }*/
  196.  
  197.                     //memset(res, 0, 100);
  198.                     //memset(req, 0, 1024);
  199.  
  200.                     memset(res, 0x00, strlen(res));
  201.                     memset(req, 0x00, strlen(req));
  202.  
  203.                     read(sc, res, sizeof(res)-1);
  204.  
  205.                     write(fileno(stdout), res, strlen(res)-1);
  206.                     printf("\n");
  207.  
  208.                    
  209.  
  210.                     if(listen(sd,10) == -1)
  211.                     {
  212.                         grumble("listen");
  213.                     }
  214.  
  215.                     //printf("listen\n");
  216.  
  217.                     /*int ss;
  218.                     ss =  sizeof(struct sockaddr_in);*/
  219.  
  220.                     //accept
  221.  
  222.  
  223.  
  224.                     /*if(strcmp(req, "port 12345\n")==0) {
  225.                         if ((sca = accept(sd, (struct sockaddr *)&client, &ss)) == -1)
  226.                         {
  227.                             grumble("accept");
  228.                         }
  229.                     }*/
  230.                    
  231.  
  232.                     /*if ((sca = accept(sd, (struct sockaddr *)&peer, &ss)) == -1)
  233.                     {
  234.                         grumble("accept");
  235.                     }
  236.  
  237.                     int n=0;
  238.  
  239.                     while(n=recv(sca, res, strlen(res), 0)) {
  240.                         printf("bla\n");
  241.                     }*/
  242.                 //}
  243.                
  244.             }
  245.  
  246.            
  247.             /*int n1=0;
  248.  
  249.             printf("%s\n", inet_ntoa(peer.sin_addr));
  250.             while(n1=recv(sca, res, strlen(res), 0)) {
  251.                 //printf("bla\n");
  252.                     write(fileno(stdout), res, strlen(res)-1);
  253.                     //printf("res\n");
  254.             }*/
  255.  
  256.  
  257.  
  258.  
  259.             user = malloc(sizeof(char)*50);
  260.             scanf("%s",user);
  261.             if (write(sc, user, strlen(user)) == -1) {
  262.                 grumble("write");
  263.             }
  264.             /*printf("---> USER %s\n",user);
  265.             printf("331 Please specify the password.\n");
  266.             printf("Password : ");*/
  267.             //sscanf("%s\n", pwd);
  268.  
  269.             pwd = malloc(sizeof(char)*50);
  270.             scanf("%s",pwd);
  271.            
  272.             if (write(sc, pwd, strlen(pwd)) == -1) {
  273.                 grumble("write");
  274.             }
  275.             //printf("---> PASS \n");
  276.             //write*/
  277.  
  278.             if(strcmp(cmd, "exit") == 0 || strcmp(cmd, "EXIT") == 0) {
  279.                 bool_=0;
  280.                 printf("exit\n");
  281.             }
  282.  
  283.             if(strcmp(cmd, "ciao") == 0 || strcmp(cmd, "CIAO") == 0) {
  284.                 close(sc);
  285.                 printf("Connection closed\n");
  286.             }
  287.  
  288.             if(strcmp(cmd, "dir") == 0 || strcmp(cmd, "DIR") == 0) {
  289.                 strcpy(req, "dir ");
  290.                 strcat(req, ".");
  291.                 write(sc, req, strlen(req));
  292.                 printf("write %s\n", req);
  293.                 recv(sc, res, 2, 0);
  294.                 printf("recv %s\n", res);
  295.             }
  296.         }
  297.  
  298.     }
  299.     return 0;
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement