PostgreSQL Source Code git master
ip.c File Reference
#include "postgres.h"
#include <unistd.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/file.h>
#include "common/ip.h"
Include dependency graph for ip.c:

Go to the source code of this file.

Functions

static int getaddrinfo_unix (const char *path, const struct addrinfo *hintsp, struct addrinfo **result)
 
static int getnameinfo_unix (const struct sockaddr_un *sa, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
 
int pg_getaddrinfo_all (const char *hostname, const char *servname, const struct addrinfo *hintp, struct addrinfo **result)
 
void pg_freeaddrinfo_all (int hint_ai_family, struct addrinfo *ai)
 
int pg_getnameinfo_all (const struct sockaddr_storage *addr, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
 

Function Documentation

◆ getaddrinfo_unix()

static int getaddrinfo_unix ( const char *  path,
const struct addrinfo *  hintsp,
struct addrinfo **  result 
)
static

Definition at line 156 of file ip.c.

158{
159 struct addrinfo hints = {0};
160 struct addrinfo *aip;
161 struct sockaddr_un *unp;
162
163 *result = NULL;
164
165 if (strlen(path) >= sizeof(unp->sun_path))
166 return EAI_FAIL;
167
168 if (hintsp == NULL)
169 {
170 hints.ai_family = AF_UNIX;
171 hints.ai_socktype = SOCK_STREAM;
172 }
173 else
174 memcpy(&hints, hintsp, sizeof(hints));
175
176 if (hints.ai_socktype == 0)
177 hints.ai_socktype = SOCK_STREAM;
178
179 if (hints.ai_family != AF_UNIX)
180 {
181 /* shouldn't have been called */
182 return EAI_FAIL;
183 }
184
185 aip = calloc(1, sizeof(struct addrinfo));
186 if (aip == NULL)
187 return EAI_MEMORY;
188
189 unp = calloc(1, sizeof(struct sockaddr_un));
190 if (unp == NULL)
191 {
192 free(aip);
193 return EAI_MEMORY;
194 }
195
196 aip->ai_family = AF_UNIX;
197 aip->ai_socktype = hints.ai_socktype;
198 aip->ai_protocol = hints.ai_protocol;
199 aip->ai_next = NULL;
200 aip->ai_canonname = NULL;
201 *result = aip;
202
203 unp->sun_family = AF_UNIX;
204 aip->ai_addr = (struct sockaddr *) unp;
205 aip->ai_addrlen = sizeof(struct sockaddr_un);
206
207 strcpy(unp->sun_path, path);
208
209 /*
210 * If the supplied path starts with @, replace that with a zero byte for
211 * the internal representation. In that mode, the entire sun_path is the
212 * address, including trailing zero bytes. But we set the address length
213 * to only include the length of the original string. That way the
214 * trailing zero bytes won't show up in any network or socket lists of the
215 * operating system. This is just a convention, also followed by other
216 * packages.
217 */
218 if (path[0] == '@')
219 {
220 unp->sun_path[0] = '\0';
221 aip->ai_addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(path);
222 }
223
224 return 0;
225}
#define calloc(a, b)
Definition: header.h:55
#define free(a)
Definition: header.h:65
Definition: un.h:12
unsigned short sun_family
Definition: un.h:13
char sun_path[108]
Definition: un.h:14

References calloc, free, sockaddr_un::sun_family, and sockaddr_un::sun_path.

Referenced by pg_getaddrinfo_all().

◆ getnameinfo_unix()

static int getnameinfo_unix ( const struct sockaddr_un sa,
int  salen,
char *  node,
int  nodelen,
char *  service,
int  servicelen,
int  flags 
)
static

Definition at line 231 of file ip.c.

235{
236 int ret;
237
238 /* Invalid arguments. */
239 if (sa == NULL || sa->sun_family != AF_UNIX ||
240 (node == NULL && service == NULL))
241 return EAI_FAIL;
242
243 if (node)
244 {
245 ret = snprintf(node, nodelen, "%s", "[local]");
246 if (ret < 0 || ret >= nodelen)
247 return EAI_MEMORY;
248 }
249
250 if (service)
251 {
252 /*
253 * Check whether it looks like an abstract socket, but it could also
254 * just be an empty string.
255 */
256 if (sa->sun_path[0] == '\0' && sa->sun_path[1] != '\0')
257 ret = snprintf(service, servicelen, "@%s", sa->sun_path + 1);
258 else
259 ret = snprintf(service, servicelen, "%s", sa->sun_path);
260 if (ret < 0 || ret >= servicelen)
261 return EAI_MEMORY;
262 }
263
264 return 0;
265}
#define snprintf
Definition: port.h:260

References snprintf.

Referenced by pg_getnameinfo_all().

◆ pg_freeaddrinfo_all()

void pg_freeaddrinfo_all ( int  hint_ai_family,
struct addrinfo *  ai 
)

Definition at line 85 of file ip.c.

86{
87 if (hint_ai_family == AF_UNIX)
88 {
89 /* struct was built by getaddrinfo_unix (see pg_getaddrinfo_all) */
90 while (ai != NULL)
91 {
92 struct addrinfo *p = ai;
93
94 ai = ai->ai_next;
95 free(p->ai_addr);
96 free(p);
97 }
98 }
99 else
100 {
101 /* struct was built by getaddrinfo() */
102 if (ai != NULL)
103 freeaddrinfo(ai);
104 }
105}

References free.

Referenced by ident_inet(), ListenServerPort(), parse_hba_auth_opt(), parse_hba_line(), PerformRadiusTransaction(), and PQconnectPoll().

◆ pg_getaddrinfo_all()

int pg_getaddrinfo_all ( const char *  hostname,
const char *  servname,
const struct addrinfo *  hintp,
struct addrinfo **  result 
)

Definition at line 56 of file ip.c.

58{
59 int rc;
60
61 /* not all versions of getaddrinfo() zero *result on failure */
62 *result = NULL;
63
64 if (hintp->ai_family == AF_UNIX)
65 return getaddrinfo_unix(servname, hintp, result);
66
67 /* NULL has special meaning to getaddrinfo(). */
68 rc = getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname,
69 servname, hintp, result);
70
71 return rc;
72}
static int getaddrinfo_unix(const char *path, const struct addrinfo *hintsp, struct addrinfo **result)
Definition: ip.c:156
static char * hostname
Definition: pg_regress.c:114

References getaddrinfo_unix(), and hostname.

Referenced by ident_inet(), ListenServerPort(), parse_hba_auth_opt(), parse_hba_line(), PerformRadiusTransaction(), and PQconnectPoll().

◆ pg_getnameinfo_all()

int pg_getnameinfo_all ( const struct sockaddr_storage *  addr,
int  salen,
char *  node,
int  nodelen,
char *  service,
int  servicelen,
int  flags 
)

Definition at line 117 of file ip.c.

121{
122 int rc;
123
124 if (addr && addr->ss_family == AF_UNIX)
125 rc = getnameinfo_unix((const struct sockaddr_un *) addr, salen,
126 node, nodelen,
127 service, servicelen,
128 flags);
129 else
130 rc = getnameinfo((const struct sockaddr *) addr, salen,
131 node, nodelen,
132 service, servicelen,
133 flags);
134
135 if (rc != 0)
136 {
137 if (node)
138 strlcpy(node, "???", nodelen);
139 if (service)
140 strlcpy(service, "???", servicelen);
141 }
142
143 return rc;
144}
static int getnameinfo_unix(const struct sockaddr_un *sa, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: ip.c:231
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45

References getnameinfo_unix(), and strlcpy().

Referenced by BackendInitialize(), check_hostname(), ClientAuthentication(), emitHostIdentityInfo(), fill_hba_line(), ident_inet(), inet_client_addr(), inet_client_port(), inet_server_addr(), inet_server_port(), ListenServerPort(), log_status_format(), pg_stat_get_activity(), pg_stat_get_backend_client_addr(), and pg_stat_get_backend_client_port().