1 /************************************************************************** 2 * Copyright (C) 2005 by Achal Dhir * 3 * achaldhir@gmail.com * 4 * * 5 * This program is free software; you can redistribute it and/or modify * 6 * it under the terms of the GNU General Public License as published by * 7 * the Free Software Foundation; either version 2 of the License, or * 8 * (at your option) any later version. * 9 * * 10 * This program is distributed in the hope that it will be useful, * 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 13 * GNU General Public License for more details. * 14 * * 15 * You should have received a copy of the GNU General Public License * 16 * along with this program; if not, write to the * 17 * Free Software Foundation, Inc., * 18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 19 ***************************************************************************/ 20 // TFTPServer.cpp 21 #define MYBYTE unsigned char 22 #define MYWORD unsigned short 23 #define MYDWORD unsigned int 24 25 #ifdef _MSC_VER 26 #define strcasecmp _stricmp 27 #define _CRT_SECURE_NO_WARNINGS 28 #pragma comment(lib, "ws2_32.lib") 29 #pragma comment(lib, "iphlpapi.lib") 30 #endif 31 32 //Constants 33 #define my_inet_addr inet_addr 34 #define MAX_SERVERS 8 35 36 //Structs 37 struct home 38 { 39 char alias[64]; 40 char target[256]; 41 }; 42 43 struct tftpConnType 44 { 45 SOCKET sock; 46 sockaddr_in addr; 47 MYDWORD server; 48 MYWORD port; 49 bool loaded; 50 bool ready; 51 }; 52 53 struct acknowledgement 54 { 55 MYWORD opcode; 56 MYWORD block; 57 }; 58 59 struct message 60 { 61 MYWORD opcode; 62 char buffer[514]; 63 }; 64 65 struct tftperror 66 { 67 MYWORD opcode; 68 MYWORD errorcode; 69 char errormessage[512]; 70 }; 71 72 struct packet 73 { 74 MYWORD opcode; 75 MYWORD block; 76 char buffer; 77 }; 78 79 struct data12 80 { 81 MYDWORD rangeStart; 82 MYDWORD rangeEnd; 83 }; 84 85 struct request 86 { 87 timeval tv; 88 fd_set readfds; 89 time_t expiry; 90 SOCKET sock; 91 SOCKET knock; 92 MYBYTE sockInd; 93 MYBYTE attempt; 94 char path[256]; 95 FILE *file; 96 char *filename; 97 char *mode; 98 char *alias; 99 MYDWORD tsize; 100 MYDWORD fblock; 101 int bytesReady; 102 int bytesRecd; 103 int bytesRead[2]; 104 packet* pkt[2]; 105 sockaddr_in client; 106 socklen_t clientsize; 107 union 108 { 109 tftperror serverError; 110 message mesout; 111 acknowledgement acout; 112 }; 113 union 114 { 115 tftperror clientError; 116 message mesin; 117 acknowledgement acin; 118 }; 119 MYWORD blksize; 120 MYWORD timeout; 121 MYWORD block; 122 MYWORD tblock; 123 }; 124 125 struct data1 126 { 127 tftpConnType tftpConn[MAX_SERVERS]; 128 MYDWORD allServers[MAX_SERVERS]; 129 MYDWORD staticServers[MAX_SERVERS]; 130 MYDWORD listenServers[MAX_SERVERS]; 131 MYWORD listenPorts[MAX_SERVERS]; 132 SOCKET maxFD; 133 bool ready; 134 bool busy; 135 }; 136 137 struct data2 138 { 139 WSADATA wsaData; 140 #ifdef __REACTOS__ 141 home homes[MAX_SERVERS]; 142 #else 143 home homes[8]; 144 #endif 145 FILE *logfile; 146 data12 hostRanges[32]; 147 char fileRead; 148 char fileWrite; 149 char fileOverwrite; 150 int minport; 151 int maxport; 152 MYDWORD failureCount; 153 MYBYTE logLevel; 154 bool ifspecified; 155 }; 156 157 struct data15 158 { 159 union 160 { 161 //MYDWORD ip; 162 unsigned ip:32; 163 MYBYTE octate[4]; 164 }; 165 }; 166 167 //Functions 168 bool detectChange(); 169 void closeConn(); 170 void getInterfaces(data1*); 171 void runProg(); 172 void processRequest(LPVOID lpParam); 173 char* myGetToken(char*, MYBYTE); 174 char* myTrim(char*, char*); 175 void init(void*); 176 bool cleanReq(request*); 177 bool addServer(MYDWORD*, MYDWORD); 178 FILE* openSection(const char*, MYBYTE, char*); 179 char* readSection(char*, FILE*); 180 bool getSection(const char*, char*, MYBYTE, char*); 181 bool isIP(char*s); 182 char* myLower(char*); 183 char* myUpper(char*); 184 char* IP2String(char*, MYDWORD); 185 MYDWORD* findServer(MYDWORD*, MYDWORD); 186 void printWindowsError(); 187 void logMess(request*, MYBYTE); 188 void logMess(char*, MYBYTE); 189