int main(int argc, char ** argv) { uint16_t l_port; int l_socket; int exit_status; if ( (l_port = get_port_from_commandline(argc, argv)) == 0 ) { return EXIT_FAILURE; } if ( (l_socket = create_tcp_server_socket(l_port)) == -1 ) { return EXIT_FAILURE; } exit_status = start_threaded_tcp_server(l_socket, echo_server); return exit_status; }
// perform FTP PASV command, prepare for passive data connection void command_pasv(CLIENT_INFO * client_info) { char line[BUFFER_SIZE] = { 0 }; client_read_line(client_info->fd, client_info->buf, &client_info->buffer_pos); extract_line(line, client_info->buf, &client_info->buffer_pos); int len = strlen(line); if (len != 4) { send_code(client_info->fd, 500); return; } if (client_info->passive_fd != 0) { close(client_info->passive_fd); client_info->passive_fd = 0; } struct sockaddr_in s; socklen_t l; l = sizeof(s); getsockname(client_info->fd, (struct sockaddr *)&s, &l); char * ip = inet_ntoa(s.sin_addr); if (! ip) epicfail("inet_ntoa"); client_info->data_connection_mode = CONN_MODE_PASSIVE; client_info->passive_fd = create_tcp_server_socket(ip, 0); l = sizeof(s); getsockname(client_info->passive_fd, (struct sockaddr *)&s, &l); int port = ntohs(s.sin_port); int ip1, ip2, ip3, ip4, port1, port2; if (sscanf(ip, "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4) != 4) epicfail("command_pasv"); port1 = port >> 8; port2 = port & 0xff; char p[64]; sprintf(p, "%d,%d,%d,%d,%d,%d", ip1, ip2, ip3, ip4, port1, port2); send_code_param(client_info->fd, 227, p); }
// main entry point int main(int argc, char * argv[]) { char * source_addr = "0.0.0.0"; int source_port = 21; strcpy(basedir, "/"); int c; while ((c = getopt (argc, argv, ":s:p:d:h")) != -1) { if (c == 's') { source_addr = optarg; } else if (c == 'p') { source_port = atoi(optarg); } else if (c == 'd') { strcpy(basedir, optarg); } else if (c == ':') { printf("-%c requires an argument\n", optopt); return 1; } else if (c == 'h') { return help(); } else if (c == '?') { printf("Unrecognized parameter. Use '-h' for help.\n"); return 1; } } struct stat s; if (stat(basedir, &s) == -1) { printf("The specified base directory does not exist.\n"); return 1; } char tmpbasedir[PATH_MAX + 1] = { 0 }; if (! realpath(basedir, tmpbasedir)) epicfail("realpath"); strcpy(basedir, tmpbasedir); printf("adoftp server is starting...\n"); printf("base directory is %s\n", basedir); if (strcmp(basedir, "/") == 0) strcpy(basedir, ""); printf("listening on %s:%d\n", source_addr, source_port); int server = create_tcp_server_socket(source_addr, source_port); while (1) { int client = accept_connection(server); pthread_t thread_id; int result = pthread_create(&thread_id, NULL, thread_proc, (void *)client); if (result) epicfail("pthread_create"); } }