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 | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame^] | 10 | #include <linux/filter.h> |
Darin Petkov | ef34f18 | 2011-08-26 14:14:40 -0700 | [diff] [blame] | 11 | #include <sys/socket.h> |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 12 | #include <sys/types.h> |
| 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: |
| 21 | virtual ~Sockets(); |
| 22 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 23 | // accept |
| 24 | virtual int Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); |
| 25 | |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame^] | 26 | // getsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, ...) |
| 27 | virtual int AttachFilter(int sockfd, struct sock_fprog *pf); |
| 28 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 29 | // bind |
| 30 | virtual int Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); |
| 31 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 32 | // setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE ...) |
| 33 | virtual int BindToDevice(int sockfd, const std::string &device); |
| 34 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 35 | // close |
| 36 | virtual int Close(int fd); |
| 37 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 38 | // connect |
| 39 | virtual int Connect(int sockfd, const struct sockaddr *addr, |
| 40 | socklen_t addrlen); |
| 41 | |
| 42 | // errno |
| 43 | virtual int Error(); |
| 44 | |
| 45 | // errno |
| 46 | virtual std::string ErrorString(); |
| 47 | |
| 48 | // getsockname |
| 49 | virtual int GetSockName(int sockfd, struct sockaddr *addr, |
| 50 | socklen_t *addrlen); |
| 51 | |
| 52 | // getsockopt(sockfd, SOL_SOCKET, SO_ERROR, ...) |
| 53 | virtual int GetSocketError(int sockfd); |
| 54 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 55 | // ioctl |
| 56 | virtual int Ioctl(int d, int request, void *argp); |
| 57 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 58 | // listen |
| 59 | virtual int Listen(int sockfd, int backlog); |
| 60 | |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame^] | 61 | // recvfrom |
| 62 | virtual ssize_t RecvFrom(int sockfd, void *buf, size_t len, int flags, |
| 63 | struct sockaddr *src_addr, socklen_t *addrlen); |
| 64 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 65 | // send |
| 66 | virtual ssize_t Send(int sockfd, const void *buf, size_t len, int flags); |
| 67 | |
| 68 | // sendto |
| 69 | virtual ssize_t SendTo(int sockfd, const void *buf, size_t len, int flags, |
| 70 | const struct sockaddr *dest_addr, socklen_t addrlen); |
| 71 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 72 | // fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK) |
| 73 | virtual int SetNonBlocking(int sockfd); |
| 74 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 75 | // socket |
| 76 | virtual int Socket(int domain, int type, int protocol); |
| 77 | }; |
| 78 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 79 | class ScopedSocketCloser { |
| 80 | public: |
| 81 | ScopedSocketCloser(Sockets *sockets, int fd); |
| 82 | ~ScopedSocketCloser(); |
| 83 | |
| 84 | private: |
| 85 | Sockets *sockets_; |
| 86 | int fd_; |
| 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(ScopedSocketCloser); |
| 89 | }; |
| 90 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 91 | } // namespace shill |
| 92 | |
| 93 | #endif // SHILL_SOCKETS_H_ |