blob: 032ee4f36804ac9a636d0c2ad895914fc4d1fa3f [file] [log] [blame]
Darin Petkov633ac6f2011-07-08 13:56:13 -07001// 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 Stewartf65320c2011-10-13 14:34:52 -07005#include "shill/sockets.h"
6
7#include <errno.h>
8#include <fcntl.h>
9#include <net/if.h>
10#include <stdio.h>
Darin Petkove0a312e2011-07-20 13:45:28 -070011#include <sys/ioctl.h>
Darin Petkov633ac6f2011-07-08 13:56:13 -070012#include <sys/socket.h>
13#include <unistd.h>
14
Paul Stewartf65320c2011-10-13 14:34:52 -070015#include <base/logging.h>
Darin Petkov633ac6f2011-07-08 13:56:13 -070016
17namespace shill {
18
19Sockets::~Sockets() {}
20
Paul Stewartf65320c2011-10-13 14:34:52 -070021int Sockets::Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
22 return accept(sockfd, addr, addrlen);
23}
24
Darin Petkov633ac6f2011-07-08 13:56:13 -070025int Sockets::Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
26 return bind(sockfd, addr, addrlen);
27}
28
Paul Stewartf65320c2011-10-13 14:34:52 -070029int 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 Petkov633ac6f2011-07-08 13:56:13 -070038int Sockets::Close(int fd) {
39 return close(fd);
40}
41
Paul Stewartf65320c2011-10-13 14:34:52 -070042int Sockets::Connect(int sockfd, const struct sockaddr *addr,
43 socklen_t addrlen) {
44 return connect(sockfd, addr, addrlen);
45}
46
47int Sockets::Error() {
48 return errno;
49}
50
51std::string Sockets::ErrorString() {
52 return std::string(strerror(Error()));
53}
54
55int Sockets::GetSockName(int sockfd,
56 struct sockaddr *addr,
57 socklen_t *addrlen) {
58 return getsockname(sockfd, addr, addrlen);
59}
60
61
62int 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 Petkove0a312e2011-07-20 13:45:28 -070072int Sockets::Ioctl(int d, int request, void *argp) {
73 return ioctl(d, request, argp);
74}
75
Paul Stewartf65320c2011-10-13 14:34:52 -070076int Sockets::Listen(int sockfd, int backlog) {
77 return listen(sockfd, backlog);
78}
79
Darin Petkov633ac6f2011-07-08 13:56:13 -070080ssize_t Sockets::Send(int sockfd, const void *buf, size_t len, int flags) {
81 return send(sockfd, buf, len, flags);
82}
83
84ssize_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 Stewartf65320c2011-10-13 14:34:52 -070089int Sockets::SetNonBlocking(int sockfd) {
90 return fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK);
91}
92
Darin Petkov633ac6f2011-07-08 13:56:13 -070093int Sockets::Socket(int domain, int type, int protocol) {
94 return socket(domain, type, protocol);
95}
96
Darin Petkove0a312e2011-07-20 13:45:28 -070097ScopedSocketCloser::ScopedSocketCloser(Sockets *sockets, int fd)
98 : sockets_(sockets),
99 fd_(fd) {}
100
101ScopedSocketCloser::~ScopedSocketCloser() {
102 sockets_->Close(fd_);
103 fd_ = -1;
104}
105
Darin Petkov633ac6f2011-07-08 13:56:13 -0700106} // namespace shill