summaryrefslogtreecommitdiff
path: root/src/org/gnu/clojure/gnunet/transport.clj
blob: ac1f9233f67e81c523eeb64c38b54c5d06e6538c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
(ns org.gnu.clojure.gnunet.transport
  (:use (org.gnu.clojure.gnunet core crypto exception hello message metrics
          parser peer util)
    clojure.contrib.monads)
  (:import (java.util Date Calendar)))

(defn hello-address-expiration
  []
  (.getTime (doto (Calendar/getInstance) (.add Calendar/HOUR_OF_DAY 12))))

(defn pong-expiration
  []
  (.getTime (doto (Calendar/getInstance) (.add Calendar/HOUR_OF_DAY 1))))

(defn idle-connection-timeout
  []
  (.getTime (doto (Calendar/getInstance) (.add Calendar/MINUTE 5))))

(def message-type-ping 32)
(def message-type-pong 33)

(def signature-purpose-pong-own 1)
(def signature-purpose-pong-using 2)

(defn encode-ping
  [ping]
  (concat
    (encode-int32 (:challenge ping))
    (:peer-id ping)
    (if-let [transport (:transport ping)]
      (concat
        (encode-utf8 (:transport ping))
        (:encoded-address ping)))))

(def parse-ping
  (domonad parser-m
    [challenge parse-uint32
     peer-id (items id-size)
     transport (optional parse-utf8)
     encoded-address (none-or-more item)]
    {:challenge challenge
     :peer-id peer-id
     :transport transport
     :encoded-address encoded-address}))

(defn encode-pong-signed-material
  [pong]
  (let [transport (encode-utf8 (:transport pong))
        address-length (+ (count transport) (count (:encoded-address pong)))]
    (encode-signed (:signature-purpose pong)
      (concat
        (encode-date (:expiration pong))
        (:peer-id pong)
        (encode-int32 address-length)
        transport
        (:encoded-address pong)))))

(defn encode-pong
  [pong signed-material]
  (concat
    (encode-int32 (:challenge pong))
    (:signature pong)
    signed-material))

