|
| 1 | +#include "config.h" |
| 2 | + |
| 3 | +#include "bytes.h" |
| 4 | +#include "client.h" |
| 5 | +#include "client_messager_glib.h" |
| 6 | +#include "messager_glib.h" |
| 7 | + |
| 8 | +static int _client_send (struct curvecpr_client *client, const unsigned char *buf, size_t num) |
| 9 | +{ |
| 10 | + struct curvecpr_client_messager_glib *cmg = client->cf.priv; |
| 11 | + |
| 12 | + return cmg->cf.ops.send(cmg, buf, num); |
| 13 | +} |
| 14 | + |
| 15 | +static int _client_recv (struct curvecpr_client *client, const unsigned char *buf, size_t num) |
| 16 | +{ |
| 17 | + struct curvecpr_client_messager_glib *cmg = client->cf.priv; |
| 18 | + |
| 19 | + return curvecpr_messager_glib_recv(&cmg->mg, buf, num); |
| 20 | +} |
| 21 | + |
| 22 | +static int _client_next_nonce(struct curvecpr_client *client, unsigned char *destination, size_t num) |
| 23 | +{ |
| 24 | + struct curvecpr_client_messager_glib *cmg = client->cf.priv; |
| 25 | + |
| 26 | + return cmg->cf.ops.next_nonce(cmg, destination, num); |
| 27 | +} |
| 28 | + |
| 29 | +static int _messager_glib_send (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num) |
| 30 | +{ |
| 31 | + struct curvecpr_client_messager_glib *cmg = mg->cf.priv; |
| 32 | + |
| 33 | + return curvecpr_client_send(&cmg->client, buf, num); |
| 34 | +} |
| 35 | + |
| 36 | +static int _messager_glib_recv (struct curvecpr_messager_glib *mg, const unsigned char *buf, size_t num) |
| 37 | +{ |
| 38 | + struct curvecpr_client_messager_glib *cmg = mg->cf.priv; |
| 39 | + |
| 40 | + return cmg->cf.ops.recv(cmg, buf, num); |
| 41 | +} |
| 42 | + |
| 43 | +static void _messager_glib_finished (struct curvecpr_messager_glib *mg, enum curvecpr_block_eofflag flag) |
| 44 | +{ |
| 45 | + struct curvecpr_client_messager_glib *cmg = mg->cf.priv; |
| 46 | + |
| 47 | + if (cmg->cf.ops.finished) |
| 48 | + cmg->cf.ops.finished(cmg, flag); |
| 49 | +} |
| 50 | + |
| 51 | +void curvecpr_client_messager_glib_new (struct curvecpr_client_messager_glib *cmg, struct curvecpr_client_messager_glib_cf *cf) |
| 52 | +{ |
| 53 | + struct curvecpr_client_cf client_cf = { |
| 54 | + .ops = { |
| 55 | + .send = _client_send, |
| 56 | + .recv = _client_recv, |
| 57 | + |
| 58 | + .next_nonce = _client_next_nonce |
| 59 | + }, |
| 60 | + .priv = cmg |
| 61 | + }; |
| 62 | + |
| 63 | + struct curvecpr_messager_glib_cf mg_cf = { |
| 64 | + .ops = { |
| 65 | + .send = _messager_glib_send, |
| 66 | + .recv = _messager_glib_recv, |
| 67 | + .finished = _messager_glib_finished |
| 68 | + }, |
| 69 | + .priv = cmg |
| 70 | + }; |
| 71 | + |
| 72 | + curvecpr_bytes_zero(cmg, sizeof(struct curvecpr_client_messager_glib)); |
| 73 | + |
| 74 | + if (cf) |
| 75 | + curvecpr_bytes_copy(&cmg->cf, cf, sizeof(struct curvecpr_client_messager_glib_cf)); |
| 76 | + |
| 77 | + /* Client configuration. */ |
| 78 | + curvecpr_bytes_copy(client_cf.my_extension, cmg->cf.my_extension, 16); |
| 79 | + |
| 80 | + curvecpr_bytes_copy(client_cf.my_global_pk, cmg->cf.my_global_pk, 32); |
| 81 | + curvecpr_bytes_copy(client_cf.my_global_sk, cmg->cf.my_global_sk, 32); |
| 82 | + |
| 83 | + curvecpr_bytes_copy(client_cf.their_extension, cmg->cf.their_extension, 16); |
| 84 | + curvecpr_bytes_copy(client_cf.their_global_pk, cmg->cf.their_global_pk, 32); |
| 85 | + curvecpr_bytes_copy(client_cf.their_domain_name, cmg->cf.their_domain_name, 256); |
| 86 | + |
| 87 | + /* Messager configuration. */ |
| 88 | + mg_cf.pending_maximum = cmg->cf.pending_maximum; |
| 89 | + mg_cf.sendmarkq_maximum = cmg->cf.sendmarkq_maximum; |
| 90 | + mg_cf.recvmarkq_maximum = cmg->cf.recvmarkq_maximum; |
| 91 | + |
| 92 | + /* Initialize client and messager. */ |
| 93 | + curvecpr_client_new(&cmg->client, &client_cf); |
| 94 | + curvecpr_messager_glib_new(&cmg->mg, &mg_cf, 1); |
| 95 | +} |
| 96 | + |
| 97 | +void curvecpr_client_messager_glib_dealloc (struct curvecpr_client_messager_glib *cmg) |
| 98 | +{ |
| 99 | + curvecpr_messager_glib_dealloc(&cmg->mg); |
| 100 | +} |
| 101 | + |
| 102 | +int curvecpr_client_messager_glib_connected (struct curvecpr_client_messager_glib *cmg) |
| 103 | +{ |
| 104 | + return curvecpr_client_connected(&cmg->client); |
| 105 | +} |
| 106 | + |
| 107 | +int curvecpr_client_messager_glib_send (struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num) |
| 108 | +{ |
| 109 | + return curvecpr_messager_glib_send(&cmg->mg, buf, num); |
| 110 | +} |
| 111 | + |
| 112 | +int curvecpr_client_messager_glib_close (struct curvecpr_client_messager_glib *cmg) |
| 113 | +{ |
| 114 | + return curvecpr_messager_glib_close(&cmg->mg); |
| 115 | +} |
| 116 | + |
| 117 | +int curvecpr_client_messager_glib_recv (struct curvecpr_client_messager_glib *cmg, const unsigned char *buf, size_t num) |
| 118 | +{ |
| 119 | + return curvecpr_client_recv(&cmg->client, buf, num); |
| 120 | +} |
| 121 | + |
| 122 | +int curvecpr_client_messager_glib_process_sendq (struct curvecpr_client_messager_glib *cmg) |
| 123 | +{ |
| 124 | + return curvecpr_messager_glib_process_sendq(&cmg->mg); |
| 125 | +} |
| 126 | + |
| 127 | +long long curvecpr_client_messager_glib_next_timeout (struct curvecpr_client_messager_glib *cmg) |
| 128 | +{ |
| 129 | + return curvecpr_messager_glib_next_timeout(&cmg->mg); |
| 130 | +} |
0 commit comments