xref: /openbmc/google-misc/ncsid/src/ncsi_sockio.h (revision 15fe169d)
1 /*
2  * Copyright 2021 Google LLC
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 #pragma once
18 
19 #include "net_iface.h"
20 #include "net_sockio.h"
21 
22 #include <sys/socket.h>
23 
24 #include <cstddef>
25 #include <cstring>
26 
27 namespace ncsi
28 {
29 
30 class SockIO : public net::SockIO
31 {
32   public:
33     SockIO() = default;
34 
35     explicit SockIO(int sockfd) : net::SockIO(sockfd)
36     {}
37 
38     // This function creates a raw socket and initializes sockfd_.
39     // If the default constructor for this class was used,
40     // this function MUST be called before the object can be used
41     // for anything else.
42     int init();
43 
44     // Since raw packet socket is used for NC-SI, it needs to be bound
45     // to the interface. This function needs to be called after init,
46     // before the socket it used for communication.
47     int bind_to_iface(const net::IFaceBase& iface);
48 
49     // Applies a filter to the interface to ignore VLAN tagged packets
50     int filter_vlans();
51 
52     // Non-blocking version of recv. Uses poll with timeout.
53     int recv(void* buf, size_t maxlen) override;
54 
55   private:
56     struct sockaddr_ll sock_addr_;
57     const int kpoll_timeout_ = 10;
58 };
59 
60 } // namespace ncsi
61