BitLab 0.1.0
BitLab: A Browser for the Bitcoin P2P Network and Blockchain
Loading...
Searching...
No Matches
peer_queue.h File Reference
#include <stdbool.h>
#include <stddef.h>

Go to the source code of this file.

Data Structures

struct  Peer
 The peer structure used to store the peer information obtained by peer discovery process. More...
 

Macros

#define MAX_PEERS   1000
 

Functions

void add_peer_to_queue (const char *ip, int port)
 Add a peer to the queue.
 
bool is_peer_queue_empty ()
 Check if the peer queue is empty.
 
bool get_peer_from_queue (char *buffer, size_t buffer_size)
 Get a peer from the queue.
 
void clear_peer_queue ()
 Clear the peer queue.
 
void print_peer_queue ()
 Print the peer queue.
 
Peerget_peer_queue (int *count)
 Get the peer queue.
 

Macro Definition Documentation

◆ MAX_PEERS

#define MAX_PEERS   1000

Definition at line 4 of file peer_queue.h.

Function Documentation

◆ add_peer_to_queue()

void add_peer_to_queue ( const char *  ip,
int  port 
)

Add a peer to the queue.

Parameters
ipThe IP address of the peer.
portThe port of the peer.

Definition at line 15 of file peer_queue.c.

16{
17 pthread_mutex_lock(&peer_queue_mutex);
18
19 // Check if the IP-port pair already exists in the queue
20 for (int i = peer_queue_start; i != peer_queue_end; i = (i + 1) % MAX_PEERS)
21 {
22 if (strcmp(peer_queue[i].ip, ip) == 0 && peer_queue[i].port == port)
23 {
24 // IP-port pair already exists, don't add it again
26 "Duplicate peer: %s:%d, not added", ip, port);
27 pthread_mutex_unlock(&peer_queue_mutex);
28 return;
29 }
30 }
31
32 // Check if the queue is full
34 {
36 "Peer queue is full, cannot add peer: %s:%d", ip, port);
37 pthread_mutex_unlock(&peer_queue_mutex);
38 return;
39 }
40
41 // Extract the port if not provided
42 if (port == 0)
43 {
44 char* colon_pos = strrchr(ip, ':');
45 if (colon_pos != NULL)
46 {
47 port = atoi(colon_pos + 1);
48 strncpy(peer_queue[peer_queue_end].ip, ip, colon_pos - ip);
49 peer_queue[peer_queue_end].ip[colon_pos - ip] = '\0';
50 }
51 else
52 {
54 "Invalid IP format, cannot extract port: %s", ip);
55 pthread_mutex_unlock(&peer_queue_mutex);
56 return;
57 }
58 }
59 else
60 {
61 strncpy(peer_queue[peer_queue_end].ip, ip,
62 sizeof(peer_queue[peer_queue_end].ip));
63 }
64
65 // Add the IP-port pair to the queue
68
69 pthread_mutex_unlock(&peer_queue_mutex);
70}
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_INFO
Definition log.h:30
@ LOG_WARN
Definition log.h:31
static int peer_queue_end
Definition peer_queue.c:12
static int peer_queue_start
Definition peer_queue.c:11
static pthread_mutex_t peer_queue_mutex
Definition peer_queue.c:13
static Peer peer_queue[MAX_PEERS]
Definition peer_queue.c:10
#define MAX_PEERS
Definition peer_queue.h:4
char ip[256]
Definition peer_queue.h:17
int port
Definition peer_queue.h:18

References BITLAB_LOG, Peer::ip, LOG_INFO, log_message(), LOG_WARN, MAX_PEERS, peer_queue, peer_queue_end, peer_queue_mutex, peer_queue_start, and Peer::port.

Referenced by handle_peer_discovery(), and send_getaddr_and_wait().

◆ clear_peer_queue()

void clear_peer_queue ( )

Clear the peer queue.

Definition at line 95 of file peer_queue.c.

96{
97 pthread_mutex_lock(&peer_queue_mutex);
100 pthread_mutex_unlock(&peer_queue_mutex);
101}

References peer_queue_end, peer_queue_mutex, and peer_queue_start.

◆ get_peer_from_queue()

bool get_peer_from_queue ( char *  buffer,
size_t  buffer_size 
)

Get a peer from the queue.

Parameters
bufferThe buffer to store the peer.
buffer_sizeThe size of the buffer.
Returns
True if the peer was successfully retrieved, false otherwise.

Definition at line 80 of file peer_queue.c.

81{
82 pthread_mutex_lock(&peer_queue_mutex);
84 {
85 pthread_mutex_unlock(&peer_queue_mutex);
86 return false;
87 }
88 snprintf(buffer, buffer_size, "%s:%d", peer_queue[peer_queue_start].ip,
91 pthread_mutex_unlock(&peer_queue_mutex);
92 return true;
93}

References MAX_PEERS, peer_queue, peer_queue_end, peer_queue_mutex, and peer_queue_start.

◆ get_peer_queue()

Peer * get_peer_queue ( int *  count)

Get the peer queue.

Definition at line 118 of file peer_queue.c.

119{
120 pthread_mutex_lock(&peer_queue_mutex);
121
123 {
124 *count = 0;
125 pthread_mutex_unlock(&peer_queue_mutex);
126 return NULL;
127 }
128
129 int size = (peer_queue_end >= peer_queue_start)
132
133 Peer* peers = malloc(size * sizeof(Peer));
134 if (peers == NULL)
135 {
136 *count = 0;
137 pthread_mutex_unlock(&peer_queue_mutex);
138 return NULL;
139 }
140
141 for (int i = 0, j = peer_queue_start; j != peer_queue_end; j = (j + 1) % MAX_PEERS,
142 i++)
143 {
144 peers[i] = peer_queue[j];
145 }
146
147 *count = size;
148 pthread_mutex_unlock(&peer_queue_mutex);
149 return peers;
150}
The peer structure used to store the peer information obtained by peer discovery process.
Definition peer_queue.h:16

References MAX_PEERS, peer_queue, peer_queue_end, peer_queue_mutex, and peer_queue_start.

Referenced by send_addr().

◆ is_peer_queue_empty()

bool is_peer_queue_empty ( )

Check if the peer queue is empty.

Returns
True if the peer queue is empty, false otherwise.

Definition at line 72 of file peer_queue.c.

73{
74 pthread_mutex_lock(&peer_queue_mutex);
75 bool empty = (peer_queue_start == peer_queue_end);
76 pthread_mutex_unlock(&peer_queue_mutex);
77 return empty;
78}

References peer_queue_end, peer_queue_mutex, and peer_queue_start.

◆ print_peer_queue()

void print_peer_queue ( )

Print the peer queue.

Definition at line 103 of file peer_queue.c.

104{
105 pthread_mutex_lock(&peer_queue_mutex);
107 {
108 guarded_print_line("Peer queue is empty");
109 pthread_mutex_unlock(&peer_queue_mutex);
110 return;
111 }
112 int k = 1;
113 for (int i = peer_queue_start; i != peer_queue_end; i = (i + 1) % MAX_PEERS)
114 guarded_print_line("%d | %s:%d", k++, peer_queue[i].ip, peer_queue[i].port);
115 pthread_mutex_unlock(&peer_queue_mutex);
116}
void guarded_print_line(const char *format,...)
Guarded print line function.
Definition utils.c:65

References guarded_print_line(), MAX_PEERS, peer_queue, peer_queue_end, peer_queue_mutex, and peer_queue_start.

Referenced by cli_peer_discovery().