github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/lnp2p/listen.go (about) 1 package lnp2p 2 3 import ( 4 "github.com/mit-dci/lit/eventbus" 5 "github.com/mit-dci/lit/lndc" 6 "github.com/mit-dci/lit/logging" 7 ) 8 9 type listeningthread struct { 10 listener *lndc.Listener 11 } 12 13 func acceptConnections(listener *lndc.Listener, port int, pm *PeerManager) { 14 15 // Set this up in-advance. 16 stopEvent := &StopListeningPortEvent{ 17 Port: port, 18 Reason: "panic", 19 } 20 21 // Do this now in case we panic so we can do cleanup. 22 defer publishStopEvent(stopEvent, pm.ebus) 23 24 // Actually start listening for connections. 25 for { 26 27 netConn, err := listener.Accept() 28 if err != nil { 29 if err.Error() != "EOF" { 30 logging.Infof("error accepting connections, exiting: %s\n", err.Error()) 31 break // usually means the socket was closed 32 } else { 33 logging.Debugf("got EOF on accepting connection, ignoring...\n") 34 continue // the testing framework generates EOFs, this is fine 35 } 36 } 37 38 lndcConn, ok := netConn.(*lndc.Conn) 39 if !ok { 40 // this should never happen 41 logging.Errorf("didn't get an lndc connection from listener, wtf?\n") 42 netConn.Close() 43 continue 44 } 45 46 rpk := pubkey(lndcConn.RemotePub()) 47 rlitaddr := convertPubkeyToLitAddr(rpk) 48 rnetaddr := lndcConn.RemoteAddr() 49 50 // Make sure we can't let ourself connect to ourself. 51 if string(rlitaddr) == pm.GetExternalAddress() { 52 logging.Infof("peermgr: Got a connection from ourselves? Dropping.") 53 lndcConn.Close() 54 continue 55 } 56 57 logging.Infof("peermgr: New connection from %s at %s\n", rlitaddr, rnetaddr.String()) 58 59 p, err := pm.handleNewConnection(lndcConn, rlitaddr) 60 if err != nil { 61 logging.Warnf("%s\n", err.Error()) 62 continue 63 } 64 65 // Start a goroutine to process inbound traffic for this peer. 66 go processConnectionInboundTraffic(p, pm) 67 68 } 69 70 // Update the stop reason. 71 stopEvent.Reason = "closed" 72 73 // Then delete the entry from listening ports. 74 pm.mtx.Lock() 75 delete(pm.listeningPorts, port) 76 pm.mtx.Unlock() 77 78 // after this the stop event will be published 79 logging.Infof("Stopped listening on %s\n", port) 80 81 } 82 83 func processConnectionInboundTraffic(peer *Peer, pm *PeerManager) { 84 85 // Set this up in-advance. 86 dcEvent := &PeerDisconnectEvent{ 87 Peer: peer, 88 Reason: "panic", 89 } 90 91 // Do this now in case we panic so we can do cleanup. 92 defer publishDisconnectEvent(dcEvent, pm.ebus) 93 94 // TODO Have chanmgr deal with channels after peer connection brought up. (eventbus) 95 96 for { 97 98 // Make a buf and read into it. 99 buf := make([]byte, 1<<24) 100 n, err := peer.conn.Read(buf) 101 if err != nil { 102 logging.Warnf("Error reading from peer: %s\n", err.Error()) 103 peer.conn.Close() 104 return 105 } 106 107 logging.Debugf("Got message of len %d from peer %s\n", n, peer.GetLnAddr()) 108 109 // Send to the message processor. 110 err = pm.mproc.HandleMessage(peer, buf[:n]) 111 if err != nil { 112 logging.Errorf("Error proccessing message: %s\n", err.Error()) 113 } 114 115 } 116 117 } 118 119 func publishStopEvent(event *StopListeningPortEvent, ebus *eventbus.EventBus) { 120 ebus.Publish(*event) 121 } 122 123 func publishDisconnectEvent(event *PeerDisconnectEvent, ebus *eventbus.EventBus) { 124 ebus.Publish(*event) 125 }