views:

108

answers:

2

To use an example response from google when sent "EHLO":

250-mx.google.com at your service, [66.501.941.15]
250-SIZE 35651584
250-8BITMIME
250-AUTH LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250 PIPELINING

Hex:

32 35 30 2D 6D 78 2E 67 6F 6F 67 6C 65 2E 63 6F 6D 20 61 74 20 79 6F 75 72 20 73 65 72 76 69 63 65 2C 20 5B 39 32 2E 34 32 31 2E 35 36 35 2E 34 32 5D 0D 0A 32 35 30 2D 53 49 5A 45 20 33 35 36 35 31 35 38 34 0D 0A 32 35 30 2D 38 42 49 54 4D 49 4D 45 0D 0A 32 35 30 2D 41 55 54 48 20 4C 4F 47 49 4E 20 50 4C 41 49 4E 0D 0A 32 35 30 2D 45 4E 48 41 4E 43 45 44 53 54 41 54 55 53 43 4F 44 45 53 0D 0A 32 35 30 20 50 49 50 45 4C 49 4E 49 4E 47 0D 0A

Since the SMTP spec states that a line must end with a CR LF (0D 0A), we can parse on those two bytes to find lines, but how do we determine the end of the response?

Over satellite, responses can be broken into pieces with a significant delay between. This means a response could end after a CR LF and not be complete ie:

250-mx.google.com at your service, [66.501.941.15]
250-SIZE 35651584
250-8BITMIME

Any logic that looks for a trailing CR LF would then assume the response is complete. IN this case I am using CryptLib to perform the SLL tunnel, but I can create the port with my own code and pass it to the library if there is some way to get an "end of response".

+7  A: 

The response ends on the line where there is no hyphen between the 250 and the name.

So, if the 4th character of the line is a space, that will be the last line of the response.

From section 4.2.1 of RFC 2821:

The format for multiline replies requires that every line, except the last, begin with the reply code, followed immediately by a hyphen, "-" (also known as minus), followed by text. The last line will begin with the reply code, followed immediately by < SP >, optionally some text, and < CRLF >.

R Samuel Klatchko
Ahh missed that. Thank you
Mike Trader
Since all response codes are 3 digit? I am assuming I can just test the fourth character of each line for the absence of a hyphen?
Mike Trader
+3  A: 

This is a multiline reply in the SMTP protocol (described in RFC 821 Appendix E)

You'll notice that the first line is formatted with

CODE-FirstLine
CODE-SecondLine

However the last line is formatted like so:

CODE LastLine

Each line is delimited by a CRLF like you explained, but you know you're on the last line of a multiline reply when the code and text are not delimited by a - (minus)

Phil
Ahh missed that. Thank you
Mike Trader