BitLab 0.1.0
BitLab: A Browser for the Bitcoin P2P Network and Blockchain
Loading...
Searching...
No Matches
ip.h File Reference
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>

Go to the source code of this file.

Data Structures

struct  addrinfo
 

Macros

#define MAX_IP_ADDR_LEN   1024
 

Functions

void get_local_ip_address (char *ip_addr)
 Get the local IP address of the machine (e.g.
 
void get_remote_ip_address (char *ip_addr)
 Get the remote IP address of the machine (e.g.
 
int lookup_address (const char *lookup_addr, char *ip_addr)
 Perform lookup of given IP address by the domain address (e.g.
 
int is_in_private_network (const char *ip_addr)
 Check if the IP address is in the private prefix (e.g.
 
int is_numeric_address (const char *ip_addr)
 Check if the IP address is a numeric address (e.g.
 
int is_valid_domain_address (const char *domain_addr)
 Check if the IP address is a valid domain address (e.g.
 
int getaddrinfo (const char *restrict node, const char *restrict service, const struct addrinfo *restrict hints, struct addrinfo **restrict res)
 
void freeaddrinfo (struct addrinfo *res)
 
const char * gai_strerror (int errcode)
 

Macro Definition Documentation

◆ MAX_IP_ADDR_LEN

#define MAX_IP_ADDR_LEN   1024

Definition at line 8 of file ip.h.

Function Documentation

◆ freeaddrinfo()

void freeaddrinfo ( struct addrinfo res)

Referenced by lookup_address().

◆ gai_strerror()

const char * gai_strerror ( int  errcode)

Referenced by lookup_address().

◆ get_local_ip_address()

void get_local_ip_address ( char *  ip_addr)

Get the local IP address of the machine (e.g.

192.168.1.10).

Parameters
ip_addrThe buffer to store the IP address.

Definition at line 11 of file ip.c.

12{
13 FILE* file = popen("hostname -I", "r");
14 if (file == NULL)
15 {
16 strcpy(ip_addr, " ");
17 return;
18 }
19 char buffer[BUFFER_SIZE];
20 if (fgets(buffer, sizeof(buffer), file) == NULL)
21 {
22 strcpy(ip_addr, " ");
23 pclose(file);
24 return;
25 }
26 pclose(file);
27 char* newline = strchr(buffer, '\n');
28 if (newline != NULL)
29 *newline = '\0';
30 strcpy(ip_addr, buffer);
31}
FILE * popen(const char *command, const char *type)
#define BUFFER_SIZE
Definition utils.h:12
int pclose(FILE *stream)

References BUFFER_SIZE, pclose(), and popen().

Referenced by cli_whoami().

◆ get_remote_ip_address()

void get_remote_ip_address ( char *  ip_addr)

Get the remote IP address of the machine (e.g.

1.1.1.1).

Parameters
ip_addrThe buffer to store the IP address.

Definition at line 33 of file ip.c.

34{
35 FILE* file = popen("curl -s ifconfig.me", "r");
36 if (file == NULL)
37 {
38 strcpy(ip_addr, " ");
39 return;
40 }
41 char buffer[BUFFER_SIZE];
42 if (fgets(buffer, sizeof(buffer), file) == NULL)
43 {
44 strcpy(ip_addr, " ");
45 pclose(file);
46 return;
47 }
48 pclose(file);
49 char* newline = strchr(buffer, '\n');
50 if (newline != NULL)
51 *newline = '\0';
52 strcpy(ip_addr, buffer);
53}

References BUFFER_SIZE, pclose(), and popen().

Referenced by cli_get_ip(), and cli_whoami().

◆ getaddrinfo()

int getaddrinfo ( const char *restrict  node,
const char *restrict  service,
const struct addrinfo *restrict  hints,
struct addrinfo **restrict  res 
)

Referenced by lookup_address().

◆ is_in_private_network()

int is_in_private_network ( const char *  ip_addr)

Check if the IP address is in the private prefix (e.g.

192.168.1.10 is, 1.1.1.1 is not).

Parameters
ip_addrThe IP address to check.
Returns
1 if the IP address is in the private network, 0 otherwise.

Definition at line 98 of file ip.c.

99{
100 if (!is_numeric_address(ip_addr))
101 {
102 char ip[BUFFER_SIZE];
103 log_message(LOG_WARN, BITLAB_LOG, __FILE__, "IP address is not numeric, performing lookup of %s whether it is in private prefix", ip_addr);
104 lookup_address(ip_addr, ip);
105 return is_in_private_network(ip);
106 }
107 unsigned int b1, b2, b3, b4;
108 if (ip_addr == NULL)
109 return 0;
110 if (sscanf(ip_addr, "%u.%u.%u.%u", &b1, &b2, &b3, &b4) != 4)
111 return 0;
112 if ((b1 == 192 && b2 == 168) ||
113 (b1 == 10) ||
114 (b1 == 172 && b2 >= 16 && b2 <= 31))
115 return 1;
116 return 0;
117}
int lookup_address(const char *lookup_addr, char *ip_addr)
Perform lookup of given IP address by the domain address (e.g.
Definition ip.c:55
int is_numeric_address(const char *ip_addr)
Check if the IP address is a numeric address (e.g.
Definition ip.c:119
int is_in_private_network(const char *ip_addr)
Check if the IP address is in the private prefix (e.g.
Definition ip.c:98
void log_message(log_level level, const char *filename, const char *source_file, const char *format,...)
Log a message used to log a message to the console or a file.
Definition log.c:89
#define BITLAB_LOG
Definition log.h:11
@ LOG_WARN
Definition log.h:31

References BITLAB_LOG, BUFFER_SIZE, is_in_private_network(), is_numeric_address(), log_message(), LOG_WARN, and lookup_address().

Referenced by is_in_private_network(), and send_getaddr_and_wait().

◆ is_numeric_address()

int is_numeric_address ( const char *  ip_addr)

Check if the IP address is a numeric address (e.g.

1.1.1.1 is, example.com is not).

Parameters
ip_addrThe IP address to check.
Returns
1 if the IP address is a numeric address, 0 otherwise.

Definition at line 119 of file ip.c.

120{
121 if (ip_addr == NULL)
122 return 0;
123 int dots = 0;
124 for (size_t i = 0; i < strlen(ip_addr); ++i)
125 {
126 if (ip_addr[i] == '.')
127 dots++;
128 else if (ip_addr[i] < '0' || ip_addr[i] > '9')
129 return 0;
130 if (dots > 3)
131 return 0;
132 }
133 return 1;
134}

Referenced by cli_connect(), is_in_private_network(), and is_valid_domain_address().

◆ is_valid_domain_address()

int is_valid_domain_address ( const char *  domain_addr)

Check if the IP address is a valid domain address (e.g.

example.com is, example or 1.1.1.1 is not).

Parameters
domain_addrThe domain address to check.
Returns
1 if the IP address is a valid domain address, 0 otherwise.

Definition at line 136 of file ip.c.

137{
138 if (domain_addr == NULL)
139 return 0;
140 if (is_numeric_address(domain_addr))
141 return 0;
142 if (strstr(domain_addr, ".") == NULL)
143 return 0;
144 char ip[BUFFER_SIZE];
145 lookup_address(domain_addr, ip);
146 if (strlen(ip) == 0)
147 log_message(LOG_WARN, BITLAB_LOG, __FILE__, "Although %s is a valid domain, it does not resolve", domain_addr);
148 return 1;
149}

References BITLAB_LOG, BUFFER_SIZE, is_numeric_address(), log_message(), LOG_WARN, and lookup_address().

Referenced by cli_get_ip().

◆ lookup_address()

int lookup_address ( const char *  lookup_addr,
char *  ip_addr 
)

Perform lookup of given IP address by the domain address (e.g.

google.com).

Parameters
lookup_addrThe current address to lookup.
ip_addrThe buffer to store the IP address.
Returns
0 if successful, otherwise -1 or 1.

Definition at line 55 of file ip.c.

56{
57 struct addrinfo hints, * res, * p;
58 int status;
59 char ipstr[INET6_ADDRSTRLEN];
60
61 memset(ip_addr, 0, MAX_IP_ADDR_LEN);
62 memset(&hints, 0, sizeof(hints));
63 hints.ai_family = AF_INET;
64 hints.ai_socktype = SOCK_STREAM;
65
66 if ((status = getaddrinfo(lookup_addr, NULL, &hints, &res)) != 0)
67 {
68 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
69 ip_addr[0] = '\0';
70 return -1;
71 }
72
73 for (p = res; p != NULL; p = p->ai_next)
74 {
75 void* addr = NULL;
76 if (p->ai_family == AF_INET)
77 {
78 struct sockaddr_in* ipv4 = (struct sockaddr_in*)p->ai_addr;
79 addr = &(ipv4->sin_addr);
80 if (addr != NULL)
81 {
82 if (strcmp(inet_ntoa(ipv4->sin_addr), "0.0.0.0") == 0)
83 log_message(LOG_WARN, BITLAB_LOG, __FILE__, "Failed to resolve %s. Appending 0.0.0.0", lookup_addr);
84 inet_ntop(p->ai_family, addr, ipstr, sizeof(ipstr));
85 strcat(ip_addr, ipstr);
86 strcat(ip_addr, " ");
87 }
88 }
89 }
90
91 if (strlen(ip_addr) > 0)
92 ip_addr[strlen(ip_addr) - 1] = '\0';
93
94 freeaddrinfo(res);
95 return 0;
96}
void freeaddrinfo(struct addrinfo *res)
const char * gai_strerror(int errcode)
int getaddrinfo(const char *restrict node, const char *restrict service, const struct addrinfo *restrict hints, struct addrinfo **restrict res)
#define MAX_IP_ADDR_LEN
Definition ip.h:8
Definition ip.h:61
struct sockaddr * ai_addr
Definition ip.h:67
struct addrinfo * ai_next
Definition ip.h:69
int ai_family
Definition ip.h:63

References addrinfo::ai_addr, addrinfo::ai_family, addrinfo::ai_next, addrinfo::ai_socktype, BITLAB_LOG, freeaddrinfo(), gai_strerror(), getaddrinfo(), log_message(), LOG_WARN, and MAX_IP_ADDR_LEN.

Referenced by cli_get_ip(), handle_peer_discovery(), is_in_private_network(), and is_valid_domain_address().