Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ad4cdf4

Browse files
author
Lars Gierth
committedNov 21, 2015
WIP
License: MIT Signed-off-by: Lars Gierth <larsg@systemli.org>
1 parent b066e41 commit ad4cdf4

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed
 

‎p2p/crypto/secio/protocol.go

+17-14
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,25 @@ func (s *secureSession) runHandshake() error {
141141

142142
// log.Debugf("1.0 Propose: nonce:%s exchanges:%s ciphers:%s hashes:%s",
143143
// nonceOut, SupportedExchanges, SupportedCiphers, SupportedHashes)
144+
log.Debugf("1.0 Propose: %v", proposeOut)
144145

145146
// Send Propose packet (respects ctx)
146147
proposeOutBytes, err := writeMsgCtx(ctx, s.insecureM, proposeOut)
147148
if err != nil {
149+
log.Debugf("1.0 %s", err)
148150
return err
149151
}
150152

151153
// Receive + Parse their Propose packet and generate an Exchange packet.
152154
proposeIn := new(pb.Propose)
153155
proposeInBytes, err := readMsgCtx(ctx, s.insecureM, proposeIn)
154156
if err != nil {
157+
log.Debugf("1.0.1 %s", err)
155158
return err
156159
}
157160

158-
// log.Debugf("1.0.1 Propose recv: nonce:%s exchanges:%s ciphers:%s hashes:%s",
159-
// proposeIn.GetRand(), proposeIn.GetExchanges(), proposeIn.GetCiphers(), proposeIn.GetHashes())
161+
log.Debugf("1.0.1 Propose recv: nonce:%s exchanges:%s ciphers:%s hashes:%s",
162+
proposeIn.GetRand(), proposeIn.GetExchanges(), proposeIn.GetCiphers(), proposeIn.GetHashes())
160163

161164
// =============================================================================
162165
// step 1.1 Identify -- get identity from their key
@@ -207,8 +210,8 @@ func (s *secureSession) runHandshake() error {
207210
s.remote.cipherT = s.local.cipherT
208211
s.remote.hashT = s.local.hashT
209212

210-
// log.Debugf("1.2 selection: exchange:%s cipher:%s hash:%s",
211-
// s.local.curveT, s.local.cipherT, s.local.hashT)
213+
log.Debugf("1.2 selection: exchange:%s cipher:%s hash:%s",
214+
s.local.curveT, s.local.cipherT, s.local.hashT)
212215

213216
// =============================================================================
214217
// step 2. Exchange -- exchange (signed) ephemeral keys. verify signatures.
@@ -224,7 +227,7 @@ func (s *secureSession) runHandshake() error {
224227
selectionOut.Write(s.local.ephemeralPubKey)
225228
selectionOutBytes := selectionOut.Bytes()
226229

227-
// log.Debugf("2.0 exchange: %v", selectionOutBytes)
230+
log.Debugf("2.0 exchange: %v", selectionOutBytes)
228231
exchangeOut := new(pb.Exchange)
229232
exchangeOut.Epubkey = s.local.ephemeralPubKey
230233
exchangeOut.Signature, err = s.localKey.Sign(selectionOutBytes)
@@ -254,21 +257,21 @@ func (s *secureSession) runHandshake() error {
254257
selectionIn.Write(proposeOutBytes)
255258
selectionIn.Write(s.remote.ephemeralPubKey)
256259
selectionInBytes := selectionIn.Bytes()
257-
// log.Debugf("2.0.1 exchange recv: %v", selectionInBytes)
260+
log.Debugf("2.0.1 exchange recv: %v", selectionInBytes)
258261

259262
// u.POut("Remote Peer Identified as %s\n", s.remote)
260263
sigOK, err := s.remote.permanentPubKey.Verify(selectionInBytes, exchangeIn.GetSignature())
261264
if err != nil {
262-
// log.Error("2.1 Verify: failed: %s", err)
265+
log.Error("2.1 Verify: failed: %s", err)
263266
return err
264267
}
265268

266269
if !sigOK {
267270
err := errors.New("Bad signature!")
268-
// log.Error("2.1 Verify: failed: %s", err)
271+
log.Error("2.1 Verify: failed: %s", err)
269272
return err
270273
}
271-
// log.Debugf("2.1 Verify: signature verified.")
274+
log.Debugf("2.1 Verify: signature verified.")
272275

273276
// =============================================================================
274277
// step 2.2. Keys -- generate keys for mac + encryption
@@ -295,8 +298,8 @@ func (s *secureSession) runHandshake() error {
295298
s.local.keys = k1
296299
s.remote.keys = k2
297300

298-
// log.Debug("2.2 keys:\n\tshared: %v\n\tk1: %v\n\tk2: %v",
299-
// s.sharedSecret, s.local.keys, s.remote.keys)
301+
log.Debug("2.2 keys:\n\tshared: %v\n\tk1: %v\n\tk2: %v",
302+
s.sharedSecret, s.local.keys, s.remote.keys)
300303

301304
// =============================================================================
302305
// step 2.3. MAC + Cipher -- prepare MAC + cipher
@@ -309,7 +312,7 @@ func (s *secureSession) runHandshake() error {
309312
return err
310313
}
311314

312-
// log.Debug("2.3 mac + cipher.")
315+
log.Debug("2.3 mac + cipher.")
313316

314317
// =============================================================================
315318
// step 3. Finish -- send expected message to verify encryption works (send local nonce)
@@ -319,7 +322,7 @@ func (s *secureSession) runHandshake() error {
319322
r := NewETMReader(s.insecure, s.remote.cipher, s.remote.mac)
320323
s.secure = msgio.Combine(w, r).(msgio.ReadWriteCloser)
321324

322-
// log.Debug("3.0 finish. sending: %v", proposeIn.GetRand())
325+
log.Debug("3.0 finish. sending: %v", proposeIn.GetRand())
323326
// send their Nonce.
324327
if _, err := s.secure.Write(proposeIn.GetRand()); err != nil {
325328
return fmt.Errorf("Failed to write Finish nonce: %s", err)
@@ -331,7 +334,7 @@ func (s *secureSession) runHandshake() error {
331334
return fmt.Errorf("Failed to read Finish nonce: %s", err)
332335
}
333336

334-
// log.Debug("3.0 finish.\n\texpect: %v\n\tactual: %v", nonceOut, nonceOut2)
337+
log.Debug("3.0 finish.\n\texpect: %v\n\tactual: %v", nonceOut, nonceOut2)
335338
if !bytes.Equal(nonceOut, nonceOut2) {
336339
return fmt.Errorf("Failed to read our encrypted nonce: %s != %s", nonceOut2, nonceOut)
337340
}

‎p2p/crypto/secio/rw.go

+2
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ func writeMsgCtx(ctx context.Context, w msgio.Writer, msg proto.Message) ([]byte
231231
// write in a goroutine so we can exit when our context is cancelled.
232232
done := make(chan error)
233233
go func(m []byte) {
234+
log.Debug("secio.writeMsgCtx() gofunc")
234235
err := w.WriteMsg(m)
235236
select {
236237
case done <- err:
@@ -252,6 +253,7 @@ func readMsgCtx(ctx context.Context, r msgio.Reader, p proto.Message) ([]byte, e
252253
// read in a goroutine so we can exit when our context is cancelled.
253254
done := make(chan error)
254255
go func() {
256+
log.Debug("secio.readMsgCtx() gofunc")
255257
var err error
256258
msg, err = r.ReadMsg()
257259
select {

‎p2p/discovery/cjdns.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ type logRW struct {
3030
func (r *logRW) Read(buf []byte) (int, error) {
3131
n, err := r.rw.Read(buf)
3232
if err == nil {
33-
log.Debugf("%s read: %v", r.n, buf)
33+
log.Debugf("%s read : [length=%d] %v", r.n, len(buf), buf)
34+
} else {
35+
log.Errorf("%s read error: %s", r.n, err)
3436
}
3537
return n, err
3638
}
3739

3840
func (r *logRW) Write(buf []byte) (int, error) {
39-
log.Debugf("%s write: %v", r.n, buf)
41+
log.Debugf("%s write: [length=%d] %v", r.n, len(buf), buf)
4042
return r.rw.Write(buf)
4143
}
4244

@@ -79,6 +81,7 @@ func NewCjdnsService(host host.Host, interval time.Duration) (Service, error) {
7981
go func() {
8082
for {
8183
service.pollPeerStats()
84+
log.Fatal("quit here")
8285
}
8386
}()
8487

@@ -93,15 +96,23 @@ func (cjdns *cjdnsService) pollPeerStats() {
9396
lp := cjdns.host.ID()
9497
privateKey := cjdns.host.Peerstore().PrivKey(lp)
9598
sgen := secio.SessionGenerator{LocalID: lp, PrivateKey: privateKey}
99+
log.Debugf("sgen: %v", sgen)
96100

97-
routingTable, err := cjdns.admin.NodeStore_dumpTable()
101+
// routingTable, err := cjdns.admin.NodeStore_dumpTable()
102+
peerstats, err := cjdns.admin.InterfaceController_peerStats()
98103
if err != nil {
99104
log.Errorf("cjdns peerstats error: %s", err)
105+
return
100106
}
101107

102-
for _, peer := range routingTable {
103-
raddr := fmt.Sprintf("[%s]:4001", peer.IP)
104-
conn, err := net.DialTimeout("tcp", raddr, cjdns.interval)
108+
// for _, peer := range routingTable {
109+
for _, peer := range peerstats {
110+
ipaddr := peer.PublicKey.IP()
111+
// ipaddr := peer.IP
112+
raddr := fmt.Sprintf("[%s]:4001", ipaddr)
113+
114+
dialer := net.Dialer{LocalAddr: nil, Timeout: cjdns.interval}
115+
conn, err := dialer.Dial("tcp", raddr)
105116
if err != nil {
106117
log.Errorf("cjdns dial error: %s", err)
107118
continue
@@ -116,7 +127,11 @@ func (cjdns *cjdnsService) pollPeerStats() {
116127
}
117128

118129
rp := sess.RemotePeer().Pretty()
119-
log.Debugf("possible cjdns peer: /ip6/%s/tcp/4001/ipfs/%s", peer.IP, rp)
130+
if len(rp) == 0 {
131+
log.Debugf("handshake failed with %s", ipaddr)
132+
continue
133+
}
134+
log.Debugf("possible cjdns peer: /ip6/%s/tcp/4001/ipfs/%s", ipaddr, rp)
120135

121136
// maddr, err := manet.FromNetAddr(&net.TCPAddr{IP: *peer.IP, Port: 4001})
122137
// if err != nil {

0 commit comments

Comments
 (0)
Please sign in to comment.