ftpserv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * Copyright (C) 2009 by egnite GmbH
  3. * Copyright (C) 2005 by egnite Software GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * $Id: ftpserv.c 5228 2013-07-19 08:48:58Z u_bonnes $
  37. */
  38. #include <stdio.h>
  39. #include <fcntl.h>
  40. #include <io.h>
  41. #include <dev/board.h>
  42. #include <dev/pnut.h>
  43. #include <dev/sbimmc.h>
  44. #include <fs/phatfs.h>
  45. #include <sys/confnet.h>
  46. #include <sys/version.h>
  47. #include <sys/heap.h>
  48. #include <sys/thread.h>
  49. #include <sys/socket.h>
  50. #include <sys/timer.h>
  51. #include <arpa/inet.h>
  52. #include <netinet/tcp.h>
  53. #include <net/route.h>
  54. #include <pro/dhcp.h>
  55. #include <pro/ftpd.h>
  56. #include <pro/wins.h>
  57. #include <pro/sntp.h>
  58. #include <pro/discover.h>
  59. /* Determine the compiler. */
  60. #if defined(__IMAGECRAFT__)
  61. #if defined(__AVR__)
  62. #define CC_STRING "ICCAVR"
  63. #else
  64. #define CC_STRING "ICC"
  65. #endif
  66. #elif defined(__GNUC__)
  67. #if defined(__AVR__)
  68. #define CC_STRING "AVRGCC"
  69. #elif defined(__arm__)
  70. #define CC_STRING "ARMGCC"
  71. #else
  72. #define CC_STRING "GCC"
  73. #endif
  74. #else
  75. #define CC_STRING "Compiler unknown"
  76. #endif
  77. /*!
  78. * \example ftpd/ftpserv.c
  79. *
  80. * FTP server sample.
  81. *
  82. * This application requires the PNUT file system, which is
  83. * not available on Ethernut 1.x or Charon II.
  84. */
  85. /*
  86. * Baudrate for debug output.
  87. */
  88. #ifndef DBG_BAUDRATE
  89. #define DBG_BAUDRATE 115200
  90. #endif
  91. /*
  92. * Wether we should use DHCP.
  93. */
  94. #define USE_DHCP
  95. /*
  96. * Wether we should run a discovery responder.
  97. */
  98. #if defined(__arm__)
  99. #define USE_DISCOVERY
  100. #endif
  101. /*
  102. * Unique MAC address of the Ethernut Board.
  103. *
  104. * Ignored if EEPROM contains a valid configuration.
  105. */
  106. #define MY_MAC { 0x00, 0x06, 0x98, 0x30, 0x00, 0x35 }
  107. /*
  108. * Unique IP address of the Ethernut Board.
  109. *
  110. * Ignored if DHCP is used.
  111. */
  112. #define MY_IPADDR "192.168.192.35"
  113. /*
  114. * IP network mask of the Ethernut Board.
  115. *
  116. * Ignored if DHCP is used.
  117. */
  118. #define MY_IPMASK "255.255.255.0"
  119. /*
  120. * Gateway IP address for the Ethernut Board.
  121. *
  122. * Ignored if DHCP is used.
  123. */
  124. #define MY_IPGATE "192.168.192.1"
  125. /*
  126. * NetBIOS name.
  127. *
  128. * Use a symbolic name with Win32 Explorer.
  129. */
  130. //#define MY_WINSNAME "ETHERNUT"
  131. /*
  132. * FTP port number.
  133. */
  134. #define FTP_PORTNUM 21
  135. /*
  136. * FTP timeout.
  137. *
  138. * The server will terminate the session, if no new command is received
  139. * within the specified number of milliseconds.
  140. */
  141. #define FTPD_TIMEOUT 600000
  142. /*
  143. * TCP buffer size.
  144. */
  145. #define TCPIP_BUFSIZ 5840
  146. /*
  147. * Maximum segment size.
  148. *
  149. * Choose 536 up to 1460. Note, that segment sizes above 536 may result
  150. * in fragmented packets. Remember, that Ethernut doesn't support TCP
  151. * fragmentation.
  152. */
  153. #define TCPIP_MSS 1460
  154. #if defined(ETHERNUT3)
  155. /* Ethernut 3 file system. */
  156. #define FSDEV devPhat0
  157. #define FSDEV_NAME "PHAT0"
  158. /* Ethernut 3 block device interface. */
  159. #define BLKDEV devNplMmc0
  160. #define BLKDEV_NAME "MMC0"
  161. #elif defined(AT91SAM7X_EK)
  162. /* SAM7X-EK file system. */
  163. #define FSDEV devPhat0
  164. #define FSDEV_NAME "PHAT0"
  165. /* SAM7X-EK block device interface. */
  166. #define BLKDEV devAt91SpiMmc0
  167. #define BLKDEV_NAME "MMC0"
  168. #elif defined(AT91SAM9260_EK)
  169. /* SAM9260-EK file system. */
  170. #define FSDEV devPhat0
  171. #define FSDEV_NAME "PHAT0"
  172. /* SAM9260-EK block device interface. */
  173. #define BLKDEV devAt91Mci0
  174. #define BLKDEV_NAME "MCI0"
  175. #elif defined(ETHERNUT2)
  176. /*
  177. * Ethernut 2 File system
  178. */
  179. #define FSDEV devPnut
  180. #define FSDEV_NAME "PNUT"
  181. #else
  182. #define FSDEV_NAME "NONE"
  183. #endif
  184. /*! \brief Local timezone, -1 for Central Europe. */
  185. #define MYTZ -1
  186. /*! \brief IP address of the host running a time daemon. */
  187. #if defined(DEV_ETHER)
  188. #define MYTIMED "130.149.17.21"
  189. #endif
  190. #ifdef ETHERNUT3
  191. /*! \brief Defined if X1226 RTC is available. */
  192. #define X12RTC_DEV
  193. #endif
  194. /*
  195. * FTP service.
  196. *
  197. * This function waits for client connect, processes the FTP request
  198. * and disconnects. Nut/Net doesn't support a server backlog. If one
  199. * client has established a connection, further connect attempts will
  200. * be rejected.
  201. *
  202. * Some FTP clients, like the Win32 Explorer, open more than one
  203. * connection for background processing. So we run this routine by
  204. * several threads.
  205. */
  206. static void FtpService(void)
  207. {
  208. #if defined(DEV_ETHER)
  209. TCPSOCKET *sock;
  210. /*
  211. * Create a socket.
  212. */
  213. if ((sock = NutTcpCreateSocket()) != 0) {
  214. /*
  215. * Set specified socket options.
  216. */
  217. #ifdef TCPIP_MSS
  218. {
  219. uint16_t mss = TCPIP_MSS;
  220. NutTcpSetSockOpt(sock, TCP_MAXSEG, &mss, sizeof(mss));
  221. }
  222. #endif
  223. #ifdef FTPD_TIMEOUT
  224. {
  225. uint32_t tmo = FTPD_TIMEOUT;
  226. NutTcpSetSockOpt(sock, SO_RCVTIMEO, &tmo, sizeof(tmo));
  227. }
  228. #endif
  229. #ifdef TCPIP_BUFSIZ
  230. {
  231. uint16_t siz = TCPIP_BUFSIZ;
  232. NutTcpSetSockOpt(sock, SO_RCVBUF, &siz, sizeof(siz));
  233. }
  234. #endif
  235. /*
  236. * Listen on our port. If we return, we got a client.
  237. */
  238. printf("\nWaiting for an FTP client...");
  239. if (NutTcpAccept(sock, FTP_PORTNUM) == 0) {
  240. printf("%s connected, %u bytes free\n", inet_ntoa(sock->so_remote_addr), (unsigned int)NutHeapAvailable());
  241. NutFtpServerSession(sock);
  242. printf("%s disconnected, %u bytes free\n", inet_ntoa(sock->so_remote_addr), (unsigned int)NutHeapAvailable());
  243. } else {
  244. puts("Accept failed");
  245. }
  246. /*
  247. * Close our socket.
  248. */
  249. NutTcpCloseSocket(sock);
  250. }
  251. #endif
  252. }
  253. /*
  254. * FTP service thread.
  255. */
  256. THREAD(FtpThread, arg)
  257. {
  258. /* Loop endless for connections. */
  259. for (;;) {
  260. FtpService();
  261. }
  262. }
  263. /*
  264. * Assign stdout to the UART device.
  265. */
  266. void InitDebugDevice(void)
  267. {
  268. uint32_t baud = DBG_BAUDRATE;
  269. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  270. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  271. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  272. }
  273. /*
  274. * Setup the ethernet device. Try DHCP first. If this is
  275. * the first time boot with empty EEPROM and no DHCP server
  276. * was found, use hardcoded values.
  277. */
  278. static int InitEthernetDevice(void)
  279. {
  280. #if defined(DEV_ETHER)
  281. uint32_t ip_addr = inet_addr(MY_IPADDR);
  282. uint32_t ip_mask = inet_addr(MY_IPMASK);
  283. uint32_t ip_gate = inet_addr(MY_IPGATE);
  284. uint8_t mac[6] = MY_MAC;
  285. if (NutRegisterDevice(&DEV_ETHER, 0x8300, 5) == 0) {
  286. printf("Configure %s...", DEV_ETHER_NAME);
  287. #ifdef USE_DHCP
  288. if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000) == 0) {
  289. puts("OK");
  290. return 0;
  291. }
  292. printf("initial boot...");
  293. if (NutDhcpIfConfig(DEV_ETHER_NAME, mac, 60000) == 0) {
  294. puts("OK");
  295. return 0;
  296. }
  297. #endif
  298. printf("No DHCP...");
  299. NutNetIfConfig(DEV_ETHER_NAME, mac, ip_addr, ip_mask);
  300. /* Without DHCP we had to set the default gateway manually.*/
  301. if(ip_gate) {
  302. printf("hard coded gate...");
  303. NutIpRouteAdd(0, 0, ip_gate, &DEV_ETHER);
  304. }
  305. puts("OK");
  306. return 0;
  307. }
  308. #endif /* DEV_ETHER */
  309. puts("No Ethernet Device");
  310. return -1;
  311. }
  312. /*
  313. * Query a time server and optionally update the hardware clock.
  314. */
  315. static int QueryTimeServer(void)
  316. {
  317. int rc = -1;
  318. #ifdef MYTIMED
  319. {
  320. time_t now;
  321. uint32_t timeserver = inet_addr(MYTIMED);
  322. /* Query network time service and set the system time. */
  323. printf("Query time from %s...", MYTIMED);
  324. if(NutSNTPGetTime(&timeserver, &now) == 0) {
  325. puts("OK");
  326. rc = 0;
  327. stime(&now);
  328. #ifdef RTC_CHIP
  329. /* If RTC hardware is available, update it. */
  330. {
  331. struct _tm *gmt = gmtime(&now);
  332. if (NutRtcSetTime(gmt)) {
  333. puts("RTC update failed");
  334. }
  335. }
  336. #endif
  337. }
  338. else {
  339. puts("failed");
  340. }
  341. }
  342. #endif
  343. return rc;
  344. }
  345. /*
  346. * Try to get initial date and time from the hardware clock or a time server.
  347. */
  348. static int InitTimeAndDate(void)
  349. {
  350. int rc = -1;
  351. /* Set the local time zone. */
  352. _timezone = MYTZ * 60L * 60L;
  353. #ifdef RTC_CHIP
  354. /* Register and query hardware RTC, if available. */
  355. if (NutRegisterRtc(&RTC_CHIP)) {
  356. puts("No hardware RTC");
  357. rc = QueryTimeServer();
  358. } else {
  359. uint32_t rs;
  360. NutRtcGetStatus(&rs);
  361. if (rs & RTC_STATUS_PF) {
  362. puts("RTC power fail detected");
  363. rc = QueryTimeServer();
  364. } else {
  365. /* RTC hardware status is fine, update our system clock. */
  366. struct _tm gmt;
  367. if (NutRtcGetTime(&gmt) == 0) {
  368. /* Assume that RTC is running at GMT. */
  369. time_t now = _mkgmtime(&gmt);
  370. if (now != -1) {
  371. stime(&now);
  372. rc = 0;
  373. }
  374. }
  375. }
  376. }
  377. #else
  378. /* No hardware RTC, query the time server if available. */
  379. rc = QueryTimeServer();
  380. #endif
  381. return rc;
  382. }
  383. /*
  384. * Main application routine.
  385. *
  386. * Nut/OS automatically calls this entry after initialization.
  387. */
  388. int main(void)
  389. {
  390. int volid;
  391. uint32_t ipgate;
  392. /* Initialize a debug output device and print a banner. */
  393. InitDebugDevice();
  394. printf("\n\nFTP Server Sample - Nut/OS %s - " CC_STRING "\n", NutVersionString());
  395. /* Initialize the Ethernet device and print our IP address. */
  396. if (InitEthernetDevice()) {
  397. for(;;);
  398. }
  399. #if defined(DEV_ETHER)
  400. printf("IP Addr: %s\n", inet_ntoa(confnet.cdn_ip_addr));
  401. printf("IP Mask: %s\n", inet_ntoa(confnet.cdn_ip_mask));
  402. NutIpRouteQuery(0, &ipgate);
  403. printf("IP Gate: %s\n", inet_ntoa(ipgate));
  404. #endif
  405. #ifdef USE_DISCOVERY
  406. /* Register a discovery responder. */
  407. printf("Start Responder...");
  408. if (NutRegisterDiscovery((uint32_t)-1, 0, DISF_INITAL_ANN)) {
  409. puts("failed");
  410. }
  411. else {
  412. puts("OK");
  413. }
  414. #endif
  415. /* Initialize system clock and calendar. */
  416. if (InitTimeAndDate() == 0) {
  417. time_t now = time(0);
  418. struct _tm *lot = localtime(&now);
  419. printf("Date: %02u.%02u.%u\n", lot->tm_mday, lot->tm_mon + 1, 1900 + lot->tm_year);
  420. printf("Time: %02u:%02u:%02u\n", lot->tm_hour, lot->tm_min, lot->tm_sec);
  421. }
  422. #ifdef FSDEV
  423. /* Initialize the file system. */
  424. printf("Register file system...");
  425. if (NutRegisterDevice(&FSDEV, 0, 0)) {
  426. puts("failed");
  427. for (;;)NutSleep(100);
  428. }
  429. puts("OK");
  430. #endif
  431. #ifdef BLKDEV
  432. /* Register block device. */
  433. printf("Register block device...");
  434. if (NutRegisterDevice(&BLKDEV, 0, 0)) {
  435. puts("failed");
  436. for (;;)NutSleep(100);
  437. }
  438. puts("OK");
  439. /* Mount partition. */
  440. printf("Mounting partition...");
  441. if ((volid = _open(BLKDEV_NAME ":1/" FSDEV_NAME, _O_RDWR | _O_BINARY)) == -1) {
  442. puts("failed");
  443. for (;;)NutSleep(100);
  444. }
  445. puts("OK");
  446. #else
  447. volid = 0;
  448. #endif
  449. #if defined(DEV_ETHER)
  450. /* Register root path. */
  451. printf("Register FTP root...");
  452. if (NutRegisterFtpRoot(FSDEV_NAME ":")) {
  453. puts("failed");
  454. for (;;)NutSleep(100);
  455. }
  456. puts("OK");
  457. #endif
  458. /* Start two additional server threads. */
  459. NutThreadCreate("ftpd0", FtpThread, 0, 1640);
  460. NutThreadCreate("ftpd1", FtpThread, 0, 1640);
  461. /* Main server thread. */
  462. for (;;) {
  463. #ifdef MY_WINSNAME
  464. NutWinsNameQuery(MY_WINSNAME, confnet.cdn_ip_addr);
  465. #endif
  466. FtpService();
  467. }
  468. return 0;
  469. }