Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef SHILL_SOCKETS_H_ |
| 6 | #define SHILL_SOCKETS_H_ |
| 7 | |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 8 | #include <linux/filter.h> |
Darin Petkov | ef34f18 | 2011-08-26 14:14:40 -0700 | [diff] [blame] | 9 | #include <sys/socket.h> |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 10 | #include <sys/types.h> |
| 11 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 14 | #include <base/basictypes.h> |
| 15 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 16 | namespace shill { |
| 17 | |
| 18 | // A "sys/socket.h" abstraction allowing mocking in tests. |
| 19 | class Sockets { |
| 20 | public: |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 21 | Sockets(); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 22 | virtual ~Sockets(); |
| 23 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 24 | // accept |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 25 | virtual int Accept(int sockfd, |
| 26 | struct sockaddr *addr, |
| 27 | socklen_t *addrlen) const; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 28 | |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 29 | // getsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, ...) |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 30 | virtual int AttachFilter(int sockfd, struct sock_fprog *pf) const; |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 31 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 32 | // bind |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 33 | virtual int Bind(int sockfd, |
| 34 | const struct sockaddr *addr, |
| 35 | socklen_t addrlen) const; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 36 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 37 | // setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE ...) |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 38 | virtual int BindToDevice(int sockfd, const std::string &device) const; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 39 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 40 | // close |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 41 | virtual int Close(int fd) const; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 42 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 43 | // connect |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 44 | virtual int Connect(int sockfd, |
| 45 | const struct sockaddr *addr, |
| 46 | socklen_t addrlen) const; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 47 | |
| 48 | // errno |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 49 | virtual int Error() const; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 50 | |
| 51 | // errno |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 52 | virtual std::string ErrorString() const; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 53 | |
| 54 | // getsockname |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 55 | virtual int GetSockName(int sockfd, |
| 56 | struct sockaddr *addr, |
| 57 | socklen_t *addrlen) const; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 58 | |
| 59 | // getsockopt(sockfd, SOL_SOCKET, SO_ERROR, ...) |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 60 | virtual int GetSocketError(int sockfd) const; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 61 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 62 | // ioctl |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 63 | virtual int Ioctl(int d, int request, void *argp) const; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 64 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 65 | // listen |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 66 | virtual int Listen(int sockfd, int backlog) const; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 67 | |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 68 | // recvfrom |
| 69 | virtual ssize_t RecvFrom(int sockfd, void *buf, size_t len, int flags, |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 70 | struct sockaddr *src_addr, socklen_t *addrlen) const; |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 71 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 72 | // select |
| 73 | virtual int Select(int nfds, |
| 74 | fd_set *readfds, |
| 75 | fd_set *writefds, |
| 76 | fd_set *exceptfds, |
| 77 | struct timeval *timeout) const; |
| 78 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 79 | // send |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 80 | virtual ssize_t Send(int sockfd, |
| 81 | const void *buf, |
| 82 | size_t len, |
| 83 | int flags) const; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 84 | |
| 85 | // sendto |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 86 | virtual ssize_t SendTo(int sockfd, |
| 87 | const void *buf, |
| 88 | size_t len, |
| 89 | int flags, |
| 90 | const struct sockaddr *dest_addr, |
| 91 | socklen_t addrlen) const; |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 92 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 93 | // fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK) |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 94 | virtual int SetNonBlocking(int sockfd) const; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 95 | |
Julius Werner | f253c84 | 2012-12-20 14:52:30 -0800 | [diff] [blame] | 96 | // setsockopt(SO_RCVBUFFORCE) |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 97 | virtual int SetReceiveBuffer(int sockfd, int size) const; |
Julius Werner | f253c84 | 2012-12-20 14:52:30 -0800 | [diff] [blame] | 98 | |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 99 | // shutdown |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 100 | virtual int ShutDown(int sockfd, int how) const; |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 101 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 102 | // socket |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 103 | virtual int Socket(int domain, int type, int protocol) const; |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame] | 104 | |
| 105 | private: |
| 106 | DISALLOW_COPY_AND_ASSIGN(Sockets); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 109 | class ScopedSocketCloser { |
| 110 | public: |
| 111 | ScopedSocketCloser(Sockets *sockets, int fd); |
| 112 | ~ScopedSocketCloser(); |
| 113 | |
| 114 | private: |
| 115 | Sockets *sockets_; |
| 116 | int fd_; |
| 117 | |
| 118 | DISALLOW_COPY_AND_ASSIGN(ScopedSocketCloser); |
| 119 | }; |
| 120 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 121 | } // namespace shill |
| 122 | |
| 123 | #endif // SHILL_SOCKETS_H_ |