blob: 17e9ff8378d47102bf1b1942f24e41e932547183 [file] [log] [blame]
Paul Stewartac1328e2012-07-20 11:55:40 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov633ac6f2011-07-08 13:56:13 -07002// 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 Stewartac1328e2012-07-20 11:55:40 -07008#include <linux/filter.h>
Darin Petkovef34f182011-08-26 14:14:40 -07009#include <sys/socket.h>
Darin Petkov633ac6f2011-07-08 13:56:13 -070010#include <sys/types.h>
11
Wade Guthriefa2100e2013-05-15 10:11:22 -070012#include <string>
13
Darin Petkove0a312e2011-07-20 13:45:28 -070014#include <base/basictypes.h>
15
Darin Petkov633ac6f2011-07-08 13:56:13 -070016namespace shill {
17
18// A "sys/socket.h" abstraction allowing mocking in tests.
19class Sockets {
20 public:
Wade Guthriefa2100e2013-05-15 10:11:22 -070021 Sockets();
Darin Petkov633ac6f2011-07-08 13:56:13 -070022 virtual ~Sockets();
23
Paul Stewartf65320c2011-10-13 14:34:52 -070024 // accept
Wade Guthrie9f62cd12013-05-15 10:40:42 -070025 virtual int Accept(int sockfd,
26 struct sockaddr *addr,
27 socklen_t *addrlen) const;
Paul Stewartf65320c2011-10-13 14:34:52 -070028
Paul Stewartac1328e2012-07-20 11:55:40 -070029 // getsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, ...)
Wade Guthrie9f62cd12013-05-15 10:40:42 -070030 virtual int AttachFilter(int sockfd, struct sock_fprog *pf) const;
Paul Stewartac1328e2012-07-20 11:55:40 -070031
Darin Petkov633ac6f2011-07-08 13:56:13 -070032 // bind
Wade Guthrie9f62cd12013-05-15 10:40:42 -070033 virtual int Bind(int sockfd,
34 const struct sockaddr *addr,
35 socklen_t addrlen) const;
Darin Petkov633ac6f2011-07-08 13:56:13 -070036
Paul Stewartf65320c2011-10-13 14:34:52 -070037 // setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE ...)
Wade Guthrie9f62cd12013-05-15 10:40:42 -070038 virtual int BindToDevice(int sockfd, const std::string &device) const;
Paul Stewartf65320c2011-10-13 14:34:52 -070039
Darin Petkov633ac6f2011-07-08 13:56:13 -070040 // close
Wade Guthrie9f62cd12013-05-15 10:40:42 -070041 virtual int Close(int fd) const;
Darin Petkov633ac6f2011-07-08 13:56:13 -070042
Paul Stewartf65320c2011-10-13 14:34:52 -070043 // connect
Wade Guthrie9f62cd12013-05-15 10:40:42 -070044 virtual int Connect(int sockfd,
45 const struct sockaddr *addr,
46 socklen_t addrlen) const;
Paul Stewartf65320c2011-10-13 14:34:52 -070047
48 // errno
Wade Guthrie9f62cd12013-05-15 10:40:42 -070049 virtual int Error() const;
Paul Stewartf65320c2011-10-13 14:34:52 -070050
51 // errno
Wade Guthrie9f62cd12013-05-15 10:40:42 -070052 virtual std::string ErrorString() const;
Paul Stewartf65320c2011-10-13 14:34:52 -070053
54 // getsockname
Wade Guthrie9f62cd12013-05-15 10:40:42 -070055 virtual int GetSockName(int sockfd,
56 struct sockaddr *addr,
57 socklen_t *addrlen) const;
Paul Stewartf65320c2011-10-13 14:34:52 -070058
59 // getsockopt(sockfd, SOL_SOCKET, SO_ERROR, ...)
Wade Guthrie9f62cd12013-05-15 10:40:42 -070060 virtual int GetSocketError(int sockfd) const;
Paul Stewartf65320c2011-10-13 14:34:52 -070061
Darin Petkove0a312e2011-07-20 13:45:28 -070062 // ioctl
Wade Guthrie9f62cd12013-05-15 10:40:42 -070063 virtual int Ioctl(int d, int request, void *argp) const;
Darin Petkove0a312e2011-07-20 13:45:28 -070064
Paul Stewartf65320c2011-10-13 14:34:52 -070065 // listen
Wade Guthrie9f62cd12013-05-15 10:40:42 -070066 virtual int Listen(int sockfd, int backlog) const;
Paul Stewartf65320c2011-10-13 14:34:52 -070067
Paul Stewartac1328e2012-07-20 11:55:40 -070068 // recvfrom
69 virtual ssize_t RecvFrom(int sockfd, void *buf, size_t len, int flags,
Wade Guthrie9f62cd12013-05-15 10:40:42 -070070 struct sockaddr *src_addr, socklen_t *addrlen) const;
Paul Stewartac1328e2012-07-20 11:55:40 -070071
Wade Guthriefa2100e2013-05-15 10:11:22 -070072 // select
73 virtual int Select(int nfds,
74 fd_set *readfds,
75 fd_set *writefds,
76 fd_set *exceptfds,
77 struct timeval *timeout) const;
78
Darin Petkov633ac6f2011-07-08 13:56:13 -070079 // send
Wade Guthrie9f62cd12013-05-15 10:40:42 -070080 virtual ssize_t Send(int sockfd,
81 const void *buf,
82 size_t len,
83 int flags) const;
Darin Petkov633ac6f2011-07-08 13:56:13 -070084
85 // sendto
Wade Guthrie9f62cd12013-05-15 10:40:42 -070086 virtual ssize_t SendTo(int sockfd,
87 const void *buf,
88 size_t len,
89 int flags,
90 const struct sockaddr *dest_addr,
91 socklen_t addrlen) const;
Darin Petkov633ac6f2011-07-08 13:56:13 -070092
Paul Stewartf65320c2011-10-13 14:34:52 -070093 // fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK)
Wade Guthrie9f62cd12013-05-15 10:40:42 -070094 virtual int SetNonBlocking(int sockfd) const;
Paul Stewartf65320c2011-10-13 14:34:52 -070095
Julius Wernerf253c842012-12-20 14:52:30 -080096 // setsockopt(SO_RCVBUFFORCE)
Wade Guthrie9f62cd12013-05-15 10:40:42 -070097 virtual int SetReceiveBuffer(int sockfd, int size) const;
Julius Wernerf253c842012-12-20 14:52:30 -080098
Prathmesh Prabhu40daa012013-04-03 10:35:03 -070099 // shutdown
Wade Guthrie9f62cd12013-05-15 10:40:42 -0700100 virtual int ShutDown(int sockfd, int how) const;
Prathmesh Prabhu40daa012013-04-03 10:35:03 -0700101
Darin Petkov633ac6f2011-07-08 13:56:13 -0700102 // socket
Wade Guthrie9f62cd12013-05-15 10:40:42 -0700103 virtual int Socket(int domain, int type, int protocol) const;
Wade Guthriefa2100e2013-05-15 10:11:22 -0700104
105 private:
106 DISALLOW_COPY_AND_ASSIGN(Sockets);
Darin Petkov633ac6f2011-07-08 13:56:13 -0700107};
108
Darin Petkove0a312e2011-07-20 13:45:28 -0700109class ScopedSocketCloser {
110 public:
111 ScopedSocketCloser(Sockets *sockets, int fd);
112 ~ScopedSocketCloser();
113
114 private:
115 Sockets *sockets_;
116 int fd_;
117
118 DISALLOW_COPY_AND_ASSIGN(ScopedSocketCloser);
119};
120
Darin Petkov633ac6f2011-07-08 13:56:13 -0700121} // namespace shill
122
123#endif // SHILL_SOCKETS_H_