(def parse-pong
  (domonad parser-m
    [challenge parse-int32
     signature (items signature-size)
     signed (parse-signed
              (domonad parser-m
                [expiration parse-date
                 peer-id (items id-size)
                 address-length parse-uint32
                 transport (none-or-more (satisfy #(not (== 0 %))))
                 zero item
                 :let [transport-length (inc (count transport))]
                 :when (>= address-length transport-length)
                 encoded-address (items (- address-length transport-length))]
                {:expiration expiration
                 :peer-id peer-id
                 :transport (String. (byte-array transport) "UTF-8")
                 :encoded-address encoded-address}))
     :let [signature-purpose (:purpose signed)]
     :when (or (== signature-purpose signature-purpose-pong-own)
             (== signature-purpose signature-purpose-pong-using))
     residue (optional item)
     :when (nil? residue)]
    (conj {:challenge challenge
           :signature signature
           :signature-purpose signature-purpose
           :signed-material (:signed-material signed)}
      (:parsed signed))))

(defn list-transport-addresses
  "Generate a list of transport descriptions."
  [addresses-map]
  (for [transport addresses-map
        address (val transport)]
    (conj
      {:transport (key transport) :encoded-address (key address)}
      (val address))))

(defn merge-address-info
  "Merge two address-info maps."
  [a b]
  (conj a b {:expiration (my-max (:expiration a) (:expiration b))}))

(defn merge-transport-addresses
  "Merge a list of transport descriptions into a transports-agent map. The input
   list is generated from parse-hello or list-transports. The input map is
   described in peer.clj."
  [address-map address-list]
  (reduce (fn [address-map new-address]
            (if-let [transport (address-map (:transport new-address))]
              (if-let [address-info (transport (:encoded-address new-address))]
                (assoc address-map (:transport new-address)
                  (assoc transport (:encoded-address new-address)
                    (merge-address-info address-info
                      (dissoc new-address :transport :encoded-address))))
                (assoc address-map (:transport new-address)
                  (assoc transport (:encoded-address new-address)
                    (dissoc new-address :transport :encoded-address))))
              (assoc address-map (:transport new-address)
                {(:encoded-address new-address)
                 (dissoc new-address :transport :encoded-address)})))
    address-map
    address-list))

(defn expire-transport-addresses
  [min-expiration addresses-list]
  (filter #(not (.after min-expiration (:expiration %))) addresses-list))

(defn hello-for-peer-message
  [peer]
  {:message-type message-type-hello
   :bytes (encode-hello
            {:public-key (deref (:public-key-atom peer))
             :transport-addresses (list-transport-addresses
                                    (deref
                                      (:transport-addresses-agent peer)))})})

(defn ping-message
  [remote-peer address challenge]
  {:message-type message-type-ping
   :bytes (encode-ping {:challenge challenge
                        :peer-id (:id remote-peer)
                        :transport (:transport address)
                        :encoded-address (:encoded-address address)})})

(defn update-transport-addresses
  [addresses new-addresses]
  (merge-transport-addresses {}
    (expire-transport-addresses (Date.)
      (concat (list-transport-addresses addresses) new-addresses))))

(defn update-remote-peers
  [remote-peers peer-id hello]
  (let [remote-peer (remote-peers peer-id)]
    (if remote-peer
      (do
        (if (:public-key hello)
          (swap! (:public-key-atom remote-peer)
            #(if (nil? %) (:public-key hello))))
        (send (:transport-addresses-agent remote-peer)
          update-transport-addresses
          (:transport-addresses hello))
        remote-peers)
      (assoc remote-peers peer-id
        (struct-map remote-peer-struct
          :public-key-atom (atom (:public-key hello))
          :id peer-id
          :transport-addresses-agent (agent
                                       (merge-transport-addresses {}
                                         (:transport-addresses hello)))
          :state-agent (agent {:is-connected false}))))))

(defn verify-transport-address
  [peer remote-peer address]
  (second
    ((domonad exception-m
       [address (fetch-state)
        :when (not (or (contains? address :latency)
                     (contains? address :send-time)))
        :when-let [transport ((deref (:transports-agent peer))
                               (:transport address))]
        :let [challenge (.nextInt (:random peer))]
        _ (set-state
            (conj address
              {:send-time (Date.)    ;; TODO: Now is not the actual send time.
               :challenge challenge}))]
       ((:emit-messages! transport) transport remote-peer
         (:encoded-address address) nil
         [(hello-for-peer-message peer)
          (ping-message remote-peer address challenge)])) address)))

(defn verify-transport-addresses
  [addresses peer remote-peer]
  (merge-transport-addresses {}
    (map (partial verify-transport-address peer remote-peer)
      (list-transport-addresses addresses))))

(defn verify-remote-peers
  [remote-peers peer]
  (doseq [[_ remote-peer] remote-peers]
    (send (:transport-addresses-agent remote-peer)
      verify-transport-addresses peer remote-peer))
  remote-peers)

(defn admit-hello!
  "Updates peer:remote-peers-agent with new information contained in hello."
  [peer hello]
  (let [peer-id (generate-id (:public-key hello))]
    (if (not (= peer-id (:id peer)))
      (let [remote-peers-agent (:remote-peers-agent peer)]
        (send remote-peers-agent update-remote-peers peer-id hello)
        (send remote-peers-agent verify-remote-peers peer)))))

(defn handle-hello!
  [peer message]
  (when-let [hello (first (parse-hello (:bytes message)))]
    (admit-hello! peer hello)))

(defn send-pong-own!
  [peer remote-peer ping]
  (.execute (:cpu-bound-executor peer)
    (fn []
      (domonad maybe-m
        [transport-addresses ((deref (:transport-addresses-agent peer))
                               (:transport ping))
         ;; XXX: Here we're looking for an exact match, gnunet allows transport
         ;; plugins to do inexact matches.
         :when (contains? transport-addresses (:encoded-address ping))]
        (let [pong {:challenge (:challenge ping)
                    :signature-purpose signature-purpose-pong-own
                    :expiration (pong-expiration)
                    :peer-id (:id peer)
                    :transport (:transport ping)
                    :encoded-address (:encoded-address ping)}
              signed-material (encode-pong-signed-material pong)
              signature (rsa-sign (:private-key peer) signed-material)
              pong (assoc pong :signature signature)
              encoded-pong (encode-pong pong signed-material)]
          ;; XXX: gnunet looks for a "reliable" connection for the pong before
          ;; sending to every known address.
          (doseq [transports (deref (:transport-addresses-agent remote-peer))
                  address (val transports)]
            (when-let [transport ((deref (:transports-agent peer))
                                   (key transports))]
              ((:emit-messages! transport) transport remote-peer (key address)
                nil
                [{:message-type message-type-pong :bytes encoded-pong}]))))))))

(defn send-pong-using!
  [peer remote-peer ping]
  (.write *out* "We don't send PONG_USING yet!\n"))

(defn handle-ping!
  [peer remote-peer message]
  (when-let [ping (first (parse-ping (:bytes message)))]
    (cond
      (not (= (:peer-id ping) (seq (:id peer)))) nil
      (:transport ping) (send-pong-own! peer remote-peer ping)
      :else (send-pong-using! peer remote-peer ping))))

(defn handle-pong-own!
  [peer remote-peer pong]
  (.execute (:cpu-bound-executor peer)
    (fn []
      (domonad maybe-m
        [public-key (deref (:public-key-atom remote-peer))
         :when (rsa-verify public-key (:signed-material pong)
                 (:signature pong))]
        (send-do-exception-m! (:transport-addresses-agent remote-peer)
          [address (with-state-field (:transport pong)
                     (fetch-val (:encoded-address pong)))
           :when (= (:challenge address) (:challenge pong))
           _ (with-state-field (:transport pong)
               (set-val (:encoded-address pong)
                 {:expiration (hello-address-expiration)
                  :latency (- (.getTime (Date.))
                             (.getTime (:send-time address)))}))]
          (metric-add peer "Peer addresses considered valid" 1))))))

(defn handle-pong-using!
  [peer remote-peer pong]
  ;; TODO - fill in this case
  (.write *out* "We don't handle PONG_USING yet!\n"))
  
(defn handle-pong!
  [peer message]
  (domonad maybe-m
    [pong (first (parse-pong (:bytes message)))
     :when-not (.after (Date.) (:expiration pong))
     remote-peer ((deref (:remote-peers-agent peer)) (:peer-id pong))]
    (condp == (:signature-purpose pong)
      signature-purpose-pong-own (handle-pong-own! peer remote-peer pong)
      signature-purpose-pong-using (handle-pong-using! peer remote-peer pong)
      nil)))

(defn emit-continuation!
  [peer transport remote-peer encoded-address result]
  (if result
    (let [addresses ((deref (:transport-addresses-agent remote-peer))
                      (:name transport))
          address (addresses encoded-address)]
      (if (contains? address :latency)
        (send (:state-agent remote-peer)
          (fn [state]
            (conj state {:is-connected true
                         :connected-transport transport
                         :connected-address encoded-address})))))))

(defn admit-message!
  [peer sender-id address message]
  ;; (.write *out* (str "Received " message "\n"))
  (send (:remote-peers-agent peer)
    (fn [remote-peers]
      (let [remote-peers (update-remote-peers remote-peers
                           sender-id {:transport-addresses [address]})
            remote-peer (remote-peers sender-id)]
        (condp = (:message-type message)
          message-type-hello (handle-hello! peer message)
          message-type-ping (handle-ping! peer remote-peer message)
          message-type-pong (handle-pong! peer message)
          (handle-receive! peer remote-peer message))
        remote-peers))))