/// <summary> /// Start the listener /// </summary> /// <param name="bind"></param> public void Start(Configuration.EndpointConfiguration config) { this.m_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); this.m_udpSocket.DontFragment = true; this.m_configuration = config; // Get the IP address IPEndPoint endpoint = null; if (config.Address.HostNameType == UriHostNameType.Dns) { endpoint = new IPEndPoint(Dns.GetHostEntry(config.Address.Host).AddressList[0], config.Address.Port); } else { endpoint = new IPEndPoint(IPAddress.Parse(config.Address.Host), config.Address.Port); } // Bind the socket this.m_udpSocket.Bind(endpoint); this.m_traceSource.TraceInfo("UDP transport bound to {0}", endpoint); // Run try { while (this.m_run) { EndPoint remote_ep = new IPEndPoint(IPAddress.Any, 0); try { Byte[] udpMessage = new Byte[this.m_configuration.MaxSize]; //bytesReceived = udpSocket.Receive(udpMessage); int bytesReceived = this.m_udpSocket.ReceiveFrom(udpMessage, ref remote_ep); IPEndPoint ipep = (IPEndPoint)remote_ep; IPAddress ipadd = ipep.Address; // Parse String udpMessageStr = System.Text.Encoding.UTF8.GetString(udpMessage).TrimEnd('\0'); var message = SyslogMessage.Parse(udpMessageStr, Guid.NewGuid()); if (this.MessageReceived != null) { this.MessageReceived.BeginInvoke(this, new SyslogMessageReceivedEventArgs(message, new Uri(String.Format("udp://{0}", remote_ep)), this.m_configuration.Address, DateTime.Now), null, null); } } catch (SyslogMessageException e) { if (this.InvalidMessageReceived != null) { this.InvalidMessageReceived.BeginInvoke(this, new SyslogMessageReceivedEventArgs(e.FaultingMessage, new Uri(String.Format("udp://{0}", remote_ep)), this.m_configuration.Address, DateTime.Now), null, null); } this.m_traceSource.TraceError(e.ToString()); } catch (Exception e) { if (this.InvalidMessageReceived != null) { this.InvalidMessageReceived.BeginInvoke(this, new SyslogMessageReceivedEventArgs(new SyslogMessage(), new Uri(String.Format("udp://{0}", remote_ep)), this.m_configuration.Address, DateTime.Now), null, null); } this.m_traceSource.TraceError(e.ToString()); } } } finally { this.m_udpSocket.Dispose(); } }
/// <summary> /// Start the service /// </summary> public void Start(Configuration.EndpointConfiguration bind) { this.m_traceSource.TraceInfo("Starting HTTP listener {0} on {1}...", bind.Name, bind.Address); if (!HttpListener.IsSupported) { throw new InvalidOperationException("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); } this.m_server.Prefixes.Add(bind.Address.ToString().Replace("0.0.0.0", "+")); this.m_server.Start(); // Run try { while (this.m_run) { // Context HttpListenerContext context = this.m_server.GetContext(); // Response context.Response.SendChunked = false; context.Response.ContentType = "text/plain"; try { HttpListenerRequest request = context.Request; HttpListenerResponse response = context.Response; // Parse the message byte[] httpRequest = new byte[request.ContentLength64]; request.InputStream.Read(httpRequest, 0, (int)request.ContentLength64); String httpMessageStr = Encoding.UTF8.GetString(httpRequest); var message = new SyslogMessage(); message.Body = httpMessageStr; message.Facility = 1; message.HostName = request.RemoteEndPoint.ToString(); message.Original = httpMessageStr; message.Version = 1; message.ProcessName = "UNKNOWN/HTTP"; message.ProcessId = "0"; message.SessionId = Guid.NewGuid(); if (request.HttpMethod != "POST") { throw new InvalidOperationException("Invalid HTTP method. Expected POST"); } //else if (request.ContentType != "application/ihe+rfc3881" && // request.ContentType != "text/xml") // throw new SyslogMessageException("Invalid content-type. Expected application/ihe+rfc3881", message); message.TypeId = "IHE+RFC-3881"; if (this.MessageReceived != null) { this.MessageReceived.BeginInvoke(this, new SyslogMessageReceivedEventArgs(message, new Uri(String.Format("http://{0}:{1}", request.RemoteEndPoint.Port, request.RemoteEndPoint.Port)), bind.Address, DateTime.Now), null, null); } response.ContentLength64 = 0; response.StatusCode = 200; response.StatusDescription = "OK"; } catch (InvalidOperationException e) { if (this.InvalidMessageReceived != null) { this.InvalidMessageReceived.BeginInvoke(this, new SyslogMessageReceivedEventArgs(new SyslogMessage(), new Uri(String.Format("http://{0}:{1}", context.Request.RemoteEndPoint.Address, context.Request.RemoteEndPoint.Port)), bind.Address, DateTime.Now), null, null); } byte[] errorBytes = Encoding.UTF8.GetBytes(e.ToString()); context.Response.ContentLength64 = errorBytes.Length; context.Response.OutputStream.Write(errorBytes, 0, errorBytes.Length); context.Response.StatusCode = 405; context.Response.StatusDescription = "Method Not Allowed"; this.m_traceSource.TraceError(e.ToString()); } catch (SyslogMessageException e) { if (this.InvalidMessageReceived != null) { this.InvalidMessageReceived.BeginInvoke(this, new SyslogMessageReceivedEventArgs(e.FaultingMessage, new Uri(String.Format("http://{0}:{1}", context.Request.RemoteEndPoint.Address, context.Request.RemoteEndPoint.Port)), bind.Address, DateTime.Now), null, null); } context.Response.StatusCode = 400; context.Response.StatusDescription = "Bad Request"; byte[] errorBytes = Encoding.UTF8.GetBytes(e.ToString()); context.Response.ContentLength64 = errorBytes.Length; context.Response.OutputStream.Write(errorBytes, 0, errorBytes.Length); this.m_traceSource.TraceError(e.ToString()); } catch (Exception e) { if (this.InvalidMessageReceived != null) { this.InvalidMessageReceived.BeginInvoke(this, new SyslogMessageReceivedEventArgs(new SyslogMessage(), new Uri(String.Format("http://{0}:{1}", context.Request.RemoteEndPoint.Address, context.Request.RemoteEndPoint.Port)), bind.Address, DateTime.Now), null, null); } context.Response.StatusCode = 500; context.Response.StatusDescription = "Internal Server Error"; byte[] errorBytes = Encoding.UTF8.GetBytes(e.ToString()); context.Response.ContentLength64 = errorBytes.Length; context.Response.OutputStream.Write(errorBytes, 0, errorBytes.Length); this.m_traceSource.TraceError(e.ToString()); } context.Response.Close(); } } catch (Exception e) { this.m_traceSource.TraceError("FATAL: {0}", e.ToString()); } finally { this.m_server.Close(); } }