]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/cmd/nncp-daemon/main.go
nncp-daemon's UCSPI-TCP compatibility
[nncp.git] / src / cmd / nncp-daemon / main.go
index b0628fe421a39c8ab37f21e8cb6f733d79b343c8..f27c2a371461b0dad558509be8a09f670c75b507 100644 (file)
@@ -40,28 +40,28 @@ func usage() {
        flag.PrintDefaults()
 }
 
-type InetdConn struct {
+type UCSPIConn struct {
        r *os.File
        w *os.File
 }
 
-func (c InetdConn) Read(p []byte) (n int, err error) {
+func (c UCSPIConn) Read(p []byte) (n int, err error) {
        return c.r.Read(p)
 }
 
-func (c InetdConn) Write(p []byte) (n int, err error) {
+func (c UCSPIConn) Write(p []byte) (n int, err error) {
        return c.w.Write(p)
 }
 
-func (c InetdConn) SetReadDeadline(t time.Time) error {
+func (c UCSPIConn) SetReadDeadline(t time.Time) error {
        return c.r.SetReadDeadline(t)
 }
 
-func (c InetdConn) SetWriteDeadline(t time.Time) error {
+func (c UCSPIConn) SetWriteDeadline(t time.Time) error {
        return c.w.SetWriteDeadline(t)
 }
 
-func (c InetdConn) Close() error {
+func (c UCSPIConn) Close() error {
        if err := c.r.Close(); err != nil {
                c.w.Close() // #nosec G104
                return err
@@ -138,7 +138,8 @@ func main() {
                cfgPath   = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
                niceRaw   = flag.String("nice", nncp.NicenessFmt(255), "Minimal required niceness")
                bind      = flag.String("bind", "[::]:5400", "Address to bind to")
-               inetd     = flag.Bool("inetd", false, "Is it started as inetd service")
+               ucspi     = flag.Bool("ucspi", false, "Is it started as UCSPI-TCP server")
+               inetd     = flag.Bool("inetd", false, "Obsolete, use -ucspi")
                maxConn   = flag.Int("maxconn", 128, "Maximal number of simultaneous connections")
                noCK      = flag.Bool("nock", false, "Do no checksum checking")
                mcdOnce   = flag.Bool("mcd-once", false, "Send MCDs once and quit")
@@ -174,6 +175,9 @@ func main() {
        if err != nil {
                log.Fatalln(err)
        }
+       if *inetd {
+               *ucspi = true
+       }
 
        ctx, err := nncp.CtxFromCmdline(
                *cfgPath,
@@ -192,11 +196,20 @@ func main() {
        }
        ctx.Umask()
 
-       if *inetd {
+       if *ucspi {
                os.Stderr.Close() // #nosec G104
-               conn := &InetdConn{os.Stdin, os.Stdout}
+               conn := &UCSPIConn{os.Stdin, os.Stdout}
                nodeIdC := make(chan *nncp.NodeId)
-               go performSP(ctx, conn, "PIPE", nice, *noCK, nodeIdC)
+               addr := "PIPE"
+               if proto := os.Getenv("PROTO"); proto == "TCP" {
+                       port := os.Getenv("TCPREMOTEPORT")
+                       if host := os.Getenv("TCPREMOTEHOST"); host == "" {
+                               addr = fmt.Sprintf("[%s]:%s", os.Getenv("TCPREMOTEIP"), port)
+                       } else {
+                               addr = fmt.Sprintf("%s:%s", host, port)
+                       }
+               }
+               go performSP(ctx, conn, addr, nice, *noCK, nodeIdC)
                nodeId := <-nodeIdC
                var autoTossFinish chan struct{}
                var autoTossBadCode chan bool