summaryrefslogtreecommitdiffstats
path: root/bmc/net_handler.cpp
blob: 14ae4fbfc9677cab9c08c00d74c0173f71d8410d (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright 2019 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "net_handler.hpp"

#include <errno.h>
#include <netinet/in.h>
#include <poll.h>
#include <sys/socket.h>
#include <unistd.h>

#include <cstdio>

namespace ipmi_flash
{

bool NetDataHandler::open()
{
    listenFd.reset(::socket(AF_INET6, SOCK_STREAM, 0));
    if (*listenFd < 0)
    {
        std::perror("Failed to create socket");
        (void)listenFd.release();
        return false;
    }

    struct sockaddr_in6 listenAddr;
    listenAddr.sin6_family = AF_INET6;
    listenAddr.sin6_port = htons(listenPort);
    listenAddr.sin6_flowinfo = 0;
    listenAddr.sin6_addr = in6addr_any;
    listenAddr.sin6_scope_id = 0;

    if (::bind(*listenFd, (struct sockaddr*)&listenAddr, sizeof(listenAddr)) <
        0)
    {
        std::perror("Failed to bind");
        return false;
    }

    if (::listen(*listenFd, 1) < 0)
    {
        std::perror("Failed to listen");
        return false;
    }
    return true;
}

bool NetDataHandler::close()
{
    connFd.reset();
    listenFd.reset();

    return true;
}

std::vector<std::uint8_t> NetDataHandler::copyFrom(std::uint32_t length)
{
    if (!connFd)
    {
        struct pollfd fds;
        fds.fd = *listenFd;
        fds.events = POLLIN;

        int ret = ::poll(&fds, 1, timeoutS * 1000);
        if (ret < 0)
        {
            std::perror("Failed to poll");
            return std::vector<uint8_t>();
        }
        else if (ret == 0)
        {
            fprintf(stderr, "Timed out waiting for connection\n");
            return std::vector<uint8_t>();
        }
        else if (fds.revents != POLLIN)
        {
            fprintf(stderr, "Invalid poll state: 0x%x\n", fds.revents);
            return std::vector<uint8_t>();
        }

        connFd.reset(::accept(*listenFd, nullptr, nullptr));
        if (*connFd < 0)
        {
            std::perror("Failed to accept connection");
            (void)connFd.release();
            return std::vector<uint8_t>();
        }

        struct timeval tv = {};
        tv.tv_sec = timeoutS;
        if (setsockopt(*connFd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
        {
            std::perror("Failed to set receive timeout");
            return std::vector<uint8_t>();
        }
    }

    std::vector<std::uint8_t> data(length);

    std::uint32_t bytesRead = 0;
    ssize_t ret;
    do
    {
        ret = read(*connFd, data.data() + bytesRead, length - bytesRead);
        if (ret < 0)
        {
            if (errno == EINTR || errno == EAGAIN)
                continue;
            std::perror("Failed to read from socket");
            break;
        }

        bytesRead += ret;
    } while (ret > 0 && bytesRead < length);

    if (bytesRead != length)
    {
        fprintf(stderr,
                "Couldn't read full expected amount. Wanted %u but got %u\n",
                length, bytesRead);
        data.resize(bytesRead);
    }

    return data;
}

bool NetDataHandler::writeMeta(const std::vector<std::uint8_t>& configuration)
{
    // TODO: have the host tool send the expected IP address that it will
    // connect from
    return true;
}

std::vector<std::uint8_t> NetDataHandler::readMeta()
{
    return std::vector<std::uint8_t>();
}

} // namespace ipmi_flash
OpenPOWER on IntegriCloud