blob: c971cb8d68002ea8e50f07624e73bdc5c7d34f46 [file] [log] [blame]
Benjamin Fair30d09a32019-10-11 16:57:47 -07001/*
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 <cstdint>
30#include <cstring>
31#include <ipmiblob/blob_errors.hpp>
32#include <memory>
Benjamin Fair4152f092019-11-25 19:07:31 -080033#include <optional>
Benjamin Fair30d09a32019-10-11 16:57:47 -070034#include <stdplus/handle/managed.hpp>
35#include <string>
36#include <vector>
37
38namespace
39{
40
41void closefd(int&& fd, const internal::Sys*& sys)
42{
43 sys->close(fd);
44}
45using Fd = stdplus::Managed<int, const internal::Sys*>::Handle<closefd>;
46
47} // namespace
48
49namespace host_tool
50{
51
52bool NetDataHandler::sendContents(const std::string& input,
53 std::uint16_t session)
54{
55 constexpr size_t blockSize = 64 * 1024;
56 Fd inputFd(std::nullopt, sys);
57
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 if (fileSize == 0)
69 {
70 std::fprintf(stderr,
71 "Zero-length file, or other file access error\n");
72 return false;
73 }
74
75 progress->start(fileSize);
76 }
77
78 Fd connFd(std::nullopt, sys);
79
80 {
81 struct addrinfo hints;
82 std::memset(&hints, 0, sizeof(hints));
83 hints.ai_flags = AI_NUMERICHOST;
Benjamin Fair2aa55342019-12-04 16:39:04 -080084 hints.ai_family = AF_UNSPEC;
Benjamin Fair30d09a32019-10-11 16:57:47 -070085 hints.ai_socktype = SOCK_STREAM;
86
87 struct addrinfo *addrs, *addr;
88 int ret = sys->getaddrinfo(host.c_str(), port.c_str(), &hints, &addrs);
89 if (ret < 0)
90 {
91 std::fprintf(stderr, "Couldn't parse address %s with port %s: %s\n",
92 host.c_str(), port.c_str(), gai_strerror(ret));
93 return false;
94 }
95
96 for (addr = addrs; addr != nullptr; addr = addr->ai_next)
97 {
98 connFd.reset(sys->socket(addr->ai_family, addr->ai_socktype,
99 addr->ai_protocol));
100 if (*connFd == -1)
101 continue;
102
103 if (sys->connect(*connFd, addr->ai_addr, addr->ai_addrlen) != -1)
104 break;
105 }
106
107 // TODO: use stdplus Managed for the addrinfo structs
108 sys->freeaddrinfo(addrs);
109
110 if (addr == nullptr)
111 {
112 std::fprintf(stderr, "Failed to connect\n");
113 return false;
114 }
115 }
116
117 try
118 {
119 int bytesSent = 0;
120 off_t offset = 0;
121
122 do
123 {
124 bytesSent = sys->sendfile(*connFd, *inputFd, &offset, blockSize);
125 if (bytesSent < 0)
126 {
127 std::fprintf(stderr, "Failed to send data to BMC: %s\n",
128 strerror(errno));
129 return false;
130 }
131 else if (bytesSent > 0)
132 {
133 /* Ok, so the data is staged, now send the blob write with
134 * the details.
135 */
136 struct ipmi_flash::ExtChunkHdr chunk;
137 chunk.length = bytesSent;
138 std::vector<std::uint8_t> chunkBytes(sizeof(chunk));
139 std::memcpy(chunkBytes.data(), &chunk, sizeof(chunk));
140
141 /* This doesn't return anything on success. */
142 blob->writeBytes(session, offset - bytesSent, chunkBytes);
143 progress->updateProgress(bytesSent);
144 }
145 } while (bytesSent > 0);
146 }
147 catch (const ipmiblob::BlobException& b)
148 {
149 return false;
150 }
151
152 return true;
153}
154
155} // namespace host_tool