yoyo
Fork of FTPClient by
FTPClient.cpp@4:8ecc32e7c69b, 2017-12-14 (annotated)
- Committer:
- akshaytom
- Date:
- Thu Dec 14 04:49:35 2017 +0000
- Revision:
- 4:8ecc32e7c69b
- Parent:
- 3:4fd8e5cd6307
- Child:
- 5:087a3ccad604
wefghjssyuiy
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ricky_Kwon | 0:61da21bfd120 | 1 | #include "mbed.h" |
Ricky_Kwon | 0:61da21bfd120 | 2 | #include "EthernetInterface.h" |
Ricky_Kwon | 0:61da21bfd120 | 3 | #include "FTPClient.h" |
Ricky_Kwon | 0:61da21bfd120 | 4 | //#define DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 5 | FTPClient::FTPClient(PinName mosi, PinName miso, PinName sclk, PinName ssel, const char* root) : _SDFileSystem(mosi, miso, sclk, ssel, root){ |
akshaytom | 4:8ecc32e7c69b | 6 | //printf("Object"); |
Ricky_Kwon | 0:61da21bfd120 | 7 | blogin = false; |
Ricky_Kwon | 0:61da21bfd120 | 8 | bopenflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 9 | brfileflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 10 | bdirflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 11 | blsflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 12 | bfdeleteflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 13 | bmkdirflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 14 | bcdflag = false; |
Ricky_Kwon | 2:dbbe55b239d5 | 15 | bquitflag = false; |
akshaytom | 4:8ecc32e7c69b | 16 | //printf("Object"); |
Ricky_Kwon | 0:61da21bfd120 | 17 | } |
akshaytom | 4:8ecc32e7c69b | 18 | |
akshaytom | 4:8ecc32e7c69b | 19 | // made for checking |
akshaytom | 4:8ecc32e7c69b | 20 | |
akshaytom | 4:8ecc32e7c69b | 21 | |
akshaytom | 4:8ecc32e7c69b | 22 | bool FTPClient::open(char ip[], int port, char id[], char pass[]){ |
akshaytom | 4:8ecc32e7c69b | 23 | //printf("\nInside Open"); |
akshaytom | 4:8ecc32e7c69b | 24 | /* |
akshaytom | 4:8ecc32e7c69b | 25 | printf("\n%s",ip); |
akshaytom | 4:8ecc32e7c69b | 26 | printf("\n%d",port); |
akshaytom | 4:8ecc32e7c69b | 27 | printf("\n%s",id); |
akshaytom | 4:8ecc32e7c69b | 28 | printf("\n%s",pass); |
akshaytom | 4:8ecc32e7c69b | 29 | printf("\nHappens"); |
akshaytom | 4:8ecc32e7c69b | 30 | printf("\nHappens\n"); |
akshaytom | 4:8ecc32e7c69b | 31 | */ |
akshaytom | 4:8ecc32e7c69b | 32 | |
akshaytom | 4:8ecc32e7c69b | 33 | FTPClientControlSock = new TCPSocketConnection(); |
akshaytom | 4:8ecc32e7c69b | 34 | FTPClientDataSock = new TCPSocketConnection(); |
akshaytom | 4:8ecc32e7c69b | 35 | //printf("\nHappens\n"); |
akshaytom | 4:8ecc32e7c69b | 36 | #if 0 |
akshaytom | 4:8ecc32e7c69b | 37 | do{ |
akshaytom | 4:8ecc32e7c69b | 38 | FTPClientControlSock->connect(ip, port); |
akshaytom | 4:8ecc32e7c69b | 39 | }while(!FTPClientControlSock->is_connected()); |
akshaytom | 4:8ecc32e7c69b | 40 | #endif |
akshaytom | 4:8ecc32e7c69b | 41 | #if 1 |
akshaytom | 4:8ecc32e7c69b | 42 | while (FTPClientControlSock->connect(ip, port) < 0) { |
akshaytom | 4:8ecc32e7c69b | 43 | #ifdef DEBUG |
akshaytom | 4:8ecc32e7c69b | 44 | printf("Unable to connect to (%s) on port (%d)\r\n", ip, port); |
akshaytom | 4:8ecc32e7c69b | 45 | #endif |
akshaytom | 4:8ecc32e7c69b | 46 | wait(1); |
akshaytom | 4:8ecc32e7c69b | 47 | } |
akshaytom | 4:8ecc32e7c69b | 48 | #endif |
akshaytom | 4:8ecc32e7c69b | 49 | |
akshaytom | 4:8ecc32e7c69b | 50 | while(!blogin){ |
akshaytom | 4:8ecc32e7c69b | 51 | size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); |
akshaytom | 4:8ecc32e7c69b | 52 | if(size > 0){ |
akshaytom | 4:8ecc32e7c69b | 53 | #ifdef DEBUG |
akshaytom | 4:8ecc32e7c69b | 54 | printf("Received message from server: %s\r\n", rbuf); |
akshaytom | 4:8ecc32e7c69b | 55 | #endif |
akshaytom | 4:8ecc32e7c69b | 56 | if (!strncmp(rbuf, "220", 3)){ |
akshaytom | 4:8ecc32e7c69b | 57 | printf("%s\r\n", rbuf); |
akshaytom | 4:8ecc32e7c69b | 58 | sprintf(sbuf, "user %s\r\n", id); |
akshaytom | 4:8ecc32e7c69b | 59 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
akshaytom | 4:8ecc32e7c69b | 60 | } |
akshaytom | 4:8ecc32e7c69b | 61 | else if (!strncmp(rbuf, "331", 3)){ |
akshaytom | 4:8ecc32e7c69b | 62 | sprintf(sbuf, "pass %s\r\n", pass); |
akshaytom | 4:8ecc32e7c69b | 63 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
akshaytom | 4:8ecc32e7c69b | 64 | } |
akshaytom | 4:8ecc32e7c69b | 65 | else if (!strncmp(rbuf, "230", 3)){ |
akshaytom | 4:8ecc32e7c69b | 66 | blogin = true; |
akshaytom | 4:8ecc32e7c69b | 67 | } |
akshaytom | 4:8ecc32e7c69b | 68 | else{ |
akshaytom | 4:8ecc32e7c69b | 69 | blogin = true; |
akshaytom | 4:8ecc32e7c69b | 70 | } |
akshaytom | 4:8ecc32e7c69b | 71 | } |
akshaytom | 4:8ecc32e7c69b | 72 | } |
akshaytom | 4:8ecc32e7c69b | 73 | return 1; |
akshaytom | 4:8ecc32e7c69b | 74 | } |
akshaytom | 4:8ecc32e7c69b | 75 | //*/ |
akshaytom | 4:8ecc32e7c69b | 76 | /* |
akshaytom | 4:8ecc32e7c69b | 77 | bool FTPClient::open(char* ip, int port, char* id, char* pass) |
akshaytom | 4:8ecc32e7c69b | 78 | { |
akshaytom | 4:8ecc32e7c69b | 79 | printf("Inside Open"); |
Ricky_Kwon | 0:61da21bfd120 | 80 | |
Ricky_Kwon | 0:61da21bfd120 | 81 | FTPClientControlSock = new TCPSocketConnection; |
Ricky_Kwon | 0:61da21bfd120 | 82 | FTPClientDataSock = new TCPSocketConnection; |
Ricky_Kwon | 0:61da21bfd120 | 83 | |
Ricky_Kwon | 0:61da21bfd120 | 84 | #if 0 |
Ricky_Kwon | 0:61da21bfd120 | 85 | do{ |
Ricky_Kwon | 0:61da21bfd120 | 86 | FTPClientControlSock->connect(ip, port); |
Ricky_Kwon | 0:61da21bfd120 | 87 | }while(!FTPClientControlSock->is_connected()); |
Ricky_Kwon | 0:61da21bfd120 | 88 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 89 | #if 1 |
Ricky_Kwon | 0:61da21bfd120 | 90 | while (FTPClientControlSock->connect(ip, port) < 0) { |
Ricky_Kwon | 0:61da21bfd120 | 91 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 92 | printf("Unable to connect to (%s) on port (%d)\r\n", ip, port); |
Ricky_Kwon | 0:61da21bfd120 | 93 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 94 | wait(1); |
Ricky_Kwon | 0:61da21bfd120 | 95 | } |
Ricky_Kwon | 0:61da21bfd120 | 96 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 97 | |
Ricky_Kwon | 0:61da21bfd120 | 98 | while(!blogin){ |
Ricky_Kwon | 0:61da21bfd120 | 99 | size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 100 | if(size > 0){ |
Ricky_Kwon | 0:61da21bfd120 | 101 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 102 | printf("Received message from server: %s\r\n", rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 103 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 104 | if (!strncmp(rbuf, "220", 3)){ |
Ricky_Kwon | 1:2630b5456598 | 105 | printf("%s\r\n", rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 106 | sprintf(sbuf, "user %s\r\n", id); |
Ricky_Kwon | 0:61da21bfd120 | 107 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 108 | } |
Ricky_Kwon | 0:61da21bfd120 | 109 | else if (!strncmp(rbuf, "331", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 110 | sprintf(sbuf, "pass %s\r\n", pass); |
Ricky_Kwon | 0:61da21bfd120 | 111 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 112 | } |
Ricky_Kwon | 0:61da21bfd120 | 113 | else if (!strncmp(rbuf, "230", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 114 | blogin = true; |
Ricky_Kwon | 0:61da21bfd120 | 115 | } |
Ricky_Kwon | 0:61da21bfd120 | 116 | else{ |
Ricky_Kwon | 0:61da21bfd120 | 117 | blogin = true; |
Ricky_Kwon | 0:61da21bfd120 | 118 | } |
Ricky_Kwon | 0:61da21bfd120 | 119 | } |
Ricky_Kwon | 0:61da21bfd120 | 120 | } |
Ricky_Kwon | 0:61da21bfd120 | 121 | return 1; |
Ricky_Kwon | 0:61da21bfd120 | 122 | } |
akshaytom | 4:8ecc32e7c69b | 123 | */ |
Ricky_Kwon | 0:61da21bfd120 | 124 | bool FTPClient::getfile(char* myfilename, char* filename){ |
Ricky_Kwon | 0:61da21bfd120 | 125 | |
Ricky_Kwon | 0:61da21bfd120 | 126 | if(blogin){ |
Ricky_Kwon | 0:61da21bfd120 | 127 | |
Ricky_Kwon | 0:61da21bfd120 | 128 | sprintf(sbuf, "pasv\r\n"); |
Ricky_Kwon | 0:61da21bfd120 | 129 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 130 | |
Ricky_Kwon | 0:61da21bfd120 | 131 | while(!brfileflag){ |
Ricky_Kwon | 0:61da21bfd120 | 132 | size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 133 | if(size > 0){ |
Ricky_Kwon | 0:61da21bfd120 | 134 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 135 | printf("Received message from server: %s\r\n", rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 136 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 137 | if (!strncmp(rbuf, "150", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 138 | fp = fopen(myfilename, "w"); |
Ricky_Kwon | 0:61da21bfd120 | 139 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 140 | printf("myfilename : %s\r\n", myfilename); |
Ricky_Kwon | 0:61da21bfd120 | 141 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 142 | while(true){ |
Ricky_Kwon | 0:61da21bfd120 | 143 | memset(rbuf, 0, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 144 | remain_datasize = FTPClientDataSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 145 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 146 | printf("remain_datasize : %d\r\n", remain_datasize); |
Ricky_Kwon | 0:61da21bfd120 | 147 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 148 | if(remain_datasize>0){ |
Ricky_Kwon | 0:61da21bfd120 | 149 | for (i = 0; i < remain_datasize; i++) { |
Ricky_Kwon | 0:61da21bfd120 | 150 | //printf("%c", rbuf[i]); |
Ricky_Kwon | 0:61da21bfd120 | 151 | fprintf(fp, "%c", rbuf[i]); |
Ricky_Kwon | 0:61da21bfd120 | 152 | } |
Ricky_Kwon | 0:61da21bfd120 | 153 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 154 | printf("#"); |
Ricky_Kwon | 0:61da21bfd120 | 155 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 156 | } |
Ricky_Kwon | 0:61da21bfd120 | 157 | else{ |
Ricky_Kwon | 0:61da21bfd120 | 158 | brfileflag = true; |
Ricky_Kwon | 0:61da21bfd120 | 159 | fclose(fp); |
Ricky_Kwon | 0:61da21bfd120 | 160 | FTPClientDataSock->close(); |
Ricky_Kwon | 0:61da21bfd120 | 161 | break; |
Ricky_Kwon | 0:61da21bfd120 | 162 | } |
Ricky_Kwon | 0:61da21bfd120 | 163 | } |
Ricky_Kwon | 0:61da21bfd120 | 164 | } |
Ricky_Kwon | 0:61da21bfd120 | 165 | else if (!strncmp(rbuf, "227", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 166 | pportc(rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 167 | |
Ricky_Kwon | 0:61da21bfd120 | 168 | #if 1 |
Ricky_Kwon | 0:61da21bfd120 | 169 | while (FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port) < 0) { |
Ricky_Kwon | 0:61da21bfd120 | 170 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 171 | printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port); |
Ricky_Kwon | 0:61da21bfd120 | 172 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 173 | wait(1); |
Ricky_Kwon | 0:61da21bfd120 | 174 | } |
Ricky_Kwon | 0:61da21bfd120 | 175 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 176 | |
Ricky_Kwon | 0:61da21bfd120 | 177 | #if 0 |
Ricky_Kwon | 0:61da21bfd120 | 178 | do{ |
Ricky_Kwon | 0:61da21bfd120 | 179 | FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port); |
Ricky_Kwon | 0:61da21bfd120 | 180 | }while(!FTPClientDataSock->is_connected()); |
Ricky_Kwon | 0:61da21bfd120 | 181 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 182 | sprintf(sbuf, "retr %s\r\n", filename); |
Ricky_Kwon | 0:61da21bfd120 | 183 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 184 | } |
Ricky_Kwon | 0:61da21bfd120 | 185 | } |
Ricky_Kwon | 0:61da21bfd120 | 186 | } |
Ricky_Kwon | 0:61da21bfd120 | 187 | brfileflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 188 | return 1; |
Ricky_Kwon | 0:61da21bfd120 | 189 | } |
Ricky_Kwon | 0:61da21bfd120 | 190 | else return 0; |
Ricky_Kwon | 0:61da21bfd120 | 191 | } |
Ricky_Kwon | 0:61da21bfd120 | 192 | bool FTPClient::putfile(char* myfilename, char* filename){ |
Ricky_Kwon | 0:61da21bfd120 | 193 | |
Ricky_Kwon | 0:61da21bfd120 | 194 | if(blogin){ |
Ricky_Kwon | 0:61da21bfd120 | 195 | |
Ricky_Kwon | 0:61da21bfd120 | 196 | sprintf(sbuf, "pasv\r\n"); |
Ricky_Kwon | 0:61da21bfd120 | 197 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 198 | |
Ricky_Kwon | 0:61da21bfd120 | 199 | while(!bsfileflag){ |
Ricky_Kwon | 0:61da21bfd120 | 200 | size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 201 | if(size > 0){ |
Ricky_Kwon | 0:61da21bfd120 | 202 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 203 | printf("Received message from server: %s\r\n", rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 204 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 205 | if (!strncmp(rbuf, "150", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 206 | fp = fopen(myfilename, "r"); |
Ricky_Kwon | 0:61da21bfd120 | 207 | fseek(fp, 0, SEEK_END); // seek to end of file |
Ricky_Kwon | 0:61da21bfd120 | 208 | remain_filesize = ftell(fp); // get current file pointer |
Ricky_Kwon | 0:61da21bfd120 | 209 | fseek(fp, 0, SEEK_SET); // seek back to beginning of file |
Ricky_Kwon | 0:61da21bfd120 | 210 | do{ |
Ricky_Kwon | 0:61da21bfd120 | 211 | memset(sbuf, 0, sizeof(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 212 | if(remain_filesize > MAX_SS) |
Ricky_Kwon | 0:61da21bfd120 | 213 | send_byte = MAX_SS; |
Ricky_Kwon | 0:61da21bfd120 | 214 | else |
Ricky_Kwon | 0:61da21bfd120 | 215 | send_byte = remain_filesize; |
Ricky_Kwon | 0:61da21bfd120 | 216 | fread (sbuf, 1, send_byte, fp); |
Ricky_Kwon | 0:61da21bfd120 | 217 | FTPClientDataSock->send(sbuf, send_byte); |
Ricky_Kwon | 0:61da21bfd120 | 218 | remain_filesize -= send_byte; |
Ricky_Kwon | 0:61da21bfd120 | 219 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 220 | printf("#"); |
Ricky_Kwon | 0:61da21bfd120 | 221 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 222 | }while(remain_filesize!=0); |
Ricky_Kwon | 0:61da21bfd120 | 223 | fclose(fp); |
Ricky_Kwon | 0:61da21bfd120 | 224 | bsfileflag = true; |
Ricky_Kwon | 0:61da21bfd120 | 225 | FTPClientDataSock->close(); |
Ricky_Kwon | 0:61da21bfd120 | 226 | break; |
Ricky_Kwon | 0:61da21bfd120 | 227 | } |
Ricky_Kwon | 0:61da21bfd120 | 228 | else if (!strncmp(rbuf, "227", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 229 | pportc(rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 230 | #if 0 |
Ricky_Kwon | 0:61da21bfd120 | 231 | do{ |
Ricky_Kwon | 0:61da21bfd120 | 232 | FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port); |
Ricky_Kwon | 0:61da21bfd120 | 233 | }while(!FTPClientDataSock->is_connected()); |
Ricky_Kwon | 0:61da21bfd120 | 234 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 235 | |
Ricky_Kwon | 0:61da21bfd120 | 236 | #if 1 |
Ricky_Kwon | 0:61da21bfd120 | 237 | while (FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port) < 0) { |
Ricky_Kwon | 0:61da21bfd120 | 238 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 239 | printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port); |
Ricky_Kwon | 0:61da21bfd120 | 240 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 241 | wait(1); |
Ricky_Kwon | 0:61da21bfd120 | 242 | } |
Ricky_Kwon | 0:61da21bfd120 | 243 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 244 | sprintf(sbuf, "stor %s\r\n", filename); |
Ricky_Kwon | 0:61da21bfd120 | 245 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 246 | } |
Ricky_Kwon | 0:61da21bfd120 | 247 | } |
Ricky_Kwon | 0:61da21bfd120 | 248 | } |
Ricky_Kwon | 0:61da21bfd120 | 249 | bsfileflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 250 | return 1; |
Ricky_Kwon | 0:61da21bfd120 | 251 | } |
Ricky_Kwon | 0:61da21bfd120 | 252 | else return 0; |
Ricky_Kwon | 0:61da21bfd120 | 253 | } |
Ricky_Kwon | 0:61da21bfd120 | 254 | bool FTPClient::dir(){ |
Ricky_Kwon | 0:61da21bfd120 | 255 | |
Ricky_Kwon | 0:61da21bfd120 | 256 | if(blogin){ |
Ricky_Kwon | 0:61da21bfd120 | 257 | |
Ricky_Kwon | 0:61da21bfd120 | 258 | sprintf(sbuf, "pasv\r\n"); |
Ricky_Kwon | 0:61da21bfd120 | 259 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 260 | |
Ricky_Kwon | 0:61da21bfd120 | 261 | while(!bdirflag){ |
Ricky_Kwon | 0:61da21bfd120 | 262 | size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 263 | if(size > 0){ |
Ricky_Kwon | 0:61da21bfd120 | 264 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 265 | printf("Received message from server: %s\r\n", rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 266 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 267 | if (!strncmp(rbuf, "150", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 268 | while(true){ |
Ricky_Kwon | 0:61da21bfd120 | 269 | memset(rbuf, 0, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 270 | size = FTPClientDataSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 271 | rbuf[size] = '\0'; |
Ricky_Kwon | 0:61da21bfd120 | 272 | if(size>0){ |
Ricky_Kwon | 0:61da21bfd120 | 273 | printf("%s", rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 274 | } |
Ricky_Kwon | 0:61da21bfd120 | 275 | else{ |
Ricky_Kwon | 0:61da21bfd120 | 276 | bdirflag = true; |
Ricky_Kwon | 0:61da21bfd120 | 277 | FTPClientDataSock->close(); |
Ricky_Kwon | 0:61da21bfd120 | 278 | break; |
Ricky_Kwon | 0:61da21bfd120 | 279 | } |
Ricky_Kwon | 0:61da21bfd120 | 280 | } |
Ricky_Kwon | 0:61da21bfd120 | 281 | } |
Ricky_Kwon | 0:61da21bfd120 | 282 | else if (!strncmp(rbuf, "227", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 283 | pportc(rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 284 | #if 0 |
Ricky_Kwon | 0:61da21bfd120 | 285 | do{ |
Ricky_Kwon | 0:61da21bfd120 | 286 | FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port); |
Ricky_Kwon | 0:61da21bfd120 | 287 | }while(!FTPClientDataSock->is_connected()); |
Ricky_Kwon | 0:61da21bfd120 | 288 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 289 | #if 1 |
Ricky_Kwon | 0:61da21bfd120 | 290 | while (FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port) < 0) { |
Ricky_Kwon | 0:61da21bfd120 | 291 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 292 | printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port); |
Ricky_Kwon | 0:61da21bfd120 | 293 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 294 | wait(1); |
Ricky_Kwon | 0:61da21bfd120 | 295 | } |
Ricky_Kwon | 0:61da21bfd120 | 296 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 297 | sprintf(sbuf, "list\r\n"); |
Ricky_Kwon | 0:61da21bfd120 | 298 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 299 | } |
Ricky_Kwon | 0:61da21bfd120 | 300 | } |
Ricky_Kwon | 0:61da21bfd120 | 301 | } |
Ricky_Kwon | 0:61da21bfd120 | 302 | bdirflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 303 | return 1; |
Ricky_Kwon | 0:61da21bfd120 | 304 | } |
Ricky_Kwon | 0:61da21bfd120 | 305 | else return 0; |
Ricky_Kwon | 0:61da21bfd120 | 306 | } |
Ricky_Kwon | 0:61da21bfd120 | 307 | |
Ricky_Kwon | 0:61da21bfd120 | 308 | bool FTPClient::ls(){ |
Ricky_Kwon | 0:61da21bfd120 | 309 | |
Ricky_Kwon | 0:61da21bfd120 | 310 | if(blogin){ |
Ricky_Kwon | 0:61da21bfd120 | 311 | |
Ricky_Kwon | 0:61da21bfd120 | 312 | sprintf(sbuf, "pasv\r\n"); |
Ricky_Kwon | 0:61da21bfd120 | 313 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 314 | |
Ricky_Kwon | 0:61da21bfd120 | 315 | while(!blsflag){ |
Ricky_Kwon | 0:61da21bfd120 | 316 | size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 317 | if(size > 0){ |
Ricky_Kwon | 0:61da21bfd120 | 318 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 319 | printf("Received message from server: %s\r\n", rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 320 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 321 | if (!strncmp(rbuf, "150", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 322 | while(true){ |
Ricky_Kwon | 0:61da21bfd120 | 323 | memset(rbuf, 0, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 324 | size = FTPClientDataSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 325 | rbuf[size] = '\0'; |
Ricky_Kwon | 0:61da21bfd120 | 326 | if(size>0){ |
Ricky_Kwon | 0:61da21bfd120 | 327 | printf("%s", rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 328 | } |
Ricky_Kwon | 0:61da21bfd120 | 329 | else{ |
Ricky_Kwon | 0:61da21bfd120 | 330 | blsflag = true; |
Ricky_Kwon | 0:61da21bfd120 | 331 | FTPClientDataSock->close(); |
Ricky_Kwon | 0:61da21bfd120 | 332 | break; |
Ricky_Kwon | 0:61da21bfd120 | 333 | } |
Ricky_Kwon | 0:61da21bfd120 | 334 | } |
Ricky_Kwon | 0:61da21bfd120 | 335 | } |
Ricky_Kwon | 0:61da21bfd120 | 336 | else if (!strncmp(rbuf, "227", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 337 | pportc(rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 338 | #if 0 |
Ricky_Kwon | 0:61da21bfd120 | 339 | do{ |
Ricky_Kwon | 0:61da21bfd120 | 340 | FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port); |
Ricky_Kwon | 0:61da21bfd120 | 341 | }while(!FTPClientDataSock->is_connected()); |
Ricky_Kwon | 0:61da21bfd120 | 342 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 343 | #if 1 |
Ricky_Kwon | 0:61da21bfd120 | 344 | while (FTPClientDataSock->connect(ftpServer_data_ip_addr_str, remote_port) < 0) { |
Ricky_Kwon | 0:61da21bfd120 | 345 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 346 | printf("Unable to connect to (%s) on port (%d)\r\n", ftpServer_data_ip_addr_str, remote_port); |
Ricky_Kwon | 0:61da21bfd120 | 347 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 348 | wait(1); |
Ricky_Kwon | 0:61da21bfd120 | 349 | } |
Ricky_Kwon | 0:61da21bfd120 | 350 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 351 | sprintf(sbuf, "nlst\r\n"); |
Ricky_Kwon | 0:61da21bfd120 | 352 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 353 | } |
Ricky_Kwon | 0:61da21bfd120 | 354 | } |
Ricky_Kwon | 0:61da21bfd120 | 355 | } |
Ricky_Kwon | 0:61da21bfd120 | 356 | blsflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 357 | return 1; |
Ricky_Kwon | 0:61da21bfd120 | 358 | } |
Ricky_Kwon | 0:61da21bfd120 | 359 | else return 0; |
Ricky_Kwon | 0:61da21bfd120 | 360 | } |
Ricky_Kwon | 0:61da21bfd120 | 361 | |
Ricky_Kwon | 0:61da21bfd120 | 362 | bool FTPClient::fdelete(char* filename){ |
Ricky_Kwon | 0:61da21bfd120 | 363 | |
Ricky_Kwon | 0:61da21bfd120 | 364 | if(blogin){ |
Ricky_Kwon | 0:61da21bfd120 | 365 | |
Ricky_Kwon | 0:61da21bfd120 | 366 | sprintf(sbuf, "dele %s\r\n", filename); |
Ricky_Kwon | 0:61da21bfd120 | 367 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 368 | |
Ricky_Kwon | 0:61da21bfd120 | 369 | while(!bfdeleteflag){ |
Ricky_Kwon | 0:61da21bfd120 | 370 | size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 371 | if(size > 0){ |
Ricky_Kwon | 0:61da21bfd120 | 372 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 373 | printf("Received message from server: %s\r\n", rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 374 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 375 | if (!strncmp(rbuf, "250", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 376 | bfdeleteflag = true; |
Ricky_Kwon | 0:61da21bfd120 | 377 | } |
Ricky_Kwon | 0:61da21bfd120 | 378 | else { |
Ricky_Kwon | 0:61da21bfd120 | 379 | return 0; |
Ricky_Kwon | 0:61da21bfd120 | 380 | } |
Ricky_Kwon | 0:61da21bfd120 | 381 | } |
Ricky_Kwon | 0:61da21bfd120 | 382 | } |
Ricky_Kwon | 0:61da21bfd120 | 383 | bfdeleteflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 384 | return 1; |
Ricky_Kwon | 0:61da21bfd120 | 385 | } |
Ricky_Kwon | 0:61da21bfd120 | 386 | else return 0; |
Ricky_Kwon | 0:61da21bfd120 | 387 | } |
Ricky_Kwon | 0:61da21bfd120 | 388 | bool FTPClient::mkdir(char* dirname){ |
Ricky_Kwon | 0:61da21bfd120 | 389 | |
Ricky_Kwon | 0:61da21bfd120 | 390 | if(blogin){ |
Ricky_Kwon | 0:61da21bfd120 | 391 | |
Ricky_Kwon | 0:61da21bfd120 | 392 | sprintf(sbuf, "xmkd %s\r\n", dirname); |
Ricky_Kwon | 0:61da21bfd120 | 393 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 394 | |
Ricky_Kwon | 0:61da21bfd120 | 395 | while(!bmkdirflag){ |
Ricky_Kwon | 0:61da21bfd120 | 396 | size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 397 | if(size > 0){ |
Ricky_Kwon | 0:61da21bfd120 | 398 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 399 | printf("Received message from server: %s\r\n", rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 400 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 401 | if (!strncmp(rbuf, "257", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 402 | bmkdirflag = true; |
Ricky_Kwon | 0:61da21bfd120 | 403 | } |
Ricky_Kwon | 0:61da21bfd120 | 404 | else { |
Ricky_Kwon | 0:61da21bfd120 | 405 | return 0; |
Ricky_Kwon | 0:61da21bfd120 | 406 | } |
Ricky_Kwon | 0:61da21bfd120 | 407 | } |
Ricky_Kwon | 0:61da21bfd120 | 408 | } |
Ricky_Kwon | 0:61da21bfd120 | 409 | bmkdirflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 410 | return 1; |
Ricky_Kwon | 0:61da21bfd120 | 411 | } |
Ricky_Kwon | 0:61da21bfd120 | 412 | else return 0; |
Ricky_Kwon | 0:61da21bfd120 | 413 | } |
Ricky_Kwon | 0:61da21bfd120 | 414 | bool FTPClient::cd(char* dirname){ |
Ricky_Kwon | 0:61da21bfd120 | 415 | |
Ricky_Kwon | 0:61da21bfd120 | 416 | if(blogin){ |
Ricky_Kwon | 0:61da21bfd120 | 417 | |
Ricky_Kwon | 0:61da21bfd120 | 418 | sprintf(sbuf, "cwd %s\r\n", dirname); |
Ricky_Kwon | 0:61da21bfd120 | 419 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 420 | |
Ricky_Kwon | 0:61da21bfd120 | 421 | while(!bcdflag){ |
Ricky_Kwon | 0:61da21bfd120 | 422 | size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 0:61da21bfd120 | 423 | if(size > 0){ |
Ricky_Kwon | 0:61da21bfd120 | 424 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 425 | printf("Received message from server: %s\r\n", rbuf); |
Ricky_Kwon | 0:61da21bfd120 | 426 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 427 | if (!strncmp(rbuf, "250", 3)){ |
Ricky_Kwon | 0:61da21bfd120 | 428 | bcdflag = true; |
Ricky_Kwon | 0:61da21bfd120 | 429 | } |
Ricky_Kwon | 0:61da21bfd120 | 430 | else { |
Ricky_Kwon | 0:61da21bfd120 | 431 | return 0; |
Ricky_Kwon | 0:61da21bfd120 | 432 | } |
Ricky_Kwon | 0:61da21bfd120 | 433 | } |
Ricky_Kwon | 0:61da21bfd120 | 434 | } |
Ricky_Kwon | 0:61da21bfd120 | 435 | bcdflag = false; |
Ricky_Kwon | 0:61da21bfd120 | 436 | return 1; |
Ricky_Kwon | 0:61da21bfd120 | 437 | } |
Ricky_Kwon | 0:61da21bfd120 | 438 | else return 0; |
Ricky_Kwon | 0:61da21bfd120 | 439 | } |
Ricky_Kwon | 1:2630b5456598 | 440 | bool FTPClient::quit(){ |
Ricky_Kwon | 1:2630b5456598 | 441 | |
Ricky_Kwon | 1:2630b5456598 | 442 | if(blogin){ |
Ricky_Kwon | 1:2630b5456598 | 443 | |
Ricky_Kwon | 1:2630b5456598 | 444 | sprintf(sbuf, "quit \r\n"); |
Ricky_Kwon | 1:2630b5456598 | 445 | FTPClientControlSock->send(sbuf, strlen(sbuf)); |
Ricky_Kwon | 1:2630b5456598 | 446 | |
Ricky_Kwon | 1:2630b5456598 | 447 | while(!bquitflag){ |
Ricky_Kwon | 1:2630b5456598 | 448 | size = FTPClientControlSock->receive(rbuf, sizeof(rbuf)); |
Ricky_Kwon | 1:2630b5456598 | 449 | if(size > 0){ |
Ricky_Kwon | 1:2630b5456598 | 450 | #ifdef DEBUG |
Ricky_Kwon | 1:2630b5456598 | 451 | printf("Received message from server: %s\r\n", rbuf); |
Ricky_Kwon | 1:2630b5456598 | 452 | #endif |
Ricky_Kwon | 1:2630b5456598 | 453 | if (!strncmp(rbuf, "250", 3)){ |
Ricky_Kwon | 1:2630b5456598 | 454 | printf("%s\r\n", rbuf); |
Ricky_Kwon | 1:2630b5456598 | 455 | bquitflag = true; |
Ricky_Kwon | 1:2630b5456598 | 456 | } |
Ricky_Kwon | 1:2630b5456598 | 457 | else { |
Ricky_Kwon | 1:2630b5456598 | 458 | return 0; |
Ricky_Kwon | 1:2630b5456598 | 459 | } |
Ricky_Kwon | 1:2630b5456598 | 460 | } |
Ricky_Kwon | 1:2630b5456598 | 461 | } |
Ricky_Kwon | 1:2630b5456598 | 462 | |
Ricky_Kwon | 1:2630b5456598 | 463 | bquitflag = false; |
Ricky_Kwon | 1:2630b5456598 | 464 | blogin = false; |
Ricky_Kwon | 3:4fd8e5cd6307 | 465 | delete FTPClientControlSock; |
Ricky_Kwon | 3:4fd8e5cd6307 | 466 | delete FTPClientDataSock; |
Ricky_Kwon | 3:4fd8e5cd6307 | 467 | |
Ricky_Kwon | 1:2630b5456598 | 468 | return 1; |
Ricky_Kwon | 1:2630b5456598 | 469 | } |
Ricky_Kwon | 1:2630b5456598 | 470 | else return 0; |
Ricky_Kwon | 1:2630b5456598 | 471 | } |
Ricky_Kwon | 0:61da21bfd120 | 472 | int FTPClient::pportc(char * arg) |
Ricky_Kwon | 0:61da21bfd120 | 473 | { |
Ricky_Kwon | 0:61da21bfd120 | 474 | char* tok=0; |
Ricky_Kwon | 0:61da21bfd120 | 475 | |
Ricky_Kwon | 0:61da21bfd120 | 476 | strtok(arg,"("); |
Ricky_Kwon | 0:61da21bfd120 | 477 | for (i = 0; i < 4; i++) |
Ricky_Kwon | 0:61da21bfd120 | 478 | { |
Ricky_Kwon | 0:61da21bfd120 | 479 | if(i==0) tok = strtok(NULL,",\r\n"); |
Ricky_Kwon | 0:61da21bfd120 | 480 | else tok = strtok(NULL,","); |
Ricky_Kwon | 0:61da21bfd120 | 481 | ftpServer_data_ip_addr[i] = (uint8_t)atoi(tok); |
Ricky_Kwon | 0:61da21bfd120 | 482 | if (!tok){ |
Ricky_Kwon | 0:61da21bfd120 | 483 | #ifndef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 484 | printf("bad pport : %s\r\n", arg); |
Ricky_Kwon | 0:61da21bfd120 | 485 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 486 | return -1; |
Ricky_Kwon | 0:61da21bfd120 | 487 | } |
Ricky_Kwon | 0:61da21bfd120 | 488 | } |
Ricky_Kwon | 0:61da21bfd120 | 489 | remote_port = 0; |
Ricky_Kwon | 0:61da21bfd120 | 490 | for (i = 0; i < 2; i++){ |
Ricky_Kwon | 0:61da21bfd120 | 491 | tok = strtok(NULL,",\r\n"); |
Ricky_Kwon | 0:61da21bfd120 | 492 | remote_port <<= 8; |
Ricky_Kwon | 0:61da21bfd120 | 493 | remote_port += atoi(tok); |
Ricky_Kwon | 0:61da21bfd120 | 494 | if (!tok){ |
Ricky_Kwon | 0:61da21bfd120 | 495 | #ifdef DEBUG |
Ricky_Kwon | 0:61da21bfd120 | 496 | printf("bad pport : %s\r\n", arg); |
Ricky_Kwon | 0:61da21bfd120 | 497 | #endif |
Ricky_Kwon | 0:61da21bfd120 | 498 | return -1; |
Ricky_Kwon | 0:61da21bfd120 | 499 | } |
Ricky_Kwon | 0:61da21bfd120 | 500 | } |
Ricky_Kwon | 0:61da21bfd120 | 501 | sprintf(ftpServer_data_ip_addr_str, "%d.%d.%d.%d", ftpServer_data_ip_addr[0], ftpServer_data_ip_addr[1], ftpServer_data_ip_addr[2], ftpServer_data_ip_addr[3]); |
Ricky_Kwon | 0:61da21bfd120 | 502 | return 0; |
Ricky_Kwon | 0:61da21bfd120 | 503 | } |
Ricky_Kwon | 0:61da21bfd120 | 504 | |
Ricky_Kwon | 0:61da21bfd120 | 505 | |
Ricky_Kwon | 0:61da21bfd120 | 506 | |
Ricky_Kwon | 0:61da21bfd120 | 507 |