Document of c-modernization-kit (porter) 1.0.0
Loading...
Searching...
No Matches
potrIpAddr.c
Go to the documentation of this file.
1
13
14#include <stddef.h>
15
16#ifndef _WIN32
17 #include <netdb.h>
18 #include <string.h>
19#else /* _WIN32 */
20 #include <ws2tcpip.h>
21#endif /* _WIN32 */
22
23#include <porter_const.h>
24
25#include "potrIpAddr.h"
26
27/* IPv4 文字列をネットワークバイトオーダーへ変換する。 */
28int parse_ipv4_addr(const char *ip_str, struct in_addr *out_addr)
29{
30 if (ip_str == NULL || out_addr == NULL)
31 {
32 return POTR_ERROR;
33 }
34
35 if (inet_pton(AF_INET, ip_str, out_addr) == 1)
36 {
37 return POTR_SUCCESS;
38 }
39 else
40 {
41 return POTR_ERROR;
42 }
43}
44
45/* ホスト名または IPv4 アドレス文字列を struct in_addr に解決する。 */
46int resolve_ipv4_addr(const char *host, struct in_addr *out_addr)
47{
48 struct addrinfo hints;
49 struct addrinfo *res = NULL;
50 int ret;
51
52 if (host == NULL || out_addr == NULL)
53 {
54 return POTR_ERROR;
55 }
56
57 memset(&hints, 0, sizeof(hints));
58 hints.ai_family = AF_INET;
59 hints.ai_socktype = SOCK_DGRAM;
60
61 ret = getaddrinfo(host, NULL, &hints, &res);
62 if (ret != 0 || res == NULL)
63 {
64 return POTR_ERROR;
65 }
66
67 /* 複数アドレスが返された場合は先頭を採用する */
68 *out_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
69
70 freeaddrinfo(res);
71 return POTR_SUCCESS;
72}
#define POTR_SUCCESS
成功の戻り値を表す定数。
#define POTR_ERROR
失敗の戻り値を表す定数。
通信ライブラリの定数ファイル。
int parse_ipv4_addr(const char *ip_str, struct in_addr *out_addr)
Definition potrIpAddr.c:28
int resolve_ipv4_addr(const char *host, struct in_addr *out_addr)
ホスト名または IPv4 アドレス文字列を struct in_addr に解決する。
Definition potrIpAddr.c:46
IPv4 アドレス変換ユーティリティ (内部用)。