ReactOS  0.4.12-dev-51-ge94618b
tftpd.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  home
 
struct  tftpConnType
 
struct  acknowledgement
 
struct  message
 
struct  tftperror
 
struct  packet
 
struct  data12
 
struct  request
 
struct  data1
 
struct  data2
 
struct  data15
 

Macros

#define MYBYTE   unsigned char
 
#define MYWORD   unsigned short
 
#define MYDWORD   unsigned int
 
#define my_inet_addr   inet_addr
 
#define MAX_SERVERS   8
 

Functions

bool detectChange ()
 
void closeConn ()
 
void getInterfaces (data1 *)
 
void runProg ()
 
void processRequest (LPVOID lpParam)
 
charmyGetToken (char *, MYBYTE)
 
charmyTrim (char *, char *)
 
void init (void *)
 
bool cleanReq (request *)
 
bool addServer (MYDWORD *, MYDWORD)
 
FILEopenSection (const char *, MYBYTE, char *)
 
charreadSection (char *, FILE *)
 
bool getSection (const char *, char *, MYBYTE, char *)
 
bool isIP (char *s)
 
charmyLower (char *)
 
charmyUpper (char *)
 
charIP2String (char *, MYDWORD)
 
MYDWORDfindServer (MYDWORD *, MYDWORD)
 
void printWindowsError ()
 
void logMess (request *, MYBYTE)
 
void logMess (char *, MYBYTE)
 

Macro Definition Documentation

◆ MAX_SERVERS

#define MAX_SERVERS   8

◆ my_inet_addr

#define my_inet_addr   inet_addr

Definition at line 33 of file tftpd.h.

Referenced by init().

◆ MYBYTE

#define MYBYTE   unsigned char

Definition at line 21 of file tftpd.h.

Referenced by addServer(), findServer(), getSection(), init(), and openSection().

◆ MYDWORD

#define MYDWORD   unsigned int

Definition at line 23 of file tftpd.h.

Referenced by detectChange(), init(), printWindowsError(), and processRequest().

◆ MYWORD

#define MYWORD   unsigned short

Definition at line 22 of file tftpd.h.

Referenced by init(), myLower(), myTokenize(), myUpper(), and processRequest().

Function Documentation

◆ addServer()

bool addServer ( MYDWORD ,
MYDWORD   
)

Definition at line 2557 of file tftpd.cpp.

Referenced by getInterfaces().

2558 {
2559  for (MYBYTE i = 0; i < MAX_SERVERS; i++)
2560  {
2561  if (!ip || array[i] == ip)
2562  return 0;
2563  else if (!array[i])
2564  {
2565  array[i] = ip;
2566  return 1;
2567  }
2568  }
2569  return 0;
2570 }
GLenum GLclampf GLint i
Definition: glfuncs.h:14
int ip[4]
Definition: rtl.c:1176
#define MYBYTE
Definition: tftpd.h:21
Definition: dhcpd.h:61
#define MAX_SERVERS
Definition: tftpd.h:34

◆ cleanReq()

bool cleanReq ( request )

Definition at line 1505 of file tftpd.cpp.

Referenced by processRequest().

1506 {
1507  //printf("cleaning\n");
1508 
1509  if (req->file)
1510  fclose(req->file);
1511 
1512  if (!(req->sock == INVALID_SOCKET))
1513  {
1514  //printf("Here\n");
1515  closesocket(req->sock);
1516  }
1517 
1518  if (req->pkt[0])
1519  free(req->pkt[0]);
1520 
1521  if (req->pkt[1])
1522  free(req->pkt[1]);
1523 
1525  activeThreads--;
1526  SetEvent(cEvent);
1527 
1528  //printf("cleaned\n");
1529 
1530  return (totalThreads <= minThreads);
1531 }
#define free
Definition: debug_ros.c:5
HANDLE cEvent
Definition: tftpd.cpp:54
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:679
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
#define closesocket
Definition: main.c:39
#define INVALID_SOCKET
Definition: winsock.h:332
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
MYWORD totalThreads
Definition: tftpd.cpp:58
MYWORD minThreads
Definition: tftpd.cpp:59
MYWORD activeThreads
Definition: tftpd.cpp:60
#define INFINITE
Definition: serial.h:102

◆ closeConn()

void closeConn ( )

Definition at line 481 of file tftpd.cpp.

Referenced by init(), runProg(), and ServiceMain().

482 {
483  for (int i = 0; i < MAX_SERVERS && network.tftpConn[i].loaded; i++)
484  if (network.tftpConn[i].ready)
486 }
bool loaded
Definition: tftpd.h:49
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define closesocket
Definition: main.c:39
data1 network
Definition: tftpd.cpp:49
bool ready
Definition: tftpd.h:50
SOCKET sock
Definition: tftpd.h:45
#define MAX_SERVERS
Definition: tftpd.h:34
tftpConnType tftpConn[MAX_SERVERS]
Definition: tftpd.h:127

◆ detectChange()

bool detectChange ( )

Definition at line 2428 of file tftpd.cpp.

Referenced by init().

2429 {
2430  if (!cfig.failureCount)
2431  {
2432  if (cfig.ifspecified)
2433  return false;
2434  }
2435 
2436  MYDWORD eventWait = UINT_MAX;
2437 
2438  if (cfig.failureCount)
2439 #ifdef __REACTOS__
2440  eventWait = 10000 * (1 << cfig.failureCount);
2441 #else
2442  eventWait = 10000 * pow(2, cfig.failureCount);
2443 #endif
2444 
2445  OVERLAPPED overlap;
2446  MYDWORD ret;
2447  HANDLE hand = NULL;
2448  overlap.hEvent = WSACreateEvent();
2449 
2450  ret = NotifyAddrChange(&hand, &overlap);
2451 
2452  if (ret != NO_ERROR)
2453  {
2455  {
2456  printf("NotifyAddrChange error...%d\n", WSAGetLastError());
2457  return true;
2458  }
2459  }
2460 
2461  if ( WaitForSingleObject(overlap.hEvent, eventWait) == WAIT_OBJECT_0 )
2462  WSACloseEvent(overlap.hEvent);
2463 
2464  network.ready = false;
2465 
2466  while (network.busy)
2467  Sleep(1000);
2468 
2469  if (cfig.failureCount)
2470  {
2471  sprintf(logBuff, "Retrying failed Listening Interfaces..");
2472  logMess(logBuff, 1);
2473  }
2474  else
2475  {
2476  sprintf(logBuff, "Network changed, re-detecting Interfaces..");
2477  logMess(logBuff, 1);
2478  }
2479 
2480  return true;
2481 }
data2 cfig
Definition: tftpd.cpp:51
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:736
bool ifspecified
Definition: tftpd.h:154
char logBuff[512]
Definition: tftpd.cpp:42
bool busy
Definition: tftpd.h:134
#define NO_ERROR
Definition: dderror.h:5
#define sprintf(buf, format,...)
Definition: sprintf.c:55
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
float pow(float __x, int __y)
Definition: _cmath.h:458
HANDLE hEvent
Definition: winbase.h:792
BOOL WSAAPI WSACloseEvent(IN WSAEVENT hEvent)
Definition: event.c:23
smooth NULL
Definition: ftsmooth.c:416
#define WSA_IO_PENDING
Definition: winsock2.h:589
bool ready
Definition: tftpd.h:133
data1 network
Definition: tftpd.cpp:49
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define WAIT_OBJECT_0
Definition: winbase.h:387
int ret
MYDWORD failureCount
Definition: tftpd.h:152
DWORD WINAPI NotifyAddrChange(PHANDLE Handle, LPOVERLAPPED overlapped)
#define UINT_MAX
Definition: limits.h:41
WSAEVENT WSAAPI WSACreateEvent(VOID)
Definition: event.c:42
#define MYDWORD
Definition: tftpd.h:23
void logMess(char *logBuff, MYBYTE logLevel)
Definition: tftpd.cpp:2585
#define printf
Definition: config.h:203

