1// 2// Copyright (C) 2012 The Android Open Source Project 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#ifndef SHILL_NET_SOCKETS_H_ 18#define SHILL_NET_SOCKETS_H_ 19 20#include <linux/filter.h> 21#include <netinet/in.h> 22#include <sys/socket.h> 23#include <sys/types.h> 24 25#include <string> 26 27#include <base/compiler_specific.h> 28#include <base/macros.h> 29 30#include "shill/net/shill_export.h" 31 32namespace shill { 33 34// A "sys/socket.h" abstraction allowing mocking in tests. 35class SHILL_EXPORT Sockets { 36 public: 37 Sockets(); 38 virtual ~Sockets(); 39 40 static const int kInvalidFileDescriptor = -1; 41 42 // accept 43 virtual int Accept(int sockfd, 44 struct sockaddr* addr, 45 socklen_t* addrlen) const; 46 47 // getsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, ...) 48 virtual int AttachFilter(int sockfd, struct sock_fprog* pf) const; 49 50 // bind 51 virtual int Bind(int sockfd, 52 const struct sockaddr* addr, 53 socklen_t addrlen) const; 54 55 // setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE ...) 56 virtual int BindToDevice(int sockfd, const std::string& device) const; 57 58 // setsockopt(s, SOL_SOCKET, SO_REUSEADDR, ...) 59 virtual int ReuseAddress(int sockfd) const; 60 61 // setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, ...) 62 virtual int AddMulticastMembership(int sockfd, in_addr_t addr) const; 63 64 // close 65 virtual int Close(int fd) const; 66 67 // connect 68 virtual int Connect(int sockfd, 69 const struct sockaddr* addr, 70 socklen_t addrlen) const; 71 72 // errno 73 virtual int Error() const; 74 75 // errno 76 virtual std::string ErrorString() const; 77 78 // getsockname 79 virtual int GetSockName(int sockfd, 80 struct sockaddr* addr, 81 socklen_t* addrlen) const; 82 83 // getsockopt(sockfd, SOL_SOCKET, SO_ERROR, ...) 84 virtual int GetSocketError(int sockfd) const; 85 86 // ioctl 87 virtual int Ioctl(int d, int request, void* argp) const; 88 89 // listen 90 virtual int Listen(int sockfd, int backlog) const; 91 92 // recvfrom 93 virtual ssize_t RecvFrom(int sockfd, void* buf, size_t len, int flags, 94 struct sockaddr* src_addr, socklen_t* addrlen) const; 95 96 // select 97 virtual int Select(int nfds, 98 fd_set* readfds, 99 fd_set* writefds, 100 fd_set* exceptfds, 101 struct timeval* timeout) const; 102 103 // send 104 virtual ssize_t Send(int sockfd, 105 const void* buf, 106 size_t len, 107 int flags) const; 108 109 // sendto 110 virtual ssize_t SendTo(int sockfd, 111 const void* buf, 112 size_t len, 113 int flags, 114 const struct sockaddr* dest_addr, 115 socklen_t addrlen) const; 116 117 // fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK) 118 virtual int SetNonBlocking(int sockfd) const; 119 120 // setsockopt(SO_RCVBUFFORCE) 121 virtual int SetReceiveBuffer(int sockfd, int size) const; 122 123 // shutdown 124 virtual int ShutDown(int sockfd, int how) const; 125 126 // socket 127 virtual int Socket(int domain, int type, int protocol) const; 128 129 private: 130 DISALLOW_COPY_AND_ASSIGN(Sockets); 131}; 132 133class SHILL_EXPORT ScopedSocketCloser { 134 public: 135 ScopedSocketCloser(Sockets* sockets, int fd); 136 ~ScopedSocketCloser(); 137 138 // Release and return the socket file descriptor, allowing the socket to 139 // remain open as the ScopedSocketCloser is destroyed. 140 int Release() WARN_UNUSED_RESULT; 141 142 private: 143 Sockets* sockets_; 144 int fd_; 145 146 DISALLOW_COPY_AND_ASSIGN(ScopedSocketCloser); 147}; 148 149} // namespace shill 150 151#endif // SHILL_NET_SOCKETS_H_ 152