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 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 5 | #include "shill/sockets.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <net/if.h> |
| 10 | #include <stdio.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 11 | #include <sys/ioctl.h> |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 12 | #include <sys/socket.h> |
| 13 | #include <unistd.h> |
| 14 | |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 15 | #include <base/posix/eintr_wrapper.h> |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 16 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 17 | #include "shill/logging.h" |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 18 | |
| 19 | namespace shill { |
| 20 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 21 | Sockets::Sockets() {} |
| 22 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 23 | Sockets::~Sockets() {} |
| 24 | |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 25 | // Some system calls can be interrupted and return EINTR, but will succeed on |
| 26 | // retry. The HANDLE_EINTR macro retries a call if it returns EINTR. For a |
| 27 | // list of system calls that can return EINTR, see 'man 7 signal' under the |
| 28 | // heading "Interruption of System Calls and Library Functions by Signal |
| 29 | // Handlers". |
| 30 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 31 | int Sockets::Accept(int sockfd, |
| 32 | struct sockaddr *addr, |
| 33 | socklen_t *addrlen) const { |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 34 | return HANDLE_EINTR(accept(sockfd, addr, addrlen)); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 37 | int Sockets::AttachFilter(int sockfd, struct sock_fprog *pf) const { |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 38 | return setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, pf, sizeof(*pf)); |
| 39 | } |
| 40 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 41 | int Sockets::Bind(int sockfd, |
| 42 | const struct sockaddr *addr, |
| 43 | socklen_t addrlen) const { |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 44 | return bind(sockfd, addr, addrlen); |
| 45 | } |
| 46 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 47 | int Sockets::BindToDevice(int sockfd, const std::string &device) const { |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 48 | char dev_name[IFNAMSIZ]; |
| 49 | CHECK_GT(sizeof(dev_name), device.length()); |
| 50 | memset(&dev_name, 0, sizeof(dev_name)); |
| 51 | snprintf(dev_name, sizeof(dev_name), "%s", device.c_str()); |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 52 | return HANDLE_EINTR(setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, &dev_name, |
| 53 | sizeof(dev_name))); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 56 | int Sockets::Close(int fd) const { |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 57 | return HANDLE_EINTR(close(fd)); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 60 | int Sockets::Connect(int sockfd, |
| 61 | const struct sockaddr *addr, |
| 62 | socklen_t addrlen) const { |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 63 | return HANDLE_EINTR(connect(sockfd, addr, addrlen)); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 66 | int Sockets::Error() const { |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 67 | return errno; |
| 68 | } |
| 69 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 70 | std::string Sockets::ErrorString() const { |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 71 | return std::string(strerror(Error())); |
| 72 | } |
| 73 | |
| 74 | int Sockets::GetSockName(int sockfd, |
| 75 | struct sockaddr *addr, |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 76 | socklen_t *addrlen) const { |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 77 | return getsockname(sockfd, addr, addrlen); |
| 78 | } |
| 79 | |
| 80 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 81 | int Sockets::GetSocketError(int sockfd) const { |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 82 | int error; |
| 83 | socklen_t optlen = sizeof(error); |
| 84 | if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &optlen) == 0) { |
| 85 | return error; |
| 86 | } |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 91 | int Sockets::Ioctl(int d, int request, void *argp) const { |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 92 | return HANDLE_EINTR(ioctl(d, request, argp)); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 95 | int Sockets::Listen(int sockfd, int backlog) const { |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 96 | return listen(sockfd, backlog); |
| 97 | } |
| 98 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 99 | ssize_t Sockets::RecvFrom(int sockfd, |
| 100 | void *buf, |
| 101 | size_t len, |
| 102 | int flags, |
| 103 | struct sockaddr *src_addr, |
| 104 | socklen_t *addrlen) const { |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 105 | return HANDLE_EINTR(recvfrom(sockfd, buf, len, flags, src_addr, addrlen)); |
Paul Stewart | ac1328e | 2012-07-20 11:55:40 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 108 | int Sockets::Select(int nfds, |
| 109 | fd_set *readfds, |
| 110 | fd_set *writefds, |
| 111 | fd_set *exceptfds, |
| 112 | struct timeval *timeout) const { |
| 113 | return HANDLE_EINTR(select(nfds, readfds, writefds, exceptfds, timeout)); |
| 114 | } |
| 115 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 116 | ssize_t Sockets::Send(int sockfd, |
| 117 | const void *buf, |
| 118 | size_t len, |
| 119 | int flags) const { |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 120 | return HANDLE_EINTR(send(sockfd, buf, len, flags)); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 123 | ssize_t Sockets::SendTo(int sockfd, |
| 124 | const void *buf, |
| 125 | size_t len, |
| 126 | int flags, |
| 127 | const struct sockaddr *dest_addr, |
| 128 | socklen_t addrlen) const { |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 129 | return HANDLE_EINTR(sendto(sockfd, buf, len, flags, dest_addr, addrlen)); |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 132 | int Sockets::SetNonBlocking(int sockfd) const { |
Gary Morain | 4178023 | 2012-07-31 15:08:31 -0700 | [diff] [blame] | 133 | return HANDLE_EINTR( |
| 134 | fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK)); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 137 | int Sockets::SetReceiveBuffer(int sockfd, int size) const { |
Julius Werner | f253c84 | 2012-12-20 14:52:30 -0800 | [diff] [blame] | 138 | // Note: kernel will set buffer to 2*size to allow for struct skbuff overhead |
| 139 | return setsockopt(sockfd, SOL_SOCKET, SO_RCVBUFFORCE, &size, sizeof(size)); |
| 140 | } |
| 141 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 142 | int Sockets::ShutDown(int sockfd, int how) const { |
Prathmesh Prabhu | 40daa01 | 2013-04-03 10:35:03 -0700 | [diff] [blame] | 143 | return HANDLE_EINTR(shutdown(sockfd, how)); |
| 144 | } |
| 145 | |
Wade Guthrie | 9f62cd1 | 2013-05-15 10:40:42 -0700 | [diff] [blame] | 146 | int Sockets::Socket(int domain, int type, int protocol) const { |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 147 | return socket(domain, type, protocol); |
| 148 | } |
| 149 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 150 | ScopedSocketCloser::ScopedSocketCloser(Sockets *sockets, int fd) |
| 151 | : sockets_(sockets), |
| 152 | fd_(fd) {} |
| 153 | |
| 154 | ScopedSocketCloser::~ScopedSocketCloser() { |
| 155 | sockets_->Close(fd_); |
| 156 | fd_ = -1; |
| 157 | } |
| 158 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 159 | } // namespace shill |