forked from gene-hightower/ghsmtp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sock.hpp
148 lines (129 loc) · 4.14 KB
/
Sock.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
This file is part of ghsmtp - Gene's simple SMTP server.
Copyright (C) 2013 Gene Hightower <gene@digilicious.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOCK_DOT_HPP
#define SOCK_DOT_HPP
#include <iostream>
#include <string>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include "Logging.hpp"
#include "SockBuffer.hpp"
class Sock {
public:
Sock(const Sock&) = delete;
Sock& operator=(const Sock&) = delete;
Sock(int fd_in, int fd_out)
: iostream_(fd_in, fd_out)
, us_addr_len_(sizeof us_addr_)
, them_addr_len_(sizeof them_addr_)
, us_addr_str_{ '\0' }
, them_addr_str_{ '\0' }
{
// Get our local IP address as "us".
if (-1 != getsockname(fd_in, reinterpret_cast<struct sockaddr*>(&us_addr_),
&us_addr_len_)) {
switch (us_addr_len_) {
case sizeof(sockaddr_in) :
PCHECK(inet_ntop(AF_INET, &(reinterpret_cast<struct sockaddr_in*>(
&us_addr_)->sin_addr),
us_addr_str_, sizeof us_addr_str_) != nullptr);
break;
case sizeof(sockaddr_in6) :
PCHECK(inet_ntop(AF_INET6, &(reinterpret_cast<struct sockaddr_in6*>(
&us_addr_)->sin6_addr),
us_addr_str_, sizeof us_addr_str_) != nullptr);
break;
default:
LOG(ERROR) << "bogus address length (" << us_addr_len_
<< ") returned from getsockname";
}
} else {
CHECK_EQ(ENOTSOCK, errno); // only acceptable error from getsockname
}
// Get the remote IP address as "them".
if (-1 != getpeername(fd_out,
reinterpret_cast<struct sockaddr*>(&them_addr_),
&them_addr_len_)) {
switch (them_addr_len_) {
case sizeof(sockaddr_in) :
PCHECK(inet_ntop(AF_INET, &(reinterpret_cast<struct sockaddr_in*>(
&them_addr_)->sin_addr),
them_addr_str_, sizeof them_addr_str_) != nullptr);
break;
case sizeof(sockaddr_in6) :
PCHECK(inet_ntop(AF_INET6, &(reinterpret_cast<struct sockaddr_in6*>(
&them_addr_)->sin6_addr),
them_addr_str_, sizeof them_addr_str_) != nullptr);
break;
default:
LOG(ERROR) << "bogus address length (" << them_addr_len_
<< ") returned from getpeername";
}
} else {
// Ignore ENOTSOCK errors from getpeername, useful for testing.
PLOG_IF(WARNING, ENOTSOCK != errno) << "getpeername failed";
}
}
char const* us_c_str() const
{
return us_addr_str_;
}
char const* them_c_str() const
{
return them_addr_str_;
}
bool has_peername() const
{
return them_addr_str_[0] != '\0';
}
bool input_ready(std::chrono::milliseconds wait)
{
return iostream_->input_ready(wait);
}
bool timed_out()
{
return iostream_->timed_out();
}
std::istream& in()
{
return iostream_;
}
std::ostream& out()
{
return iostream_;
}
void starttls()
{
iostream_->starttls();
}
bool tls()
{
return iostream_->tls();
}
std::string tls_info()
{
return iostream_->tls_info();
}
private:
boost::iostreams::stream<SockBuffer> iostream_;
socklen_t us_addr_len_;
socklen_t them_addr_len_;
sockaddr_storage us_addr_;
sockaddr_storage them_addr_;
char us_addr_str_[INET6_ADDRSTRLEN];
char them_addr_str_[INET6_ADDRSTRLEN];
};
#endif // SOCK_DOT_HPP