3
3
#include "config.h"
4
4
#include <assert.h>
5
5
#include <ccan/io/io.h>
6
+ #include <common/cryptomsg.h>
7
+ #include <common/per_peer_state.h>
6
8
#include <common/status.h>
7
9
#include <common/utils.h>
8
10
#include <connectd/multiplex.h>
9
11
#include <errno.h>
10
12
#include <sys/socket.h>
11
13
#include <sys/types.h>
14
+ #include <wire/wire_io.h>
15
+
16
+ void queue_peer_msg (struct peer * peer , const u8 * msg TAKES )
17
+ {
18
+ msg_enqueue (peer -> peer_outq , msg );
19
+ }
12
20
13
21
/* These four function handle subd->peer */
14
22
static struct io_plan * after_final_msg (struct io_conn * peer_conn ,
@@ -24,25 +32,39 @@ static struct io_plan *after_final_msg(struct io_conn *peer_conn,
24
32
return io_close (peer_conn );
25
33
}
26
34
35
+ static struct io_plan * encrypt_and_send (struct peer * peer ,
36
+ const u8 * msg TAKES ,
37
+ struct io_plan * (* next )
38
+ (struct io_conn * peer_conn ,
39
+ struct peer * peer ))
40
+ {
41
+ /* We free this and the encrypted version in next write_to_peer */
42
+ peer -> sent_to_peer = cryptomsg_encrypt_msg (peer , & peer -> pps -> cs , msg );
43
+ return io_write (peer -> to_peer ,
44
+ peer -> sent_to_peer ,
45
+ tal_bytelen (peer -> sent_to_peer ),
46
+ next , peer );
47
+ }
48
+
27
49
static struct io_plan * write_to_peer (struct io_conn * peer_conn ,
28
50
struct peer * peer )
29
51
{
52
+ const u8 * msg ;
30
53
assert (peer -> to_peer == peer_conn );
31
54
32
55
/* Free last sent one (if any) */
33
- tal_free (peer -> sent_to_peer );
56
+ peer -> sent_to_peer = tal_free (peer -> sent_to_peer );
34
57
35
58
/* Pop tail of send queue */
36
- peer -> sent_to_peer = msg_dequeue (peer -> peer_outq );
59
+ msg = msg_dequeue (peer -> peer_outq );
37
60
38
61
/* Nothing to send? */
39
- if (!peer -> sent_to_peer ) {
62
+ if (!msg ) {
40
63
/* Send final once subd is not longer connected */
41
64
if (peer -> final_msg && !peer -> to_subd ) {
42
- return io_write (peer_conn ,
43
- peer -> final_msg ,
44
- tal_bytelen (peer -> final_msg ),
45
- after_final_msg , peer );
65
+ return encrypt_and_send (peer ,
66
+ peer -> final_msg ,
67
+ after_final_msg );
46
68
}
47
69
/* Tell them to read again, */
48
70
io_wake (& peer -> subd_in );
@@ -52,102 +74,102 @@ static struct io_plan *write_to_peer(struct io_conn *peer_conn,
52
74
write_to_peer , peer );
53
75
}
54
76
55
- return io_write (peer_conn ,
56
- peer -> sent_to_peer ,
57
- tal_bytelen (peer -> sent_to_peer ),
58
- write_to_peer , peer );
77
+ return encrypt_and_send (peer , take (msg ), write_to_peer );
59
78
}
60
79
61
80
static struct io_plan * read_from_subd (struct io_conn * subd_conn ,
62
81
struct peer * peer );
63
82
static struct io_plan * read_from_subd_done (struct io_conn * subd_conn ,
64
83
struct peer * peer )
65
84
{
66
- size_t len = ((size_t * )peer -> subd_in )[1023 ];
67
- assert (peer -> to_subd == subd_conn );
68
-
69
- /* Trim to length */
70
- tal_resize (& peer -> subd_in , len );
71
-
72
- /* Tell them to write. */
73
- msg_enqueue (peer -> peer_outq , take (peer -> subd_in ));
85
+ /* Tell them to encrypt & write. */
86
+ queue_peer_msg (peer , take (peer -> subd_in ));
74
87
peer -> subd_in = NULL ;
88
+
75
89
/* Wait for them to wake us */
76
90
return io_wait (subd_conn , & peer -> subd_in , read_from_subd , peer );
77
91
}
78
92
79
93
static struct io_plan * read_from_subd (struct io_conn * subd_conn ,
80
94
struct peer * peer )
81
95
{
82
- /* We stash the length at the end */
83
- size_t * buf = tal_arr (peer , size_t , 1024 );
84
- assert (peer -> to_subd == subd_conn );
85
-
86
- peer -> subd_in = (u8 * )buf ;
87
- return io_read_partial (subd_conn , peer -> subd_in ,
88
- sizeof (size_t ) * 1023 ,
89
- & buf [1023 ],
90
- read_from_subd_done , peer );
96
+ return io_read_wire (subd_conn , peer , & peer -> subd_in ,
97
+ read_from_subd_done , peer );
91
98
}
92
99
93
100
/* These four function handle peer->subd */
94
101
static struct io_plan * write_to_subd (struct io_conn * subd_conn ,
95
102
struct peer * peer )
96
103
{
104
+ const u8 * msg ;
97
105
assert (peer -> to_subd == subd_conn );
98
106
99
- /* Free last sent one (if any) */
100
- tal_free (peer -> sent_to_subd );
101
-
102
107
/* Pop tail of send queue */
103
- peer -> sent_to_subd = msg_dequeue (peer -> subd_outq );
108
+ msg = msg_dequeue (peer -> subd_outq );
104
109
105
110
/* Nothing to send? */
106
- if (!peer -> sent_to_subd ) {
107
- /* Tell them to read again, */
111
+ if (!msg ) {
112
+ /* Tell them to read again. */
108
113
io_wake (& peer -> peer_in );
109
114
110
115
/* Wait for them to wake us */
111
116
return msg_queue_wait (subd_conn , peer -> subd_outq ,
112
117
write_to_subd , peer );
113
118
}
114
119
115
- return io_write (subd_conn ,
116
- peer -> sent_to_subd ,
117
- tal_bytelen (peer -> sent_to_subd ),
118
- write_to_subd , peer );
120
+ return io_write_wire (subd_conn , take (msg ), write_to_subd , peer );
119
121
}
120
122
121
- static struct io_plan * read_from_peer (struct io_conn * peer_conn ,
122
- struct peer * peer );
123
- static struct io_plan * read_from_peer_done (struct io_conn * peer_conn ,
123
+ static struct io_plan * read_hdr_from_peer (struct io_conn * peer_conn ,
124
+ struct peer * peer );
125
+ static struct io_plan * read_body_from_peer_done (struct io_conn * peer_conn ,
126
+ struct peer * peer )
127
+ {
128
+ u8 * decrypted ;
129
+
130
+ decrypted = cryptomsg_decrypt_body (NULL , & peer -> pps -> cs ,
131
+ peer -> peer_in );
132
+ if (!decrypted )
133
+ return io_close (peer_conn );
134
+ tal_free (peer -> peer_in );
135
+
136
+ /* Tell them to write. */
137
+ msg_enqueue (peer -> subd_outq , take (decrypted ));
138
+
139
+ /* Wait for them to wake us */
140
+ return io_wait (peer_conn , & peer -> peer_in , read_hdr_from_peer , peer );
141
+ }
142
+
143
+ static struct io_plan * read_body_from_peer (struct io_conn * peer_conn ,
124
144
struct peer * peer )
125
145
{
126
- size_t len = ((size_t * )peer -> peer_in )[1023 ];
127
- assert (peer -> to_peer == peer_conn );
146
+ u16 len ;
128
147
129
- /* Trim to length */
130
- tal_resize ( & peer -> peer_in , len );
148
+ if (! cryptomsg_decrypt_header ( & peer -> pps -> cs , peer -> peer_in , & len ))
149
+ return io_close ( peer_conn );
131
150
132
- /* Tell them to write. */
133
- msg_enqueue (peer -> subd_outq , take (peer -> peer_in ));
134
- peer -> peer_in = NULL ;
135
- /* Wait for them to wake us */
136
- return io_wait (peer_conn , & peer -> peer_in , read_from_peer , peer );
151
+ tal_resize (& peer -> peer_in , (u32 )len + CRYPTOMSG_BODY_OVERHEAD );
152
+ return io_read (peer_conn , peer -> peer_in , tal_count (peer -> peer_in ),
153
+ read_body_from_peer_done , peer );
137
154
}
138
155
139
- static struct io_plan * read_from_peer (struct io_conn * peer_conn ,
140
- struct peer * peer )
156
+ static struct io_plan * read_hdr_from_peer (struct io_conn * peer_conn ,
157
+ struct peer * peer )
141
158
{
142
- /* We stash the length at the end */
143
- size_t * buf = tal_arr (peer , size_t , 1024 );
144
159
assert (peer -> to_peer == peer_conn );
145
160
146
- peer -> peer_in = (u8 * )buf ;
147
- return io_read_partial (peer_conn , peer -> peer_in ,
148
- sizeof (size_t ) * 1023 ,
149
- & buf [1023 ],
150
- read_from_peer_done , peer );
161
+ /* BOLT #8:
162
+ *
163
+ * ### Receiving and Decrypting Messages
164
+ *
165
+ * In order to decrypt the _next_ message in the network
166
+ * stream, the following steps are completed:
167
+ *
168
+ * 1. Read _exactly_ 18 bytes from the network buffer.
169
+ */
170
+ peer -> peer_in = tal_arr (peer , u8 , CRYPTOMSG_HDR_SIZE );
171
+ return io_read (peer_conn , peer -> peer_in , CRYPTOMSG_HDR_SIZE ,
172
+ read_body_from_peer , peer );
151
173
}
152
174
153
175
static struct io_plan * subd_conn_init (struct io_conn * subd_conn , struct peer * peer )
@@ -200,7 +222,7 @@ struct io_plan *multiplex_peer_setup(struct io_conn *peer_conn,
200
222
tal_add_destructor2 (peer_conn , destroy_peer_conn , peer );
201
223
202
224
return io_duplex (peer_conn ,
203
- read_from_peer (peer_conn , peer ),
225
+ read_hdr_from_peer (peer_conn , peer ),
204
226
write_to_peer (peer_conn , peer ));
205
227
}
206
228
0 commit comments