| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | #include <SFML/Network/Ftp.hpp> |
| 29 | #include <SFML/Network/IPAddress.hpp> |
| 30 | #include <algorithm> |
| 31 | #include <fstream> |
| 32 | #include <iterator> |
| 33 | #include <sstream> |
| 34 | |
| 35 | |
| 36 | namespace sf |
| 37 | { |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | class Ftp::DataChannel : NonCopyable |
| 43 | { |
| 44 | public : |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | DataChannel(Ftp& Owner); |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | ~DataChannel(); |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | Ftp::Response Open(Ftp::TransferMode Mode); |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | void Send(const std::vector<char>& Data); |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | void Receive(std::vector<char>& Data); |
| 70 | |
| 71 | private : |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | Ftp& myFtp; |
| 77 | SocketTCP myDataSocket; |
| 78 | }; |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | Ftp::Response::Response(Status Code, const std::string& Message) : |
| 85 | myStatus (Code), |
| 86 | myMessage(Message) |
| 87 | { |
| 88 | |
| 89 | } |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | bool Ftp::Response::IsOk() const |
| 97 | { |
| 98 | return myStatus < 400; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | Ftp::Response::Status Ftp::Response::GetStatus() const |
| 106 | { |
| 107 | return myStatus; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | const std::string& Ftp::Response::GetMessage() const |
| 115 | { |
| 116 | return myMessage; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | Ftp::DirectoryResponse::DirectoryResponse(Ftp::Response Resp) : |
| 124 | Ftp::Response(Resp) |
| 125 | { |
| 126 | if (IsOk()) |
| 127 | { |
| 128 | |
| 129 | std::string::size_type Begin = Resp.GetMessage().find('"', 0); |
| 130 | std::string::size_type End = Resp.GetMessage().find('"', Begin + 1); |
| 131 | myDirectory = Resp.GetMessage().substr(Begin + 1, End - Begin - 1); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | const std::string& Ftp::DirectoryResponse::GetDirectory() const |
| 140 | { |
| 141 | return myDirectory; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | Ftp::ListingResponse::ListingResponse(Ftp::Response Resp, const std::vector<char>& Data) : |
| 149 | Ftp::Response(Resp) |
| 150 | { |
| 151 | if (IsOk()) |
| 152 | { |
| 153 | |
| 154 | std::string Paths(Data.begin(), Data.end()); |
| 155 | std::string::size_type LastPos = 0; |
| 156 | for (std::string::size_type Pos = Paths.find("\r\n"); Pos != std::string::npos; Pos = Paths.find("\r\n", LastPos)) |
| 157 | { |
| 158 | myFilenames.push_back(Paths.substr(LastPos, Pos - LastPos)); |
| 159 | LastPos = Pos + 2; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | std::size_t Ftp::ListingResponse::GetCount() const |
| 169 | { |
| 170 | return myFilenames.size(); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | const std::string& Ftp::ListingResponse::GetFilename(std::size_t Index) const |
| 178 | { |
| 179 | return myFilenames[Index]; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | Ftp::~Ftp() |
| 187 | { |
| 188 | Disconnect(); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | Ftp::Response Ftp::Connect(const IPAddress& Server, unsigned short Port, float Timeout) |
| 196 | { |
| 197 | |
| 198 | if (myCommandSocket.Connect(Port, Server, Timeout) != Socket::Done) |
| 199 | return Response(Response::ConnectionFailed); |
| 200 | |
| 201 | |
| 202 | return GetResponse(); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | Ftp::Response Ftp::Login() |
| 210 | { |
| 211 | return Login("anonymous", "user@sfml-dev.org"); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | Ftp::Response Ftp::Login(const std::string& UserName, const std::string& Password) |
| 219 | { |
| 220 | Response Resp = SendCommand("USER", UserName); |
| 221 | if (Resp.IsOk()) |
| 222 | Resp = SendCommand("PASS", Password); |
| 223 | |
| 224 | return Resp; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | Ftp::Response Ftp::Disconnect() |
| 232 | { |
| 233 | |
| 234 | Response Resp = SendCommand("QUIT"); |
| 235 | if (Resp.IsOk()) |
| 236 | myCommandSocket.Close(); |
| 237 | |
| 238 | return Resp; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | Ftp::Response Ftp::KeepAlive() |
| 246 | { |
| 247 | return SendCommand("NOOP"); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | Ftp::DirectoryResponse Ftp::GetWorkingDirectory() |
| 255 | { |
| 256 | return DirectoryResponse(SendCommand("PWD")); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | Ftp::ListingResponse Ftp::GetDirectoryListing(const std::string& Directory) |
| 265 | { |
| 266 | |
| 267 | std::vector<char> DirData; |
| 268 | DataChannel Data(*this); |
| 269 | Response Resp = Data.Open(Ascii); |
| 270 | if (Resp.IsOk()) |
| 271 | { |
| 272 | |
| 273 | Resp = SendCommand("NLST", Directory); |
| 274 | if (Resp.IsOk()) |
| 275 | { |
| 276 | |
| 277 | Data.Receive(DirData); |
| 278 | |
| 279 | |
| 280 | Resp = GetResponse(); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return ListingResponse(Resp, DirData); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | Ftp::Response Ftp::ChangeDirectory(const std::string& Directory) |
| 292 | { |
| 293 | return SendCommand("CWD", Directory); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | Ftp::Response Ftp::ParentDirectory() |
| 301 | { |
| 302 | return SendCommand("CDUP"); |
| 303 | } |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | Ftp::Response Ftp::MakeDirectory(const std::string& Name) |
| 310 | { |
| 311 | return SendCommand("MKD", Name); |
| 312 | } |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | Ftp::Response Ftp::DeleteDirectory(const std::string& Name) |
| 319 | { |
| 320 | return SendCommand("RMD", Name); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | Ftp::Response Ftp::RenameFile(const std::string& File, const std::string& NewName) |
| 328 | { |
| 329 | Response Resp = SendCommand("RNFR", File); |
| 330 | if (Resp.IsOk()) |
| 331 | Resp = SendCommand("RNTO", NewName); |
| 332 | |
| 333 | return Resp; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | Ftp::Response Ftp::DeleteFile(const std::string& Name) |
| 341 | { |
| 342 | return SendCommand("DELE", Name); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | Ftp::Response Ftp::Download(const std::string& DistantFile, const std::string& DestPath, TransferMode Mode) |
| 350 | { |
| 351 | |
| 352 | DataChannel Data(*this); |
| 353 | Response Resp = Data.Open(Mode); |
| 354 | if (Resp.IsOk()) |
| 355 | { |
| 356 | |
| 357 | Resp = SendCommand("RETR", DistantFile); |
| 358 | if (Resp.IsOk()) |
| 359 | { |
| 360 | |
| 361 | std::vector<char> FileData; |
| 362 | Data.Receive(FileData); |
| 363 | |
| 364 | |
| 365 | Resp = GetResponse(); |
| 366 | if (Resp.IsOk()) |
| 367 | { |
| 368 | |
| 369 | std::string Filename = DistantFile; |
| 370 | std::string::size_type Pos = Filename.find_last_of("/\\"); |
| 371 | if (Pos != std::string::npos) |
| 372 | Filename = Filename.substr(Pos + 1); |
| 373 | |
| 374 | |
| 375 | std::string Path = DestPath; |
| 376 | if (!Path.empty() && (Path[Path.size() - 1] != '\\') && (Path[Path.size() - 1] != '/')) |
| 377 | Path += "/"; |
| 378 | |
| 379 | |
| 380 | std::ofstream File((Path + Filename).c_str(), std::ios_base::binary); |
| 381 | if (!File) |
| 382 | return Response(Response::InvalidFile); |
| 383 | if (!FileData.empty()) |
| 384 | File.write(&FileData[0], static_cast<std::streamsize>(FileData.size())); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return Resp; |
| 390 | } |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | Ftp::Response Ftp::Upload(const std::string& LocalFile, const std::string& DestPath, TransferMode Mode) |
| 397 | { |
| 398 | |
| 399 | std::ifstream File(LocalFile.c_str(), std::ios_base::binary); |
| 400 | if (!File) |
| 401 | return Response(Response::InvalidFile); |
| 402 | File.seekg(0, std::ios::end); |
| 403 | std::size_t Length = File.tellg(); |
| 404 | File.seekg(0, std::ios::beg); |
| 405 | std::vector<char> FileData(Length); |
| 406 | if (Length > 0) |
| 407 | File.read(&FileData[0], static_cast<std::streamsize>(Length)); |
| 408 | |
| 409 | |
| 410 | std::string Filename = LocalFile; |
| 411 | std::string::size_type Pos = Filename.find_last_of("/\\"); |
| 412 | if (Pos != std::string::npos) |
| 413 | Filename = Filename.substr(Pos + 1); |
| 414 | |
| 415 | |
| 416 | std::string Path = DestPath; |
| 417 | if (!Path.empty() && (Path[Path.size() - 1] != '\\') && (Path[Path.size() - 1] != '/')) |
| 418 | Path += "/"; |
| 419 | |
| 420 | |
| 421 | DataChannel Data(*this); |
| 422 | Response Resp = Data.Open(Mode); |
| 423 | if (Resp.IsOk()) |
| 424 | { |
| 425 | |
| 426 | Resp = SendCommand("STOR", Path + Filename); |
| 427 | if (Resp.IsOk()) |
| 428 | { |
| 429 | |
| 430 | Data.Send(FileData); |
| 431 | |
| 432 | |
| 433 | Resp = GetResponse(); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | return Resp; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | Ftp::Response Ftp::SendCommand(const std::string& Command, const std::string& Parameter) |
| 445 | { |
| 446 | |
| 447 | std::string CommandStr; |
| 448 | if (Parameter != "") |
| 449 | CommandStr = Command + " " + Parameter + "\r\n"; |
| 450 | else |
| 451 | CommandStr = Command + "\r\n"; |
| 452 | |
| 453 | |
| 454 | if (myCommandSocket.Send(CommandStr.c_str(), CommandStr.length()) != sf::Socket::Done) |
| 455 | return Response(Response::ConnectionClosed); |
| 456 | |
| 457 | |
| 458 | return GetResponse(); |
| 459 | } |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | Ftp::Response Ftp::GetResponse() |
| 467 | { |
| 468 | |
| 469 | |
| 470 | |
| 471 | unsigned int LastCode = 0; |
| 472 | bool IsInsideMultiline = false; |
| 473 | std::string Message; |
| 474 | |
| 475 | for (;;) |
| 476 | { |
| 477 | |
| 478 | char Buffer[1024]; |
| 479 | std::size_t Length; |
| 480 | if (myCommandSocket.Receive(Buffer, sizeof(Buffer), Length) != sf::Socket::Done) |
| 481 | return Response(Response::ConnectionClosed); |
| 482 | |
| 483 | |
| 484 | std::istringstream In(std::string(Buffer, Length), std::ios_base::binary); |
| 485 | while (In) |
| 486 | { |
| 487 | |
| 488 | unsigned int Code; |
| 489 | if (In >> Code) |
| 490 | { |
| 491 | |
| 492 | char Sep = 0; |
| 493 | In.get(Sep); |
| 494 | |
| 495 | |
| 496 | if ((Sep == '-') && !IsInsideMultiline) |
| 497 | { |
| 498 | |
| 499 | IsInsideMultiline = true; |
| 500 | |
| 501 | |
| 502 | if (LastCode == 0) |
| 503 | LastCode = Code; |
| 504 | |
| 505 | |
| 506 | std::getline(In, Message); |
| 507 | |
| 508 | |
| 509 | Message.erase(Message.length() - 1); |
| 510 | Message = Sep + Message + "\n"; |
| 511 | } |
| 512 | else |
| 513 | { |
| 514 | |
| 515 | |
| 516 | if ((Sep != '-') && ((Code == LastCode) || (LastCode == 0))) |
| 517 | { |
| 518 | |
| 519 | IsInsideMultiline = false; |
| Value stored to 'IsInsideMultiline' is never read |
| 520 | |
| 521 | |
| 522 | std::string Line; |
| 523 | std::getline(In, Line); |
| 524 | |
| 525 | |
| 526 | Line.erase(Line.length() - 1); |
| 527 | |
| 528 | |
| 529 | if (Code == LastCode) |
| 530 | { |
| 531 | std::ostringstream Out; |
| 532 | Out << Code << Sep << Line; |
| 533 | Message += Out.str(); |
| 534 | } |
| 535 | else |
| 536 | { |
| 537 | Message = Sep + Line; |
| 538 | } |
| 539 | |
| 540 | |
| 541 | return Response(static_cast<Response::Status>(Code), Message); |
| 542 | } |
| 543 | else |
| 544 | { |
| 545 | |
| 546 | |
| 547 | |
| 548 | |
| 549 | std::string Line; |
| 550 | std::getline(In, Line); |
| 551 | |
| 552 | if (!Line.empty()) |
| 553 | { |
| 554 | |
| 555 | Line.erase(Line.length() - 1); |
| 556 | |
| 557 | |
| 558 | std::ostringstream Out; |
| 559 | Out << Code << Sep << Line << "\n"; |
| 560 | Message += Out.str(); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | else if (LastCode != 0) |
| 566 | { |
| 567 | |
| 568 | |
| 569 | |
| 570 | In.clear(); |
| 571 | |
| 572 | |
| 573 | std::string Line; |
| 574 | std::getline(In, Line); |
| 575 | |
| 576 | if (!Line.empty()) |
| 577 | { |
| 578 | |
| 579 | Line.erase(Line.length() - 1); |
| 580 | |
| 581 | |
| 582 | Message += Line + "\n"; |
| 583 | } |
| 584 | } |
| 585 | else |
| 586 | { |
| 587 | |
| 588 | return Response(Response::InvalidResponse); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | |
| 594 | } |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | Ftp::DataChannel::DataChannel(Ftp& Owner) : |
| 601 | myFtp(Owner) |
| 602 | { |
| 603 | |
| 604 | } |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | Ftp::DataChannel::~DataChannel() |
| 611 | { |
| 612 | |
| 613 | myDataSocket.Close(); |
| 614 | } |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | Ftp::Response Ftp::DataChannel::Open(Ftp::TransferMode Mode) |
| 621 | { |
| 622 | |
| 623 | Ftp::Response Resp = myFtp.SendCommand("PASV"); |
| 624 | if (Resp.IsOk()) |
| 625 | { |
| 626 | |
| 627 | std::string::size_type begin = Resp.GetMessage().find_first_of("0123456789"); |
| 628 | if (begin != std::string::npos) |
| 629 | { |
| 630 | sf::Uint8 Data[6] = {0, 0, 0, 0, 0, 0}; |
| 631 | std::string Str = Resp.GetMessage().substr(begin); |
| 632 | std::size_t Index = 0; |
| 633 | for (int i = 0; i < 6; ++i) |
| 634 | { |
| 635 | |
| 636 | while (isdigit(Str[Index])) |
| 637 | { |
| 638 | Data[i] = Data[i] * 10 + (Str[Index] - '0'); |
| 639 | Index++; |
| 640 | } |
| 641 | |
| 642 | |
| 643 | Index++; |
| 644 | } |
| 645 | |
| 646 | |
| 647 | unsigned short Port = Data[4] * 256 + Data[5]; |
| 648 | sf::IPAddress Address(static_cast<sf::Uint8>(Data[0]), |
| 649 | static_cast<sf::Uint8>(Data[1]), |
| 650 | static_cast<sf::Uint8>(Data[2]), |
| 651 | static_cast<sf::Uint8>(Data[3])); |
| 652 | |
| 653 | |
| 654 | if (myDataSocket.Connect(Port, Address) == Socket::Done) |
| 655 | { |
| 656 | |
| 657 | std::string ModeStr; |
| 658 | switch (Mode) |
| 659 | { |
| 660 | case Ftp::Binary : ModeStr = "I"; break; |
| 661 | case Ftp::Ascii : ModeStr = "A"; break; |
| 662 | case Ftp::Ebcdic : ModeStr = "E"; break; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | Resp = myFtp.SendCommand("TYPE", ModeStr); |
| 667 | } |
| 668 | else |
| 669 | { |
| 670 | |
| 671 | Resp = Ftp::Response(Ftp::Response::ConnectionFailed); |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | return Resp; |
| 677 | } |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | void Ftp::DataChannel::Receive(std::vector<char>& Data) |
| 684 | { |
| 685 | |
| 686 | Data.clear(); |
| 687 | char Buffer[1024]; |
| 688 | std::size_t Received; |
| 689 | while (myDataSocket.Receive(Buffer, sizeof(Buffer), Received) == sf::Socket::Done) |
| 690 | { |
| 691 | std::copy(Buffer, Buffer + Received, std::back_inserter(Data)); |
| 692 | } |
| 693 | |
| 694 | |
| 695 | myDataSocket.Close(); |
| 696 | } |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | void Ftp::DataChannel::Send(const std::vector<char>& Data) |
| 703 | { |
| 704 | |
| 705 | if (!Data.empty()) |
| 706 | myDataSocket.Send(&Data[0], Data.size()); |
| 707 | |
| 708 | |
| 709 | myDataSocket.Close(); |
| 710 | } |
| 711 | |
| 712 | } |