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