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 <cstddef>
23 
24 namespace ncsi
25 {
26 
27 class SockIO : public net::SockIO
28 {
29   public:
30     SockIO() = default;
31 
32     explicit SockIO(int sockfd) : net::SockIO(sockfd) {}
33 
34     // This function creates a raw socket and initializes sockfd_.
35     // If the default constructor for this class was used,
36     // this function MUST be called before the object can be used
37     // for anything else.
38     int init();
39 
40     // Since raw packet socket is used for NC-SI, it needs to be bound
41     // to the interface. This function needs to be called after init,
42     // before the socket it used for communication.
43     int bind_to_iface(const net::IFaceBase& iface);
44 
45     // Applies a filter to the interface to ignore VLAN tagged packets
46     int filter_vlans();
47 
48     // Non-blocking version of recv. Uses poll with timeout.
49     int recv(void* buf, size_t maxlen) override;
50 
51   private:
52     const int kpoll_timeout_ = 10;
53 };
54 
55 } // namespace ncsi
56