MCPcopy Create free account
hub / github.com/Tencent/phxsql / SockAddrToIPPort

Function SockAddrToIPPort

phxsqlproxy/phxsqlproxyutil.cpp:151–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

149}
150
151int SockAddrToIPPort(struct sockaddr * addr, std::string & ip, int & port) {
152 if (addr->sa_family == AF_INET) {
153 struct sockaddr_in * addr_in = (struct sockaddr_in *) addr;
154 char buf[INET_ADDRSTRLEN] = { 0 };
155 if (inet_ntop(AF_INET, &addr_in->sin_addr, buf, sizeof(buf)) == NULL) {
156 LogError("inet_ntop failed, ret NULL, errno(%d:%s)", errno, strerror(errno));
157 return -__LINE__;
158 }
159 ip = string(buf);
160 port = ntohs(addr_in->sin_port);
161 } else if (addr->sa_family == AF_INET6) {
162 struct sockaddr_in6 * addr_in6 = (struct sockaddr_in6 *) addr;
163 char buf[INET6_ADDRSTRLEN] = { 0 };
164 if (inet_ntop(AF_INET6, &addr_in6->sin6_addr, buf, sizeof(buf)) == NULL) {
165 LogError("inet_ntop failed, ret NULL, errno(%d:%s)", errno, strerror(errno));
166 return -__LINE__;
167 }
168 ip = string(buf);
169 port = ntohs(addr_in6->sin6_port);
170 } else {
171 LogError("unknow sa_family %d", addr->sa_family);
172 return -__LINE__;
173 }
174 return 0;
175}
176
177int GetSockName(int fd, std::string & ip, int & port) {
178 struct sockaddr_storage addr_storage;

Callers 4

GetSockNameFunction · 0.85
GetPeerNameFunction · 0.85
IsProxyHeaderNeedMethod · 0.85
MakeProxyHeaderV1Method · 0.85

Calls 1

LogErrorFunction · 0.85

Tested by

no test coverage detected