◆ findServer()

MYDWORD* findServer ( MYDWORD ,
MYDWORD   
)

Definition at line 2572 of file tftpd.cpp.

Referenced by init().

2573 {
2574  if (ip)
2575  {
2576  for (MYBYTE i = 0; i < MAX_SERVERS && array[i]; i++)
2577  {
2578  if (array[i] == ip)
2579  return &(array[i]);
2580  }
2581  }
2582  return 0;
2583 }
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define MYBYTE
Definition: tftpd.h:21
Definition: dhcpd.h:61
#define MAX_SERVERS
Definition: tftpd.h:34

◆ getInterfaces()

void getInterfaces ( data1 )

Definition at line 2519 of file tftpd.cpp.

Referenced by init().

2520 {
2521  memset(network, 0, sizeof(data1));
2522 
2523  SOCKET sd = WSASocket(PF_INET, SOCK_DGRAM, 0, 0, 0, 0);
2524 
2525  if (sd == INVALID_SOCKET)
2526  return;
2527 
2529  unsigned long nBytesReturned;
2530 
2531  if (WSAIoctl(sd, SIO_GET_INTERFACE_LIST, 0, 0, &InterfaceList,
2532  sizeof(InterfaceList), &nBytesReturned, 0, 0) == SOCKET_ERROR)
2533  return ;
2534 
2535  int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
2536 
2537  for (int i = 0; i < nNumInterfaces; ++i)
2538  {
2539  sockaddr_in *pAddress = (sockaddr_in*)&(InterfaceList[i].iiAddress);
2540  u_long nFlags = InterfaceList[i].iiFlags;
2541 
2542  if (pAddress->sin_addr.s_addr)
2543  {
2544  addServer(network->allServers, pAddress->sin_addr.s_addr);
2545 
2546  if (!(nFlags & IFF_POINTTOPOINT) && (nFlags & IFF_UP))
2547  {
2548  addServer(network->staticServers, pAddress->sin_addr.s_addr);
2549  }
2550  }
2551  }
2552 
2553  closesocket(sd);
2554 }
#define SOCKET_ERROR
Definition: winsock.h:333
return
Definition: dirsup.c:529
struct _INTERFACE_INFO INTERFACE_INFO
unsigned long u_long
Definition: linux.h:269
#define IFF_UP
Definition: ws2ipdef.h:21
_In_ PUSBD_INTERFACE_LIST_ENTRY InterfaceList
Definition: usbdlib.h:168
MYDWORD staticServers[MAX_SERVERS]
Definition: tftpd.h:129
Definition: tftpd.h:125
INT WSAAPI WSAIoctl(IN SOCKET s, IN DWORD dwIoControlCode, IN LPVOID lpvInBuffer, IN DWORD cbInBuffer, OUT LPVOID lpvOutBuffer, IN DWORD cbOutBuffer, OUT LPDWORD lpcbBytesReturned, IN LPWSAOVERLAPPED lpOverlapped, IN LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
Definition: ioctl.c:46
bool addServer(MYDWORD *array, MYDWORD ip)
Definition: tftpd.cpp:2557
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define closesocket
Definition: main.c:39
#define PF_INET
Definition: winsock.h:373
data1 network
Definition: tftpd.cpp:49
#define INVALID_SOCKET
Definition: winsock.h:332
#define WSASocket
Definition: winsock2.h:2407
#define IFF_POINTTOPOINT
Definition: ws2ipdef.h:24
static const WCHAR sd[]
Definition: suminfo.c:287
MYDWORD allServers[MAX_SERVERS]
Definition: tftpd.h:128
struct in_addr sin_addr
Definition: winsock.h:512
#define MAX_SERVERS
Definition: tftpd.h:34
UINT_PTR SOCKET
Definition: winsock.h:47
#define SOCK_DGRAM
Definition: winsock.h:336
#define SIO_GET_INTERFACE_LIST
Definition: ws2ipdef.h:62
#define memset(x, y, z)
Definition: compat.h:39

◆ getSection()

bool getSection ( const char ,
char ,
MYBYTE  ,
char  
)

Definition at line 1533 of file tftpd.cpp.

1534 {
1535  //printf("%s=%s\n",fileName,sectionName);
1536  char section[128];
1537  sprintf(section, "[%s]", sectionName);
1538  myUpper(section);
1539  FILE *f = fopen(fileName, "rt");
1540  char buff[512];
1541  MYBYTE found = 0;
1542 
1543  if (f)
1544  {
1545  while (fgets(buff, 511, f))
1546  {
1547  myUpper(buff);
1548  myTrim(buff, buff);
1549 
1550  if (strstr(buff, section) == buff)
1551  {
1552  found++;
1553  if (found == serial)
1554  {
1555  //printf("%s=%s\n",fileName,sectionName);
1556  while (fgets(buff, 511, f))
1557  {
1558  myTrim(buff, buff);
1559 
1560  if (strstr(buff, "[") == buff)
1561  break;
1562 
1563  if (((*buff) >= '0' && (*buff) <= '9') || ((*buff) >= 'A' && (*buff) <= 'Z') || ((*buff) >= 'a' && (*buff) <= 'z') || ((*buff) && strchr("/\\?*", (*buff))))
1564  {
1565  buffer += sprintf(buffer, "%s", buff);
1566  buffer++;
1567  }
1568  }
1569  break;
1570  }
1571  }
1572  }
1573  fclose(f);
1574  }
1575 
1576  *buffer = 0;
1577  *(buffer + 1) = 0;
1578  return (found == serial);
1579 }
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
GLuint buffer
Definition: glext.h:5915
Definition: parser.c:55
char * myUpper(char *string)
Definition: tftpd.cpp:1785
#define sprintf(buf, format,...)
Definition: sprintf.c:55
GLfloat f
Definition: glext.h:7540
#define MYBYTE
Definition: tftpd.h:21
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
uint32_t serial
Definition: fsck.fat.h:64
char * strchr(const char *String, int ch)
Definition: utclib.c:501
char * myTrim(char *target, char *source)
Definition: tftpd.cpp:1689
static unsigned char buff[32768]
Definition: fatten.c:17

◆ init()

void init ( void )

Definition at line 1805 of file tftpd.cpp.

Referenced by runProg(), and ServiceMain().

1806 {
1807  memset(&cfig, 0, sizeof(cfig));
1808 
1810  char *fileExt = strrchr(extbuff, '.');
1811  *fileExt = 0;
1812  sprintf(iniFile, "%s.ini", extbuff);
1813  sprintf(lnkFile, "%s.url", extbuff);
1814  fileExt = strrchr(extbuff, '\\');
1815  *fileExt = 0;
1816  fileExt++;
1817  sprintf(logFile, "%s\\log\\%s%%Y%%m%%d.log", extbuff, fileExt);
1818 
1819  FILE *f = NULL;
1820  char raw[512];
1821  char name[512];
1822  char value[512];
1823 
1824  if (verbatim)
1825  {
1826  cfig.logLevel = 2;
1827  printf("%s\n\n", sVersion);
1828  }
1829  else if ((f = openSection("LOGGING", 1, iniFile)))
1830  {
1831  cfig.logLevel = 1;
1832  tempbuff[0] = 0;
1833 
1834  while (readSection(raw, f))
1835  {
1836  if (!strcasecmp(raw, "None"))
1837  cfig.logLevel = 0;
1838  else if (!strcasecmp(raw, "Errors"))
1839  cfig.logLevel = 1;
1840  else if (!strcasecmp(raw, "All"))
1841  cfig.logLevel = 2;
1842  else
1843  sprintf(tempbuff, "Section [LOGGING], Invalid LogLevel: %s", raw);
1844  }
1845  }
1846 
1847  if (!verbatim && cfig.logLevel && logFile[0])
1848  {
1849  time_t t = time(NULL);
1850  tm *ttm = localtime(&t);
1851  loggingDay = ttm->tm_yday;
1852  strftime(extbuff, sizeof(extbuff), logFile, ttm);
1853 
1854  cfig.logfile = fopen(extbuff, "at");
1855 
1856  if (cfig.logfile)
1857  {
1858  WritePrivateProfileString("InternetShortcut","URL", extbuff, lnkFile);
1859  WritePrivateProfileString("InternetShortcut","IconIndex", "0", lnkFile);
1860  WritePrivateProfileString("InternetShortcut","IconFile", extbuff, lnkFile);
1861  sprintf(logBuff, "%s Starting..", sVersion);
1862  logMess(logBuff, 1);
1863 
1864  if (tempbuff[0])
1865  logMess(tempbuff, 0);
1866  }
1867  }
1868 
1869  MYWORD wVersionRequested = MAKEWORD(1, 1);
1870  WSAStartup(wVersionRequested, &cfig.wsaData);
1871 
1872  if (cfig.wsaData.wVersion != wVersionRequested)
1873  {
1874  sprintf(logBuff, "WSAStartup Error");
1875  logMess(logBuff, 1);
1876  }
1877 
1878  if ((f = openSection("HOME", 1, iniFile)))
1879  {
1880  while (readSection(raw, f))
1881  {
1882  mySplit(name, value, raw, '=');
1883 
1884  if (strlen(value))
1885  {
1886  if (!cfig.homes[0].alias[0] && cfig.homes[0].target[0])
1887  {
1888  sprintf(logBuff, "Section [HOME], alias and bare path mixup, entry %s ignored", raw);
1889  logMess(logBuff, 1);
1890  }
1891  else if (strchr(name, notFileSep) || strchr(name, fileSep) || strchr(name, '>') || strchr(name, '<') || strchr(name, '.'))
1892  {
1893  sprintf(logBuff, "Section [HOME], invalid chars in alias %s, entry ignored", name);
1894  logMess(logBuff, 1);
1895  }
1896  else if (name[0] && strlen(name) < 64 && value[0])
1897  {
1898 #ifdef __REACTOS__
1899  for (int i = 0; i < MAX_SERVERS; i++)
1900 #else
1901  for (int i = 0; i < 8; i++)
1902 #endif
1903  {
1904  if (cfig.homes[i].alias[0] && !strcasecmp(name, cfig.homes[i].alias))
1905  {
1906  sprintf(logBuff, "Section [HOME], Duplicate Entry: %s ignored", raw);
1907  logMess(logBuff, 1);
1908  break;
1909  }
1910  else if (!cfig.homes[i].alias[0])
1911  {
1912  strcpy(cfig.homes[i].alias, name);
1913  strcpy(cfig.homes[i].target, value);
1914 
1915  if (cfig.homes[i].target[strlen(cfig.homes[i].target) - 1] != fileSep)
1916  {
1917  tempbuff[0] = fileSep;
1918  tempbuff[1] = 0;
1920  }
1921 
1922  break;
1923  }
1924  }
1925  }
1926  else
1927  {
1928  sprintf(logBuff, "Section [HOME], alias %s too large", name);
1929  logMess(logBuff, 1);
1930  }
1931  }
1932  else if (!cfig.homes[0].alias[0] && !cfig.homes[0].target[0])
1933  {
1934  strcpy(cfig.homes[0].target, name);
1935 
1936  if (cfig.homes[0].target[strlen(cfig.homes[0].target) - 1] != fileSep)
1937  {
1938  tempbuff[0] = fileSep;
1939  tempbuff[1] = 0;
1941  }
1942  }
1943  else if (cfig.homes[0].alias[0])
1944  {
1945  sprintf(logBuff, "Section [HOME], alias and bare path mixup, entry %s ignored", raw);
1946  logMess(logBuff, 1);
1947  }
1948  else if (cfig.homes[0].target[0])
1949  {
1950  sprintf(logBuff, "Section [HOME], Duplicate Path: %s ignored", raw);
1951  logMess(logBuff, 1);
1952  }
1953  else
1954  {
1955  sprintf(logBuff, "Section [HOME], missing = sign, Invalid Entry: %s ignored", raw);
1956  logMess(logBuff, 1);
1957  }
1958  }
1959  }
1960 
1961  if (!cfig.homes[0].target[0])
1962  {
1964  char *iniFileExt = strrchr(cfig.homes[0].target, fileSep);
1965  *(++iniFileExt) = 0;
1966  }
1967 
1968  cfig.fileRead = true;
1969 
1970  if ((f = openSection("TFTP-OPTIONS", 1, iniFile)))
1971  {
1972  while (readSection(raw, f))
1973  {
1974  mySplit(name, value, raw, '=');
1975 
1976  if (strlen(value))
1977  {
1978  if (!strcasecmp(name, "blksize"))
1979  {
1980  MYDWORD tblksize = atol(value);
1981 
1982  if (tblksize < 512)
1983  blksize = 512;
1984  else if (tblksize > USHRT_MAX - 32)
1985  blksize = USHRT_MAX - 32;
1986  else
1987  blksize = tblksize;
1988  }
1989  else if (!strcasecmp(name, "threadpoolsize"))
1990  {
1991  minThreads = atol(value);
1992  if (minThreads < 1)
1993  minThreads = 0;
1994  else if (minThreads > 100)
1995  minThreads = 100;
1996  }
1997  else if (!strcasecmp(name, "timeout"))
1998  {
1999  timeout = atol(value);
2000  if (timeout < 1)
2001  timeout = 1;
2002  else if (timeout > UCHAR_MAX)
2003  timeout = UCHAR_MAX;
2004  }
2005  else if (!strcasecmp(name, "Read"))
2006  {
2007  if (strchr("Yy", *value))
2008  cfig.fileRead = true;
2009  else
2010  cfig.fileRead = false;
2011  }
2012  else if (!strcasecmp(name, "Write"))
2013  {
2014  if (strchr("Yy", *value))
2015  cfig.fileWrite = true;
2016  else
2017  cfig.fileWrite = false;
2018  }
2019  else if (!strcasecmp(name, "Overwrite"))
2020  {
2021  if (strchr("Yy", *value))
2022  cfig.fileOverwrite = true;
2023  else
2024  cfig.fileOverwrite = false;
2025  }
2026  else if (!strcasecmp(name, "port-range"))
2027  {
2028  char *ptr = strchr(value, '-');
2029  if (ptr)
2030  {
2031  *ptr = 0;
2032  cfig.minport = atol(value);
2033  cfig.maxport = atol(++ptr);
2034 
2035  if (cfig.minport < 1024 || cfig.minport >= USHRT_MAX || cfig.maxport < 1024 || cfig.maxport >= USHRT_MAX || cfig.minport > cfig.maxport)
2036  {
2037  cfig.minport = 0;
2038  cfig.maxport = 0;
2039 
2040  sprintf(logBuff, "Invalid port range %s", value);
2041  logMess(logBuff, 1);
2042  }
2043  }
2044  else
2045  {
2046  sprintf(logBuff, "Invalid port range %s", value);
2047  logMess(logBuff, 1);
2048  }
2049  }
2050  else
2051  {
2052  sprintf(logBuff, "Warning: unknown option %s, ignored", name);
2053  logMess(logBuff, 1);
2054  }
2055  }
2056  }
2057  }
2058 
2059  if ((f = openSection("ALLOWED-CLIENTS", 1, iniFile)))
2060  {
2061 #ifdef __REACTOS__
2062  MYWORD i = 0;
2063 #else
2064  int i = 0;
2065 #endif
2066 
2067  while (readSection(raw, f))
2068  {
2069 #ifdef __REACTOS__
2070  if (i < _countof(cfig.hostRanges))
2071 #else
2072  if (i < 32)
2073 #endif
2074  {
2075  MYDWORD rs = 0;
2076  MYDWORD re = 0;
2077  mySplit(name, value, raw, '-');
2078  rs = htonl(my_inet_addr(name));
2079 
2080  if (strlen(value))
2081  re = htonl(my_inet_addr(value));
2082  else
2083  re = rs;
2084 
2085  if (rs && rs != INADDR_NONE && re && re != INADDR_NONE && rs <= re)
2086  {
2087  cfig.hostRanges[i].rangeStart = rs;
2088  cfig.hostRanges[i].rangeEnd = re;
2089  i++;
2090  }
2091  else
2092  {
2093  sprintf(logBuff, "Section [ALLOWED-CLIENTS] Invalid entry %s in ini file, ignored", raw);
2094  logMess(logBuff, 1);
2095  }
2096  }
2097  }
2098  }
2099 
2100  if (verbatim)
2101  {
2102  printf("starting TFTP...\n");
2103  }
2104  else
2105  {
2106  sprintf(logBuff, "starting TFTP service");
2107  logMess(logBuff, 1);
2108  }
2109 
2110  for (int i = 0; i < MAX_SERVERS; i++)
2111  if (cfig.homes[i].target[0])
2112  {
2113  sprintf(logBuff, "alias /%s is mapped to %s", cfig.homes[i].alias, cfig.homes[i].target);
2114  logMess(logBuff, 1);
2115  }
2116 
2117  if (cfig.hostRanges[0].rangeStart)
2118  {
2119  char temp[128];
2120 
2121 #ifdef __REACTOS__
2122  for (MYWORD i = 0; i < _countof(cfig.hostRanges) && cfig.hostRanges[i].rangeStart; i++)
2123 #else
2124  for (MYWORD i = 0; i <= sizeof(cfig.hostRanges) && cfig.hostRanges[i].rangeStart; i++)
2125 #endif
2126  {
2127  sprintf(logBuff, "%s", "permitted clients: ");
2129  strcat(logBuff, temp);
2130  sprintf(temp, "%s", IP2String(tempbuff, htonl(cfig.hostRanges[i].rangeEnd)));
2131  strcat(logBuff, temp);
2132  logMess(logBuff, 1);
2133  }
2134  }
2135  else
2136  {
2137  sprintf(logBuff, "%s", "permitted clients: all");
2138  logMess(logBuff, 1);
2139  }
2140 
2141  if (cfig.minport)
2142  {
2143  sprintf(logBuff, "server port range: %u-%u", cfig.minport, cfig.maxport);
2144  logMess(logBuff, 1);
2145  }
2146  else
2147  {
2148  sprintf(logBuff, "server port range: all");
2149  logMess(logBuff, 1);
2150  }
2151 
2152  sprintf(logBuff, "max blksize: %u", blksize);
2153  logMess(logBuff, 1);
2154  sprintf(logBuff, "default blksize: %u", 512);
2155  logMess(logBuff, 1);
2156  sprintf(logBuff, "default timeout: %u", timeout);
2157  logMess(logBuff, 1);
2158  sprintf(logBuff, "file read allowed: %s", cfig.fileRead ? "Yes" : "No");
2159  logMess(logBuff, 1);
2160  sprintf(logBuff, "file create allowed: %s", cfig.fileWrite ? "Yes" : "No");
2161  logMess(logBuff, 1);
2162  sprintf(logBuff, "file overwrite allowed: %s", cfig.fileOverwrite ? "Yes" : "No");
2163  logMess(logBuff, 1);
2164 
2165  if (!verbatim)
2166  {
2167  sprintf(logBuff, "logging: %s", cfig.logLevel > 1 ? "all" : "errors");
2168  logMess(logBuff, 1);
2169  }
2170 
2171  lEvent = CreateEvent(
2172  NULL, // default security descriptor
2173  FALSE, // ManualReset
2174  TRUE, // Signalled
2175  TEXT("AchalTFTServerLogEvent")); // object name
2176 
2177  if (lEvent == NULL)
2178  {
2179  printf("CreateEvent error: %lu\n", GetLastError());
2180  exit(-1);
2181  }
2182  else if ( GetLastError() == ERROR_ALREADY_EXISTS )
2183  {
2184  sprintf(logBuff, "CreateEvent opened an existing Event\nServer May already be Running");
2185  logMess(logBuff, 0);
2186  exit(-1);
2187  }
2188 
2189  tEvent = CreateEvent(
2190  NULL, // default security descriptor
2191  FALSE, // ManualReset
2192  FALSE, // Signalled
2193  TEXT("AchalTFTServerThreadEvent")); // object name
2194 
2195  if (tEvent == NULL)
2196  {
2197  printf("CreateEvent error: %lu\n", GetLastError());
2198  exit(-1);
2199  }
2200  else if ( GetLastError() == ERROR_ALREADY_EXISTS )
2201  {
2202  sprintf(logBuff, "CreateEvent opened an existing Event\nServer May already be Running");
2203  logMess(logBuff, 0);
2204  exit(-1);
2205  }
2206 
2207  sEvent = CreateEvent(
2208  NULL, // default security descriptor
2209  FALSE, // ManualReset
2210  TRUE, // Signalled
2211  TEXT("AchalTFTServerSocketEvent")); // object name
2212 
2213  if (sEvent == NULL)
2214  {
2215  printf("CreateEvent error: %lu\n", GetLastError());
2216  exit(-1);
2217  }
2218  else if ( GetLastError() == ERROR_ALREADY_EXISTS )
2219  {
2220  sprintf(logBuff, "CreateEvent opened an existing Event\nServer May already be Running");
2221  logMess(logBuff, 0);
2222  exit(-1);
2223  }
2224 
2225  cEvent = CreateEvent(
2226  NULL, // default security descriptor
2227  FALSE, // ManualReset
2228  TRUE, // Signalled
2229  TEXT("AchalTFTServerCountEvent")); // object name
2230 
2231  if (cEvent == NULL)
2232  {
2233  printf("CreateEvent error: %lu\n", GetLastError());
2234  exit(-1);
2235  }
2236  else if ( GetLastError() == ERROR_ALREADY_EXISTS )
2237  {
2238  sprintf(logBuff, "CreateEvent opened an existing Event\nServer May already be Running");
2239  logMess(logBuff, 0);
2240  exit(-1);
2241  }
2242 
2243  if (minThreads)
2244  {
2245  for (int i = 0; i < minThreads; i++)
2246  {
2247  _beginthread(
2248  processRequest, // thread function
2249  0, // default security attributes
2250  NULL); // argument to thread function
2251  }
2252 
2253  sprintf(logBuff, "thread pool size: %u", minThreads);
2254  logMess(logBuff, 1);
2255  }
2256 
2257  for (int i = 0; i < MAX_SERVERS && network.tftpConn[i].port; i++)
2258  {
2259  sprintf(logBuff, "listening on: %s:%i", IP2String(tempbuff, network.tftpConn[i].server), network.tftpConn[i].port);
2260  logMess(logBuff, 1);
2261  }
2262 
2263  do
2264  {
2265  memset(&newNetwork, 0, sizeof(data1));
2266 
2267  bool bindfailed = false;
2268 
2269  if ((f = openSection("LISTEN-ON", 1, iniFile)))
2270  {
2271  MYBYTE i = 0;
2272 
2273  while (readSection(raw, f))
2274  {
2275  MYWORD port = 69;
2276 
2277  cfig.ifspecified = true;
2278  mySplit(name, value, raw, ':');
2279 
2280  if (value[0])
2281  port = atoi(value);
2282 
2283  if(i < MAX_SERVERS)
2284  {
2285  if (isIP(name))
2286  {
2287  MYDWORD addr = my_inet_addr(name);
2288 
2289  if (!addr)
2290  {
2291  newNetwork.listenServers[0] = 0;
2293  fclose(f);
2294  break;
2295  }
2296  else if (!findServer(newNetwork.listenServers, addr))
2297  {
2300  i++;
2301  }
2302  }
2303  else
2304  {
2305  sprintf(logBuff, "Warning: Section [LISTEN-ON], Invalid Interface Address %s, ignored", raw);
2306  logMess(logBuff, 1);
2307  }
2308  }
2309  }
2310  }
2311 
2312  if (!cfig.ifspecified)
2313  {
2314  sprintf(logBuff, "detecting Interfaces..");
2315  logMess(logBuff, 1);
2317 
2318  for (MYBYTE n = 0; n < MAX_SERVERS && newNetwork.staticServers[n]; n++)
2319  {
2321  newNetwork.listenPorts[n] = 69;
2322  }
2323  }
2324 
2325  MYBYTE i = 0;
2326 
2327  for (int j = 0; j < MAX_SERVERS && newNetwork.listenPorts[j]; j++)
2328  {
2329  int k = 0;
2330 
2331  for (; k < MAX_SERVERS && network.tftpConn[k].loaded; k++)
2332  {
2334  break;
2335  }
2336 
2338  {
2339  memcpy(&(newNetwork.tftpConn[i]), &(network.tftpConn[k]), sizeof(tftpConnType));
2340 
2343 
2344  network.tftpConn[k].ready = false;
2345  //printf("%d, %s found\n", i, IP2String(tempbuff, newNetwork.tftpConn[i].server));
2346  i++;
2347  continue;
2348  }
2349  else
2350  {
2352 
2354  {
2355  bindfailed = true;
2356  sprintf(logBuff, "Failed to Create Socket");
2357  logMess(logBuff, 1);
2358  continue;
2359  }
2360 
2361  //printf("Socket %u\n", newNetwork.tftpConn[i].sock);
2362 
2363  errno = 0;
2367  int nRet = bind(newNetwork.tftpConn[i].sock, (sockaddr*)&newNetwork.tftpConn[i].addr, sizeof(struct sockaddr_in));
2368 
2369  if (nRet == SOCKET_ERROR || errno)
2370  {
2371  bindfailed = true;
2374  logMess(logBuff, 1);
2375  continue;
2376  }
2377 
2378  newNetwork.tftpConn[i].loaded = true;
2379  newNetwork.tftpConn[i].ready = true;
2382 
2383  //printf("%d, %s created\n", i, IP2String(tempbuff, newNetwork.tftpConn[i].server));
2384 
2387 
2388  if (!newNetwork.listenServers[j])
2389  break;
2390 
2391  i++;
2392  }
2393  }
2394 
2395  if (bindfailed)
2396  cfig.failureCount++;
2397  else
2398  cfig.failureCount = 0;
2399 
2400  closeConn();
2401  memcpy(&network, &newNetwork, sizeof(data1));
2402 
2403  //printf("%i %i %i\n", network.tftpConn[0].ready, network.dnsUdpConn[0].ready, network.dnsTcpConn[0].ready);
2404 
2405  if (!network.tftpConn[0].ready)
2406  {
2407  sprintf(logBuff, "No Static Interface ready, Waiting...");
2408  logMess(logBuff, 1);
2409  continue;
2410  }
2411 
2412  for (int i = 0; i < MAX_SERVERS && network.tftpConn[i].loaded; i++)
2413  {
2414  sprintf(logBuff, "Listening On: %s:%d", IP2String(tempbuff, network.tftpConn[i].server), network.tftpConn[i].port);
2415  logMess(logBuff, 1);
2416  }
2417 
2418  network.ready = true;
2419 
2420  } while (detectChange());
2421 
2422  //printf("Exiting Init\n");
2423 
2424  _endthread();
2425  return;
2426 }
#define CreateEvent
Definition: winbase.h:3562
int maxport
Definition: tftpd.h:151
#define SOCKET_ERROR
Definition: winsock.h:333
data2 cfig
Definition: tftpd.cpp:51
MYWORD listenPorts[MAX_SERVERS]
Definition: tftpd.h:131
GLenum GLclampf GLint GLenum GLuint GLenum GLenum GLsizei GLenum const GLvoid GLfloat GLfloat GLfloat GLfloat GLclampd GLint GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean GLboolean GLboolean GLboolean GLint GLenum GLsizei const GLvoid GLenum GLint GLenum GLint GLint GLsizei GLint GLenum GLint GLint GLint GLint GLsizei GLenum GLsizei const GLuint GLboolean GLenum GLenum GLint GLsizei GLenum GLsizei GLenum const GLvoid GLboolean const GLboolean GLenum const GLdouble const GLfloat const GLdouble const GLfloat GLenum GLint GLint GLint GLint GLint GLint j
Definition: glfuncs.h:98
Definition: get.c:139
#define TRUE
Definition: types.h:120
bool ifspecified
Definition: tftpd.h:154
_CRTIMP void __cdecl _endthread(void)
Definition: thread.c:95
SOCKET WSAAPI socket(IN INT af, IN INT type, IN INT protocol)
Definition: socklife.c:143
u_short sin_port
Definition: winsock.h:511
char * readSection(char *buff, FILE *f)
Definition: tftpd.cpp:1611
char notFileSep
Definition: tftpd.cpp:44
char * strcat(char *DstString, const char *SrcString)
Definition: utclib.c:568
#define strcasecmp
Definition: fake.h:9
FILE * logfile
Definition: tftpd.h:145
#define htonl(x)
Definition: module.h:212
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define MAKEWORD(a, b)
Definition: typedefs.h:247
char sVersion[]
Definition: tftpd.cpp:36
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
char fileSep
Definition: tftpd.cpp:43
bool loaded
Definition: tftpd.h:49
WORD wVersion
Definition: winsock.h:517
#define _countof(array)
Definition: fontsub.cpp:30
#define INADDR_NONE
Definition: tcp.c:42
HANDLE cEvent
Definition: tftpd.cpp:54
MYDWORD staticServers[MAX_SERVERS]
Definition: tftpd.h:129
char logBuff[512]
Definition: tftpd.cpp:42
#define MYWORD
Definition: tftpd.h:22
GLdouble GLdouble t
Definition: gl.h:2047
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
Definition: tftpd.h:125
Definition: dhcpd.h:245
int errno
_Check_return_ _CRTIMP _CONST_RETURN char *__cdecl strrchr(_In_z_ const char *_Str, _In_ int _Ch)
__u16 time
Definition: mkdosfs.c:366
char tempbuff[256]
Definition: tftpd.cpp:40
char alias[64]
Definition: tftpd.h:39
#define USHRT_MAX
Definition: limits.h:38
#define sprintf(buf, format,...)
Definition: sprintf.c:55
GLuint n
Definition: s_context.h:57
bool detectChange()
Definition: tftpd.cpp:2428
GLenum GLclampf GLint i
Definition: glfuncs.h:14
SOCKET maxFD
Definition: tftpd.h:132
#define closesocket
Definition: main.c:39
#define _MAX_PATH
Definition: utility.h:77
static PVOID ptr
Definition: dispmode.c:27
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
smooth NULL
Definition: ftsmooth.c:416
MYDWORD rangeStart
Definition: tftpd.h:81
bool ready
Definition: tftpd.h:133
char logFile[_MAX_PATH]
Definition: tftpd.cpp:38
#define PF_INET
Definition: winsock.h:373
data1 network
Definition: tftpd.cpp:49
bool ready
Definition: tftpd.h:50
MYDWORD rangeEnd
Definition: tftpd.h:82
char target[256]
Definition: tftpd.h:40
size_t CDECL strftime(char *str, size_t max, const char *format, const struct tm *mstm)
Definition: strftime.c:293
void processRequest(void *lpParam)
Definition: tftpd.cpp:488
GLfloat f
Definition: glext.h:7540
#define MYBYTE
Definition: tftpd.h:21
#define INVALID_SOCKET
Definition: winsock.h:332
char iniFile[_MAX_PATH]
Definition: tftpd.cpp:37
char lnkFile[_MAX_PATH]
Definition: tftpd.cpp:39
data12 hostRanges[32]
Definition: tftpd.h:146
home homes[8]
Definition: tftpd.h:143
void getInterfaces(data1 *network)
Definition: tftpd.cpp:2519
WSADATA wsaData
Definition: tftpd.h:139
char fileWrite
Definition: tftpd.h:148
#define GetModuleFileName
Definition: winbase.h:3645
MYDWORD server
Definition: tftpd.h:47
MYDWORD listenServers[MAX_SERVERS]
Definition: tftpd.h:130
_Check_return_ long __cdecl atol(_In_z_ const char *_Str)
SOCKET sock
Definition: tftpd.h:45
GLenum const GLvoid * addr
Definition: glext.h:9621
MYWORD loggingDay
Definition: tftpd.cpp:48
HANDLE lEvent
Definition: tftpd.cpp:56
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
MYDWORD failureCount
Definition: tftpd.h:152
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
static stack_node_t temp
Definition: rpn.c:18
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
Definition: time.h:76
_CRTIMP struct tm *__cdecl localtime(const time_t *_Time)
Definition: time.h:426
HANDLE tEvent
Definition: tftpd.cpp:53
MYDWORD * findServer(MYDWORD *array, MYDWORD ip)
Definition: tftpd.cpp:2572
#define my_inet_addr
Definition: tftpd.h:33
struct in_addr sin_addr
Definition: winsock.h:512
char fileOverwrite
Definition: tftpd.h:149
char verbatim
Definition: tftpd.cpp:46
int tm_yday
Definition: time.h:84
MYBYTE logLevel
Definition: tftpd.h:153
void mySplit(char *name, char *value, char *source, char splitChar)
Definition: tftpd.cpp:1730
MYWORD blksize
Definition: tftpd.cpp:45
sockaddr_in addr
Definition: tftpd.h:46
#define TEXT(s)
Definition: k32.h:26
FILE * openSection(const char *sectionName, MYBYTE serial, char *fileName)
Definition: tftpd.cpp:1581
__kernel_time_t time_t
Definition: linux.h:252
#define MAX_SERVERS
Definition: tftpd.h:34
#define UCHAR_MAX
Definition: limits.h:25
MYWORD port
Definition: tftpd.h:48
HANDLE sEvent
Definition: tftpd.cpp:55
MYWORD minThreads
Definition: tftpd.cpp:59
char * strchr(const char *String, int ch)
Definition: utclib.c:501
_CRTIMP uintptr_t __cdecl _beginthread(_In_ void(__cdecl *_StartAddress)(void *), _In_ unsigned _StackSize, _In_opt_ void *_ArgList)
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
char extbuff[_MAX_PATH]
Definition: tftpd.cpp:41
Definition: name.c:36
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
#define AF_INET
Definition: tcpip.h:117
char fileRead
Definition: tftpd.h:147
data1 newNetwork
Definition: tftpd.cpp:50
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
void exit(int exitcode)
Definition: _exit.c:33
char * IP2String(char *target, MYDWORD ip)
Definition: tftpd.cpp:1759
#define htons(x)
Definition: module.h:213
#define SOCK_DGRAM
Definition: winsock.h:336
tftpConnType tftpConn[MAX_SERVERS]
Definition: tftpd.h:127
USHORT port
Definition: uri.c:227
#define memset(x, y, z)
Definition: compat.h:39
#define MYDWORD
Definition: tftpd.h:23
int minport
Definition: tftpd.h:150
int k
Definition: mpi.c:3369
bool isIP(char *string)
Definition: tftpd.cpp:1767
short sin_family
Definition: winsock.h:510
void logMess(char *logBuff, MYBYTE logLevel)
Definition: tftpd.cpp:2585
#define printf
Definition: config.h:203
void closeConn()
Definition: tftpd.cpp:481
#define WritePrivateProfileString
Definition: winbase.h:3737

◆ IP2String()

char* IP2String ( char ,
MYDWORD   
)

Definition at line 1759 of file tftpd.cpp.

Referenced by init(), and logMess().

1760 {
1761  data15 inaddr;
1762  inaddr.ip = ip;
1763  sprintf(target, "%u.%u.%u.%u", inaddr.octate[0], inaddr.octate[1], inaddr.octate[2], inaddr.octate[3]);
1764  return target;
1765 }
#define sprintf(buf, format,...)
Definition: sprintf.c:55
int ip[4]
Definition: rtl.c:1176
unsigned ip
Definition: tftpd.h:162
GLenum target
Definition: glext.h:7315
Definition: tftpd.h:157
MYBYTE octate[4]
Definition: tftpd.h:163

◆ isIP()

bool isIP ( char s)

Definition at line 1767 of file tftpd.cpp.

Referenced by init().

1768 {
1769  int j = 0;
1770 
1771  for (; *string; string++)
1772  {
1773  if (*string == '.' && *(string + 1) != '.')
1774  j++;
1775  else if (*string < '0' || *string > '9')
1776  return 0;
1777  }
1778 
1779  if (j == 3)
1780  return 1;
1781  else
1782  return 0;
1783 }
GLenum GLclampf GLint GLenum GLuint GLenum GLenum GLsizei GLenum const GLvoid GLfloat GLfloat GLfloat GLfloat GLclampd GLint GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean GLboolean GLboolean GLboolean GLint GLenum GLsizei const GLvoid GLenum GLint GLenum GLint GLint GLsizei GLint GLenum GLint GLint GLint GLint GLsizei GLenum GLsizei const GLuint GLboolean GLenum GLenum GLint GLsizei GLenum GLsizei GLenum const GLvoid GLboolean const GLboolean GLenum const GLdouble const GLfloat const GLdouble const GLfloat GLenum GLint GLint GLint GLint GLint GLint j
Definition: glfuncs.h:98
char string[160]
Definition: util.h:11

◆ logMess() [1/2]

void logMess ( request ,
MYBYTE   
)

Definition at line 2622 of file tftpd.cpp.

2623 {
2625 
2626  char tempbuff[256];
2627 
2628  if (verbatim)
2629  {
2630  if (!req->serverError.errormessage[0])
2631  sprintf(req->serverError.errormessage, strerror(errno));
2632 
2633  if (req->path[0])
2634  printf("Client %s:%u %s, %s\n", IP2String(tempbuff, req->client.sin_addr.s_addr), ntohs(req->client.sin_port), req->path, req->serverError.errormessage);
2635  else
2636  printf("Client %s:%u, %s\n", IP2String(tempbuff, req->client.sin_addr.s_addr), ntohs(req->client.sin_port), req->serverError.errormessage);
2637  }
2638  else if (cfig.logfile && logLevel <= cfig.logLevel)
2639  {
2640  time_t t = time(NULL);
2641  tm *ttm = localtime(&t);
2642 
2643  if (ttm->tm_yday != loggingDay)
2644  {
2645  loggingDay = ttm->tm_yday;
2646  strftime(extbuff, sizeof(extbuff), logFile, ttm);
2647  fprintf(cfig.logfile, "Logging Continued on file %s\n", extbuff);
2648  fclose(cfig.logfile);
2649  cfig.logfile = fopen(extbuff, "at");
2650 
2651  if (cfig.logfile)
2652  {
2653  fprintf(cfig.logfile, "%s\n\n", sVersion);
2654  WritePrivateProfileString("InternetShortcut","URL", extbuff, lnkFile);
2655  WritePrivateProfileString("InternetShortcut","IconIndex", "0", lnkFile);
2656  WritePrivateProfileString("InternetShortcut","IconFile", extbuff, lnkFile);
2657  }
2658  else
2659  return;
2660  }
2661 
2662  strftime(extbuff, sizeof(extbuff), "%d-%b-%y %X", ttm);
2663 
2664  if (req->path[0])
2665  fprintf(cfig.logfile, "[%s] Client %s:%u %s, %s\n", extbuff, IP2String(tempbuff, req->client.sin_addr.s_addr), ntohs(req->client.sin_port), req->path, req->serverError.errormessage);
2666  else
2667  fprintf(cfig.logfile, "[%s] Client %s:%u, %s\n", extbuff, IP2String(tempbuff, req->client.sin_addr.s_addr), ntohs(req->client.sin_port), req->serverError.errormessage);
2668 
2669  fflush(cfig.logfile);
2670  }
2671  SetEvent(lEvent);
2672 }
data2 cfig
Definition: tftpd.cpp:51
FILE * logfile
Definition: tftpd.h:145
char sVersion[]
Definition: tftpd.cpp:36
GLdouble GLdouble t
Definition: gl.h:2047
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:679
int errno
__u16 time
Definition: mkdosfs.c:366
char tempbuff[256]
Definition: tftpd.cpp:40
const char * strerror(int err)
Definition: compat_str.c:23
#define ntohs(x)
Definition: module.h:208
#define sprintf(buf, format,...)
Definition: sprintf.c:55
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
smooth NULL
Definition: ftsmooth.c:416
char logFile[_MAX_PATH]
Definition: tftpd.cpp:38
size_t CDECL strftime(char *str, size_t max, const char *format, const struct tm *mstm)
Definition: strftime.c:293
char lnkFile[_MAX_PATH]
Definition: tftpd.cpp:39
MYWORD loggingDay
Definition: tftpd.cpp:48
HANDLE lEvent
Definition: tftpd.cpp:56
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
Definition: time.h:76
_CRTIMP struct tm *__cdecl localtime(const time_t *_Time)
Definition: time.h:426
char verbatim
Definition: tftpd.cpp:46
int tm_yday
Definition: time.h:84
MYBYTE logLevel
Definition: tftpd.h:153
_Check_return_opt_ _CRTIMP int __cdecl fflush(_Inout_opt_ FILE *_File)
__kernel_time_t time_t
Definition: linux.h:252
char extbuff[_MAX_PATH]
Definition: tftpd.cpp:41
char * IP2String(char *target, MYDWORD ip)
Definition: tftpd.cpp:1759
#define INFINITE
Definition: serial.h:102
#define printf
Definition: config.h:203
#define WritePrivateProfileString
Definition: winbase.h:3737

◆ logMess() [2/2]

void logMess ( char ,
MYBYTE   
)

Definition at line 2585 of file tftpd.cpp.

Referenced by detectChange(), init(), processRequest(), runProg(), and ServiceMain().

2586 {
2588 
2589  if (verbatim)
2590  printf("%s\n", logBuff);
2591  else if (cfig.logfile && logLevel <= cfig.logLevel)
2592  {
2593  time_t t = time(NULL);
2594  tm *ttm = localtime(&t);
2595 
2596  if (ttm->tm_yday != loggingDay)
2597  {
2598  loggingDay = ttm->tm_yday;
2599  strftime(extbuff, sizeof(extbuff), logFile, ttm);
2600  fprintf(cfig.logfile, "Logging Continued on file %s\n", extbuff);
2601  fclose(cfig.logfile);
2602  cfig.logfile = fopen(extbuff, "at");
2603 
2604  if (cfig.logfile)
2605  {
2606  fprintf(cfig.logfile, "%s\n\n", sVersion);
2607  WritePrivateProfileString("InternetShortcut","URL", extbuff, lnkFile);
2608  WritePrivateProfileString("InternetShortcut","IconIndex", "0", lnkFile);
2609  WritePrivateProfileString("InternetShortcut","IconFile", extbuff, lnkFile);
2610  }
2611  else
2612  return;
2613  }
2614 
2615  strftime(extbuff, sizeof(extbuff), "%d-%b-%y %X", ttm);
2616  fprintf(cfig.logfile, "[%s] %s\n", extbuff, logBuff);
2617  fflush(cfig.logfile);
2618  }
2619  SetEvent(lEvent);
2620 }
data2 cfig
Definition: tftpd.cpp:51
FILE * logfile
Definition: tftpd.h:145
char sVersion[]
Definition: tftpd.cpp:36
char logBuff[512]
Definition: tftpd.cpp:42
GLdouble GLdouble t
Definition: gl.h:2047
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:679
__u16 time
Definition: mkdosfs.c:366
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
smooth NULL
Definition: ftsmooth.c:416
char logFile[_MAX_PATH]
Definition: tftpd.cpp:38
size_t CDECL strftime(char *str, size_t max, const char *format, const struct tm *mstm)
Definition: strftime.c:293
char lnkFile[_MAX_PATH]
Definition: tftpd.cpp:39
MYWORD loggingDay
Definition: tftpd.cpp:48
HANDLE lEvent
Definition: tftpd.cpp:56
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
Definition: time.h:76
_CRTIMP struct tm *__cdecl localtime(const time_t *_Time)
Definition: time.h:426
char verbatim
Definition: tftpd.cpp:46
int tm_yday
Definition: time.h:84
MYBYTE logLevel
Definition: tftpd.h:153
_Check_return_opt_ _CRTIMP int __cdecl fflush(_Inout_opt_ FILE *_File)
__kernel_time_t time_t
Definition: linux.h:252
char extbuff[_MAX_PATH]
Definition: tftpd.cpp:41
#define INFINITE
Definition: serial.h:102
#define printf
Definition: config.h:203
#define WritePrivateProfileString
Definition: winbase.h:3737

◆ myGetToken()

char* myGetToken ( char ,
MYBYTE   
)

Definition at line 1628 of file tftpd.cpp.

1629 {
1630  while (*buff)
1631  {
1632  if (index)
1633  index--;
1634  else
1635  break;
1636 
1637  buff += strlen(buff) + 1;
1638  }
1639 
1640  return buff;
1641 }
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
GLuint index
Definition: glext.h:6031
static unsigned char buff[32768]
Definition: fatten.c:17

◆ myLower()

char* myLower ( char )

Definition at line 1795 of file tftpd.cpp.

1796 {
1797  char diff = 'a' - 'A';
1798  MYWORD len = strlen(string);
1799  for (int i = 0; i < len; i++)
1800  if (string[i] >= 'A' && string[i] <= 'Z')
1801  string[i] += diff;
1802  return string;
1803 }
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define MYWORD
Definition: tftpd.h:22
GLenum GLclampf GLint i
Definition: glfuncs.h:14
GLenum GLsizei len
Definition: glext.h:6722
char string[160]
Definition: util.h:11

◆ myTrim()

char* myTrim ( char ,
char  
)

Definition at line 1689 of file tftpd.cpp.

Referenced by getSection(), mySplit(), openSection(), and readSection().

1690 {
1691  while ((*source) && (*source) <= 32)
1692  source++;
1693 
1694  int i = 0;
1695 
1696  for (; i < 511 && source[i]; i++)
1697  target[i] = source[i];
1698 
1699  target[i] = source[i];
1700  i--;
1701 
1702  for (; i >= 0 && target[i] <= 32; i--)
1703  target[i] = 0;
1704 
1705  return target;
1706 }
GLenum GLclampf GLint i
Definition: glfuncs.h:14
GLenum target
Definition: glext.h:7315

◆ myUpper()

char* myUpper ( char )

Definition at line 1785 of file tftpd.cpp.

Referenced by getSection(), and openSection().

1786 {
1787  char diff = 'a' - 'A';
1788  MYWORD len = strlen(string);
1789  for (int i = 0; i < len; i++)
1790  if (string[i] >= 'a' && string[i] <= 'z')
1791  string[i] -= diff;
1792  return string;
1793 }
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define MYWORD
Definition: tftpd.h:22
GLenum GLclampf GLint i
Definition: glfuncs.h:14
GLenum GLsizei len
Definition: glext.h:6722
char string[160]
Definition: util.h:11

◆ openSection()

FILE* openSection ( const char ,
MYBYTE  ,
char  
)

Definition at line 1581 of file tftpd.cpp.

Referenced by init().

1582 {
1583  //printf("%s=%s\n",fileName,sectionName);
1584  char section[128];
1585  sprintf(section, "[%s]", sectionName);
1586  myUpper(section);
1587  FILE *f = fopen(fileName, "rt");
1588  char buff[512];
1589  MYBYTE found = 0;
1590 
1591  if (f)
1592  {
1593  while (fgets(buff, 511, f))
1594  {
1595  myUpper(buff);
1596  myTrim(buff, buff);
1597 
1598  if (strstr(buff, section) == buff)
1599  {
1600  found++;
1601 
1602  if (found == serial)
1603  return f;
1604  }
1605  }
1606  fclose(f);
1607  }
1608  return NULL;
1609 }
char * strstr(char *String1, char *String2)
Definition: utclib.c:653
Definition: parser.c:55
char * myUpper(char *string)
Definition: tftpd.cpp:1785
#define sprintf(buf, format,...)
Definition: sprintf.c:55
smooth NULL
Definition: ftsmooth.c:416
GLfloat f
Definition: glext.h:7540
#define MYBYTE
Definition: tftpd.h:21
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
uint32_t serial
Definition: fsck.fat.h:64
#define f
Definition: ke_i.h:83
char * myTrim(char *target, char *source)
Definition: tftpd.cpp:1689
static unsigned char buff[32768]
Definition: fatten.c:17

◆ printWindowsError()

void printWindowsError ( )

Definition at line 323 of file tftpd.cpp.

Referenced by installService(), main(), and uninstallService().

324 {
325  MYDWORD dw = GetLastError();
326 
327  if (dw)
328  {
329  LPVOID lpMsgBuf;
330 
335  NULL,
336  dw,
338  (LPTSTR) &lpMsgBuf,
339  0, NULL );
340 
341  printf("Error: %s\nPress Enter..\n", (LPTSTR)lpMsgBuf);
342  getchar();
343  }
344 }
#define LANG_NEUTRAL
Definition: nls.h:22
#define SUBLANG_DEFAULT
Definition: nls.h:168
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
CHAR * LPTSTR
Definition: xmlstorage.h:192
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:400
smooth NULL
Definition: ftsmooth.c:416
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:404
REFIID LPVOID DWORD dw
Definition: atlbase.h:40
#define FormatMessage
Definition: winbase.h:3609
#define FORMAT_MESSAGE_IGNORE_INSERTS
Definition: winbase.h:401
_Check_return_ _CRTIMP int __cdecl getchar(void)
Definition: file.c:3627
#define MAKELANGID(p, s)
Definition: nls.h:15
#define MYDWORD
Definition: tftpd.h:23
#define printf
Definition: config.h:203

