-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlink_addr.cpp
47 lines (41 loc) · 1.13 KB
/
link_addr.cpp
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
/*
Copyright (c) 2012-2014 The SSDB Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
*/
#include <string.h>
#include <stdio.h>
#include "link_addr.h"
namespace redis {
LinkAddr::LinkAddr(const char* ip, int port) {
parse(ip, port);
}
LinkAddr::LinkAddr(bool is_ipv4) {
this->is_ipv4(is_ipv4);
}
void LinkAddr::is_ipv4(bool yn) {
if (yn) {
ipv4 = true;
family = AF_INET;
addrlen = sizeof(addr4);
} else {
ipv4 = false;
family = AF_INET6;
addrlen = sizeof(addr6);
}
}
void LinkAddr::parse(const char* ip, int port) {
this->is_ipv4(strchr(ip, ':') == NULL);
if (ipv4) {
bzero(&addr4, sizeof(addr4));
addr4.sin_family = family;
addr4.sin_port = htons((short)port);
inet_pton(family, ip, &addr4.sin_addr);
} else {
bzero(&addr6, sizeof(addr6));
addr6.sin6_family = family;
addr6.sin6_port = htons((short)port);
inet_pton(family, ip, &addr6.sin6_addr);
}
}
}; // namespace redis