summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2010-11-20 21:43:03 -0800
committerDavid Barksdale <amatus.amongus@gmail.com>2010-11-20 21:43:03 -0800
commit10ffdbf1363507c250c1148340063a8e0741a8ac (patch)
tree2924ac37821827a845e1beb8001f7e3965487a9d /src
parent34eda9478fbe9ec22c763afdc8c871836a1a60fd (diff)
We can forward FS_GET requests, but not very intelligently. FS_PUT are dropped.
Diffstat (limited to 'src')
-rw-r--r--src/org/gnu/clojure/gnunet/bloomfilter.clj4
-rw-r--r--src/org/gnu/clojure/gnunet/core.clj65
-rw-r--r--src/org/gnu/clojure/gnunet/filesharing.clj69
-rw-r--r--src/org/gnu/clojure/gnunet/peer.clj1
4 files changed, 111 insertions, 28 deletions
diff --git a/src/org/gnu/clojure/gnunet/bloomfilter.clj b/src/org/gnu/clojure/gnunet/bloomfilter.clj
index cd9dd33..c7b1531 100644
--- a/src/org/gnu/clojure/gnunet/bloomfilter.clj
+++ b/src/org/gnu/clojure/gnunet/bloomfilter.clj
@@ -10,6 +10,10 @@
:size size
:k k})
+(defn encode-bloomfilter
+ [bloomfilter]
+ {:bitmap bloomfilter})
+
(defn parse-bloomfilter
[k]
(domonad parser-m
diff --git a/src/org/gnu/clojure/gnunet/core.clj b/src/org/gnu/clojure/gnunet/core.clj
index 32e48a7..a9ee858 100644
--- a/src/org/gnu/clojure/gnunet/core.clj
+++ b/src/org/gnu/clojure/gnunet/core.clj
@@ -81,10 +81,10 @@
(encode-utf8 "authentication key"))))
(defn encode-core-ping
- [ping aes-key remote-peer-id]
- (let [iv (derive-iv aes-key (:iv-seed ping) remote-peer-id)]
+ [ping aes-key iv-seed remote-peer-id]
+ (let [iv (derive-iv aes-key iv-seed remote-peer-id)]
(concat
- (encode-int32 (:iv-seed ping))
+ (encode-int32 iv-seed)
(aes-encrypt aes-key iv
(concat
(:peer-id ping)
@@ -107,11 +107,10 @@
ping))
(defn encode-core-pong
- [pong aes-key remote-peer-id]
- (let [iv (derive-pong-iv aes-key (:iv-seed pong) (:challenge pong)
- remote-peer-id)]
+ [pong aes-key iv-seed remote-peer-id]
+ (let [iv (derive-pong-iv aes-key iv-seed (:challenge pong) remote-peer-id)]
(concat
- (encode-int32 (:iv-seed pong))
+ (encode-int32 iv-seed)
(aes-encrypt aes-key iv
(concat
(encode-int32 (:challenge pong))
@@ -136,6 +135,22 @@
:when pong]
pong))
+(defn encode-core-encrypted-message
+ [message aes-key iv-seed aes-key-created remote-peer-id]
+ (let [plaintext (concat
+ (encode-int32 (:sequence-number message))
+ (encode-int32 (:inbound-bw-limit message))
+ (encode-date (:timestamp message))
+ (mapcat encode-message (:messages message)))
+ iv (derive-iv aes-key iv-seed remote-peer-id)
+ ciphertext (aes-encrypt aes-key iv plaintext)
+ auth-key (derive-auth-key aes-key iv-seed aes-key-created)
+ hmac (hmac-sha-512 auth-key ciphertext)]
+ (concat
+ (encode-int32 iv-seed)
+ hmac
+ ciphertext)))
+
(defn parse-core-encrypted-message
[aes-key aes-key-created peer-id]
(domonad parser-m
@@ -192,10 +207,9 @@
set-key (assoc set-key :signature signature)
encoded-set-key (encode-set-key set-key signed-material)
iv-seed (.nextInt (:random peer))
- ping {:iv-seed iv-seed
- :challenge challenge
+ ping {:challenge challenge
:peer-id (:id remote-peer)}
- encoded-ping (encode-core-ping ping encrypt-key
+ encoded-ping (encode-core-ping ping encrypt-key iv-seed
(:id remote-peer))]
(emit-messages! peer remote-peer
[{:message-type message-type-core-set-key :bytes encoded-set-key}
@@ -252,11 +266,10 @@
(let [bw-in (:bw-in state)
encrypt-key (:encrypt-key state)
iv-seed (.nextInt (:random peer))
- pong {:iv-seed iv-seed
- :challenge (:challenge ping)
+ pong {:challenge (:challenge ping)
:inbound-bw-limit bw-in
:peer-id (:id peer)}
- encoded-pong (encode-core-pong pong encrypt-key
+ encoded-pong (encode-core-pong pong encrypt-key iv-seed
(:id remote-peer))]
(emit-messages! peer remote-peer
[{:message-type message-type-core-pong :bytes encoded-pong}]))))))
@@ -327,7 +340,8 @@
:encrypt-key-created (Date.)
:ping-challenge (.nextInt (:random peer))
;; TODO: Make this a real number
- :bw-in 20000}))
+ :bw-in 20000
+ :next-sequence-number 1}))
(defn handle-receive!
[peer remote-peer message]
@@ -345,3 +359,26 @@
message-type-core-pong (handle-core-pong! peer remote-peer message)
nil)
state))))
+
+(defn core-send!
+ [peer remote-peer message]
+ (send-do-exception-m! (:state-agent remote-peer)
+ [status (fetch-val :status)
+ :when (== status peer-status-key-confirmed)
+ encrypt-key (fetch-val :encrypt-key)
+ encrypt-key-created (fetch-val :encrypt-key-created)
+ bw-in (fetch-val :bw-in)
+ seq-num (update-val :next-sequence-number inc)]
+ (.execute (:cpu-bound-executor peer)
+ (fn []
+ (let [iv-seed (.nextInt (:random peer))
+ core-message {:sequence-number seq-num
+ :inbound-bw-limit bw-in
+ :timestamp (Date.)
+ :messages [message]}
+ encoded-message (encode-core-encrypted-message core-message
+ encrypt-key iv-seed encrypt-key-created
+ (:id remote-peer))]
+ (emit-messages! peer remote-peer
+ [{:messsage-type message-type-core-encrypted-message
+ :bytes encoded-message}]))))))
diff --git a/src/org/gnu/clojure/gnunet/filesharing.clj b/src/org/gnu/clojure/gnunet/filesharing.clj
index 5b868e0..53fee2a 100644
--- a/src/org/gnu/clojure/gnunet/filesharing.clj
+++ b/src/org/gnu/clojure/gnunet/filesharing.clj
@@ -1,6 +1,6 @@
(ns org.gnu.clojure.gnunet.filesharing
- (:use (org.gnu.clojure.gnunet bloomfilter crypto exception message metrics
- parser peer)
+ (:use (org.gnu.clojure.gnunet bloomfilter core crypto exception message
+ metrics parser peer)
clojure.contrib.monads)
(:import (java.util Date PriorityQueue)
java.util.concurrent.TimeUnit))
@@ -17,6 +17,30 @@
(def ttl-max 1073741824)
(def max-pending-requests 32768)
+(defn encode-get-message
+ [get-message]
+ (let [hash-bitmap 0
+ hash-bitmap (if (nil? (:return-to get-message))
+ hash-bitmap
+ (bit-set hash-bitmap bit-return-to))
+ hash-bitmap (if (nil? (:sks-namespace get-message))
+ hash-bitmap
+ (bit-set hash-bitmap bit-sks-namespace))
+ hash-bitmap (if (nil? (:transmit-to get-message))
+ hash-bitmap
+ (bit-set hash-bitmap bit-transmit-to))]
+ (concat
+ (encode-int32 (:block-type get-message))
+ (encode-int32 (:priority get-message))
+ (encode-int32 (:ttl get-message))
+ (encode-int32 (:filter-mutator get-message))
+ (encode-int32 hash-bitmap)
+ (:return-to get-message)
+ (:sks-namespace get-message)
+ (:transmit-to get-message)
+ (when (:bloomfilter get-message)
+ (encode-bloomfilter (:bloomfilter get-message))))))
+
(def parse-get-message
(domonad parser-m
[block-type parse-int32
@@ -70,32 +94,48 @@
_ (set-val :trust (- trust priority))]
priority))
+(defn get-processing-delay!
+ [peer]
+ (long (+ 1000 (.nextInt (:random peer) ttl-decrement))))
+
(defn target-peer-select
- [query best candidate]
- (if (= (key candidate) (:return-to query))
+ [return-to-id best candidate]
+ (if (= return-to-id (key candidate))
best
;; TODO: come on, seriously?
candidate))
(defn forward-request!
- [peer query-id]
+ [peer query-id return-to-id]
(send-do-exception-m! (:state-agent peer)
[query (with-state-field :queries
- (fetch-val query-id))
+ (with-state-field query-id
+ (fetch-val return-to-id)))
:when-not (nil? query)
- :let [send-to (reduce (partial target-peer-select query) nil
+ :let [send-to (reduce (partial target-peer-select return-to-id) nil
(deref (:remote-peers-agent peer)))]
:when (if (nil? send-to)
(do (.schedule (:scheduled-executor peer)
- (partial forward-request! peer query-id)
- (+ 1000 (.nextInt (:random peer) ttl-decrement))
+ (partial forward-request! peer query-id return-to-id)
+ (get-processing-delay! peer)
TimeUnit/MILLISECONDS)
+ (metric-add peer
+ "Filesharing requests delayed, no suitable destination" 1)
false)
true)]
(send-do-exception-m! (:state-agent send-to)
[is-connected (fetch-val :is-connected)
- :when is-connected]
- nil)))
+ status (fetch-val :status)
+ :when (if (and is-connected (== status peer-status-key-confirmed))
+ true
+ (do (.schedule (:scheduled-executor peer)
+ (partial forward-request! peer query-id return-to-id)
+ (get-processing-delay! peer)
+ TimeUnit/MILLISECONDS)
+ false))]
+ (let [get-message (encode-get-message query)]
+ (core-send! peer send-to
+ {:message-type message-type-fs-get :bytes get-message})))))
(def ttl-comparator
(reify java.util.Comparator
@@ -126,7 +166,7 @@
(.nextInt (:random peer) ttl-decrement))]
:let [start-time (Date.)]]
(send-do-exception-m! (:state-agent peer)
- [queries (fetch-val :queries)
+ [queries (fetch-val :queries {})
:let [query (queries (:query get-message) {})]
:let [duplicate (query (:id return-to))]
:when (if (nil? duplicate)
@@ -139,7 +179,8 @@
{:priority priority
:ttl ttl
:start-time start-time
- :anonymity 1})))]
+ :anonymity 1
+ :return-to nil})))]
ttl-queue (fetch-val :ttl-queue (PriorityQueue. 1 ttl-comparator))
:let [_ (.add ttl-queue (with-meta [(:query get-message) (:id return-to)]
{:ttl (+ ttl (.getTime start-time))}))]
@@ -155,7 +196,7 @@
(dissoc queries (first expired))
(assoc queries (first expired) query))))]
_ (set-val :queries queries)]
- (forward-request! peer (:query get-message)))))
+ (forward-request! peer (:query get-message) (:id return-to)))))
(defn admit-put!
[peer remote-peer message])
diff --git a/src/org/gnu/clojure/gnunet/peer.clj b/src/org/gnu/clojure/gnunet/peer.clj
index f441e1d..0619088 100644
--- a/src/org/gnu/clojure/gnunet/peer.clj
+++ b/src/org/gnu/clojure/gnunet/peer.clj
@@ -31,6 +31,7 @@
;; :encrypt-key-created (java.util.Date)
;; :ping-challenge (int)
;; :bw-in (int)
+ ;; :last-sequence-number-sent (int)
;; (filesharing layer)
;; :trust (int)
;; :average-priority (float)