public static IPEndPoint ParseIPEndPoint(FtpReply ftpReply) { if (!ftpReply.Success) { return null; } var matches = Regex.Match(ftpReply.ResponseMessage, "([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+)"); if (!matches.Success) { return null; } if (matches.Groups.Count != 7) { return null; } var ipAddress = ParseIPAddress(from index in Enumerable.Range(1, 4) let octet = matches.Groups[index].Value select octet); var p1 = matches.Groups[5].Value; var p2 = matches.Groups[6].Value; var port = ParsePassivePort(p1, p2); var ipEndPoint = new IPEndPoint(ipAddress, port); return ipEndPoint; }
public static FtpReply ParseFtpReply(string data) { if (string.IsNullOrEmpty(data)) { return FtpReply.Failed; } var ftpResponseType = FtpResponseType.None; var messages = new List<string>(); var stringResponseCode = string.Empty; var responseCode = 0; var responseMessage = string.Empty; var lines = data.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries); foreach (var line in lines) { var match = Regex.Match(line, FtpReply.RegularExpressionForParsing); if (match.Success) { if (match.Groups.Count > 1) { stringResponseCode = match.Groups[1].Value; } if (match.Groups.Count > 2) { responseMessage = match.Groups[2].Value; } if (!string.IsNullOrWhiteSpace(stringResponseCode)) { var firstCharacter = stringResponseCode.First(); var character = firstCharacter.ToString(CultureInfo.InvariantCulture); var intFtpResponseType = int.Parse(character); ftpResponseType = (FtpResponseType) intFtpResponseType; responseCode = int.Parse(stringResponseCode); } } messages.Add(line); } var ftpReply = new FtpReply(ftpResponseType, responseCode, responseMessage, messages, data); return ftpReply; }