| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
int ftp_cwd (int sck, char *Directory, int verbose) { |
int ftp_cwd (int sck, char *Directory, int verbose) |
| 56 |
|
{ |
| 57 |
|
|
| 58 |
char buffer[1024]={'\0'}; |
char buffer[1024]={'\0'}; |
| 59 |
char *buf; |
char *buf; |
| 60 |
int n = 0; |
int n = 0; |
| 61 |
|
struct timeval tm; //tv_sec - tv_usec |
| 62 |
|
fd_set readfds; |
| 63 |
|
|
|
buf = (char *)malloc(1024*sizeof(char)); |
|
| 64 |
|
|
| 65 |
memset(buffer, 0x0, 1024); |
buf = (char *)malloc(1024*sizeof(char)); |
|
memset(buf, 0x0, 1024); |
|
| 66 |
|
|
| 67 |
if ( strlen(Directory) > 1015 ) { |
memset(buffer, 0x0, 1024); |
| 68 |
|
memset(buf, 0x0, 1024); |
| 69 |
|
|
| 70 |
|
if ( strlen(Directory) > 1015 ) { |
| 71 |
free(buf); |
free(buf); |
| 72 |
|
|
| 73 |
return DIR_TOO_LONG; |
return DIR_TOO_LONG; |
| 74 |
} |
} |
| 75 |
|
|
| 76 |
|
|
| 77 |
sprintf(buffer, "CWD %s\n", Directory); |
sprintf(buffer, "CWD %s\n", Directory); |
| 78 |
|
|
| 79 |
|
|
| 80 |
if (write(sck, buffer, strlen(buffer)) == -1) { |
if (write(sck, buffer, strlen(buffer)) == -1) { |
| 81 |
|
|
| 82 |
if (verbose) { |
if (verbose) { |
| 83 |
|
|
| 84 |
if (errno == EBADF) { |
if (errno == EBADF) { |
| 85 |
fprintf(stderr, "Socket Descriptor Not Valid!\n"); |
fprintf(stderr, "Socket Descriptor Not Valid!\n"); |
| 86 |
free(buf); |
free(buf); |
| 87 |
|
|
| 88 |
return -1; |
return -1; |
| 89 |
} else if (errno == EIO) { |
} else if (errno == EIO) { |
| 90 |
fprintf(stderr, "I/O Error!\n"); |
fprintf(stderr, "I/O Error!\n"); |
| 91 |
free(buf); |
free(buf); |
| 92 |
|
|
| 93 |
return -1; |
return -1; |
| 94 |
} else if (errno == EINTR) { |
} else if (errno == EINTR) { |
| 95 |
fprintf(stderr, "Signal Interrupted The write() Function\n"); |
fprintf(stderr, "Signal Interrupted The write() Function\n"); |
| 96 |
free(buf); |
free(buf); |
| 97 |
|
|
| 98 |
return -1; |
return -1; |
| 99 |
} |
} |
| 100 |
|
|
| 101 |
} else { |
} else { |
| 102 |
|
|
| 103 |
if (errno == EBADF) { |
if (errno == EBADF) { |
| 104 |
free(buf); |
free(buf); |
| 105 |
|
|
| 106 |
return -1; |
return -1; |
| 107 |
} else if (errno == EIO) { |
} else if (errno == EIO) { |
| 108 |
free(buf); |
free(buf); |
| 109 |
|
|
| 110 |
return -1; |
return -1; |
| 111 |
} else if (errno == EINTR) { |
} else if (errno == EINTR) { |
| 112 |
free(buf); |
free(buf); |
| 113 |
|
|
| 114 |
return -1; |
return -1; |
| 115 |
} |
} |
| 116 |
|
|
| 117 |
} |
} |
| 118 |
|
|
| 119 |
free(buf); |
free(buf); |
| 120 |
|
|
| 121 |
return -1; |
return -1; |
| 122 |
|
|
| 123 |
} |
} |
| 124 |
|
|
| 125 |
sleep(1); //wait.. |
sleep(1); //wait.. |
| 126 |
struct timeval tm; //tv_sec - tv_usec |
tm.tv_sec = 3; |
| 127 |
fd_set readfds; |
tm.tv_usec = 0; |
|
tm.tv_sec = 3; |
|
|
tm.tv_usec = 0; |
|
| 128 |
|
|
| 129 |
FD_ZERO(&readfds); |
FD_ZERO(&readfds); |
| 130 |
|
|
| 131 |
FD_SET(sck, &readfds); |
FD_SET(sck, &readfds); |
| 132 |
|
|
| 133 |
|
if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) { |
| 134 |
|
if (verbose) { |
| 135 |
|
perror("select()"); |
| 136 |
|
} |
| 137 |
|
free(buf); |
| 138 |
|
|
| 139 |
|
return -1; |
| 140 |
|
} else { |
| 141 |
|
|
| 142 |
if (select(sck+1, &readfds, NULL, NULL, &tm) < 0 ) { |
if (FD_ISSET(sck, &readfds)) { |
| 143 |
|
|
| 144 |
|
if ( (n = recv(sck, buf, 1022, 0)) < 0) { |
| 145 |
if (verbose) { |
if (verbose) { |
| 146 |
perror("select()"); |
perror("recv()"); |
| 147 |
} |
} |
| 148 |
free(buf); |
free(buf); |
| 149 |
|
|
| 150 |
return -1; |
return -1; |
| 151 |
} else { |
} |
|
|
|
|
if (FD_ISSET(sck, &readfds)) { |
|
| 152 |
|
|
| 153 |
if ( (n = recv(sck, buf, 1022, 0)) < 0) { |
if (verbose) |
| 154 |
if (verbose) { |
printf("[Server] %s\n", buf); |
|
perror("recv()"); |
|
|
} |
|
|
free(buf); |
|
|
|
|
|
return -1; |
|
|
} |
|
|
|
|
|
if (verbose) |
|
|
printf("[Server] %s\n", buf); |
|
| 155 |
|
|
| 156 |
} |
} |
| 157 |
else { |
else { |
| 158 |
if (verbose) |
if (verbose) |
| 159 |
printf("[-] Timeout\n"); |
printf("[-] Timeout\n"); |
| 160 |
|
|
| 161 |
return -1; |
return -1; |
|
} |
|
| 162 |
} |
} |
| 163 |
|
} |
| 164 |
|
|
| 165 |
buf[n] = '\0'; |
buf[n] = '\0'; |
| 166 |
|
|
| 167 |
if (ftp_dir_handler(buf, 0) != 0) { |
if (ftp_dir_handler(buf, 0) != 0) { |
| 168 |
free(buf); |
free(buf); |
| 169 |
|
|
| 170 |
return -1; //Error |
return -1; //Error |
| 171 |
} |
} |
| 172 |
|
|
| 173 |
free(buf); |
free(buf); |
| 174 |
|
|
| 175 |
return 0; |
return 0; |
| 176 |
|
|
| 177 |
} |
} |