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
|
#pragma once
#include <util/network/sock.h>
#include "http.h"
class TInet64StreamSocket: public TStreamSocket {
protected:
TInet64StreamSocket(const TInet64StreamSocket& parent, SOCKET fd)
: TStreamSocket(fd)
, AF(parent.AF)
{
}
public:
TInet64StreamSocket(int af = {}) {
CreateSocket(af);
}
std::shared_ptr<ISockAddr> MakeAddress(const TString& address, int port) {
if (!address) {
if (AF == AF_INET6) {
return std::make_shared<TSockAddrInet6>("::", port);
} else {
return std::make_shared<TSockAddrInet>(INADDR_ANY, port);
}
}
if (NHttp::IsIPv6(address)) {
return std::make_shared<TSockAddrInet6>(address.data(), port);
} else {
return std::make_shared<TSockAddrInet>(address.data(), port);
}
}
static std::shared_ptr<ISockAddr> MakeAddress(const sockaddr_storage& storage) {
std::shared_ptr<ISockAddr> addr;
switch (storage.ss_family) {
case AF_INET:
addr = std::make_shared<TSockAddrInet>();
break;
case AF_INET6:
addr = std::make_shared<TSockAddrInet6>();
break;
}
if (addr) {
memcpy(addr->SockAddr(), &storage, addr->Size());
}
return addr;
}
std::optional<TInet64StreamSocket> Accept(std::shared_ptr<ISockAddr>& acceptedAddr) {
sockaddr_storage addrStorage = {};
socklen_t addrLen = sizeof(addrStorage);
SOCKET s = accept((SOCKET)*this, reinterpret_cast<sockaddr*>(&addrStorage), &addrLen);
if (s == INVALID_SOCKET) {
return {};
}
acceptedAddr = MakeAddress(addrStorage);
return TInet64StreamSocket(*this, s);
}
protected:
int AF = 0;
void CreateSocket(int af) {
SOCKET s;
if (af == 0) {
s = socket(AF = AF_INET6, SOCK_STREAM, 0);
if (s < 0) {
s = socket(AF = AF_INET, SOCK_STREAM, 0);
}
} else {
s = socket(AF = af, SOCK_STREAM, 0);
}
if (AF == AF_INET6) {
SetSockOpt(s, SOL_SOCKET, IPV6_V6ONLY, (int)false);
}
TSocketHolder sock(s);
sock.Swap(*this);
}
};
|