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