4 * Internet Protocol ancestor channel class
6 * Portable Windows Library
8 * Copyright (c) 1993-2002 Equivalence Pty. Ltd.
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
27 * Revision 1.20 2007/04/18 23:49:50 csoutheren
28 * Add usage of precompiled headers
30 * Revision 1.19 2005/11/30 12:47:37 csoutheren
31 * Removed tabs, reformatted some code, and changed tags for Doxygen
33 * Revision 1.18 2004/11/11 07:34:50 csoutheren
34 * Added #include <ptlib.h>
36 * Revision 1.17 2002/11/06 22:47:24 robertj
37 * Fixed header comment (copyright etc)
39 * Revision 1.16 2002/09/16 01:08:59 robertj
40 * Added #define so can select if #pragma interface/implementation is used on
41 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan.
43 * Revision 1.15 2001/09/26 09:37:04 robertj
44 * Added virtual keyword to a lot of functions so can be overridden.
46 * Revision 1.14 1999/03/09 08:01:46 robertj
47 * Changed comments for doc++ support (more to come).
49 * Revision 1.13 1999/02/16 08:07:10 robertj
50 * MSVC 6.0 compatibility changes.
52 * Revision 1.12 1998/09/23 06:19:40 robertj
53 * Added open source copyright license.
55 * Revision 1.11 1996/09/14 13:09:13 robertj
57 * rearranged sockets to help support IPX.
58 * added indirect channel class and moved all protocols to descend from it,
59 * separating the protocol from the low level byte transport.
61 * Revision 1.10 1996/05/15 10:07:00 robertj
62 * Added access function to set intercharacter line read timeout.
64 * Revision 1.9 1996/05/09 12:14:02 robertj
65 * Rewrote the "unread" buffer usage and then used it to improve ReadLine() performance.
67 * Revision 1.8 1996/03/31 08:43:38 robertj
68 * Added version of WriteCommand() and ExecteCommand() without argument string.
70 * Revision 1.7 1996/03/16 04:35:32 robertj
71 * Added PString parameter version of UnRead().
72 * Changed lastResponseCode to an integer.
73 * Added ParseReponse() for splitting reponse line into code and info.
75 * Revision 1.6 1996/02/13 12:57:05 robertj
76 * Added access to the last response in an application socket.
78 * Revision 1.5 1996/02/03 11:33:16 robertj
79 * Changed RadCmd() so can distinguish between I/O error and unknown command.
81 * Revision 1.4 1996/01/23 13:08:43 robertj
82 * Major rewrite for HTTP support.
84 * Revision 1.3 1995/06/17 11:12:15 robertj
85 * Documentation update.
87 * Revision 1.2 1995/06/17 00:39:53 robertj
88 * More implementation.
90 * Revision 1.1 1995/06/04 13:17:16 robertj
95 #ifndef _PINTERNETPROTOCOL
96 #define _PINTERNETPROTOCOL
110 /** A TCP/IP socket for process/application layer high level protocols. All of
111 these protocols execute commands and responses in a standard manner.
113 A command consists of a line starting with a short, case insensitive command
114 string terminated by a space or the end of the line. This may be followed
115 by optional arguments.
117 A response to a command is usually a number and/or a short string eg "OK".
118 The response may be followed by additional information about the response
119 but this is not typically used by the protocol. It is only for user
120 information and may be tracked in log files etc.
122 All command and reponse lines of the protocol are terminated by a CR/LF
123 pair. A command or response line may be followed by additional data as
124 determined by the protocol, but this data is "outside" the protocol
125 specification as defined by this class.
127 The default read timeout is to 10 minutes by the constructor.
129 class PInternetProtocol
: public PIndirectChannel
131 PCLASSINFO(PInternetProtocol
, PIndirectChannel
)
135 const char * defaultServiceName
, ///< Service name for the protocol.
136 PINDEX cmdCount
, ///< Number of command strings.
137 char const * const * cmdNames
///< Strings for each command.
139 // Create an unopened TCP/IP protocol socket channel.
143 // Overrides from class PChannel.
144 /** Low level read from the channel.
146 This override also supports the mechanism in the <A>UnRead()</A>
147 function allowing characters to be be "put back" into the data stream.
148 This allows a look-ahead required by the logic of some protocols. This
149 is completely independent of the standard iostream mechanisms which do
150 not support the level of timeout control required by the protocols.
153 TRUE if at least len bytes were written to the channel.
156 void * buf
, ///< Pointer to a block of memory to receive the read bytes.
157 PINDEX len
///< Maximum number of bytes to read into the buffer.
160 /** Low level write to the channel.
162 This override assures that the sequence CR/LF/./CR/LF does not occur by
163 byte stuffing an extra '.' character into the data stream, whenever a
164 line begins with a '.' character.
166 Note that this only occurs if the member variable
167 <CODE>stuffingState</CODE> has been set to some value other than
168 <CODE>DontStuff</CODE>, usually <CODE>StuffIdle</CODE>. Also, if the
169 <CODE>newLineToCRLF</CODE> member variable is TRUE then all occurrences
170 of a '\n' character will be translated to a CR/LF pair.
173 TRUE if at least len bytes were written to the channel.
176 const void * buf
, ///< Pointer to a block of memory to write.
177 PINDEX len
///< Number of bytes to write.
180 /** Set the maximum timeout between characters within a line. Default
183 void SetReadLineTimeout(
184 const PTimeInterval
& t
187 // New functions for class.
188 /** Connect a socket to a remote host for the internet protocol.
191 TRUE if the channel was successfully connected to the remote host.
193 virtual BOOL
Connect(
194 const PString
& address
, ///< Address of remote machine to connect to.
195 WORD port
= 0 ///< Port number to use for the connection.
197 virtual BOOL
Connect(
198 const PString
& address
, ///< Address of remote machine to connect to.
199 const PString
& service
///< Service name to use for the connection.
202 /** Accept a server socket to a remote host for the internet protocol.
205 TRUE if the channel was successfully connected to the remote host.
208 PSocket
& listener
///< Address of remote machine to connect to.
211 /** Get the default service name or port number to use in socket
215 string for the default service name.
217 const PString
& GetDefaultService() const;
219 /** Get the eventual socket for the series of indirect channels that may
220 be between the current protocol and the actual I/O channel.
222 This will assert if the I/O channel is not an IP socket.
225 TRUE if the string and CR/LF were completely written.
227 PIPSocket
* GetSocket() const;
229 /** Write a string to the socket channel followed by a CR/LF pair. If there
230 are any lone CR or LF characters in the <CODE>line</CODE> parameter
231 string, then these are translated into CR/LF pairs.
234 TRUE if the string and CR/LF were completely written.
236 virtual BOOL
WriteLine(
237 const PString
& line
///< String to write as a command line.
240 /** Read a string from the socket channel up to a CR/LF pair.
242 If the <CODE>unstuffLine</CODE> parameter is set then the function will
243 remove the '.' character from the start of any line that begins with
244 two consecutive '.' characters. A line that has is exclusively a '.'
245 character will make the function return FALSE.
247 Note this function will block for the time specified by the
248 <A>PChannel::SetReadTimeout()</A> function for only the first character
249 in the line. The rest of the characters must each arrive within the time
250 set by the <CODE>readLineTimeout</CODE> member variable. The timeout is
251 set back to the original setting when the function returns.
254 TRUE if a CR/LF pair was received, FALSE if a timeout or error occurred.
256 virtual BOOL
ReadLine(
257 PString
& line
, ///< String to receive a CR/LF terminated line.
258 BOOL allowContinuation
= FALSE
///< Flag to handle continued lines.
261 /** Put back the characters into the data stream so that the next
262 <A>Read()</A> function call will return them first.
265 int ch
///< Individual character to be returned.
268 const PString
& str
///< String to be put back into data stream.
271 const void * buffer
, ///< Characters to be put back into data stream.
272 PINDEX len
///< Number of characters to be returned.
275 /** Write a single line for a command. The command name for the command
276 number is output, then a space, the the <CODE>param</CODE> string
277 followed at the end with a CR/LF pair.
279 If the <CODE>cmdNumber</CODE> parameter is outside of the range of
280 valid command names, then the function does not send anything and
283 This function is typically used by client forms of the socket.
286 TRUE if the command was completely written.
288 virtual BOOL
WriteCommand(
289 PINDEX cmdNumber
///< Number of command to write.
291 virtual BOOL
WriteCommand(
292 PINDEX cmdNumber
, ///< Number of command to write.
293 const PString
& param
///< Extra parameters required by the command.
296 /** Read a single line of a command which ends with a CR/LF pair. The
297 command number for the command name is parsed from the input, then the
298 remaining text on the line is returned in the <CODE>args</CODE>
301 If the command does not match any of the command names then the entire
302 line is placed in the <CODE>args</CODE> parameter and a value of
303 P_MAX_INDEX is returned.
305 Note this function will block for the time specified by the
306 <A>PChannel::SetReadTimeout()</A> function.
308 This function is typically used by server forms of the socket.
311 TRUE if something was read, otherwise an I/O error occurred.
313 virtual BOOL
ReadCommand(
315 ///< Number of the command parsed from the command line, or P_MAX_INDEX
317 PString
& args
///< String to receive the arguments to the command.
320 /** Write a response code followed by a text string describing the response
321 to a command. The form of the response is to place the code string,
322 then the info string.
324 If the <CODE>info</CODE> parameter has multiple lines then each line
325 has the response code at the start. A '-' character separates the code
326 from the text on all lines but the last where a ' ' character is used.
328 The first form assumes that the response code is a 3 digit numerical
329 code. The second form allows for any arbitrary string to be the code.
331 This function is typically used by server forms of the socket.
334 TRUE if the response was completely written.
336 virtual BOOL
WriteResponse(
337 unsigned numericCode
, ///< Response code for command response.
338 const PString
& info
///< Extra information available after response code.
340 virtual BOOL
WriteResponse(
341 const PString
& code
, ///< Response code for command response.
342 const PString
& info
///< Extra information available after response code.
345 /** Read a response code followed by a text string describing the response
346 to a command. The form of the response is to have the code string,
347 then the info string.
349 The response may have multiple lines in it. A '-' character separates
350 the code from the text on all lines but the last where a ' ' character
351 is used. The <CODE>info</CODE> parameter will have placed in it all of
352 the response lines separated by a single '\n' character.
354 The first form places the response code and info into the protected
355 member variables <CODE>lastResponseCode</CODE> and
356 <CODE>lastResponseInfo</CODE>.
358 This function is typically used by client forms of the socket.
361 TRUE if the response was completely read without a socket error.
363 virtual BOOL
ReadResponse();
364 virtual BOOL
ReadResponse(
365 int & code
, ///< Response code for command response.
366 PString
& info
///< Extra information available after response code.
369 /** Write a command to the socket, using <CODE>WriteCommand()</CODE> and
370 await a response using <CODE>ReadResponse()</CODE>. The first character
371 of the response is returned, as well as the entire response being saved
372 into the protected member variables <CODE>lastResponseCode</CODE> and
373 <CODE>lastResponseInfo</CODE>.
375 This function is typically used by client forms of the socket.
378 First character of response string or '\0' if a socket error occurred.
380 virtual int ExecuteCommand(
381 PINDEX cmdNumber
///< Number of command to write.
383 virtual int ExecuteCommand(
384 PINDEX cmdNumber
, ///< Number of command to write.
385 const PString
& param
///< Extra parameters required by the command.
388 /** Return the code associated with the last response received by the
394 int GetLastResponseCode() const;
396 /** Return the last response received by the socket.
401 PString
GetLastResponseInfo() const;
405 /** Parse a response line string into a response code and any extra info
406 on the line. Results are placed into the member variables
407 <CODE>lastResponseCode</CODE> and <CODE>lastResponseInfo</CODE>.
409 The default bahaviour looks for a space or a '-' and splits the code
410 and info either side of that character, then returns FALSE.
413 Position of continuation character in response, 0 if no continuation
416 virtual PINDEX
ParseResponse(
417 const PString
& line
///< Input response line to be parsed
421 PString defaultServiceName
;
422 // Default Service name to use for the internet protocol socket.
424 PStringArray commandNames
;
425 // Names of each of the command codes.
427 PCharArray unReadBuffer
;
428 // Buffer for characters put back into the data stream.
431 // Buffer count for characters put back into the data stream.
433 PTimeInterval readLineTimeout
;
434 // Time for characters in a line to be received.
437 DontStuff
, StuffIdle
, StuffCR
, StuffCRLF
, StuffCRLFdot
, StuffCRLFdotCR
439 // Do byte stuffing of '.' characters in output to the socket channel.
442 // Translate \n characters to CR/LF pairs.
444 int lastResponseCode
;
445 PString lastResponseInfo
;
449 BOOL
AttachSocket(PIPSocket
* socket
);
457 // End Of File ///////////////////////////////////////////////////////////////