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 | |
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 | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 15 | #include <base/logging.h> |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | Sockets::~Sockets() {} |
| 20 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 21 | int Sockets::Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { |
| 22 | return accept(sockfd, addr, addrlen); |
| 23 | } |
| 24 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 25 | int Sockets::Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { |
| 26 | return bind(sockfd, addr, addrlen); |
| 27 | } |
| 28 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 29 | int Sockets::BindToDevice(int sockfd, const std::string &device) { |
| 30 | char dev_name[IFNAMSIZ]; |
| 31 | CHECK_GT(sizeof(dev_name), device.length()); |
| 32 | memset(&dev_name, 0, sizeof(dev_name)); |
| 33 | snprintf(dev_name, sizeof(dev_name), "%s", device.c_str()); |
| 34 | return setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, &dev_name, |
| 35 | sizeof(dev_name)); |
| 36 | } |
| 37 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 38 | int Sockets::Close(int fd) { |
| 39 | return close(fd); |
| 40 | } |
| 41 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 42 | int Sockets::Connect(int sockfd, const struct sockaddr *addr, |
| 43 | socklen_t addrlen) { |
| 44 | return connect(sockfd, addr, addrlen); |
| 45 | } |
| 46 | |
| 47 | int Sockets::Error() { |
| 48 | return errno; |
| 49 | } |
| 50 | |
| 51 | std::string Sockets::ErrorString() { |
| 52 | return std::string(strerror(Error())); |
| 53 | } |
| 54 | |
| 55 | int Sockets::GetSockName(int sockfd, |
| 56 | struct sockaddr *addr, |
| 57 | socklen_t *addrlen) { |
| 58 | return getsockname(sockfd, addr, addrlen); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | int Sockets::GetSocketError(int sockfd) { |
| 63 | int error; |
| 64 | socklen_t optlen = sizeof(error); |
| 65 | if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &optlen) == 0) { |
| 66 | return error; |
| 67 | } |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 72 | int Sockets::Ioctl(int d, int request, void *argp) { |
| 73 | return ioctl(d, request, argp); |
| 74 | } |
| 75 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 76 | int Sockets::Listen(int sockfd, int backlog) { |
| 77 | return listen(sockfd, backlog); |
| 78 | } |
| 79 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 80 | ssize_t Sockets::Send(int sockfd, const void *buf, size_t len, int flags) { |
| 81 | return send(sockfd, buf, len, flags); |
| 82 | } |
| 83 | |
| 84 | ssize_t Sockets::SendTo(int sockfd, const void *buf, size_t len, int flags, |
| 85 | const struct sockaddr *dest_addr, socklen_t addrlen) { |
| 86 | return sendto(sockfd, buf, len, flags, dest_addr, addrlen); |
| 87 | } |
| 88 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 89 | int Sockets::SetNonBlocking(int sockfd) { |
| 90 | return fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK); |
| 91 | } |
| 92 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 93 | int Sockets::Socket(int domain, int type, int protocol) { |
| 94 | return socket(domain, type, protocol); |
| 95 | } |
| 96 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 97 | ScopedSocketCloser::ScopedSocketCloser(Sockets *sockets, int fd) |
| 98 | : sockets_(sockets), |
| 99 | fd_(fd) {} |
| 100 | |
| 101 | ScopedSocketCloser::~ScopedSocketCloser() { |
| 102 | sockets_->Close(fd_); |
| 103 | fd_ = -1; |
| 104 | } |
| 105 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 106 | } // namespace shill |