Here I am attaching the source code for FTP client, which does work exactly like wput ftp command:client.c File code# include "clientHeader.h"struct argDetails{ char userName [50]; char passWord [50]; char ipAddress [30]; char src [200]; char dst [200];};dataCon ();//const char * conData);void exitFun (void){ printf ("\n> IND Closing all the socket connection....."); if ((shutdown (cSock, SHUT_RD)) < 0) { printf ("\n> ERR Shutdown got failed...."); exit (1); } close (cSock);}int flag = 1;char buff [1024];pthread_t thread_data;char command [100];struct argDetails argDet;char pasvResp [100];int dSock = 0;main (int argc, char * argv []){ int count = 2; struct sockaddr_in ftpSer; pthread_t thread; if ((signal (SIGINT, exitFun)) < 0) { printf ("\n> ERR Signal.... "); exit (1); } if (argc == 3) { memset (&argDet, 0, sizeof (argDet)); printf ("\n> IND Checking arguments...."); strcpy (argDet.src, argv [2]); // strcpy (argDet.dst, argv [3]); // printf ("Source: %s\n", argDet.src);// printf ("Destination: %s\n", argDet.dst); checkArg (argv [1]);// printf ("I am here"); } else { printf ("\n> ERR Please pass valid arguments....."); exit (1); } // printf ("I am here"); if ((cSock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 1) { printf ("\n> ERR Socket System call....."); exit (1); }/* //-------------------------------Semaphore--------------------------------------- if ((semget (SEMKEY, 2, IPC_CREAT | SEMPERM)) < 0) { printf ("\n%s\n", strerror (errno)); exit (1); }*/ //------------------------------------------------------------------------------- // pthread_create(&thread, NULL, my_recv, NULL); //--------------Connect system call -------------------------------- memset (&ftpSer, 0, sizeof (ftpSer)); ftpSer.sin_family = AF_INET; ftpSer.sin_port = htons (21); // printf ("Connect IP address: %s ", argDet.ipAddress); ftpSer.sin_addr.s_addr = inet_addr (argDet.ipAddress);//("192.168.100.27"); //("64.177.239.154"); if ((connect (cSock, (struct sockaddr *) &ftpSer, sizeof (struct sockaddr))) < 0) { printf ("\n> ERR Connect system call......"); perror (" "); exit (1); } printf ("\n> IND Control Connection got established"); //------------------------------------------------------------------- { char buff [1024]; memset (buff, 0, 1024); if ((recv (cSock, buff, 1024, 0)) < 0) { printf ("\n> ERR Recv system call got failed."); perror (" "); printf ("\n%s\n\n", strerror (errno)); exit (1); } printf ("\n> RES %s", buff); } //------------------------------------------------------------------- printf ("\n>"); // while (1) { char tep [200];// getchar (); sprintf (tep, "USER %s\n", argDet.userName); printf ("===> %s", tep); cmdTrans (tep); // getchar (); sprintf (tep, "PASS %s\n", argDet.passWord); printf ("===> %s", tep); cmdTrans (tep);// getchar (); sprintf (tep, "PWD\n"); printf ("===> %s", tep); cmdTrans (tep); // getchar (); sprintf (tep, "CWD %s\n", argDet.dst); printf ("===> %s", tep); cmdTrans (tep); // getchar (); sprintf (tep, "PASV\n"); printf ("===> %s", tep); cmdTrans (tep); // getchar (); sprintf (tep, "STOR %s\n", argDet.src); printf ("===> %s", tep); cmdTrans (tep); pthread_join (thread_data, NULL);// getchar (); sprintf (tep, "QUIT\n"); printf ("===> %s", tep); cmdTrans (tep); /* int len = 0; if ((memset (command, 0, 100)) < 0) { printf ("\nError: %s\n", strerror (errno)); exit (1); } if((fgets(command, 100, stdin)) < 0) { printf ("\nError: %s\n", strerror (errno)); exit (1); } if ((send (cSock, command, strlen (command), 0)) < 0) { printf ("\nSend system call got failed\n"); exit (1); } printf (">\n"); { memset (buff, 0, 1024); if ((recv (cSock, buff, 1024, 0)) < 0) { printf ("\nRecv system call got failed\n"); perror (" "); printf ("\n%s\n\n", strerror (errno)); exit (1); } printf ("%s\n>", buff); } printf ("\nFlag: %d\n", flag); if (((strcmp ("pasv\n", command)) == 0) | ((strcmp("PASV\n", command) == 0))) { if (flag) { flag = 0; printf ("\nExecuting PASV command\n"); pthread_create(&thread_data, NULL, dataCon, NULL); } } */ // pthread_join (thread, NULL); close (cSock); }}cmdTrans (char * command1){ { int len = 0; // printf ("Command : %s", command1); // len = strlen (command); // command [len] = '\n'; //------------------------For sending STOR command ---- //Data connection is not established imediately after the PASV //command, instead IP address and Port numbers are saved, and after// sending STOR command, data connection is established. if (!(((strstr (command1, "stor")) != NULL) | ((strstr (command1, "STOR") != NULL)))) { if ((send (cSock, command1, strlen (command1), 0)) < 0) { printf ("\n> ERR Send system call got failed...."); exit (1); } // printf (">\n"); { memset (buff, 0, 1024); if ((recv (cSock, buff, 1024, 0)) < 0) { printf ("\n> ERR Recv system call got failed...."); perror (" "); printf ("\n%s\n\n", strerror (errno)); exit (1); } printf ("\n> %s", buff); if (strstr (buff, "Please login") != NULL) { printf ("\n> ERR Anonymous access is restricted....."); exit (1); } } } else { printf ("\n> IND Creating Data Connection....."); strcpy (command, command1); //Thread to create Data connection pthread_create(&thread_data, NULL, dataCon, NULL); return 0; } if (((strstr (command1, "pasv")) != NULL) | ((strstr (command1, "PASV") != NULL))) { { printf ("\n> IND Executing PASV command....."); // Save the IP address and port numbers strcpy (pasvResp, buff); // pthread_create(&thread_data, NULL, dataCon, NULL); return 0; } }/* if (((strstr (command, "stor")) != NULL) | ((strstr (command, "STOR") != NULL))) { printf ("\nExecuting STOR command\n"); pthread_create(&thread_data, NULL, dataCon, NULL); return 0; }*/ if (((strstr (command1, "quit")) != NULL) | ((strstr (command1, "QUIT") != NULL))) { printf ("\n> IND Program termination...."); close (cSock); exit (0); } return 0; }}dataCon ()//const char * conData){ char temp [60]; char IP [40]; char * tempIp; char ip1 [4], ip2 [4], ip3 [4], ip4 [4], * cpt1, * cpt2; int ipt1 = 0, ipt2 = 0; int port; struct sockaddr_in ftpDataSer; char conData [100]; strcpy (conData, pasvResp);// printf ("%s\n\n", conData);// printf ("\nI am here\n"); // printf ("%s\n\n", conData); memset (temp, 0, 40); strtok (conData, "(");// printf ("\n%s", temp); strcpy (temp, strtok (NULL, ")"));// printf ("\n%s", temp); memset (ip1, 0, 4); strcpy (ip1, strtok (temp, ",")); memset (ip2, 0, 4); strcpy (ip2, strtok (NULL, ",")); memset (ip3, 0, 4); strcpy (ip3, strtok (NULL, ",")); memset (ip4, 0, 4); strcpy (ip4, strtok (NULL, ",")); memset (IP, 0, 20); strcat (IP, ip1); strcat (IP, "."); strcat (IP, ip2); strcat (IP, "."); strcat (IP, ip3); strcat (IP, "."); strcat (IP, ip4); // sprintf (IP, "%S.%S.%S.%S", ip1, ip2, ip3, ip4); // printf ("\n\nIP: %s\n", IP); cpt1 = strtok (NULL, ",");ipt1 = atoi (cpt1); cpt2 = strtok (NULL, ",");ipt2 = atoi (cpt2); port = (ipt1 * 256) + ipt2; // printf ("\n\nPort : %d\n", port); if ((dSock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { printf ("\n> ERR: Data Connection SOCK......."); exit (1); } //--------------Connect system call -------------------------------- memset (&ftpDataSer, 0, sizeof (ftpDataSer)); ftpDataSer.sin_family = AF_INET; ftpDataSer.sin_port = htons (port); ftpDataSer.sin_addr.s_addr = inet_addr (IP); if ((connect (dSock, (struct sockaddr *) &ftpDataSer, sizeof (struct sockaddr))) < 0) { printf ("\n> ERR Connect system call........"); perror (" "); exit (1); } printf ("\n> IND Data Connection got established....."); //------------------------------------------------------------------- if (((strstr (command, "STOR")) != NULL) | ((strstr(command, "stor")) != NULL)) { if ((send (cSock, command, strlen (command), 0)) < 0) { printf ("\n> ERR Send system call got failed......"); exit (1); } { memset (buff, 0, 1024); if ((recv (cSock, buff, 1024, 0)) < 0) { printf ("\n> ERR Recv system call got failed....."); perror (" "); printf ("\n%s\n\n", strerror (errno)); exit (1); } printf ("\n> RES %s", buff); } int fd; if ((fd = open (argDet.src, O_RDONLY)) == NULL) { printf ("\n> ERR Could not open file for transfer......"); exit (1); } while (1) { char buffer [512]; int count = 0; memset (buffer, 0, 512); count = read (fd, buffer, 512); if (count <= 0) break; else { if (send (dSock, buffer, count, 0) < 0) { printf ("\n> ERR Send command in the file transfer......"); exit (1); } } } } else { char buff [1024]; { memset (buff, 0, 1024); if ((recv (dSock, buff, 1024, 0)) < 0) { printf ("\n> ERR Recv system call got failed....."); perror (" "); printf ("\n%s\n\n", strerror (errno)); exit (1); } printf ("\n> RESP Data Connection :%s", buff); close (dSock); exit (0); } } exit (0);}checkArg (char * argv)//, struct argDetails * argDet){ char temp [100], * token, * temp1; int count = 3; temp1 = (char *) malloc (100); strcpy (temp1, argv); printf ("ip: %s\n\n", temp1); if (strstr (temp1, "@") == NULL) { printf ("\n> IND Anonymous...........\n"); strcpy (argDet.ipAddress, strtok (temp1, "/"));// printf ("\nIP address: %s", argDet.ipAddress); strcpy (argDet.dst, strtok (NULL, "\0\n"));// printf ("\nDestination: %s", argDet.dst); // strcpy (argDet.ipAddress, temp1); strcpy (argDet.userName, "anonymous"); strcpy (argDet.passWord, "pass");// printf ("\nUSER: %s\n", argDet.userName);// printf ("PASS: %s\n", argDet.passWord); } else { if ((strcpy (argDet.userName, strtok (temp1, ":."))) != NULL) { if ((strcpy (argDet.passWord, strtok (NULL, "@"))) != NULL) { if ((strcpy (argDet.ipAddress, strtok (NULL, "/"))) == NULL) { printf ("\n> ERR Please check the UserName or passWord or IP Address..."); exit (1); } else { if ((strcpy (argDet.dst, strtok (NULL, " \t"))) == NULL) { printf ("\n> ERR Please check the UserName or passWord or IP Address...."); exit (1); } } } else { printf ("\n> ERR Please check the UserName or passWord...."); exit (1); } } else { printf ("\n> ERR Please check the UserName or passWord...."); exit (1); } } // printf ("\nUSER: %s\n", argDet.userName);// printf ("PASS: %s\n", argDet.passWord);// printf ("Source: %s\n", argDet.src);// printf ("Destination: %s\n", argDet.dst); //-----------------Check that IP address----------------// printf (" Check IP Address: %s\n\n", argDet.ipAddress); memset (temp, 0, 100); strcpy (temp, argDet.ipAddress); if ((strtok (temp, ".")) == NULL) { printf ("\n> ERR Please check the IP address format...."); exit (1); } while (count --) { if ((strtok (NULL, ".")) == NULL) { printf ("\n> ERR Please check the IP address format...."); exit (1); } } //----------------------------------------------------- // printf ("\n\nPerfect....\n");}/*int my_recv (void){ char buff [1024]; while (1) { memset (buff, 0, 1024); if ((recv (cSock, buff, 1024, 0)) < 0) { printf ("\nRecv system call got failed\n"); perror (" "); printf ("\n%s\n\n", strerror (errno)); exit (1); } printf ("%s\n", buff); if (((strcmp ("pasv\n", command)) == 0) | ((strcmp("PASV\n", command) == 0))) { printf ("\nExecuting PASV command\n"); pthread_create(&thread_data, NULL, dataCon, buff); // dataCon (buff); } }}*/+++++++++++++++++++++++++++++++++++++++clientHeader.h File code# include <sys/types.h># include <sys/stat.h># include <fcntl.h>#include <sys/types.h>#include <sys/socket.h>#include <netdb.h>#include <netinet/in.h>#include <stdio.h># include <errno.h>extern int errno;# include <string.h># include <sys/ipc.h># include <sys/sem.h># include <sys/types.h># define SEMKEY (ket_t)333# define SEMPERM 0666# include <pthread.h>struct sembuf sem_lock [2] = { 0, 0, SEM_UNDO, 0, 1, SEM_UNDO};struct sembuf sem_unlock [1] = { 0, -1, SEM_UNDO};int my_recv (void);int cSock;# include <signal.h>++++++++++++++++++++++++++++++++++++++++Client program compilation and execution:history command output from the linux system:891 vim client.c 892 vim client.c 893 gcc client.c -lpthread -o myput 894 ./myput 192.168.100.27 test.txt dayanand/ftpClient/test 895 vim client.c 896 gcc client.c -lpthread -o myput 897 ./myput 192.168.100.27 test.txt dayanand/ftpClient/test 898 vim client.c 899 clear 900 ls -l 901 vim client.c 902 ./myput 192.168.100.27 test.txt dayanand/ftpClient/test 903 gcc client.c -lpthread -o myput 904 ./myput 192.168.100.27 test.txt dayanand/ftpClient/test 905 clear 906 vim client.c 907 vim client.c 908 vim client.c 909 vim client.c 910 gcc client.c -lpthread -o myput 911 clear 912 ./myput 192.168.100.27 test.txt dayanand/ftpClient/test 913 clear 914 gcc client.c -lpthread -o myput 915 ./myput sut.happynewyear192.168.100.27 test.txt dayanand/ftpClient/testclear 916 clear 917 clear 918 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 919 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 920 clear 921 vim client.c 922 gcc client.c -lpthread -o myput 923 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 924 vim client.c 925 gcc client.c -lpthread -o myput 926 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 927 vim client.c 928 gcc client.c -lpthread -o myput 929 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 930 vim client.c 931 gcc client.c -lpthread -o myput 932 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 933 vim client.c 934 gcc client.c -lpthread -o myput 935 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 936 cleaer 937 clear 938 vim client.c 939 gcc client.c -lpthread -o myput 940 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 941 vim client.c 942 gcc client.c -lpthread -o myput 943 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 944 vim client.c 945 gcc client.c -lpthread -o myput 946 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 947 vim client.c 948 gcc client.c -lpthread -o myput 949 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 950 vim client.c 951 vim client.c 952 gcc client.c -lpthread -o myput 953 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 954 vim client.c 955 kill -9 -1 956 cd dayanand/ftpClient/ 957 clear 958 ls -l 959 vim client.c 960 gcc client.c -lpthread -o myput 961 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 962 vim client.c 963 gcc client.c -lpthread -o myput 964 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 965 vim client.c 966 gcc client.c -lpthread -o myput 967 ./myput sut.happynewyear2006@192.168.100.27 test.txt dayanand/ftpClient/test 968 vim client.c 969 gcc client.c -lpthread -o myput 970 ./myput sut.happynewyear2006@192.168.100.27 test.txt dayanand/ftpClient/test 971 vim client.c 972 gcc client.c -lpthread -o myput 973 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 974 gcc -g client.c -lpthread -o myput 975 gdb ./myput 976 clear 977 gcc client.c -lpthread -o myput 978 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 979 clear 980 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 981 clear 982 vim client.c 983 gcc client.c -lpthread -o myput 984 make clear 985 clear 986 vim Makefile 987 mmake clear 988 make clear 989 clear 990 ls -l 991 gcc client.c -lpthread -o myput 992 ./myput sut.happynewyear2006@192.169.100.27/datanand/ftpClient/test test.txt 993 clear 994 vim Makefile 995 vim client.c 996 gcc -g client.c -lpthread -o myput 997 gdb ./myput 998 clear 999 kill -9 -1 1000 ftp 192.168.100.27 1001 clear 1002 cd test 1003 clear 1004 ls -l 1005 cat test.txt 1006 ftp 192.168.100.27 1007 clear 1008 cat test.txt 1009 clear 1010 history 1011 history > history.txt++++++++++++++++++++++Makefile file codeclient: client.o cc -o client client.oclient.o: client.c cc -c client.cclear: rm -v -f -r client *.o a.out myput
|