//**************************************************************** //** ** //** PHilip RUshik. 2010,2011,2012,2013 ** //** Distributed under the terms of the Beerware License ** //** Philip Rushik wrote this code. As long as you retain ** //** this notice, you can do whatever you want with this ** //** stuff. If we meet someday, and you think this stuff ** //** is worth it, you can buy me a beer in return. ** //** --Philip Rushik ** //** ** //** TFTP Server ** //** ** //** Serves files via the TFTP protocol. ** //** ** //**************************************************************** #include #include #include #include #include #include #include #define true 0x14 #define false 0 #define TITLE "RFC1350 Compliant TFTP Server" #define VER "1-rel" //---------------------------------------------------------------- // -- // Version numbering system explanation: -- // Version numbers are in the form n-**** where n is the major -- // version number (Major version numbers begin at 0) and **** is-- // a 2-4 letter abbreviation indicating the development stage. -- // Possible abbreviations are: -- // dev - Development Stage -- // test - Pre-release Testing Stage -- // demo - Demonstration Build -- // beta - Beta Release Stage -- // rc - Release Candidate -- // rel - Public Release -- // -- // Major number increments whenever the stage is reduced or -- // reset. e.g. "0-rel" becomes "1-dev" -- // -- //---------------------------------------------------------------- int RRQ(char*,short); int WRQ(char*,short); int sock, length, n; struct sockaddr_in server; struct hostent *hp; unsigned char buffer[1024]; //String comparison short unsigned int scomp(char*,char*); short unsigned int lscomp(char*,char*); void error(int,char*); int main(int argc, char *argv[]) { //Print out name and version numbers fprintf(stderr,"%s:\t %s\n \tServe files via the TFTP protocol\n\n", TITLE, VER); //Set up and open datagram socket sock=socket(AF_INET, SOCK_DGRAM, 0); //Setup socket properties server.sin_family = AF_INET; server.sin_addr.s_addr=INADDR_ANY; server.sin_port = htons(69); length = sizeof(struct sockaddr_in); n=bind(sock,(struct sockaddr *)&server,length); //Finish setting up datagram socket. It should be open. if (n) { fprintf(stderr,"Error opening socket.\n\n\n"); return 0xBAD; } char cwd[FILENAME_MAX]; getcwd(cwd, sizeof(cwd)); printf ("Files in %s/ are available.\n\n", cwd); while (true) { printf("\nlistening...\n"); memset(buffer,0,1024); n=recvfrom(sock,buffer,256,0,(struct sockaddr *) &server,&length); char fname[212]; memset(fname,0,212); int i=0; int RQ=(buffer[0]*100)+buffer[1]; buffer[i]='X'; buffer[i+1]='X'; sscanf(buffer,"%*c%*c%s",fname); for (i=2;buffer[i]!='\0'&&i