diff options
author | ch3 <buenger@mytum.de> | 2023-09-20 14:45:38 +0200 |
---|---|---|
committer | ch3 <buenger@mytum.de> | 2024-04-26 19:04:59 +0200 |
commit | ef816b853f915e6d14133c10b900a25717997a37 (patch) | |
tree | c154d8cc40a83a13d008e0982b14ff30c4701259 | |
parent | 70dc509ab1fd961c9dbf53bda139319f06ca0364 (diff) | |
download | gnunet-ef816b853f915e6d14133c10b900a25717997a37.tar.gz gnunet-ef816b853f915e6d14133c10b900a25717997a37.zip |
cong: (test_)underlay_dummy: logging, restructure, scheduling
-rw-r--r-- | src/service/core/gnunet_core_underlay_dummy.c | 198 | ||||
-rw-r--r-- | src/service/core/test_core_underlay_dummy.c | 22 |
2 files changed, 161 insertions, 59 deletions
diff --git a/src/service/core/gnunet_core_underlay_dummy.c b/src/service/core/gnunet_core_underlay_dummy.c index e0ef4c5bb..4cf3e2160 100644 --- a/src/service/core/gnunet_core_underlay_dummy.c +++ b/src/service/core/gnunet_core_underlay_dummy.c | |||
@@ -42,10 +42,13 @@ extern "C" { | |||
42 | #endif | 42 | #endif |
43 | 43 | ||
44 | 44 | ||
45 | // FIXME use gnunet's own wrappers! | ||
45 | #include <sys/socket.h> | 46 | #include <sys/socket.h> |
46 | #include <unistd.h> | 47 | #include <unistd.h> |
48 | #include <errno.h> | ||
47 | #include "gnunet_core_underlay_dummy.h" | 49 | #include "gnunet_core_underlay_dummy.h" |
48 | #include "gnunet_util_lib.h" | 50 | #include "gnunet_util_lib.h" |
51 | #include "gnunet_scheduler_lib.h" | ||
49 | 52 | ||
50 | #define LOG(kind, ...) GNUNET_log_from (kind, "core-underlay-dummy", __VA_ARGS__) | 53 | #define LOG(kind, ...) GNUNET_log_from (kind, "core-underlay-dummy", __VA_ARGS__) |
51 | 54 | ||
@@ -59,12 +62,128 @@ extern "C" { | |||
59 | */ | 62 | */ |
60 | struct GNUNET_CORE_UNDERLAY_DUMMY_Handle | 63 | struct GNUNET_CORE_UNDERLAY_DUMMY_Handle |
61 | { | 64 | { |
65 | // TODO document | ||
62 | GNUNET_CORE_UNDERLAY_DUMMY_NotifyConnect notify_connect; | 66 | GNUNET_CORE_UNDERLAY_DUMMY_NotifyConnect notify_connect; |
67 | GNUNET_CORE_UNDERLAY_DUMMY_NotifyDisconnect notify_disconnect; | ||
68 | GNUNET_CORE_UNDERLAY_DUMMY_NotifyAddressChange notify_address_change; | ||
69 | void *cls; | ||
63 | char *recv_addr; | 70 | char *recv_addr; |
71 | int64_t fd; | ||
72 | struct GNUNET_HashCode network_location_hash; | ||
73 | uint64_t network_generation_id; | ||
74 | struct GNUNET_SCHEDULER_Task *listen_task; | ||
75 | struct GNUNET_SCHEDULER_Task *open_socket_task; | ||
64 | }; | 76 | }; |
65 | 77 | ||
66 | 78 | ||
67 | /** | 79 | /** |
80 | * Listen for a connection on the dummy's socket | ||
81 | * | ||
82 | * @param cls the hanlde to the dummy passed as closure | ||
83 | */ | ||
84 | void do_listen (void *cls) | ||
85 | { | ||
86 | struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *h = cls; | ||
87 | |||
88 | h->listen_task = NULL; | ||
89 | int64_t fd_client; | ||
90 | struct sockaddr_un addr_client; | ||
91 | struct sockaddr_un addr_from; | ||
92 | socklen_t from_len = sizeof(addr_from); | ||
93 | |||
94 | LOG(GNUNET_ERROR_TYPE_INFO, "Listening for incoming connections\n"); | ||
95 | fd_client = accept (h->fd, (struct sockaddr *) &addr_client, &from_len); | ||
96 | if (fd_client < 0) | ||
97 | { | ||
98 | //LOG(GNUNET_ERROR_TYPE_ERROR, "Error accepting incoming connection, %s", strerror(errno)); | ||
99 | LOG(GNUNET_ERROR_TYPE_ERROR, "Error accepting incoming connection\n"); | ||
100 | close (h->fd); | ||
101 | close (fd_client); | ||
102 | return; | ||
103 | } | ||
104 | LOG(GNUNET_ERROR_TYPE_INFO, "Peer connected\n"); | ||
105 | // TODO use GNUNET_CORE_UNDERLAY_NotifyConnect to signal that another 'peer' | ||
106 | // connected | ||
107 | // | ||
108 | // TODO pass received messages to mq | ||
109 | //h->listen_task = GNUNET_SCHEDULER_add_now (do_listen, h); | ||
110 | } | ||
111 | |||
112 | |||
113 | /** | ||
114 | * Shut the dummy down | ||
115 | * | ||
116 | * Release our sockets, free memory and cancel scheduled tasks. | ||
117 | * | ||
118 | * @param cls handle to the dummy passed as closure | ||
119 | */ | ||
120 | void do_shutdown (void *cls) | ||
121 | { | ||
122 | struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *h = cls; | ||
123 | |||
124 | if (NULL != h->listen_task) GNUNET_SCHEDULER_cancel (h->listen_task); | ||
125 | // TODO release all sockets | ||
126 | close(h->fd); | ||
127 | // TODO free all memory | ||
128 | } | ||
129 | |||
130 | /** | ||
131 | * Main running task | ||
132 | * | ||
133 | * Sets up socket. | ||
134 | * | ||
135 | * @param cls | ||
136 | */ | ||
137 | void do_open_socket (void *cls) | ||
138 | { | ||
139 | struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *h = cls; | ||
140 | struct sockaddr_un addr; | ||
141 | char buff[BUFF_SIZE]; | ||
142 | |||
143 | if ((h->fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) | ||
144 | { | ||
145 | LOG(GNUNET_ERROR_TYPE_ERROR, "Fd does not open\n"); | ||
146 | return; | ||
147 | } | ||
148 | |||
149 | memset(&addr, 0, sizeof (addr)); | ||
150 | addr.sun_family = AF_UNIX; | ||
151 | unlink (SERVER_ADDR_BASE); | ||
152 | if (bind (h->fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) | ||
153 | { | ||
154 | // TODO use a counter to just use the 'next' free socket | ||
155 | LOG(GNUNET_ERROR_TYPE_ERROR, "Faild binding to socket\n"); | ||
156 | close(h->fd); | ||
157 | return; | ||
158 | } | ||
159 | |||
160 | h->recv_addr = GNUNET_malloc (strlen (SERVER_ADDR_BASE) + 1); | ||
161 | h->recv_addr = GNUNET_strdup (SERVER_ADDR_BASE); | ||
162 | if (NULL != h->notify_address_change) | ||
163 | { | ||
164 | // FIXME compute the network_location_hash and network_generation_id | ||
165 | // FIXME _schedule_now() | ||
166 | h->notify_address_change (h->cls, | ||
167 | h->network_location_hash, | ||
168 | h->network_generation_id); | ||
169 | } | ||
170 | |||
171 | LOG(GNUNET_ERROR_TYPE_INFO, "Mark socket as accepting connections\n"); | ||
172 | if (listen (h->fd, BACKLOG) < 0) | ||
173 | { | ||
174 | //LOG(GNUNET_ERROR_TYPE_ERROR, "Failed listening to socket: %s", strerror(errno)); | ||
175 | LOG(GNUNET_ERROR_TYPE_ERROR, "Failed listening to socket\n"); | ||
176 | close(h->fd); | ||
177 | return; | ||
178 | } | ||
179 | LOG(GNUNET_ERROR_TYPE_INFO, "Going to listen for connections\n"); | ||
180 | |||
181 | h->listen_task = GNUNET_SCHEDULER_add_now (do_listen, h); | ||
182 | LOG(GNUNET_ERROR_TYPE_INFO, "Listen task: %p\n", h->listen_task); | ||
183 | } | ||
184 | |||
185 | |||
186 | /** | ||
68 | * Connect to the core underlay dummy service. Note that the connection may | 187 | * Connect to the core underlay dummy service. Note that the connection may |
69 | * complete (or fail) asynchronously. | 188 | * complete (or fail) asynchronously. |
70 | * | 189 | * |
@@ -91,58 +210,22 @@ GNUNET_CORE_UNDERLAY_DUMMY_connect (const struct GNUNET_CONFIGURATION_Handle *cf | |||
91 | // has or has not its open connections | 210 | // has or has not its open connections |
92 | 211 | ||
93 | struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *h; | 212 | struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *h; |
94 | int64_t fd, fd_client; | ||
95 | struct sockaddr_un addr; | ||
96 | struct sockaddr_un addr_client; | ||
97 | int64_t len; | ||
98 | char buff[BUFF_SIZE]; | ||
99 | struct sockaddr_un addr_from; | ||
100 | socklen_t from_len = sizeof(addr_from); | ||
101 | 213 | ||
102 | h = GNUNET_malloc (sizeof (struct GNUNET_CORE_UNDERLAY_DUMMY_Handle)); | 214 | h = GNUNET_malloc (sizeof (struct GNUNET_CORE_UNDERLAY_DUMMY_Handle)); |
103 | h->notify_connect = nc; | 215 | h->notify_connect = nc; |
216 | h->notify_disconnect = nd; | ||
217 | h->notify_address_change = na; | ||
218 | h->cls = cls; | ||
219 | // FIXME treat 0 as special, invalid value? | ||
220 | memset (&h->network_location_hash, 0, sizeof (struct GNUNET_HashCode)); | ||
221 | // FIXME treat 0 as special, invalid value? | ||
222 | h->network_generation_id = 0; | ||
104 | 223 | ||
105 | if ((fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) | 224 | // FIXME this needs to potentially be cancelled in _disconnect |
106 | { | 225 | h->open_socket_task = GNUNET_SCHEDULER_add_now (do_open_socket, h); |
107 | LOG(GNUNET_ERROR_TYPE_ERROR, "Fd does not open"); | ||
108 | return NULL; | ||
109 | } | ||
110 | 226 | ||
111 | memset(&addr, 0, sizeof (addr)); | 227 | LOG(GNUNET_ERROR_TYPE_INFO, "Core connected\n"); |
112 | addr.sun_family = AF_UNIX; | ||
113 | strcpy (addr.sun_path, SERVER_ADDR_BASE); | ||
114 | unlink (SERVER_ADDR_BASE); | ||
115 | if (bind (fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) | ||
116 | { | ||
117 | // TODO use a counter to just use the 'next' free socket | ||
118 | LOG(GNUNET_ERROR_TYPE_ERROR, "Faild binding to socket"); | ||
119 | close(fd); | ||
120 | return NULL; | ||
121 | } | ||
122 | // TODO we could now use GNUNET_CORE_UNDERLAY_NotifyAddressChange to signal | ||
123 | // that we actually got the address | ||
124 | h->recv_addr = GNUNET_malloc (strlen (SERVER_ADDR_BASE)); | ||
125 | h->recv_addr = GNUNET_strdup (SERVER_ADDR_BASE); | ||
126 | 228 | ||
127 | if (listen (fd, BACKLOG) < 0) | ||
128 | { | ||
129 | LOG(GNUNET_ERROR_TYPE_ERROR, "Failed listening to socket"); | ||
130 | close(fd); | ||
131 | return NULL; | ||
132 | } | ||
133 | |||
134 | fd_client = accept (fd, (struct sockaddr *) &addr_client, &from_len); | ||
135 | if (fd_client < 0) | ||
136 | { | ||
137 | LOG(GNUNET_ERROR_TYPE_ERROR, "Error accepting incoming connection"); | ||
138 | close (fd); | ||
139 | close (fd_client); | ||
140 | return NULL; | ||
141 | } | ||
142 | // TODO use GNUNET_CORE_UNDERLAY_NotifyConnect to signal that another 'peer' | ||
143 | // connected | ||
144 | // | ||
145 | // TODO pass received messages to mq | ||
146 | return h; | 229 | return h; |
147 | } | 230 | } |
148 | 231 | ||
@@ -156,7 +239,22 @@ void | |||
156 | GNUNET_CORE_UNDERLAY_DUMMY_disconnect | 239 | GNUNET_CORE_UNDERLAY_DUMMY_disconnect |
157 | (struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *handle) | 240 | (struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *handle) |
158 | { | 241 | { |
242 | LOG (GNUNET_ERROR_TYPE_INFO, "Core disconnects\n"); | ||
243 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Core disconnects\n"); | ||
244 | LOG (GNUNET_ERROR_TYPE_INFO, "listen task: %p\n", handle->listen_task); | ||
159 | // TODO delete and close everything | 245 | // TODO delete and close everything |
246 | if (NULL != handle->listen_task) | ||
247 | { | ||
248 | LOG (GNUNET_ERROR_TYPE_INFO, "Cancelling listen task\n"); | ||
249 | GNUNET_SCHEDULER_cancel (handle->listen_task); | ||
250 | } | ||
251 | if (NULL != handle->open_socket_task) | ||
252 | { | ||
253 | LOG (GNUNET_ERROR_TYPE_INFO, "Cancelling open socket task\n"); | ||
254 | GNUNET_SCHEDULER_cancel (handle->open_socket_task); | ||
255 | } | ||
256 | |||
257 | //GNUNET_SCHEDULER_shutdown (); | ||
160 | } | 258 | } |
161 | 259 | ||
162 | 260 | ||
@@ -214,7 +312,7 @@ GNUNET_CORE_UNDERLAY_DUMMY_connect_to_peer ( | |||
214 | 312 | ||
215 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0) | 313 | if ((fd = socket (AF_UNIX, SOCK_STREAM, 0) ) < 0) |
216 | { | 314 | { |
217 | LOG(GNUNET_ERROR_TYPE_ERROR, "Failure opening socket"); | 315 | LOG(GNUNET_ERROR_TYPE_ERROR, "Failure opening socket\n"); |
218 | return; | 316 | return; |
219 | } | 317 | } |
220 | 318 | ||
@@ -225,7 +323,7 @@ GNUNET_CORE_UNDERLAY_DUMMY_connect_to_peer ( | |||
225 | unlink (CLIENT_ADDR_BASE); | 323 | unlink (CLIENT_ADDR_BASE); |
226 | if (bind (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) | 324 | if (bind (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) |
227 | { | 325 | { |
228 | LOG(GNUNET_ERROR_TYPE_ERROR, "Failed to bind to socket"); | 326 | LOG(GNUNET_ERROR_TYPE_ERROR, "Failed to bind to socket\n"); |
229 | return; | 327 | return; |
230 | } | 328 | } |
231 | 329 | ||
@@ -233,11 +331,11 @@ GNUNET_CORE_UNDERLAY_DUMMY_connect_to_peer ( | |||
233 | strcpy (addr_receiver.sun_path, peer_address); | 331 | strcpy (addr_receiver.sun_path, peer_address); |
234 | if (connect (fd, (struct sockaddr *) &addr_receiver, sizeof(addr)) < 0) | 332 | if (connect (fd, (struct sockaddr *) &addr_receiver, sizeof(addr)) < 0) |
235 | { | 333 | { |
236 | LOG(GNUNET_ERROR_TYPE_ERROR, "failed to connect to the socket"); | 334 | LOG(GNUNET_ERROR_TYPE_ERROR, "failed to connect to the socket\n"); |
237 | return; | 335 | return; |
238 | } | 336 | } |
239 | //GNUNET_MQ_queue_for_callbacks () | 337 | //GNUNET_MQ_queue_for_callbacks () |
240 | //h.notify_connect(cls, 1, peer_address, mq); | 338 | //h.notify_connect(cls, 1, peer_address, mq); // if non NULL! |
241 | // FIXME: proper array | 339 | // FIXME: proper array |
242 | // FIXME: proper address format | 340 | // FIXME: proper address format |
243 | } | 341 | } |
diff --git a/src/service/core/test_core_underlay_dummy.c b/src/service/core/test_core_underlay_dummy.c index dad359594..a2111097e 100644 --- a/src/service/core/test_core_underlay_dummy.c +++ b/src/service/core/test_core_underlay_dummy.c | |||
@@ -62,14 +62,14 @@ void address_change_cb (void *cls, | |||
62 | uint64_t network_generation_id) | 62 | uint64_t network_generation_id) |
63 | { | 63 | { |
64 | address_callback = GNUNET_YES; | 64 | address_callback = GNUNET_YES; |
65 | //LOG(GNUNET_ERROR_TYPE_INFO, "Got informed of address change"); | 65 | LOG(GNUNET_ERROR_TYPE_INFO, "Got informed of address change\n"); |
66 | printf("Got informed of address change\n"); | ||
67 | } | 66 | } |
68 | 67 | ||
69 | int main (void) | 68 | |
69 | void run_test (void *cls) | ||
70 | { | 70 | { |
71 | LOG(GNUNET_ERROR_TYPE_INFO, "Connecting to underlay dummy"); | 71 | GNUNET_log_setup ("test-core-underlay-dummy", "DEBUG", NULL); |
72 | printf("Connecting to underlay dummy\n"); | 72 | LOG(GNUNET_ERROR_TYPE_INFO, "Connecting to underlay dummy\n"); |
73 | struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *h = | 73 | struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *h = |
74 | GNUNET_CORE_UNDERLAY_DUMMY_connect (NULL, //cfg | 74 | GNUNET_CORE_UNDERLAY_DUMMY_connect (NULL, //cfg |
75 | NULL, // handlers | 75 | NULL, // handlers |
@@ -77,11 +77,15 @@ int main (void) | |||
77 | NULL, // nc | 77 | NULL, // nc |
78 | NULL, // nd | 78 | NULL, // nd |
79 | address_change_cb); // na | 79 | address_change_cb); // na |
80 | LOG(GNUNET_ERROR_TYPE_INFO, "Connected to underlay dummy, disconnecting"); | 80 | LOG(GNUNET_ERROR_TYPE_INFO, "Connected to underlay dummy, disconnecting\n"); |
81 | printf("Connected to underlay dummy, disconnecting\n"); | ||
82 | GNUNET_CORE_UNDERLAY_DUMMY_disconnect (h); | 81 | GNUNET_CORE_UNDERLAY_DUMMY_disconnect (h); |
83 | LOG(GNUNET_ERROR_TYPE_INFO, "Disconnected to underlay dummy"); | 82 | LOG(GNUNET_ERROR_TYPE_INFO, "Disconnected from underlay dummy\n"); |
84 | printf("Disconnected from underlay dummy\n"); | 83 | } |
84 | |||
85 | int main (void) | ||
86 | { | ||
87 | GNUNET_SCHEDULER_run (run_test, NULL); | ||
88 | //GNUNET_SCHEDULER_shutdown (); | ||
85 | 89 | ||
86 | if (GNUNET_YES != address_callback) return -1; | 90 | if (GNUNET_YES != address_callback) return -1; |
87 | return 0; | 91 | return 0; |