private static int r4_lookupPort(AbstractNode node) { int port = 0; try { OtpOutputStream obuf = new OtpOutputStream(); using (TcpClient s = new TcpClient(node.Host, EpmdPort.get())) { // build and send epmd request // length[2], tag[1], alivename[n] (length = n+1) obuf.write2BE(node.Alive.Length + 1); obuf.write1(port4req); obuf.writeN(Encoding.GetEncoding("iso-8859-1").GetBytes(node.Alive)); // send request obuf.WriteTo(s.GetStream()); if (traceLevel >= traceThreshold) { log.Debug("-> LOOKUP (r4) " + node); } // receive and decode reply // resptag[1], result[1], port[2], ntype[1], proto[1], // disthigh[2], distlow[2], nlen[2], alivename[n], // elen[2], edata[m] byte[] tmpbuf = new byte[100]; int n = s.GetStream().Read(tmpbuf, 0, tmpbuf.Length); if (n < 0) { // this was an r3 node => not a failure (yet) throw new IOException("Nameserver not responding on " + node.Host + " when looking up " + node.Alive); } OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); int response = ibuf.read1(); if (response == port4resp) { int result = ibuf.read1(); if (result == 0) { port = ibuf.read2BE(); node.Type = ibuf.read1(); node.Proto = ibuf.read1(); node.DistHigh = ibuf.read2BE(); node.DistLow = ibuf.read2BE(); // ignore rest of fields } } } } catch (SocketException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Nameserver not responding on " + node.Host); } catch (IOException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Nameserver not responding on " + node.Host + " when looking up " + node.Alive); } catch (OtpErlangDecodeException) { if (traceLevel >= traceThreshold) { log.Debug("<- (invalid response)"); } throw new IOException("Nameserver not responding on " + node.Host + " when looking up " + node.Alive); } if (traceLevel >= traceThreshold) { if (port == 0) { log.Debug("<- NOT FOUND"); } else { log.Debug("<- PORT " + port); } } return port; }
/* * this function will get an exception if it tries to talk to an r3 epmd, or * if something else happens that it cannot forsee. In both cases we return * an exception (and the caller should try again, using the r3 protocol). If * we manage to successfully communicate with an r4 epmd, we return either * the socket, or null, depending on the result. */ private static TcpClient r4_publish(OtpLocalNode node) { TcpClient s = null; try { OtpOutputStream obuf = new OtpOutputStream(); s = new TcpClient(node.Host, EpmdPort.get()); obuf.write2BE(node.Alive.Length + 13); obuf.write1(publish4req); obuf.write2BE(node.Port); obuf.write1(node.Type); obuf.write1(node.Proto); obuf.write2BE(node.DistHigh); obuf.write2BE(node.DistLow); obuf.write2BE(node.Alive.Length); obuf.writeN(Encoding.GetEncoding("iso-8859-1").GetBytes(node.Alive)); obuf.write2BE(0); // No extra // send request obuf.WriteTo(s.GetStream()); if (traceLevel >= traceThreshold) { log.Debug("-> PUBLISH (r4) " + node + " port=" + node.Port); } // get reply byte[] tmpbuf = new byte[100]; int n = s.GetStream().Read(tmpbuf, 0, tmpbuf.Length); if (n < 0) { // this was an r3 node => not a failure (yet) if (s != null) { s.Close(); } throw new IOException("Nameserver not responding on " + node.Host + " when publishing " + node.Alive); } OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); int response = ibuf.read1(); if (response == publish4resp) { int result = ibuf.read1(); if (result == 0) { node.Creation = ibuf.read2BE(); if (traceLevel >= traceThreshold) { log.Debug("<- OK"); } return s; // success } } } catch (SocketException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Nameserver not responding on " + node.Host); } catch (IOException) { // epmd closed the connection = fail if (s != null) { s.Close(); } if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Nameserver not responding on " + node.Host + " when publishing " + node.Alive); } catch (OtpErlangDecodeException) { if (s != null) { s.Close(); } if (traceLevel >= traceThreshold) { log.Debug("<- (invalid response)"); } throw new IOException("Nameserver not responding on " + node.Host + " when publishing " + node.Alive); } if (s != null) { s.Close(); } return null; }
private static TcpClient r3_publish(OtpLocalNode node) { TcpClient s; try { OtpOutputStream obuf = new OtpOutputStream(); s = new TcpClient(node.Host, EpmdPort.get()); obuf.write2BE(node.Alive.Length + 3); obuf.write1(publish3req); obuf.write2BE(node.Port); obuf.writeN(Encoding.GetEncoding("iso-8859-1").GetBytes(node.Alive)); // send request obuf.WriteTo(s.GetStream()); if (traceLevel >= traceThreshold) { log.Debug("-> PUBLISH (r3) " + node + " port=" + node.Port); } byte[] tmpbuf = new byte[100]; int n = s.GetStream().Read(tmpbuf, 0, tmpbuf.Length); if (n < 0) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } return null; } OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); if (ibuf.read1() == publish3ok) { node.Creation = ibuf.read2BE(); if (traceLevel >= traceThreshold) { log.Debug("<- OK"); } return s; // success - don't close socket } } catch (SocketException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Nameserver not responding on " + node.Host); } catch (IOException) { // epmd closed the connection = fail if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Nameserver not responding on " + node.Host + " when publishing " + node.Alive); } catch (OtpErlangDecodeException) { if (traceLevel >= traceThreshold) { log.Debug("<- (invalid response)"); } throw new IOException("Nameserver not responding on " + node.Host + " when publishing " + node.Alive); } return null; // failure }
private static int r3_lookupPort(AbstractNode node) { int port = 0; try { OtpOutputStream obuf = new OtpOutputStream(); using (TcpClient s = new TcpClient(node.Host, EpmdPort.get())) { // build and send epmd request // length[2], tag[1], alivename[n] (length = n+1) obuf.write2BE(node.Alive.Length + 1); obuf.write1(port3req); obuf.writeN(Encoding.GetEncoding("iso-8859-1").GetBytes(node.Alive)); // send request obuf.WriteTo(s.GetStream()); if (traceLevel >= traceThreshold) { log.Debug("-> LOOKUP (r3) " + node); } // receive and decode reply byte[] tmpbuf = new byte[100]; s.GetStream().Read(tmpbuf, 0, tmpbuf.Length); OtpInputStream ibuf = new OtpInputStream(tmpbuf, 0); port = ibuf.read2BE(); } } catch (SocketException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Nameserver not responding on " + node.Host); } catch (IOException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Nameserver not responding on " + node.Host + " when looking up " + node.Alive); } catch (OtpErlangDecodeException) { if (traceLevel >= traceThreshold) { log.Debug("<- (invalid response)"); } throw new IOException("Nameserver not responding on " + node.Host + " when looking up " + node.Alive); } if (traceLevel >= traceThreshold) { if (port == 0) { log.Debug("<- NOT FOUND"); } else { log.Debug("<- PORT " + port); } } return port; }
// Ask epmd to close his end of the connection. // Caller should close his epmd socket as well. // This method is pretty forgiving... /** * Unregister from Epmd. Other nodes wishing to connect will no longer be * able to. * * <p> * This method does not report any failures. */ public static void unPublishPort(OtpLocalNode node) { try { using (TcpClient s = new TcpClient(Dns.GetHostName(), EpmdPort.get())) { OtpOutputStream obuf = new OtpOutputStream(); obuf.write2BE(node.Alive.Length + 1); obuf.write1(stopReq); obuf.writeN(Encoding.GetEncoding("iso-8859-1").GetBytes(node.Alive)); obuf.WriteTo(s.GetStream()); // don't even wait for a response (is there one?) if (traceLevel >= traceThreshold) { log.Debug("-> UNPUBLISH " + node + " port=" + node.Port); log.Debug("<- OK (assumed)"); } } } catch (Exception) /* ignore all failures */ { } }
private void r4_port(TcpClient s, OtpInputStream ibuf) { try { int len = (int)(ibuf.Length - 1); byte[] alive = new byte[len]; ibuf.readN(alive); String name = OtpErlangString.newString(alive); OtpPublishedNode node = null; if (traceLevel >= traceThreshold) { log.Debug("<- PORT (r4) " + name); } lock (portmap) { if (portmap.ContainsKey(name)) { node = portmap[name]; } } OtpOutputStream obuf = new OtpOutputStream(); if (node != null) { obuf.write1(port4resp); obuf.write1(0); obuf.write2BE(node.Port); obuf.write1(node.Type); obuf.write1(node.Proto); obuf.write2BE(node.DistHigh); obuf.write2BE(node.DistLow); obuf.write2BE(len); obuf.writeN(alive); obuf.write2BE(0); } else { obuf.write1(port4resp); obuf.write1(1); } obuf.WriteTo(s.GetStream()); } catch (IOException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Request not responding"); } return; }
private void r4_names(TcpClient s, OtpInputStream ibuf) { try { if (traceLevel >= traceThreshold) { log.Debug("<- NAMES(r4) "); } OtpOutputStream obuf = new OtpOutputStream(); obuf.write4BE(EpmdPort.get()); lock (portmap) { foreach (KeyValuePair<string, OtpPublishedNode> pair in portmap) { OtpPublishedNode node = pair.Value; string info = String.Format("name {0} at port {1}\n", node.Alive, node.Port); byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(info); obuf.writeN(bytes); } } obuf.WriteTo(s.GetStream()); } catch (IOException) { if (traceLevel >= traceThreshold) { log.Debug("<- (no response)"); } throw new IOException("Request not responding"); } return; }