/* CSC112 Final Assignment Implementing DFSP: "Distributed File Synchronization Protocol" This Assignment is to be completed by groups of AT MOST THREE. This assignment asks you to use file descriptors and TCP sockets to implement a distributed file synchronization ultility. The motivation is, let's say you have two copies of a file you're editing on two computers. From time to time you will edit one version and forget to make the same changes to the other copy. You want a program by which you can connect to the other computer and synchronize the two files. The program should determine which version of the file is more recent (via the moddate function I've provided you below), and make sure that both computers hold the latest version. Note: this assignment is not meant to be difficult. You do not have to use either multiple threads, pipes, or SelectRFD, although you are welcome to implement a multi-threaded solution for extra credit. Following the examples I gave you, your are to write two programs, a client and a server. The protocol is specified by the following: 0. After establishing connection, the client and server must complete a secret "handshake". You don't want other people to use this program to mess up or steal your files! Here's an example of a handshake: server sends the client some random n. Client, upon receiving n, sends back another number m. Server checks that n+m=137. If not, server breaks connection. You are to design your own handshake (don't use mine, and don't underestimate its importance). 1. After the authentication handshake, the client sends the server a 0-terminated string of the file name to be synchronized. 2. Server sends back a long (size is sizeof(long)) indicating the time of last modification of the file on the server. If file doesn't exist, server sends back -1 and session terminates. 3. Client sends a long indicating the time of last modification of it's version of the file. 4. At this point, both client and server should know who has the latest version (in case of tie, server is assumed to have latest file). Both sides will know if it should be sending or receiving the file. Side with the latest version will send the contents of the file. The receiving side needs to receive the contents and write it to its local disk. Refer to the file copy program I have on the webpage for help on how to open, read, and write to disk files. 5. (I've not yet decided whether to make this part optional): There's a flaw in the above protocol: what if one of the computers have the wrong time? We will assume that the server has the correct time. Modify the above protocol so that before step 1, the client should send the server it's current system time (via the currenttime function I gave you below). The server compares the client's system time with its own system time. If the times are significantly different (say more than a one minute difference), then the server should adjust the time of modification of the client's version of the file accordingly. For this step to work correctly you must also change step 4, because the client may wrongly assume that it has the later (or earlier) file. It's the server who must determine who really has the latest file. The server needs to inform the client whether it should send or receive. Don't forget to compile with gcc -lnsl -lsocket on Solaris (but not Linux). *** Additional Hints *** To find your system's IP address: /sbin/ifconfig -a (under hme0 heading) To view current network activity: netstat | more To monitor TCP activity: netstat -n -P tcp to read/write a long: long x; x = (long)readInt(fd); writeInt(fd,(int)x); (If you use read(fd,&x,sizeof(long)), your program won't work between a Linux machine and a Sparc machine.) */ #include "simpleipc.c" #include "simpletcp.c" #include /* use this for the server port: */ #define DFSPORT 30003 // this function returns the time when a file was last modified. // it returns -1 if on error (like when file doesn't exit). // The time value is the number of seconds since January 1st, 1970 long moddate(char * A) { int i; struct stat st; i = stat(A,&st); if (i<0) return -1; return (long) st.st_mtime; } // this function returns the current system time: long currenttime() { struct timeval tv; gettimeofday(&tv,NULL); return (long) tv.tv_sec; } int main() // illustrationg of use { printf("%d\n%d\n%d\n",moddate("simpletcp.c"),currenttime(),sizeof(long)); exit(0); }