-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnet.cc
More file actions
158 lines (135 loc) · 4.36 KB
/
Copy pathnet.cc
File metadata and controls
158 lines (135 loc) · 4.36 KB
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
149
150
151
152
153
154
155
156
157
158
//------------------------------------------------------------//
// //
// Copyright (c) Texas A&M University //
// //
// CAN-to-Ethernet gateway service for Raspberry PI //
// //
// Authored by Justus Languell <justus@tamu.edu> //
// //
//------------------------------------------------------------//
#include "net.h"
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdexcept>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cerrno>
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
static bool SockAddrGetHostInfo(const sockaddr* sa, std::string& ip, uint16_t& port)
{
char buf[INET6_ADDRSTRLEN] = {0};
if (sa->sa_family == AF_INET)
{
const sockaddr_in* s = reinterpret_cast<const sockaddr_in*>(sa);
if (!::inet_ntop(AF_INET, &s->sin_addr, buf, sizeof(buf))) return false;
ip.assign(buf);
port = ntohs(s->sin_port);
return true;
}
else if (sa->sa_family == AF_INET6)
{
const sockaddr_in6* s6 = reinterpret_cast<const sockaddr_in6*>(sa);
if (!::inet_ntop(AF_INET6, &s6->sin6_addr, buf, sizeof(buf))) return false;
ip.assign(buf);
port = ntohs(s6->sin6_port);
return true;
}
return false;
}
NetConn::NetConn(int _fd) : fd(_fd)
{
if (fd < 0) return;
int one = 1;
::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
}
NetConn::~NetConn() { if (fd >= 0) ::close(fd); }
ssize_t NetConn::Recv(void* buf, size_t len)
{
for (;;)
{
ssize_t r = ::recv(fd, buf, len, 0);
if (r < 0 && errno == EINTR) continue;
return r;
}
}
ssize_t NetConn::Send(const void* buf, size_t len)
{
for (;;)
{
ssize_t w = ::send(fd, buf, len, MSG_NOSIGNAL);
if (w < 0 && errno == EINTR) continue;
return w;
}
}
void NetConn::ShutdownRD() { ::shutdown(fd, SHUT_RD); }
void NetConn::ShutdownWR() { ::shutdown(fd, SHUT_WR); }
void NetConn::ShutdownRW() { ::shutdown(fd, SHUT_RDWR); }
bool NetConn::PeerAddress(std::string& ip, uint16_t& port) const
{
sockaddr_storage ss; socklen_t slen = sizeof(ss);
if (::getpeername(fd, reinterpret_cast<sockaddr*>(&ss), &slen) < 0) return false;
return SockAddrGetHostInfo(reinterpret_cast<sockaddr*>(&ss), ip, port);
}
bool NetConn::LocalAddress(std::string& ip, uint16_t& port) const
{
sockaddr_storage ss; socklen_t slen = sizeof(ss);
if (::getsockname(fd, reinterpret_cast<sockaddr*>(&ss), &slen) < 0) return false;
return SockAddrGetHostInfo(reinterpret_cast<sockaddr*>(&ss), ip, port);
}
std::string NetConn::PeerAddress() const
{
std::string ip; uint16_t port{};
if (!PeerAddress(ip, port)) return {};
return ip + ":" + std::to_string(port);
}
std::string NetConn::LocalAddress() const
{
std::string ip; uint16_t port{};
if (!LocalAddress(ip, port)) return {};
return ip + ":" + std::to_string(port);
}
NetSocket::NetSocket(std::uint16_t port)
{
fd = ::socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
fprintf(stderr, "Error creating network socket socket\n");
}
int one = 1;
::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
#ifdef SO_REUSEPORT
::setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one));
#endif
::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(port);
if (::bind(fd, (sockaddr*)&addr, sizeof(addr)) < 0) {
fprintf(stderr, "Error binding to network socket\n");
::close(fd);
}
if (::listen(fd, 8) < 0) {
fprintf(stderr, "Error listening to network socket\n");
::close(fd);
}
}
NetSocket::~NetSocket()
{
if (fd >= 0) ::close(fd);
}
std::shared_ptr<NetConn> NetSocket::Accept()
{
sockaddr_in cli{};
socklen_t len = sizeof(cli);
int cfd = ::accept(fd, (sockaddr*)&cli, &len);
if (cfd < 0) {
// fprintf(stderr, "Error accepting to client connection\n");
return nullptr;
}
return std::make_shared<NetConn>(cfd);
}