private PRSMessage RequestPort(string serviceName) { PRSMessage response = null; if (ports.SingleOrDefault(p => p.ServiceName == serviceName && !p.Available) == null) { // client has requested the lowest available port, so find it! PortReservation reservation = null; reservation = ports.FirstOrDefault(p => p.Available); // if found an avialable port, reserve it and send SUCCESS if (reservation != null) { reservation.Reserve(serviceName); response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, serviceName, reservation.Port, PRSMessage.STATUS.SUCCESS); } else { response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, serviceName, 0, PRSMessage.STATUS.ALL_PORTS_BUSY); } } else { response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, serviceName, 0, PRSMessage.STATUS.SERVICE_IN_USE); } return(response); }
private static PortReservation CreatePortReservation(string line, int lineNumber) { var tokens = Tokenize(line); // Sanity check if (tokens.Count < 3 || tokens.Count > 4) { throw new ArgumentException("Netstat entry does not a valid number of tokens!"); } var portReservation = new PortReservation { Protocol = tokens[0], LocalAddress = ExtractHostname(tokens[1]), LocalPort = ExtractPort(tokens[1]), ForeignAddress = ExtractHostname(tokens[2]), ForeignPort = ExtractPort(tokens[2]), Line = lineNumber, }; if (tokens.Count == 4) { portReservation.TcpState = tokens[3]; } return(portReservation); }
public override JObject ParseLogDocument(TextReader reader) { IList <NetstatEntry> entries = new List <NetstatEntry>(); NetstatEntry entry = new NetstatEntry(); string line; int lineCounter = 0; while ((line = ReadLine(reader)) != null) { lineCounter++; // Ignore headers & empty lines. if (!IsNetstatContent(line)) { continue; } IList <string> tokens = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToList(); if (tokens.Count == 1) { if (tokens[0].StartsWith("[") && tokens[0].EndsWith("]")) { entry.Process = tokens[0].Trim('[', ']'); entry.Line = lineCounter; entries.Add(entry); entry = new NetstatEntry(); } else { entry.Component = tokens[0]; } } else if (tokens.Count == 3 || tokens.Count == 4) { var portReservation = new PortReservation { Protocol = tokens[0], LocalAddress = ExtractHostname(tokens[1]), LocalPort = ExtractPort(tokens[1]), ForeignAddress = ExtractHostname(tokens[2]), ForeignPort = ExtractPort(tokens[2]) }; if (tokens.Count == 4) { portReservation.TcpState = tokens[3]; } entry.TransportReservations.Add(portReservation); } } // Roll up entries into JObject JObject netstatJson = CreateJObject(entries); return(InsertMetadata(netstatJson)); }
public override JObject ParseLogDocument(TextReader reader) { var portReservations = new List <PortReservation>(); var unixDomainSockets = new List <UnixDomainSocket>(); string line; int lineCounter = 0; while ((line = ReadLine(reader)) != null) { lineCounter++; try { for (int i = 0; i < netstatRegexes.Count; i++) { var netstatRegexToTry = netstatRegexes[i]; var fields = netstatRegexToTry.Regex.MatchNamedCaptures(line); if (fields.Any()) { switch (netstatRegexToTry.Type) { case NetstatRegex.EntryType.ActiveInternetConnection: PortReservation portReservation = BuildPortReservationEntry(fields, lineCounter); portReservations.Add(portReservation); break; case NetstatRegex.EntryType.UnixDomainSocket: UnixDomainSocket unixDomainSocket = BuildUnixDomainSocketEntry(fields, lineCounter); unixDomainSockets.Add(unixDomainSocket); break; } // Promote matching regex to front of list to optimize matches of future entries. netstatRegexes.RemoveAt(i); netstatRegexes.Insert(0, netstatRegexToTry); break; } } } catch (Exception ex) { throw new ParsingException(String.Format("Failed to parse line {0} of document: {1}", lineCounter, ex.Message), ex); } } // Roll up entries into JObject return(CreateNetstatJson(portReservations, unixDomainSockets)); }
public PRS(ushort startingClientPort, ushort endingClientPort, int keepAliveTimeout) { // save parameters this.startingClientPort = startingClientPort; this.endingClientPort = endingClientPort; this.keepAliveTimeout = keepAliveTimeout; this.numPorts = endingClientPort - startingClientPort + 1; // initialize to not stopped stopped = false; // initialize port reservations // get the total amount of numbers between start and end (inclusively) ports = new PortReservation[numPorts]; for (ushort i = 0; i < numPorts; i++) { ports[i] = new PortReservation((ushort)(startingClientPort + i)); } }
public PRS(ushort startingClientPort, ushort endingClientPort, int keepAliveTimeout) { // save parameters this.startingClientPort = startingClientPort; this.endingClientPort = endingClientPort; this.keepAliveTimeout = keepAliveTimeout; // initialize to not stopped stopped = false; // initialize port reservations numPorts = endingClientPort - startingClientPort + 1; //inclusive ports = new PortReservation[numPorts]; //loop through port res array, fill in port numbers for (ushort port = startingClientPort; port <= endingClientPort; port++) { //array is zero based, port numbers start at starting port ports[port - startingClientPort] = new PortReservation(port); } }
private PRSMessage RequestPort(string serviceName) { PRSMessage response = null; // client has requested the lowest available port, so find it! PortReservation reservation = ports.FirstOrDefault(port => port.Available); if (reservation != null) { // if found an available port, reserve it and send SUCCESS reservation.Reserve(serviceName); response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, serviceName, reservation.Port, PRSMessage.STATUS.SUCCESS); } else { // else, none available, send ALL_PORTS_BUSY response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, serviceName, 0, PRSMessage.STATUS.ALL_PORTS_BUSY); } return(response); }
public PRSMessage HandleMessage(PRSMessage msg) { // handle one message and return a response PRSMessage response = null; //check for expired ports CheckForExpiredPorts(); switch (msg.MsgType) { case PRSMessage.MESSAGE_TYPE.REQUEST_PORT: { //try to reserve requested port and send requested port back in response response = RequestPort(msg.ServiceName); } break; case PRSMessage.MESSAGE_TYPE.KEEP_ALIVE: { // client has requested that we keep their port alive // find the reserved port by port# and service name PortReservation reservation = ports.FirstOrDefault(p => !p.Available && p.ServiceName == msg.ServiceName && p.Port == msg.Port); // if found, keep it alive and send SUCCESS else if (reservation != null) { reservation.KeepAlive(); response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, reservation.ServiceName, reservation.Port, PRSMessage.STATUS.SUCCESS); } else { response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, msg.ServiceName, 0, PRSMessage.STATUS.SERVICE_NOT_FOUND); } } break; case PRSMessage.MESSAGE_TYPE.CLOSE_PORT: { // client has requested that we close their port // find the reserved port by port# and service name PortReservation reservation = ports.FirstOrDefault(p => !p.Available && p.ServiceName == msg.ServiceName && p.Port == msg.Port); // if found, keep it alive and send SUCCESS else if (reservation != null) { reservation.Close(); response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, msg.ServiceName, reservation.Port, PRSMessage.STATUS.SUCCESS); } else { response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, msg.ServiceName, msg.Port, PRSMessage.STATUS.SERVICE_NOT_FOUND); } } break; case PRSMessage.MESSAGE_TYPE.LOOKUP_PORT: { // client wants to know the reserved port number for a named service // find the port PortReservation reservation = ports.FirstOrDefault(p => !p.Available && p.ServiceName == msg.ServiceName); // if found, send port number back // else, SERVICE_NOT_FOUND if (reservation != null) { response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, msg.ServiceName, reservation.Port, PRSMessage.STATUS.SUCCESS); } else { response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, msg.ServiceName, msg.Port, PRSMessage.STATUS.SERVICE_NOT_FOUND); } } break; case PRSMessage.MESSAGE_TYPE.STOP: { // client is telling us to close the appliation down // stop the PRS and return SUCCESS stopped = true; response = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, "", 0, PRSMessage.STATUS.SUCCESS); } break; } return(response); }