blob: e0cc70bd02209f79961169890ae6109a18dd78d2 [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
5#ifndef SHILL_SOCKETS_H_
6#define SHILL_SOCKETS_H_
7
Paul Stewartf65320c2011-10-13 14:34:52 -07008#include <string>
9
Darin Petkovef34f182011-08-26 14:14:40 -070010#include <sys/socket.h>
Darin Petkov633ac6f2011-07-08 13:56:13 -070011#include <sys/types.h>
12
Darin Petkove0a312e2011-07-20 13:45:28 -070013#include <base/basictypes.h>
14
Darin Petkov633ac6f2011-07-08 13:56:13 -070015namespace shill {
16
17// A "sys/socket.h" abstraction allowing mocking in tests.
18class Sockets {
19 public:
20 virtual ~Sockets();
21
Paul Stewartf65320c2011-10-13 14:34:52 -070022 // accept
23 virtual int Accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
24
Darin Petkov633ac6f2011-07-08 13:56:13 -070025 // bind
26 virtual int Bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
27
Paul Stewartf65320c2011-10-13 14:34:52 -070028 // setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE ...)
29 virtual int BindToDevice(int sockfd, const std::string &device);
30
Darin Petkov633ac6f2011-07-08 13:56:13 -070031 // close
32 virtual int Close(int fd);
33
Paul Stewartf65320c2011-10-13 14:34:52 -070034 // connect
35 virtual int Connect(int sockfd, const struct sockaddr *addr,
36 socklen_t addrlen);
37
38 // errno
39 virtual int Error();
40
41 // errno
42 virtual std::string ErrorString();
43
44 // getsockname
45 virtual int GetSockName(int sockfd, struct sockaddr *addr,
46 socklen_t *addrlen);
47
48 // getsockopt(sockfd, SOL_SOCKET, SO_ERROR, ...)
49 virtual int GetSocketError(int sockfd);
50
Darin Petkove0a312e2011-07-20 13:45:28 -070051 // ioctl
52 virtual int Ioctl(int d, int request, void *argp);
53
Paul Stewartf65320c2011-10-13 14:34:52 -070054 // listen
55 virtual int Listen(int sockfd, int backlog);
56
Darin Petkov633ac6f2011-07-08 13:56:13 -070057 // send
58 virtual ssize_t Send(int sockfd, const void *buf, size_t len, int flags);
59
60 // sendto
61 virtual ssize_t SendTo(int sockfd, const void *buf, size_t len, int flags,
62 const struct sockaddr *dest_addr, socklen_t addrlen);
63
Paul Stewartf65320c2011-10-13 14:34:52 -070064 // fcntl(sk, F_SETFL, fcntl(sk, F_GETFL) | O_NONBLOCK)
65 virtual int SetNonBlocking(int sockfd);
66
Darin Petkov633ac6f2011-07-08 13:56:13 -070067 // socket
68 virtual int Socket(int domain, int type, int protocol);
69};
70
Darin Petkove0a312e2011-07-20 13:45:28 -070071class ScopedSocketCloser {
72 public:
73 ScopedSocketCloser(Sockets *sockets, int fd);
74 ~ScopedSocketCloser();
75
76 private:
77 Sockets *sockets_;
78 int fd_;
79
80 DISALLOW_COPY_AND_ASSIGN(ScopedSocketCloser);
81};
82
Darin Petkov633ac6f2011-07-08 13:56:13 -070083} // namespace shill
84
85#endif // SHILL_SOCKETS_H_