mirror of
https://github.com/samhocevar/rinetd.git
synced 2025-03-22 15:50:08 +08:00
Refactor getaddrinfo error reporting.
This commit is contained in:
parent
e3b47d086a
commit
1060048fe8
15
src/net.c
15
src/net.c
@ -10,6 +10,7 @@
|
|||||||
# include <config.h>
|
# include <config.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
|
||||||
void setSocketDefaults(SOCKET fd) {
|
void setSocketDefaults(SOCKET fd) {
|
||||||
@ -32,13 +33,23 @@ void setSocketDefaults(SOCKET fd) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
struct addrinfo getAddrInfoHint(int protocol) {
|
int getAddrInfoWithProto(char *address, char *port, int protocol, struct addrinfo **ai)
|
||||||
return (struct addrinfo) {
|
{
|
||||||
|
struct addrinfo hints = {
|
||||||
.ai_family = AF_UNSPEC,
|
.ai_family = AF_UNSPEC,
|
||||||
.ai_protocol = protocol,
|
.ai_protocol = protocol,
|
||||||
.ai_socktype = protocol == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
|
.ai_socktype = protocol == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
|
||||||
.ai_flags = AI_PASSIVE,
|
.ai_flags = AI_PASSIVE,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int ret = getaddrinfo(address, port, &hints, ai);
|
||||||
|
if (ret != 0) {
|
||||||
|
fprintf(stderr, "rinetd: cannot resolve host \"%s\" port %s "
|
||||||
|
"(getaddrinfo() error: %s)\n",
|
||||||
|
address, port ? port : "<null>", gai_strerror(ret));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sameSocketAddress(struct sockaddr_storage *a, struct sockaddr_storage *b) {
|
int sameSocketAddress(struct sockaddr_storage *a, struct sockaddr_storage *b) {
|
||||||
|
@ -85,6 +85,6 @@ static inline int GetLastError(void) {
|
|||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
void setSocketDefaults(SOCKET fd);
|
void setSocketDefaults(SOCKET fd);
|
||||||
struct addrinfo getAddrInfoHint(int protocol);
|
|
||||||
int sameSocketAddress(struct sockaddr_storage *a, struct sockaddr_storage *b);
|
int sameSocketAddress(struct sockaddr_storage *a, struct sockaddr_storage *b);
|
||||||
|
int getAddrInfoWithProto(char *address, char *port, int protocol, struct addrinfo **ai);
|
||||||
uint16_t getPort(struct addrinfo* ai);
|
uint16_t getPort(struct addrinfo* ai);
|
||||||
|
13
src/rinetd.c
13
src/rinetd.c
@ -267,10 +267,9 @@ void addServer(char *bindAddress, char *bindPort, int bindProtocol,
|
|||||||
};
|
};
|
||||||
|
|
||||||
/* Make a server socket */
|
/* Make a server socket */
|
||||||
struct addrinfo hints = getAddrInfoHint(bindProtocol), *ai;
|
struct addrinfo *ai;
|
||||||
int ret = getaddrinfo(bindAddress, bindPort, &hints, &ai);
|
int ret = getAddrInfoWithProto(bindAddress, bindPort, bindProtocol, &ai);
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
fprintf(stderr, "rinetd: getaddrinfo error: %s\n", gai_strerror(ret));
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,10 +307,8 @@ void addServer(char *bindAddress, char *bindPort, int bindProtocol,
|
|||||||
si.fromAddrInfo = ai;
|
si.fromAddrInfo = ai;
|
||||||
|
|
||||||
/* Resolve destination address. */
|
/* Resolve destination address. */
|
||||||
hints = getAddrInfoHint(connectProtocol);
|
ret = getAddrInfoWithProto(connectAddress, connectPort, connectProtocol, &ai);
|
||||||
ret = getaddrinfo(connectAddress, connectPort, &hints, &ai);
|
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
fprintf(stderr, "rinetd: getaddrinfo error: %s\n", gai_strerror(ret));
|
|
||||||
freeaddrinfo(si.fromAddrInfo);
|
freeaddrinfo(si.fromAddrInfo);
|
||||||
closesocket(si.fd);
|
closesocket(si.fd);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -320,10 +317,8 @@ void addServer(char *bindAddress, char *bindPort, int bindProtocol,
|
|||||||
|
|
||||||
/* Resolve source address if applicable. */
|
/* Resolve source address if applicable. */
|
||||||
if (sourceAddress) {
|
if (sourceAddress) {
|
||||||
hints = getAddrInfoHint(connectProtocol);
|
ret = getAddrInfoWithProto(sourceAddress, NULL, connectProtocol, &ai);
|
||||||
ret = getaddrinfo(sourceAddress, NULL, &hints, &ai);
|
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
fprintf(stderr, "rinetd: getaddrinfo error: %s\n", gai_strerror(ret));
|
|
||||||
freeaddrinfo(si.fromAddrInfo);
|
freeaddrinfo(si.fromAddrInfo);
|
||||||
freeaddrinfo(si.toAddrInfo);
|
freeaddrinfo(si.toAddrInfo);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
Loading…
Reference in New Issue
Block a user