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