BitLab 0.1.0
BitLab: A Browser for the Bitcoin P2P Network and Blockchain
Loading...
Searching...
No Matches
peer_discovery.c
Go to the documentation of this file.
1#include "peer_discovery.h"
2
3#include <string.h>
4#include <stdint.h>
5#include <netinet/in.h>
6#include <pthread.h>
7
8#include "peer_queue.h"
9#include "state.h"
10#include "utils.h"
11#include "cli.h"
12#include "ip.h"
13
14static const char* dns_seeds[] =
15{
16 "seed.bitcoin.sipa.be.",
17 "seed.btc.petertodd.org.",
18 "dnsseed.emzy.de.",
19 NULL
20};
21
22static const char* hardcoded_peers[] =
23{
24 "23.84.108.213:8333",
25 "87.207.45.218:8333",
26 "35.207.115.204:8333",
27 "65.108.202.25:8333",
28 NULL
29};
30
31void* handle_peer_discovery(void* arg)
32{
33 usleep(1000000); // 1 s
34 while (!get_exit_flag())
35 {
37 {
39 int peer_count = 0;
40 bool search = false;
41
43 {
44 // query hardcoded seeds
45 search = true;
46 for (int i = 0; hardcoded_peers[i] != NULL; ++i)
47 {
49 guarded_print_line("Added hardcoded peer: %s", hardcoded_peers[i]);
50 peer_count++;
51 }
52 }
54 {
55 // query DNS seeds
56 search = true;
57
59 {
60 for (int i = 0; dns_seeds[i] != NULL; ++i)
61 {
62 char ip_address[BUFFER_SIZE];
63 lookup_address(dns_seeds[i], ip_address);
64 char* token = strtok(ip_address, " ");
65 while (token != NULL && *token != '\0')
66 {
67 if (strcmp(token, "0.0.0.0") == 0)
68 {
69 log_message(LOG_ERROR, BITLAB_LOG, __FILE__, "Invalid IP from DNS seed: %s", dns_seeds[i]);
70 break;
71 }
72 add_peer_to_queue(token, 8333);
73 token = strtok(NULL, " ");
74 peer_count++;
75 }
76 }
77 }
78 else
79 {
80 char ip_address[BUFFER_SIZE];
82 char* token = strtok(ip_address, " ");
83 while (token != NULL && *token != '\0')
84 {
85 if (strcmp(token, "0.0.0.0") == 0)
86 {
87 log_message(LOG_ERROR, BITLAB_LOG, __FILE__, "Invalid IP from DNS seed: %s", get_peer_discovery_dns_domain());
88 break;
89 }
90 add_peer_to_queue(token, 8333);
91 token = strtok(NULL, " ");
92 peer_count++;
93 }
94 }
95 }
96 else
97 {
98 log_message(LOG_ERROR, BITLAB_LOG, __FILE__, "Peer discovery operation or no valid state set");
100 }
101
102 // [ ] Implement peer processing using the Bitcoin P2P protocol
103
104 if (peer_count > 0)
105 {
106 log_message(LOG_INFO, BITLAB_LOG, __FILE__, "Peer discovery succeeded: found %d peers", peer_count);
108 }
109 else if (!search)
110 {
111 log_message(LOG_ERROR, BITLAB_LOG, __FILE__, "Peer discovery failed: no valid state set");
113 }
114 else
115 {
116 log_message(LOG_ERROR, BITLAB_LOG, __FILE__, "Peer discovery failed: no peers found");
118 }
119 }
120 usleep(100000); // 100 ms
121 }
122 if (arg) {} // dummy code to avoid unused parameter warning
123 log_message(LOG_INFO, BITLAB_LOG, __FILE__, "Exiting peer discovery thread");
124 pthread_exit(NULL);
125}
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
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_ERROR
Definition log.h:32
@ LOG_INFO
Definition log.h:30
static const char * hardcoded_peers[]
void * handle_peer_discovery(void *arg)
Peer discovery handler thread.
static const char * dns_seeds[]
void add_peer_to_queue(const char *ip, int port)
Add a peer to the queue.
Definition peer_queue.c:15
bool get_peer_discovery()
Get the peer discovery operation.
Definition state.c:150
bool get_peer_discovery_in_progress()
Get the peer discovery in progress state.
Definition state.c:158
bool get_peer_discovery_hardcoded_seeds()
Get the peer discovery hardcoded seeds state.
Definition state.c:206
bool get_peer_discovery_succeeded()
Get the peer discovery succeeded state.
Definition state.c:166
void start_peer_discovery_progress()
Start the peer discovery progress.
Definition state.c:107
const char * get_peer_discovery_dns_domain()
Get the peer discovery DNS domain.
Definition state.c:239
sig_atomic_t get_exit_flag()
Get the exit flag.
Definition state.c:80
void finish_peer_discovery_progress(bool succeeded)
Finish the peer discovery progress.
Definition state.c:117
bool get_peer_discovery_dns_lookup()
Get the peer discovery DNS lookup state.
Definition state.c:214
#define BUFFER_SIZE
Definition utils.h:12
void guarded_print_line(const char *format,...)
Guarded print line function.
Definition utils.c:65
void usleep(unsigned int usec)
char * strtok(char *str, const char *delimiters)