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     // This function creates a raw socket and initializes sockfd_.
38     // If the default constructor for this class was used,
39     // this function MUST be called before the object can be used
40     // for anything else.
41     int init();
42 
43     // Since raw packet socket is used for NC-SI, it needs to be bound
44     // to the interface. This function needs to be called after init,
45     // before the socket it used for communication.
46     int bind_to_iface(const net::IFaceBase& iface);
47 
48     // Applies a filter to the interface to ignore VLAN tagged packets
49     int filter_vlans();
50 
51     // Non-blocking version of recv. Uses poll with timeout.
52     int recv(void* buf, size_t maxlen) override;
53 
54   private:
55     struct sockaddr_ll sock_addr_;
56     const int kpoll_timeout_ = 10;
57 };
58 
59 } // namespace ncsi
60