1 /* 2 * Copyright 2019 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "net.hpp" 18 19 #include "data.hpp" 20 #include "flags.hpp" 21 22 #include <errno.h> 23 #include <fcntl.h> 24 #include <netdb.h> 25 #include <sys/sendfile.h> 26 #include <sys/socket.h> 27 #include <sys/types.h> 28 29 #include <ipmiblob/blob_errors.hpp> 30 #include <stdplus/handle/managed.hpp> 31 32 #include <cstdint> 33 #include <cstring> 34 #include <memory> 35 #include <optional> 36 #include <string> 37 #include <vector> 38 39 namespace 40 { 41 42 void closefd(int&& fd, const internal::Sys*& sys) 43 { 44 sys->close(fd); 45 } 46 using Fd = stdplus::Managed<int, const internal::Sys*>::Handle<closefd>; 47 48 } // namespace 49 50 namespace host_tool 51 { 52 53 bool NetDataHandler::sendContents(const std::string& input, 54 std::uint16_t session) 55 { 56 constexpr size_t blockSize = 64 * 1024; 57 Fd inputFd(std::nullopt, sys); 58 59 inputFd.reset(sys->open(input.c_str(), O_RDONLY)); 60 if (*inputFd < 0) 61 { 62 (void)inputFd.release(); 63 std::fprintf(stderr, "Unable to open file: '%s'\n", input.c_str()); 64 return false; 65 } 66 67 std::int64_t fileSize = sys->getSize(input.c_str()); 68 Fd connFd(std::nullopt, sys); 69 70 { 71 struct addrinfo hints; 72 std::memset(&hints, 0, sizeof(hints)); 73 hints.ai_flags = AI_NUMERICHOST; 74 hints.ai_family = AF_UNSPEC; 75 hints.ai_socktype = SOCK_STREAM; 76 77 struct addrinfo *addrs, *addr; 78 int ret = sys->getaddrinfo(host.c_str(), port.c_str(), &hints, &addrs); 79 if (ret < 0) 80 { 81 std::fprintf(stderr, "Couldn't parse address %s with port %s: %s\n", 82 host.c_str(), port.c_str(), gai_strerror(ret)); 83 return false; 84 } 85 86 for (addr = addrs; addr != nullptr; addr = addr->ai_next) 87 { 88 connFd.reset(sys->socket(addr->ai_family, addr->ai_socktype, 89 addr->ai_protocol)); 90 if (*connFd == -1) 91 continue; 92 93 if (sys->connect(*connFd, addr->ai_addr, addr->ai_addrlen) != -1) 94 break; 95 } 96 97 // TODO: use stdplus Managed for the addrinfo structs 98 sys->freeaddrinfo(addrs); 99 100 if (addr == nullptr) 101 { 102 std::fprintf(stderr, "Failed to connect\n"); 103 return false; 104 } 105 } 106 107 try 108 { 109 int bytesSent = 0; 110 off_t offset = 0; 111 112 progress->start(fileSize); 113 114 do 115 { 116 bytesSent = sys->sendfile(*connFd, *inputFd, &offset, blockSize); 117 if (bytesSent < 0) 118 { 119 std::fprintf(stderr, "Failed to send data to BMC: %s\n", 120 strerror(errno)); 121 progress->abort(); 122 return false; 123 } 124 else if (bytesSent > 0) 125 { 126 /* Ok, so the data is staged, now send the blob write with 127 * the details. 128 */ 129 struct ipmi_flash::ExtChunkHdr chunk; 130 chunk.length = bytesSent; 131 std::vector<std::uint8_t> chunkBytes(sizeof(chunk)); 132 std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk)); 133 134 /* This doesn't return anything on success. */ 135 blob->writeBytes(session, offset - bytesSent, chunkBytes); 136 progress->updateProgress(bytesSent); 137 } 138 } while (bytesSent > 0); 139 } 140 catch (const ipmiblob::BlobException& b) 141 { 142 progress->abort(); 143 return false; 144 } 145 146 progress->finish(); 147 return true; 148 } 149 150 } // namespace host_tool 151