◆ processRequest()

void processRequest ( LPVOID  lpParam)

◆ readSection()

char* readSection ( char ,
FILE  
)

Definition at line 1611 of file tftpd.cpp.

Referenced by init().

1612 {
1613  while (fgets(buff, 511, f))
1614  {
1615  myTrim(buff, buff);
1616 
1617  if (*buff == '[')
1618  break;
1619 
1620  if (((*buff) >= '0' && (*buff) <= '9') || ((*buff) >= 'A' && (*buff) <= 'Z') || ((*buff) >= 'a' && (*buff) <= 'z') || ((*buff) && strchr("/\\?*", (*buff))))
1621  return buff;
1622  }
1623 
1624  fclose(f);
1625  return NULL;
1626 }
smooth NULL
Definition: ftsmooth.c:416
GLfloat f
Definition: glext.h:7540
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
char * strchr(const char *String, int ch)
Definition: utclib.c:501
char * myTrim(char *target, char *source)
Definition: tftpd.cpp:1689
static unsigned char buff[32768]
Definition: fatten.c:17

◆ runProg()

void runProg ( )

Definition at line 393 of file tftpd.cpp.

Referenced by main().

394 {
395  verbatim = true;
396 
397  if (_beginthread(init, 0, 0) == 0)
398  {
399  if (cfig.logLevel)
400  {
401  sprintf(logBuff, "Thread Creation Failed");
402  logMess(logBuff, 1);
403  }
404  exit(-1);
405  }
406 
407  fd_set readfds;
408  timeval tv;
409  int fdsReady = 0;
410  tv.tv_sec = 20;
411  tv.tv_usec = 0;
412 
413  printf("\naccepting requests..\n");
414 
415  do
416  {
417  network.busy = false;
418 
419  //printf("Active=%u Total=%u\n",activeThreads, totalThreads);
420 
421  if (!network.tftpConn[0].ready || !network.ready)
422  {
423  Sleep(1000);
424  continue;
425  }
426 
427  FD_ZERO(&readfds);
428 
429  for (int i = 0; i < MAX_SERVERS && network.tftpConn[i].ready; i++)
430  FD_SET(network.tftpConn[i].sock, &readfds);
431 
432  fdsReady = select(network.maxFD, &readfds, NULL, NULL, &tv);
433 
434  if (!network.ready)
435  continue;
436 
437  //errno = WSAGetLastError();
438 
439  //if (errno)
440  // printf("%d\n", errno);
441 
442  for (int i = 0; fdsReady > 0 && i < MAX_SERVERS && network.tftpConn[i].ready; i++)
443  {
444  if (network.ready)
445  {
446  network.busy = true;
447 
448  if (FD_ISSET(network.tftpConn[i].sock, &readfds))
449  {
450  //printf("%d Requests Waiting\n", fdsReady);
451 
453 
454  currentServer = i;
455 
457  {
458  _beginthread(
459  processRequest, // thread function
460  0, // default security attributes
461  NULL); // argument to thread function
462  }
463  SetEvent(tEvent);
464 
465  //printf("thread signalled=%u\n",SetEvent(tEvent));
466 
468  fdsReady--;
469  SetEvent(sEvent);
470  }
471  }
472  }
473  }
474  while (true);
475 
476  closeConn();
477 
478  WSACleanup();
479 }
Definition: winsock.h:66
data2 cfig
Definition: tftpd.cpp:51
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:736
unsigned long tv_sec
Definition: linux.h:1738
char logBuff[512]
Definition: tftpd.cpp:42
INT WSAAPI WSACleanup(VOID)
Definition: startup.c:60
bool busy
Definition: tftpd.h:134
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:679
#define FD_ZERO(set)
Definition: winsock.h:96
#define FD_SET(fd, set)
Definition: winsock.h:89
#define sprintf(buf, format,...)
Definition: sprintf.c:55
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
GLenum GLclampf GLint i
Definition: glfuncs.h:14
SOCKET maxFD
Definition: tftpd.h:132
#define FD_ISSET(fd, set)
Definition: winsock.h:100
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
smooth NULL
Definition: ftsmooth.c:416
unsigned long tv_usec
Definition: linux.h:1739
bool ready
Definition: tftpd.h:133
data1 network
Definition: tftpd.cpp:49
bool ready
Definition: tftpd.h:50
void processRequest(void *lpParam)
Definition: tftpd.cpp:488
void init(void *lpParam)
Definition: tftpd.cpp:1805
SOCKET sock
Definition: tftpd.h:45
HANDLE tEvent
Definition: tftpd.cpp:53
char verbatim
Definition: tftpd.cpp:46
MYBYTE logLevel
Definition: tftpd.h:153
MYBYTE currentServer
Definition: tftpd.cpp:57
#define MAX_SERVERS
Definition: tftpd.h:34
HANDLE sEvent
Definition: tftpd.cpp:55
MYWORD totalThreads
Definition: tftpd.cpp:58
_CRTIMP uintptr_t __cdecl _beginthread(_In_ void(__cdecl *_StartAddress)(void *), _In_ unsigned _StackSize, _In_opt_ void *_ArgList)
MYWORD activeThreads
Definition: tftpd.cpp:60
void exit(int exitcode)
Definition: _exit.c:33
#define INFINITE
Definition: serial.h:102
tftpConnType tftpConn[MAX_SERVERS]
Definition: tftpd.h:127
void logMess(char *logBuff, MYBYTE logLevel)
Definition: tftpd.cpp:2585
#define printf
Definition: config.h:203
void closeConn()
Definition: tftpd.cpp:481