-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathSocketAddr.cpp
More file actions
executable file
·140 lines (117 loc) · 3.35 KB
/
Copy pathSocketAddr.cpp
File metadata and controls
executable file
·140 lines (117 loc) · 3.35 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
/*
Copyright © 2017-2020, orcaer@yeah.net All rights reserved.
Author: orcaer@yeah.net
Last modified: 2019-8-5
Description: https://github.com/wlgq2/uv-cpp
*/
#include "include/SocketAddr.hpp"
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include <cstdlib>
using namespace uv;
uv::SocketAddr::SocketAddr(const std::string&& ip, unsigned short port, IPV ipv)
:ip_(ip),
port_(port),
ipv_(ipv)
{
if (ipv == Ipv6)
{
::uv_ip6_addr(ip.c_str(), port, &ipv6_);
}
else
{
::uv_ip4_addr(ip.c_str(), port, &ipv4_);
}
}
uv::SocketAddr::SocketAddr(const std::string& ip, unsigned short port, IPV ipv)
:SocketAddr(std::move(ip), port, ipv)
{
}
uv::SocketAddr::SocketAddr(const sockaddr* addr, IPV ipv)
:ipv_(ipv)
{
if (ipv_ == Ipv4)
{
ipv4_ = *(reinterpret_cast<const sockaddr_in*>(addr));
}
else
{
ipv6_ = *(reinterpret_cast<const sockaddr_in6*>(addr));
}
port_ = GetIpAndPort((const sockaddr_storage *)(addr), ip_, ipv);
}
const sockaddr * uv::SocketAddr::Addr()
{
return (ipv_ == Ipv6) ? reinterpret_cast<const sockaddr*>(&ipv6_) : reinterpret_cast<const sockaddr*>(&ipv4_);
}
void uv::SocketAddr::toStr(std::string & str)
{
str = ip_ + ":" + std::to_string(port_);
}
std::string uv::SocketAddr::toStr()
{
std::string str = ip_ + ":" + std::to_string(port_);
return str;
}
SocketAddr::IPV uv::SocketAddr::Ipv()
{
return ipv_;
}
void uv::SocketAddr::AddrToStr(uv_tcp_t* client, std::string& addrStr, IPV ipv)
{
struct sockaddr_storage addr;
int len = sizeof(struct sockaddr_storage);
::uv_tcp_getpeername(client, (struct sockaddr *)&addr, &len);
uint16_t port = GetIpAndPort(&addr, addrStr, ipv);
addrStr += ":" + std::to_string(port);
}
uint16_t uv::SocketAddr::GetIpAndPort(const sockaddr_storage* addr, std::string& out, IPV ipv)
{
auto inet = (Ipv6 == ipv) ? AF_INET6 : AF_INET;
if (Ipv6 == ipv)
{
char ip[64];
struct sockaddr_in6* addr6 = (struct sockaddr_in6 *)addr;
//低版本windows可能找不到inet_ntop函数。
#if _MSC_VER
DWORD size = sizeof(ip);
WSAAddressToString((LPSOCKADDR)addr6, sizeof(sockaddr_in6), NULL, ip, &size);
out = std::string(ip);
auto index = out.rfind(":");
if (index >= 0)
{
out.resize(index);
}
return (htons(addr6->sin6_port));
#else
std::string str(inet_ntop(inet, (void *)&(addr6->sin6_addr), ip, 64));
out.swap(str);
return(htons(addr6->sin6_port));
#endif
}
else
{
struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
std::string str(inet_ntoa(addr4->sin_addr));
out.swap(str);
return htons(addr4->sin_port);
}
}
std::pair<std::string, int> uv::SocketAddr::ExtractIpAndPort(const std::string& str)
{
size_t delim_pos = str.find(':');
if (delim_pos == std::string::npos) {
throw std::invalid_argument("Invalid format: missing ':' delimiter.");
}
std::string ip = str.substr(0, delim_pos);
std::string port_str = str.substr(delim_pos + 1);
try {
int port = std::stoi(port_str);
return std::make_pair(ip, port);
} catch (const std::exception& e) {
throw std::invalid_argument("Invalid port: cannot convert to an integer.");
}
}