├── SConscript ├── inc ├── ftp_session_cmd.h ├── ftp.h └── ftp_session.h ├── .gitignore ├── README.md ├── src ├── ftp.c ├── ftp_session.c └── ftp_session_cmd.c └── LICENSE /SConscript: -------------------------------------------------------------------------------- 1 | # RT-Thread building script for bridge 2 | 3 | from building import * 4 | 5 | cwd = GetCurrentDir() 6 | src = Glob('src/*.c') 7 | 8 | CPPPATH = [cwd + '/inc'] 9 | 10 | group = DefineGroup('agile_ftp', src, depend = ['PKG_USING_AGILE_FTP'], CPPPATH = CPPPATH) 11 | 12 | Return('group') 13 | -------------------------------------------------------------------------------- /inc/ftp_session_cmd.h: -------------------------------------------------------------------------------- 1 | #ifndef __FTP_SESSION_CMD_H 2 | #define __FTP_SESSION_CMD_H 3 | #include 4 | #include 5 | #include 6 | #include "ftp_session.h" 7 | 8 | struct ftp_session_cmd 9 | { 10 | char *cmd; 11 | int (*cmd_fn)(struct ftp_session *session, char *cmd, char *cmd_param); 12 | }; 13 | 14 | int ftp_session_cmd_process(struct ftp_session *session, char *cmd, char *cmd_param); 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | .vscode 45 | 46 | # Kernel Module Compile Results 47 | *.mod* 48 | *.cmd 49 | .tmp_versions/ 50 | modules.order 51 | Module.symvers 52 | Mkfile.old 53 | dkms.conf 54 | -------------------------------------------------------------------------------- /inc/ftp.h: -------------------------------------------------------------------------------- 1 | #ifndef __FTP_H 2 | #define __FTP_H 3 | #include 4 | 5 | int ftp_get_max_session_num(void); 6 | int ftp_set_max_session_num(int num); 7 | const char *ftp_get_session_username(void); 8 | int ftp_set_session_username(const char *username); 9 | const char *ftp_get_session_password(void); 10 | int ftp_set_session_password(const char *password); 11 | const char *ftp_get_session_welcome_msg(void); 12 | int ftp_set_session_welcome_msg(const char *welcome_msg); 13 | int ftp_session_force_quit(void); 14 | 15 | int ftp_force_restart(void); 16 | int ftp_get_port(void); 17 | int ftp_set_port(int port); 18 | int ftp_init(rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /inc/ftp_session.h: -------------------------------------------------------------------------------- 1 | #ifndef __FTP_SESSION_H 2 | #define __FTP_SESSION_H 3 | #include 4 | #include 5 | #include 6 | 7 | enum ftp_session_state 8 | { 9 | FTP_SESSION_STATE_USER = 0, 10 | FTP_SESSION_STATE_PASSWD, 11 | FTP_SESSION_STATE_PROCESS 12 | }; 13 | 14 | struct ftp_session 15 | { 16 | int fd; 17 | int port_pasv_fd; 18 | int is_anonymous; 19 | int offset; 20 | struct sockaddr_in remote; 21 | enum ftp_session_state state; 22 | char currentdir[256]; 23 | rt_uint8_t force_quit; 24 | rt_tick_t tick_timeout; 25 | rt_slist_t slist; 26 | }; 27 | 28 | 29 | int ftp_session_create(int fd, struct sockaddr_in *addr, socklen_t addr_len); 30 | int ftp_session_force_quit(void); 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Agile Ftp 2 | 3 | ## 介绍 4 | 5 | 基于RT-Thread,运行在嵌入式上的高并发高性能轻量级FTP服务器。 6 | 7 | ## 快速上手 8 | 9 | 1. `#include "ftp.h"` 10 | 2. `ftp_init(2048, 27, 100);` 11 | 3. 默认端口21,默认用户名和密码都为 loogg 12 | 13 | ## API介绍 14 | 15 | 1. `ftp_get_max_session_num` 16 | 获取最大客户端数量 17 | 18 | 2. `ftp_set_max_session_num` 19 | 设置最大客户端数量 20 | 21 | 3. `ftp_get_session_username` 22 | 获取客户端用户名 23 | 24 | 4. `ftp_set_session_username` 25 | 设置客户端用户名 26 | 27 | 5. `ftp_get_session_password` 28 | 获取客户端密码 29 | 30 | 6. `ftp_set_session_password` 31 | 设置客户端密码 32 | 33 | 7. `ftp_get_session_welcome_msg` 34 | 获取客户端欢迎词 35 | 36 | 8. `ftp_set_session_welcome_msg` 37 | 设置客户端欢迎词 38 | 39 | 9. `ftp_session_force_quit` 40 | 强制关闭所有客户端连接(异步) 41 | 42 | 10. `ftp_force_restart` 43 | 强制重启服务器(异步) 44 | 45 | 11. `ftp_get_port` 46 | 获取服务器监听端口 47 | 48 | 12. `ftp_set_port` 49 | 设置服务器监听端口 50 | 51 | 13. `ftp_init` 52 | 初始化ftp服务 53 | 54 | ## 动态设置参数 55 | 56 | - 使用 `ftp_force_restart` 和 `ftp_session_force_quit` 57 | 58 | ## 联系方式 & 感谢 59 | 60 | - 维护:malongwei 61 | - 主页: 62 | - 邮箱:<2544047213@qq.com> 63 | -------------------------------------------------------------------------------- /src/ftp.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #if RT_VER_NUM < 0x40100 4 | #include 5 | #include 6 | #else 7 | #include 8 | #include 9 | #include 10 | #endif 11 | 12 | #include 13 | #include 14 | #include 15 | #include "ftp_session.h" 16 | 17 | #define DBG_TAG "ftp" 18 | #define DBG_LVL DBG_INFO 19 | #include 20 | 21 | #ifndef FTP_DEFAULT_PORT 22 | #define FTP_DEFAULT_PORT 21 23 | #endif 24 | 25 | static int ftp_port = FTP_DEFAULT_PORT; 26 | static rt_uint8_t force_restart = 0; 27 | 28 | int ftp_force_restart(void) 29 | { 30 | force_restart = 1; 31 | return RT_EOK; 32 | } 33 | 34 | int ftp_get_port(void) 35 | { 36 | return ftp_port; 37 | } 38 | 39 | int ftp_set_port(int port) 40 | { 41 | if((port <= 0) || (port > 65535)) 42 | return -RT_ERROR; 43 | ftp_port = port; 44 | return RT_EOK; 45 | } 46 | 47 | static void ftp_entry(void *parameter) 48 | { 49 | int server_fd = -1; 50 | int enable = 1; 51 | int flags; 52 | struct sockaddr_in addr; 53 | socklen_t addrlen; 54 | 55 | // select使用 56 | fd_set readset, exceptset; 57 | // select超时时间 58 | struct timeval select_timeout; 59 | select_timeout.tv_sec = 1; 60 | select_timeout.tv_usec = 0; 61 | 62 | rt_thread_mdelay(5000); 63 | 64 | _ftp_start: 65 | server_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 66 | if(server_fd < 0) 67 | goto _ftp_restart; 68 | 69 | if(setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&enable, sizeof(enable)) < 0) 70 | goto _ftp_restart; 71 | 72 | rt_memset(&addr, 0, sizeof(addr)); 73 | addr.sin_family = AF_INET; 74 | addr.sin_port = htons(ftp_port); 75 | addr.sin_addr.s_addr = htonl(INADDR_ANY); 76 | if(bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) 77 | goto _ftp_restart; 78 | 79 | if(listen(server_fd, 1) < 0) 80 | goto _ftp_restart; 81 | 82 | flags = fcntl(server_fd, F_GETFL, 0); 83 | flags |= O_NONBLOCK; 84 | fcntl(server_fd, F_SETFL, flags); 85 | 86 | LOG_I("service launched success."); 87 | while(1) 88 | { 89 | if(force_restart) 90 | { 91 | force_restart = 0; 92 | ftp_session_force_quit(); 93 | break; 94 | } 95 | 96 | FD_ZERO(&readset); 97 | FD_ZERO(&exceptset); 98 | 99 | FD_SET(server_fd, &readset); 100 | FD_SET(server_fd, &exceptset); 101 | 102 | int rc = select(server_fd + 1, &readset, RT_NULL, &exceptset, &select_timeout); 103 | if(rc < 0) 104 | break; 105 | if(rc > 0) 106 | { 107 | if (FD_ISSET(server_fd, &exceptset)) 108 | break; 109 | if (FD_ISSET(server_fd, &readset)) 110 | { 111 | addrlen = sizeof(struct sockaddr_in); 112 | int client_fd = accept(server_fd, (struct sockaddr *)&addr, &addrlen); 113 | if(client_fd < 0) 114 | break; 115 | if(ftp_session_create(client_fd, &addr, addrlen) != RT_EOK) 116 | close(client_fd); 117 | } 118 | } 119 | } 120 | 121 | _ftp_restart: 122 | LOG_W("service go wrong, now wait restarting..."); 123 | if(server_fd >= 0) 124 | { 125 | close(server_fd); 126 | server_fd = -1; 127 | } 128 | 129 | rt_thread_mdelay(1000); 130 | goto _ftp_start; 131 | } 132 | 133 | int ftp_init(rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick) 134 | { 135 | rt_thread_t tid = rt_thread_create("ftp", ftp_entry, RT_NULL, stack_size, priority, tick); 136 | RT_ASSERT(tid != RT_NULL); 137 | rt_thread_startup(tid); 138 | 139 | rt_kprintf("\r\n[FTP] Powered by Ma Longwei\r\n"); 140 | rt_kprintf("[FTP] github: https://github.com/loogg\r\n"); 141 | rt_kprintf("[FTP] Email: 2544047213@qq.com\r\n"); 142 | 143 | return RT_EOK; 144 | } 145 | -------------------------------------------------------------------------------- /src/ftp_session.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #if RT_VER_NUM < 0x40100 4 | #include 5 | #include 6 | #else 7 | #include 8 | #include 9 | #include 10 | #endif 11 | 12 | #include 13 | #include 14 | #include 15 | #include "ftp_session.h" 16 | #include "ftp_session_cmd.h" 17 | 18 | #ifndef FTP_MAX_SESSION_NUM 19 | #define FTP_MAX_SESSION_NUM 10 20 | #endif 21 | 22 | #ifndef FTP_SESSION_USERNAME 23 | #define FTP_SESSION_USERNAME "loogg" 24 | #endif 25 | 26 | #ifndef FTP_SESSION_PASSWORD 27 | #define FTP_SESSION_PASSWORD "loogg" 28 | #endif 29 | 30 | #ifndef FTP_SESSION_WELCOME_MSG 31 | #define FTP_SESSION_WELCOME_MSG "220 -= welcome on RT-Thread FTP server =-\r\n" 32 | #endif 33 | 34 | #ifndef FTP_SESSION_TIMEOUT 35 | #define FTP_SESSION_TIMEOUT 3 36 | #endif 37 | 38 | static int ftp_max_session_num = FTP_MAX_SESSION_NUM; 39 | static char ftp_session_username[64] = FTP_SESSION_USERNAME; 40 | static char ftp_session_password[64] = FTP_SESSION_PASSWORD; 41 | static char ftp_session_welcome_msg[100] = FTP_SESSION_WELCOME_MSG; 42 | static rt_slist_t session_header = RT_SLIST_OBJECT_INIT(session_header); 43 | 44 | int ftp_get_max_session_num(void) 45 | { 46 | return ftp_max_session_num; 47 | } 48 | 49 | int ftp_set_max_session_num(int num) 50 | { 51 | if(num <= 0) 52 | return -RT_ERROR; 53 | 54 | ftp_max_session_num = num; 55 | return RT_EOK; 56 | } 57 | 58 | const char *ftp_get_session_username(void) 59 | { 60 | return ftp_session_username; 61 | } 62 | 63 | int ftp_set_session_username(const char *username) 64 | { 65 | if(username == RT_NULL) 66 | return -RT_ERROR; 67 | 68 | rt_strncpy(ftp_session_username, username, sizeof(ftp_session_username) - 1); 69 | ftp_session_username[sizeof(ftp_session_username) - 1] = '\0'; 70 | return RT_EOK; 71 | } 72 | 73 | const char *ftp_get_session_password(void) 74 | { 75 | return ftp_session_password; 76 | } 77 | 78 | int ftp_set_session_password(const char *password) 79 | { 80 | if(password == RT_NULL) 81 | return -RT_ERROR; 82 | 83 | rt_strncpy(ftp_session_password, password, sizeof(ftp_session_password) - 1); 84 | ftp_session_password[sizeof(ftp_session_password) - 1] = '\0'; 85 | return RT_EOK; 86 | } 87 | 88 | const char *ftp_get_session_welcome_msg(void) 89 | { 90 | return ftp_session_welcome_msg; 91 | } 92 | 93 | int ftp_set_session_welcome_msg(const char *welcome_msg) 94 | { 95 | if(welcome_msg == RT_NULL) 96 | return -RT_ERROR; 97 | 98 | rt_strncpy(ftp_session_welcome_msg, welcome_msg, sizeof(ftp_session_welcome_msg) - 1); 99 | ftp_session_welcome_msg[sizeof(ftp_session_welcome_msg) - 1] = '\0'; 100 | return RT_EOK; 101 | } 102 | 103 | static int ftp_session_get_num(void) 104 | { 105 | int num = 0; 106 | 107 | rt_base_t level = rt_hw_interrupt_disable(); 108 | num = rt_slist_len(&session_header); 109 | rt_hw_interrupt_enable(level); 110 | 111 | return num; 112 | } 113 | 114 | static int ftp_session_delete(struct ftp_session * session) 115 | { 116 | rt_base_t level = rt_hw_interrupt_disable(); 117 | rt_slist_remove(&session_header, &(session->slist)); 118 | rt_hw_interrupt_enable(level); 119 | 120 | close(session->fd); 121 | if(session->port_pasv_fd >= 0) 122 | close(session->port_pasv_fd); 123 | rt_free(session); 124 | 125 | return RT_EOK; 126 | } 127 | 128 | static int ftp_session_read(struct ftp_session *session, uint8_t *buf, int bufsz, int timeout) 129 | { 130 | int bytes = 0; 131 | int rc = 0; 132 | 133 | if(bufsz <= 0) 134 | return bufsz; 135 | 136 | while(bytes < bufsz) 137 | { 138 | rc = recv(session->fd, &buf[bytes], (size_t)(bufsz - bytes), MSG_DONTWAIT); 139 | if(rc <= 0) 140 | return -1; 141 | 142 | bytes += rc; 143 | if(bytes >= bufsz) 144 | break; 145 | 146 | if(timeout > 0) 147 | { 148 | fd_set readset, exceptset; 149 | struct timeval interval; 150 | 151 | interval.tv_sec = timeout / 1000; 152 | interval.tv_usec = (timeout % 1000) * 1000; 153 | 154 | FD_ZERO(&readset); 155 | FD_ZERO(&exceptset); 156 | FD_SET(session->fd, &readset); 157 | FD_SET(session->fd, &exceptset); 158 | 159 | rc = select(session->fd + 1, &readset, RT_NULL, &exceptset, &interval); 160 | if(rc < 0) 161 | return -1; 162 | if(rc == 0) 163 | break; 164 | if(FD_ISSET(session->fd, &exceptset)) 165 | return -1; 166 | } 167 | else 168 | break; 169 | } 170 | 171 | return bytes; 172 | } 173 | 174 | static int ftp_session_process(struct ftp_session * session, char *cmd_buf) 175 | { 176 | int result = RT_EOK; 177 | 178 | /* remove \r\n */ 179 | char *ptr = cmd_buf; 180 | while (*ptr) 181 | { 182 | if ((*ptr == '\r') || (*ptr == '\n')) 183 | *ptr = 0; 184 | ptr ++; 185 | } 186 | 187 | char *cmd = cmd_buf; 188 | char *cmd_param = strchr(cmd, ' '); 189 | if(cmd_param) 190 | { 191 | *cmd_param = '\0'; 192 | cmd_param++; 193 | } 194 | 195 | switch(session->state) 196 | { 197 | case FTP_SESSION_STATE_USER: 198 | { 199 | if(strstr(cmd, "USER") != cmd) 200 | { 201 | char *reply = "502 Not Implemented.\r\n"; 202 | send(session->fd, reply, strlen(reply), 0); 203 | break; 204 | } 205 | 206 | if(strcmp(cmd_param, "anonymous") == 0) 207 | { 208 | session->is_anonymous = 1; 209 | char *reply = "331 anonymous login OK send e-mail address for password.\r\n"; 210 | send(session->fd, reply, strlen(reply), 0); 211 | session->state = FTP_SESSION_STATE_PASSWD; 212 | break; 213 | } 214 | 215 | if(strcmp(cmd_param, ftp_session_username) == 0) 216 | { 217 | session->is_anonymous = 0; 218 | char *reply = "331 Password required.\r\n"; 219 | send(session->fd, reply, strlen(reply), 0); 220 | session->state = FTP_SESSION_STATE_PASSWD; 221 | break; 222 | } 223 | 224 | char *reply = "530 Login incorrect. Bye.\r\n"; 225 | send(session->fd, reply, strlen(reply), 0); 226 | result = -RT_ERROR; 227 | } 228 | break; 229 | 230 | case FTP_SESSION_STATE_PASSWD: 231 | { 232 | if(strstr(cmd, "PASS") != cmd) 233 | { 234 | char *reply = "502 Not Implemented.\r\n"; 235 | send(session->fd, reply, strlen(reply), 0); 236 | break; 237 | } 238 | 239 | if(session->is_anonymous || (strcmp(cmd_param, ftp_session_password) == 0)) 240 | { 241 | char *reply = "230 User logged in\r\n"; 242 | send(session->fd, reply, strlen(reply), 0); 243 | rt_memset(session->currentdir, 0, sizeof(session->currentdir)); 244 | session->currentdir[0] = '/'; 245 | session->state = FTP_SESSION_STATE_PROCESS; 246 | break; 247 | } 248 | 249 | char *reply = "530 Login incorrect. Bye.\r\n"; 250 | send(session->fd, reply, strlen(reply), 0); 251 | result = -RT_ERROR; 252 | } 253 | break; 254 | 255 | case FTP_SESSION_STATE_PROCESS: 256 | { 257 | int rc = ftp_session_cmd_process(session, cmd, cmd_param); 258 | if(rc != RT_EOK) 259 | { 260 | result = -RT_ERROR; 261 | break; 262 | } 263 | } 264 | break; 265 | 266 | default: 267 | result = -RT_ERROR; 268 | break; 269 | } 270 | 271 | session->tick_timeout = rt_tick_get() + rt_tick_from_millisecond(FTP_SESSION_TIMEOUT * 1000); 272 | 273 | return result; 274 | } 275 | 276 | static void ftp_client_entry(void *parameter) 277 | { 278 | struct ftp_session *session = parameter; 279 | int option = 1; 280 | int rc = setsockopt(session->fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&option, sizeof(int)); 281 | if(rc < 0) 282 | goto _exit; 283 | 284 | int flags; 285 | flags = fcntl(session->fd, F_GETFL, 0); 286 | flags |= O_NONBLOCK; 287 | fcntl(session->fd, F_SETFL, flags); 288 | 289 | session->port_pasv_fd = -1; 290 | session->is_anonymous = 0; 291 | session->offset = 0; 292 | session->state = FTP_SESSION_STATE_USER; 293 | session->tick_timeout = rt_tick_get() + rt_tick_from_millisecond(FTP_SESSION_TIMEOUT * 1000); 294 | 295 | char cmd_buf[1024]; 296 | 297 | // select使用 298 | fd_set readset, exceptset; 299 | // select超时时间 300 | struct timeval select_timeout; 301 | select_timeout.tv_sec = 1; 302 | select_timeout.tv_usec = 0; 303 | 304 | send(session->fd, ftp_session_welcome_msg, strlen(ftp_session_welcome_msg), 0); 305 | 306 | while(1) 307 | { 308 | if(session->force_quit) 309 | break; 310 | 311 | FD_ZERO(&readset); 312 | FD_ZERO(&exceptset); 313 | 314 | FD_SET(session->fd, &readset); 315 | FD_SET(session->fd, &exceptset); 316 | 317 | rc = select(session->fd + 1, &readset, RT_NULL, &exceptset, &select_timeout); 318 | if(rc < 0) 319 | break; 320 | if(rc > 0) 321 | { 322 | if(FD_ISSET(session->fd, &exceptset)) 323 | break; 324 | if(FD_ISSET(session->fd, &readset)) 325 | { 326 | int cmd_len = ftp_session_read(session, (uint8_t *)cmd_buf, sizeof(cmd_buf) - 1, 30); 327 | if(cmd_len <= 0) 328 | break; 329 | cmd_buf[cmd_len] = '\0'; 330 | if(ftp_session_process(session, cmd_buf) != RT_EOK) 331 | break; 332 | } 333 | } 334 | 335 | if((rt_tick_get() - session->tick_timeout) < (RT_TICK_MAX / 2)) 336 | break; 337 | } 338 | 339 | _exit: 340 | ftp_session_delete(session); 341 | } 342 | 343 | int ftp_session_create(int fd, struct sockaddr_in *addr, socklen_t addr_len) 344 | { 345 | if(fd < 0) 346 | return -RT_ERROR; 347 | 348 | if(ftp_session_get_num() >= ftp_max_session_num) 349 | return -RT_ERROR; 350 | 351 | struct ftp_session *session = rt_malloc(sizeof(struct ftp_session)); 352 | if(session == RT_NULL) 353 | return -RT_ERROR; 354 | rt_memset(session, 0, sizeof(struct ftp_session)); 355 | session->fd = fd; 356 | rt_memcpy(&(session->remote), addr, addr_len); 357 | rt_slist_init(&(session->slist)); 358 | rt_base_t level = rt_hw_interrupt_disable(); 359 | rt_slist_append(&session_header, &(session->slist)); 360 | rt_hw_interrupt_enable(level); 361 | 362 | rt_thread_t tid = rt_thread_create("ftpc", ftp_client_entry, session, 4096, 27, 100); 363 | RT_ASSERT(tid != RT_NULL); 364 | rt_thread_startup(tid); 365 | 366 | return RT_EOK; 367 | } 368 | 369 | int ftp_session_force_quit(void) 370 | { 371 | rt_slist_t *node; 372 | rt_base_t level = rt_hw_interrupt_disable(); 373 | rt_slist_for_each(node, &session_header) 374 | { 375 | struct ftp_session *session = rt_slist_entry(node, struct ftp_session, slist); 376 | session->force_quit = 1; 377 | } 378 | rt_hw_interrupt_enable(level); 379 | 380 | return RT_EOK; 381 | } 382 | -------------------------------------------------------------------------------- /src/ftp_session_cmd.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #if RT_VER_NUM < 0x40100 4 | #include 5 | #include 6 | #else 7 | #include 8 | #include 9 | #include 10 | #endif 11 | 12 | #include 13 | #include 14 | #include 15 | #include "ftp_session_cmd.h" 16 | 17 | static int ftp_create_dir(const char *path) 18 | { 19 | int result = RT_EOK; 20 | 21 | DIR *dir = opendir(path); 22 | if(dir == RT_NULL) 23 | { 24 | if(mkdir(path, 0x777) != 0) 25 | result = -RT_ERROR; 26 | } 27 | else 28 | closedir(dir); 29 | 30 | return result; 31 | } 32 | 33 | static char* ftp_normalize_path(char* fullpath) 34 | { 35 | char *dst0, *dst, *src; 36 | 37 | src = fullpath; 38 | dst = fullpath; 39 | 40 | dst0 = dst; 41 | while (1) 42 | { 43 | char c = *src; 44 | 45 | if (c == '.') 46 | { 47 | if (!src[1]) src ++; /* '.' and ends */ 48 | else if (src[1] == '/') 49 | { 50 | /* './' case */ 51 | src += 2; 52 | 53 | while ((*src == '/') && (*src != '\0')) src ++; 54 | continue; 55 | } 56 | else if (src[1] == '.') 57 | { 58 | if (!src[2]) 59 | { 60 | /* '..' and ends case */ 61 | src += 2; 62 | goto up_one; 63 | } 64 | else if (src[2] == '/') 65 | { 66 | /* '../' case */ 67 | src += 3; 68 | 69 | while ((*src == '/') && (*src != '\0')) src ++; 70 | goto up_one; 71 | } 72 | } 73 | } 74 | 75 | /* copy up the next '/' and erase all '/' */ 76 | while ((c = *src++) != '\0' && c != '/') *dst ++ = c; 77 | 78 | if (c == '/') 79 | { 80 | *dst ++ = '/'; 81 | while (c == '/') c = *src++; 82 | 83 | src --; 84 | } 85 | else if (!c) break; 86 | 87 | continue; 88 | 89 | up_one: 90 | dst --; 91 | if (dst < dst0) return RT_NULL; 92 | while (dst0 < dst && dst[-1] != '/') dst --; 93 | } 94 | 95 | *dst = '\0'; 96 | 97 | /* remove '/' in the end of path if exist */ 98 | dst --; 99 | if ((dst != fullpath) && (*dst == '/')) *dst = '\0'; 100 | 101 | return fullpath; 102 | } 103 | 104 | static int port_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 105 | { 106 | if (session->port_pasv_fd >= 0) 107 | { 108 | close(session->port_pasv_fd); 109 | session->port_pasv_fd = -1; 110 | } 111 | 112 | char *reply = RT_NULL; 113 | int portcom[6]; 114 | char iptmp[100]; 115 | int index = 0; 116 | char *ptr = cmd_param; 117 | while (ptr != RT_NULL) 118 | { 119 | if (*ptr == ',') 120 | ptr++; 121 | portcom[index] = atoi(ptr); 122 | if ((portcom[index] < 0) || (portcom[index] > 255)) 123 | break; 124 | index++; 125 | if (index == 6) 126 | break; 127 | ptr = strchr(ptr, ','); 128 | } 129 | if (index < 6) 130 | { 131 | reply = "504 invalid parameter.\r\n"; 132 | send(session->fd, reply, strlen(reply), 0); 133 | return RT_EOK; 134 | } 135 | 136 | snprintf(iptmp, sizeof(iptmp), "%d.%d.%d.%d", portcom[0], portcom[1], portcom[2], portcom[3]); 137 | 138 | int rc = -RT_ERROR; 139 | do 140 | { 141 | session->port_pasv_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 142 | if (session->port_pasv_fd < 0) 143 | break; 144 | struct timeval tv; 145 | tv.tv_sec = 20; 146 | tv.tv_usec = 0; 147 | if (setsockopt(session->port_pasv_fd, SOL_SOCKET, SO_SNDTIMEO, (const void *)&tv, sizeof(struct timeval)) < 0) 148 | break; 149 | struct sockaddr_in addr; 150 | rt_memset(&addr, 0, sizeof(addr)); 151 | addr.sin_family = AF_INET; 152 | addr.sin_port = htons(portcom[4] * 256 + portcom[5]); 153 | addr.sin_addr.s_addr = inet_addr(iptmp); 154 | if (connect(session->port_pasv_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) 155 | break; 156 | 157 | rc = RT_EOK; 158 | } while (0); 159 | 160 | if (rc != RT_EOK) 161 | { 162 | reply = "425 Can't open data connection.\r\n"; 163 | send(session->fd, reply, strlen(reply), 0); 164 | if (session->port_pasv_fd >= 0) 165 | { 166 | close(session->port_pasv_fd); 167 | session->port_pasv_fd = -1; 168 | } 169 | return RT_EOK; 170 | } 171 | 172 | reply = "200 Port Command Successful.\r\n"; 173 | send(session->fd, reply, strlen(reply), 0); 174 | return RT_EOK; 175 | } 176 | 177 | static int pwd_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 178 | { 179 | char *reply = rt_malloc(1024); 180 | if(reply == RT_NULL) 181 | return -RT_ERROR; 182 | 183 | snprintf(reply, 1024, "257 \"%s\" is current directory.\r\n", session->currentdir); 184 | send(session->fd, reply, strlen(reply), 0); 185 | rt_free(reply); 186 | return RT_EOK; 187 | } 188 | 189 | static int type_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 190 | { 191 | // Ignore it 192 | char *reply = RT_NULL; 193 | if (strcmp(cmd_param, "I") == 0) 194 | reply = "200 Type set to binary.\r\n"; 195 | else 196 | reply = "200 Type set to ascii.\r\n"; 197 | 198 | send(session->fd, reply, strlen(reply), 0); 199 | return RT_EOK; 200 | } 201 | 202 | static int syst_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 203 | { 204 | char *reply = "215 RT-Thread RTOS\r\n"; 205 | send(session->fd, reply, strlen(reply), 0); 206 | return RT_EOK; 207 | } 208 | 209 | static int quit_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 210 | { 211 | char *reply = "221 Bye!\r\n"; 212 | send(session->fd, reply, strlen(reply), 0); 213 | 214 | return -RT_ERROR; 215 | } 216 | 217 | static int list_statbuf_get(struct dirent *dirent, struct stat *s, char *buf, int bufsz) 218 | { 219 | int ret = 0; 220 | struct tm ftm; 221 | struct tm ntm; 222 | time_t now_time; 223 | 224 | // file type 225 | rt_memset(buf, '-', 10); 226 | 227 | if (S_ISDIR(s->st_mode)) 228 | buf[0] = 'd'; 229 | 230 | if (s->st_mode & S_IRUSR) 231 | buf[1] = 'r'; 232 | if (s->st_mode & S_IWUSR) 233 | buf[2] = 'w'; 234 | if (s->st_mode & S_IXUSR) 235 | buf[3] = 'x'; 236 | 237 | if (s->st_mode & S_IRGRP) 238 | buf[4] = 'r'; 239 | if (s->st_mode & S_IWGRP) 240 | buf[5] = 'w'; 241 | if (s->st_mode & S_IXGRP) 242 | buf[6] = 'x'; 243 | 244 | if (s->st_mode & S_IROTH) 245 | buf[7] = 'r'; 246 | if (s->st_mode & S_IWOTH) 247 | buf[8] = 'w'; 248 | if (s->st_mode & S_IXOTH) 249 | buf[9] = 'x'; 250 | 251 | ret += 10; 252 | buf[ret++] = ' '; 253 | 254 | // user info 255 | ret += snprintf(buf + ret, bufsz - ret, "%d %s %s", s->st_nlink, "admin", "admin"); 256 | buf[ret++] = ' '; 257 | 258 | // file size 259 | ret += snprintf(buf + ret, bufsz - ret, "%d", s->st_size); 260 | buf[ret++] = ' '; 261 | 262 | // file date 263 | gmtime_r(&s->st_mtime, &ftm); 264 | now_time = time(RT_NULL); 265 | gmtime_r(&now_time, &ntm); 266 | 267 | if (ftm.tm_year == ntm.tm_year) { 268 | ret += strftime(buf + ret, bufsz - ret, "%b %d %H:%M", &ftm); 269 | } else { 270 | ret += strftime(buf + ret, bufsz - ret, "%b %d %Y", &ftm); 271 | } 272 | 273 | buf[ret++] = ' '; 274 | 275 | // file name 276 | ret += snprintf(buf + ret, bufsz - ret, "%s\r\n", dirent->d_name); 277 | 278 | return ret; 279 | } 280 | 281 | static int list_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 282 | { 283 | char *reply = RT_NULL; 284 | if (session->port_pasv_fd < 0) 285 | { 286 | reply = "502 Not Implemented.\r\n"; 287 | send(session->fd, reply, strlen(reply), 0); 288 | return RT_EOK; 289 | } 290 | 291 | DIR *dir = opendir(session->currentdir); 292 | if(dir == RT_NULL) 293 | { 294 | reply = rt_malloc(1024); 295 | if(reply == RT_NULL) 296 | return -RT_ERROR; 297 | 298 | snprintf(reply, 1024, "550 directory \"%s\" can't open.\r\n", session->currentdir); 299 | send(session->fd, reply, strlen(reply), 0); 300 | rt_free(reply); 301 | return RT_EOK; 302 | } 303 | 304 | reply = "150 Opening Binary mode connection for file list.\r\n"; 305 | send(session->fd, reply, strlen(reply), 0); 306 | 307 | struct dirent *dirent = RT_NULL; 308 | char tmp[256]; 309 | struct stat s; 310 | do { 311 | dirent = readdir(dir); 312 | if (dirent == RT_NULL) 313 | break; 314 | snprintf(tmp, sizeof(tmp), "%s/%s", session->currentdir, dirent->d_name); 315 | rt_memset(&s, 0, sizeof(struct stat)); 316 | if (stat(tmp, &s) != 0) 317 | continue; 318 | 319 | int stat_len = list_statbuf_get(dirent, &s, tmp, sizeof(tmp)); 320 | if (stat_len <= 0) 321 | continue; 322 | 323 | send(session->port_pasv_fd, tmp, stat_len, 0); 324 | } while (dirent != RT_NULL); 325 | 326 | closedir(dir); 327 | 328 | close(session->port_pasv_fd); 329 | session->port_pasv_fd = -1; 330 | 331 | reply = "226 Transfert Complete.\r\n"; 332 | send(session->fd, reply, strlen(reply), 0); 333 | return RT_EOK; 334 | } 335 | 336 | static int nlist_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 337 | { 338 | char *reply = RT_NULL; 339 | if (session->port_pasv_fd < 0) 340 | { 341 | reply = "502 Not Implemented.\r\n"; 342 | send(session->fd, reply, strlen(reply), 0); 343 | return RT_EOK; 344 | } 345 | 346 | DIR *dir = opendir(session->currentdir); 347 | if (dir == RT_NULL) 348 | { 349 | reply = rt_malloc(1024); 350 | if(reply == RT_NULL) 351 | return -RT_ERROR; 352 | 353 | snprintf(reply, 1024, "550 directory \"%s\" can't open.\r\n", session->currentdir); 354 | send(session->fd, reply, strlen(reply), 0); 355 | rt_free(reply); 356 | return RT_EOK; 357 | } 358 | 359 | reply = "150 Opening Binary mode connection for file list.\r\n"; 360 | send(session->fd, reply, strlen(reply), 0); 361 | 362 | struct dirent *dirent = RT_NULL; 363 | char tmp[256]; 364 | do 365 | { 366 | dirent = readdir(dir); 367 | if (dirent == RT_NULL) 368 | break; 369 | snprintf(tmp, sizeof(tmp), "%s\r\n", dirent->d_name); 370 | send(session->port_pasv_fd, tmp, strlen(tmp), 0); 371 | } while (dirent != RT_NULL); 372 | 373 | closedir(dir); 374 | 375 | close(session->port_pasv_fd); 376 | session->port_pasv_fd = -1; 377 | 378 | reply = "226 Transfert Complete.\r\n"; 379 | send(session->fd, reply, strlen(reply), 0); 380 | return RT_EOK; 381 | } 382 | 383 | static int build_full_path(char *buf, int bufsz, const char *path) 384 | { 385 | if(path[0] == '/') 386 | snprintf(buf, bufsz, "%s", path); 387 | else 388 | { 389 | strcat(buf, "/"); 390 | int remain_len = bufsz - strlen(buf) - 1; 391 | strncat(buf, path, remain_len); 392 | } 393 | 394 | if(ftp_normalize_path(buf) == RT_NULL) 395 | return -RT_ERROR; 396 | 397 | return RT_EOK; 398 | } 399 | 400 | static int cwd_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 401 | { 402 | if(build_full_path(session->currentdir, sizeof(session->currentdir), cmd_param) != RT_EOK) 403 | return -RT_ERROR; 404 | 405 | char *reply = RT_NULL; 406 | DIR *dir = opendir(session->currentdir); 407 | if (dir == RT_NULL) 408 | { 409 | reply = rt_malloc(1024); 410 | if(reply == RT_NULL) 411 | return -RT_ERROR; 412 | 413 | snprintf(reply, 1024, "550 directory \"%s\" can't open.\r\n", session->currentdir); 414 | send(session->fd, reply, strlen(reply), 0); 415 | rt_free(reply); 416 | return RT_EOK; 417 | } 418 | 419 | closedir(dir); 420 | 421 | reply = rt_malloc(1024); 422 | if(reply == RT_NULL) 423 | return -RT_ERROR; 424 | 425 | snprintf(reply, 1024, "250 Changed to directory \"%s\"\r\n", session->currentdir); 426 | send(session->fd, reply, strlen(reply), 0); 427 | rt_free(reply); 428 | return RT_EOK; 429 | } 430 | 431 | static int cdup_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 432 | { 433 | if(build_full_path(session->currentdir, sizeof(session->currentdir), "..") != RT_EOK) 434 | return -RT_ERROR; 435 | 436 | char *reply = RT_NULL; 437 | DIR *dir = opendir(session->currentdir); 438 | if (dir == RT_NULL) 439 | { 440 | reply = rt_malloc(1024); 441 | if(reply == RT_NULL) 442 | return -RT_ERROR; 443 | 444 | snprintf(reply, 1024, "550 directory \"%s\" can't open.\r\n", session->currentdir); 445 | send(session->fd, reply, strlen(reply), 0); 446 | rt_free(reply); 447 | return RT_EOK; 448 | } 449 | 450 | closedir(dir); 451 | 452 | reply = rt_malloc(1024); 453 | if(reply == RT_NULL) 454 | return -RT_ERROR; 455 | 456 | snprintf(reply, 1024, "250 Changed to directory \"%s\"\r\n", session->currentdir); 457 | send(session->fd, reply, strlen(reply), 0); 458 | rt_free(reply); 459 | return RT_EOK; 460 | } 461 | 462 | static int mkd_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 463 | { 464 | char *reply = RT_NULL; 465 | if(session->is_anonymous) 466 | { 467 | reply = "550 Permission denied.\r\n"; 468 | send(session->fd, reply, strlen(reply), 0); 469 | return RT_EOK; 470 | } 471 | 472 | char path[256]; 473 | snprintf(path, sizeof(path), "%s", session->currentdir); 474 | if(build_full_path(path, sizeof(path), cmd_param) != RT_EOK) 475 | return -RT_ERROR; 476 | 477 | reply = rt_malloc(1024); 478 | if(reply == RT_NULL) 479 | return -RT_ERROR; 480 | 481 | if(ftp_create_dir(path) != RT_EOK) 482 | snprintf(reply, 1024, "550 directory \"%s\" create error.\r\n", path); 483 | else 484 | snprintf(reply, 1024, "257 directory \"%s\" successfully created.\r\n", path); 485 | 486 | send(session->fd, reply, strlen(reply), 0); 487 | rt_free(reply); 488 | return RT_EOK; 489 | } 490 | 491 | static int rmd_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 492 | { 493 | char *reply = RT_NULL; 494 | if(session->is_anonymous) 495 | { 496 | reply = "550 Permission denied.\r\n"; 497 | send(session->fd, reply, strlen(reply), 0); 498 | return RT_EOK; 499 | } 500 | 501 | char path[256]; 502 | snprintf(path, sizeof(path), "%s", session->currentdir); 503 | if(build_full_path(path, sizeof(path), cmd_param) != RT_EOK) 504 | return -RT_ERROR; 505 | 506 | reply = rt_malloc(1024); 507 | if(reply == RT_NULL) 508 | return -RT_ERROR; 509 | 510 | if(unlink(path) != 0) 511 | snprintf(reply, 1024, "550 directory \"%s\" delete error.\r\n", path); 512 | else 513 | snprintf(reply, 1024, "257 directory \"%s\" successfully deleted.\r\n", path); 514 | 515 | send(session->fd, reply, strlen(reply), 0); 516 | rt_free(reply); 517 | return RT_EOK; 518 | } 519 | 520 | static int dele_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 521 | { 522 | char *reply = RT_NULL; 523 | if(session->is_anonymous) 524 | { 525 | reply = "550 Permission denied.\r\n"; 526 | send(session->fd, reply, strlen(reply), 0); 527 | return RT_EOK; 528 | } 529 | 530 | char path[256]; 531 | snprintf(path, sizeof(path), "%s", session->currentdir); 532 | if(build_full_path(path, sizeof(path), cmd_param) != RT_EOK) 533 | return -RT_ERROR; 534 | 535 | reply = rt_malloc(1024); 536 | if(reply == RT_NULL) 537 | return -RT_ERROR; 538 | 539 | if(unlink(path) != 0) 540 | snprintf(reply, 1024, "550 file \"%s\" delete error.\r\n", path); 541 | else 542 | snprintf(reply, 1024, "250 file \"%s\" successfully deleted.\r\n", path); 543 | 544 | send(session->fd, reply, strlen(reply), 0); 545 | rt_free(reply); 546 | return RT_EOK; 547 | } 548 | 549 | static int size_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 550 | { 551 | char *reply = RT_NULL; 552 | char path[256]; 553 | snprintf(path, sizeof(path), "%s", session->currentdir); 554 | if(build_full_path(path, sizeof(path), cmd_param) != RT_EOK) 555 | return -RT_ERROR; 556 | 557 | struct stat s; 558 | rt_memset(&s, 0, sizeof(struct stat)); 559 | if(stat(path, &s) != 0) 560 | { 561 | reply = rt_malloc(1024); 562 | if(reply == RT_NULL) 563 | return -RT_ERROR; 564 | 565 | snprintf(reply, 1024, "550 \"%s\" : not a regular file\r\n", path); 566 | send(session->fd, reply, strlen(reply), 0); 567 | rt_free(reply); 568 | return RT_EOK; 569 | } 570 | 571 | if(!S_ISREG(s.st_mode)) 572 | { 573 | reply = rt_malloc(1024); 574 | if(reply == RT_NULL) 575 | return -RT_ERROR; 576 | 577 | snprintf(reply, 1024, "550 \"%s\" : not a regular file\r\n", path); 578 | send(session->fd, reply, strlen(reply), 0); 579 | rt_free(reply); 580 | return RT_EOK; 581 | } 582 | 583 | reply = rt_malloc(1024); 584 | if(reply == RT_NULL) 585 | return -RT_ERROR; 586 | 587 | snprintf(reply, 1024, "213 %d\r\n", s.st_size); 588 | send(session->fd, reply, strlen(reply), 0); 589 | rt_free(reply); 590 | return RT_EOK; 591 | } 592 | 593 | static int rest_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 594 | { 595 | char *reply = RT_NULL; 596 | 597 | int offset = atoi(cmd_param); 598 | if(offset < 0) 599 | { 600 | reply = "504 invalid parameter.\r\n"; 601 | send(session->fd, reply, strlen(reply), 0); 602 | session->offset = 0; 603 | return RT_EOK; 604 | } 605 | 606 | reply = "350 Send RETR or STOR to start transfert.\r\n"; 607 | send(session->fd, reply, strlen(reply), 0); 608 | session->offset = offset; 609 | return RT_EOK; 610 | } 611 | 612 | static int retr_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 613 | { 614 | char *reply = RT_NULL; 615 | if (session->port_pasv_fd < 0) 616 | { 617 | reply = "502 Not Implemented.\r\n"; 618 | send(session->fd, reply, strlen(reply), 0); 619 | session->offset = 0; 620 | return RT_EOK; 621 | } 622 | 623 | char path[256]; 624 | snprintf(path, sizeof(path), "%s", session->currentdir); 625 | if(build_full_path(path, sizeof(path), cmd_param) != RT_EOK) 626 | return -RT_ERROR; 627 | 628 | int fd = open(path, O_RDONLY); 629 | if(fd < 0) 630 | { 631 | reply = rt_malloc(1024); 632 | if(reply == RT_NULL) 633 | return -RT_ERROR; 634 | 635 | snprintf(reply, 1024, "550 \"%s\" : not a regular file\r\n", path); 636 | send(session->fd, reply, strlen(reply), 0); 637 | rt_free(reply); 638 | session->offset = 0; 639 | return RT_EOK; 640 | } 641 | 642 | int rc = -RT_ERROR; 643 | int file_size = 0; 644 | do 645 | { 646 | file_size = lseek(fd, 0, SEEK_END); 647 | lseek(fd, 0, SEEK_SET); 648 | if(file_size <= 0) 649 | break; 650 | 651 | rc = RT_EOK; 652 | }while(0); 653 | 654 | if(rc != RT_EOK) 655 | { 656 | close(fd); 657 | 658 | reply = rt_malloc(1024); 659 | if(reply == RT_NULL) 660 | return -RT_ERROR; 661 | 662 | snprintf(reply, 1024, "550 \"%s\" : not a regular file\r\n", path); 663 | send(session->fd, reply, strlen(reply), 0); 664 | rt_free(reply); 665 | session->offset = 0; 666 | return RT_EOK; 667 | } 668 | 669 | reply = rt_malloc(4096); 670 | if(reply == RT_NULL) 671 | { 672 | close(fd); 673 | return -RT_ERROR; 674 | } 675 | 676 | if((session->offset > 0) && (session->offset < file_size)) 677 | { 678 | lseek(fd, session->offset, SEEK_SET); 679 | snprintf(reply, 4096, "150 Opening binary mode data connection for \"%s\" (%d/%d bytes).\r\n", 680 | path, file_size - session->offset, file_size); 681 | } 682 | else 683 | { 684 | snprintf(reply, 4096, "150 Opening binary mode data connection for \"%s\" (%d bytes).\r\n", 685 | path, file_size); 686 | } 687 | send(session->fd, reply, strlen(reply), 0); 688 | 689 | int recv_bytes = 0; 690 | int result = RT_EOK; 691 | while((recv_bytes = read(fd, reply, 4096)) > 0) 692 | { 693 | if(send(session->port_pasv_fd, reply, recv_bytes, 0) != recv_bytes) 694 | { 695 | result = -RT_ERROR; 696 | break; 697 | } 698 | } 699 | 700 | rt_free(reply); 701 | close(fd); 702 | close(session->port_pasv_fd); 703 | session->port_pasv_fd = -1; 704 | 705 | if(result != RT_EOK) 706 | return -RT_ERROR; 707 | 708 | reply = "226 Finished.\r\n"; 709 | send(session->fd, reply, strlen(reply), 0); 710 | session->offset = 0; 711 | return RT_EOK; 712 | } 713 | 714 | static int stor_cmd_receive(int socket, uint8_t *buf, int bufsz, int timeout) 715 | { 716 | if((socket < 0) || (buf == RT_NULL) || (bufsz <= 0) || (timeout <= 0)) 717 | return -RT_ERROR; 718 | 719 | int len = 0; 720 | int rc = 0; 721 | fd_set rset; 722 | struct timeval tv; 723 | 724 | FD_ZERO(&rset); 725 | FD_SET(socket, &rset); 726 | tv.tv_sec = timeout / 1000; 727 | tv.tv_usec = (timeout % 1000) * 1000; 728 | 729 | while(bufsz > 0) 730 | { 731 | rc = select(socket + 1, &rset, RT_NULL, RT_NULL, &tv); 732 | if(rc <= 0) 733 | break; 734 | 735 | rc = recv(socket, buf + len, bufsz, MSG_DONTWAIT); 736 | if(rc <= 0) 737 | break; 738 | 739 | len += rc; 740 | bufsz -= rc; 741 | 742 | tv.tv_sec = 3; 743 | tv.tv_usec = 0; 744 | FD_ZERO(&rset); 745 | FD_SET(socket, &rset); 746 | } 747 | 748 | if(rc >= 0) 749 | rc = len; 750 | 751 | return rc; 752 | } 753 | 754 | static int stor_cmd_fn(struct ftp_session *session, char *cmd, char *cmd_param) 755 | { 756 | session->offset = 0; 757 | 758 | char *reply = RT_NULL; 759 | if(session->is_anonymous) 760 | { 761 | reply = "550 Permission denied.\r\n"; 762 | send(session->fd, reply, strlen(reply), 0); 763 | return RT_EOK; 764 | } 765 | 766 | if (session->port_pasv_fd < 0) 767 | { 768 | reply = "502 Not Implemented.\r\n"; 769 | send(session->fd, reply, strlen(reply), 0); 770 | return RT_EOK; 771 | } 772 | 773 | char path[256]; 774 | snprintf(path, sizeof(path), "%s", session->currentdir); 775 | if(build_full_path(path, sizeof(path), cmd_param) != RT_EOK) 776 | return -RT_ERROR; 777 | 778 | int fd = open(path, O_CREAT | O_RDWR | O_TRUNC); 779 | if(fd < 0) 780 | { 781 | reply = rt_malloc(1024); 782 | if(reply == RT_NULL) 783 | return -RT_ERROR; 784 | 785 | snprintf(reply, 1024, "550 Cannot open \"%s\" for writing.\r\n", path); 786 | send(session->fd, reply, strlen(reply), 0); 787 | rt_free(reply); 788 | return RT_EOK; 789 | } 790 | 791 | reply = rt_malloc(4096); 792 | if(reply == RT_NULL) 793 | { 794 | close(fd); 795 | return -RT_ERROR; 796 | } 797 | 798 | snprintf(reply, 4096, "150 Opening binary mode data connection for \"%s\".\r\n", path); 799 | send(session->fd, reply, strlen(reply), 0); 800 | 801 | int result = RT_EOK; 802 | int timeout = 3000; 803 | while(1) 804 | { 805 | int recv_bytes = stor_cmd_receive(session->port_pasv_fd, (uint8_t *)reply, 4096, timeout); 806 | if(recv_bytes < 0) 807 | { 808 | result = -RT_ERROR; 809 | break; 810 | } 811 | if(recv_bytes == 0) 812 | break; 813 | if(write(fd, reply, recv_bytes) != recv_bytes) 814 | { 815 | result = -RT_ERROR; 816 | break; 817 | } 818 | fsync(fd); 819 | 820 | timeout = 3000; 821 | } 822 | 823 | rt_free(reply); 824 | close(fd); 825 | close(session->port_pasv_fd); 826 | session->port_pasv_fd = -1; 827 | 828 | if(result != RT_EOK) 829 | return -RT_ERROR; 830 | 831 | reply = "226 Finished.\r\n"; 832 | send(session->fd, reply, strlen(reply), 0); 833 | return RT_EOK; 834 | } 835 | 836 | static struct ftp_session_cmd session_cmds[] = 837 | { 838 | {"PORT", port_cmd_fn}, 839 | {"PWD", pwd_cmd_fn}, 840 | {"XPWD", pwd_cmd_fn}, 841 | {"TYPE", type_cmd_fn}, 842 | {"SYST", syst_cmd_fn}, 843 | {"QUIT", quit_cmd_fn}, 844 | {"LIST", list_cmd_fn}, 845 | {"NLST", nlist_cmd_fn}, 846 | {"CWD", cwd_cmd_fn}, 847 | {"CDUP", cdup_cmd_fn}, 848 | {"MKD", mkd_cmd_fn}, 849 | {"RMD", rmd_cmd_fn}, 850 | {"DELE", dele_cmd_fn}, 851 | {"SIZE", size_cmd_fn}, 852 | {"REST", rest_cmd_fn}, 853 | {"RETR", retr_cmd_fn}, 854 | {"STOR", stor_cmd_fn} 855 | }; 856 | 857 | int ftp_session_cmd_process(struct ftp_session *session, char *cmd, char *cmd_param) 858 | { 859 | int array_cnt = sizeof(session_cmds) / sizeof(session_cmds[0]); 860 | struct ftp_session_cmd *session_cmd = RT_NULL; 861 | 862 | for (int i = 0; i < array_cnt; i++) 863 | { 864 | if(strstr(cmd, session_cmds[i].cmd) == cmd) 865 | { 866 | session_cmd = &session_cmds[i]; 867 | break; 868 | } 869 | } 870 | 871 | if(session_cmd == RT_NULL) 872 | { 873 | char *reply = "502 Not Implemented.\r\n"; 874 | send(session->fd, reply, strlen(reply), 0); 875 | return RT_EOK; 876 | } 877 | 878 | int result = session_cmd->cmd_fn(session, cmd, cmd_param); 879 | 880 | return result; 881 | } 882 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | (This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.) 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | {description} 474 | Copyright (C) {year} {fullname} 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 489 | USA 490 | 491 | Also add information on how to contact you by electronic and paper mail. 492 | 493 | You should also get your employer (if you work as a programmer) or your 494 | school, if any, to sign a "copyright disclaimer" for the library, if 495 | necessary. Here is a sample; alter the names: 496 | 497 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 498 | library `Frob' (a library for tweaking knobs) written by James Random 499 | Hacker. 500 | 501 | {signature of Ty Coon}, 1 April 1990 502 | Ty Coon, President of Vice 503 | 504 | That's all there is to it! 505 | --------------------------------------------------------------------------------