|
NAMENet::SMPP - pure Perl implementation of SMPP 3.4 over TCPSYNOPSISuse Net::SMPP; $smpp = Net::SMPP->new_transceiver($host, port=>$port, system_id => 'yourusername', password => 'secret', ) or die; DESCRIPTIONImplements Short Message Peer to Peer protocol, which is frequently used to pass short messages between mobile operators implementing short message service (SMS). This is applicable to both european GSM and american CDMA/TDMA systems.This documentation is not intended to be complete reference to SMPP protocol - use the SMPP specification documents (see references section) to obtain exact operation and parameter names and their meaning. You may also need to obtain site specific documentation about the remote end and any protocol extensions that it supports or demands before you start a project. This document follows the convention of spelling parameter names exactly as they appear in the SMPP v3.4 documentation. SMPP v4.0 support also follows the respective documentation, except where v4.0 usage is in conflict with v3.4 usage, in which case the latter prevails (in practise I believe no such conflicts remain in the madule at present). For a complete list of error code and optional parameter enumerations, the reader is encouraged to consult the source code or SMPP speciofications. Despite its name, SMPP protocol defines a client (ESME) and a server (often called SMSC in the mobile operator world). Client usually initiates the TCP connection and does bind to log in. After binding, a series of request response pairs, called PDUs (protocol data units) is exchanged. Request can be initiated by either end (hence "peer-to-peer"?) and the other end reponds. Requests are numbered with a sequence number and each response has corresponding sequence number. This allows several requests to be pending at the same time. Conceptually this is similar to IMAP or LDAP message IDs. Usually the $smpp object maintains the sequence numbers by itself and the programmer need not concern himself with their exact values, but should a need to override them arise, the seq argument can be supplied to any request or response method. Normally this module operates in synchronous mode, meaning that a method that sends a request will also block until it gets the corresponding response. Internal command used for waiting for response is $resp_pdu = $smpp->wait_pdu($cmd_id, $seq); If, while waiting for a particular response, other PDUs are received they are either handled by handlers (set up by constructor) or discarded. Both command code and sequence number must match. Typically a handler for enquire command is set up while all other commands are silently dropped. This practise may not be very suitable for transceiver mode of operation and certainly is not suitable for implementing a SMSC. Synchronous operation makes it impossible to interleave SMPP operations, thus it should be regarded as a simplified programming model for simple tasks. Anyone requiring more advanced control has to use the asynchronous mode and take up the burden of understanding and implementing more of the message flow logic in his own application. In synchronous mode request PDU methods return a Net::SMPP::PDU object representing the response, if all went well protocolwise, or undef if there was a protocol level error. If undef was returned, the reason for the failure can be extracted from ${*$smpp}{smpperror} and ${*$smpp}{smpperrorcode} (actual codes are undocumented at the moment, but are guaranteed not to change) variables and the global variable $!. These variables are meaningless if anything else than undef was returned. The response itself may be an error response if there was an application level error in the remote end. In this case the application level error can be determined from $pdu->{status} field. Some responses also have optional parameters that further clarify the failure, see documentation for each operation. If a protocol level error happens, probably the only safe action is to destroy the connection object (e.g. undef $smpp). If an application level error happens, then depending on how the remote end has been implemented it may be possible to continue operation. Module can also be used asynchronously by specifying async=>1 to the constructor. In this mode command methods return immediately with the sequence number of the PDU and user should poll for any responses using $pdu = $smpp->wait_pdu($cmd_id, $seq); Typically wait_pdu() is used to wait for a response, but if wait_pdu() is used to wait for a command, the caller should generate appropriate response. If caller wants to receive next available PDU, he can call $pdu = $smpp->read_pdu(); which will block until a PDU is received from the stream. The caller would then have to check if the PDU is a response or a request and take appropriate action. The smsc.pl example program supplied with this distribution demonstrates a possible framework for handling both requests and responses. If the caller does not want to block on wait_pdu() or read_pdu(), he must use select() to determine if the socket is readable (*** what if SSL layer gets inserted?). Even if the socket selects for reading, there may not be enough data to complete the PDU, so the call may still block. Currently there is no reliable mechanism for avoiding this. If this bothers you, you may consider allocating a separate process for each connection so that blocking does not matter, or you may set up some sort of timeout (see perlipc(1) man page) or you may rewrite this module and contribute patches. Response methods always return the sequence number, irrespective of synchronous or asynchronous mode, or undef if an error happened. CONSTRUCTORS
REQUEST PDU METHODSEach request PDU method constructs a PDU from list of arguments supplied and sends it to the wire.If async mode has been enabled (by specifying "async=>1" in the constructor or as an argument to the method), the methods return sequence number of the PDU just sent. This number can be later used to match up the response, like this: $seq = $smpp->query_sm(message_id => $msg_id) or die; ... $resp_pdu = $smpp->wait_pdu(Net::SMPP::CMD_query_sm_resp, $seq) or die; die "Response indicated error: " . $resp_pdu->explain_status() if $resp_pdu->status; If async mode is not enabled (i.e. "async=>1" was not specified neither in constructor nor the method), the method will wait for the corresponding response and return Net::SMPP::PDU object representing that response. The application should check the outcome of the operation from the status field of the response PDU, like this: $resp_pdu = $smpp->query_sm(message_id => $msg_id) or die; die "Response indicated error: " . $resp_pdu->explain_status() if $resp_pdu->status; All request PDU methods optionally take "seq=>123" argument that allows explicit specification of the sequence number. The default is to increment internally stored sequence number by one and use that. Most PDUs have mandatory parameters and optional parameters. If mandatory parameter is not supplied, it is inherited from the smpp object. This means that the parameter can either be set as an argument to the constructor or it is inherited from built-in defaults in the innards of Net::SMPP (see "Default" table from line 217 onwards). Some mandatory parameters can not be defaulted - if they are missing a die results. In descriptions below, defaultable mandatory parameters are show with the default value and comment indicating that its defaultable. Optional parameters can be supplied to all PDUs (although the SMPP spec does not allow optional parameters for some PDUs, the module does not check for this) by listing them in the order that they should be appended to the end of the PDU. Optional parameters can not be defaulted - if the parameter is not supplied, it simply is not included in the PDU. Optional parameters are not supported by previous versions of the SMPP protocol (up to and including 3.3). Applications wishing to be downwards compatible should not make use of optional parameters. Standard optional parameters can be supplied by their name (see "param_tab" in the Net::SMPP source code, around line 345, for list of known optional parameters), but the programmer still needs to supply the value of the parameter in the expected format (one often has to use pack to construct the value). Consult SMPP specifications for the correct format. It is possible to supply arbitrary unsupported optional parameters by simply supplying the parameter tag as a decimal number. Consult your site dependent documentation to figure out the correct tags and to determine the correct format for the value. When optional parameters are returned in response PDUs, they are decoded and made available under both numeric tag and symbolic tag, if known. For example the delivery_failure_reson of data_sm_resp can be accessed both as $resp->{delivery_failure_reson} and $resp->{1061}. The application needs to interpret the formatting of optional parameters itself. The module always assumes they are strings, while often they actually are interpretted as integers. Consult SMPP specifications and site dependent documentation for correct format and use unpack to obtain the numbers. If an unknown nonnumeric parameter tags are supplied a warning is issued and parameter is skipped. In general the Net::SMPP module does not enforce SMPP specifications. This means that it will happily accept too long or too short values for manatory or optional parameters. Also the internal formatting of the parameter values is not checked in any way. The programmer should consult the SMPP specifications to learn the correct length and format of each mandatory and optional parameter. Similarily, if the remote end returns incorrect PDUs and Net::SMPP is able to parse them (usually because length fields match), then Net::SMPP will not perform any further checks. This means that some fields may be longer than allowed for in the specifications. I opted to leave the checks out at this stage because I needed a flexible module that allowed me to explore even nonconformant SMSC implementations. If the lack of sanity checks bothers you, formulate such checks and submit me a patch. Ideally one could at construction time supply an argument like "strict=>1" to enable the sanity checks.
RESPONSE PDU METHODSResponse PDU methods are used to indicate outcome of requested commands. Typically these methods would be used by someone implementing a server (SMSC).Response PDUs do not have separate asynchronous behaviour pattern.
MESSAGE ENCODING AND LENGTHMany SMS technologies have inherent message length
limits. For example GSM specifies length to be 140 bytes. Using 7 bit
encoding, this holds the 160 characters that people are familiar with.
Net::SMPP does not enforce this limit in any way, i.e. if you create too long
message, then it is your problem. You should at application layer make sure
you stay within limits.
Net::SMPP also does not automatically perform the encoding, not even if you set data_encoding parameter. Application layer is responsible for performing the encoding and setting the data_encoding parameter accordingly. To assist in performing the usual 7 bit encoding, following functions are provided (but you have to call them explicitly):
The rationale for leaving encoding and length issues at application layer is two fold: 1. often the data is just copied through to another message or protocol, thus we do not really care how it is encoded or how long it is. Presumably it was valid at origin. 2. This policy avoids underlying technology dependencies in the module. Often local deployments have all the manner of hacks that make this area very difficult to chart. So I leave it to local application developer to find out what is locally needed. OTHER METHODS
EXAMPLESTypical client:use Net::SMPP; $smpp = Net::SMPP->new_transceiver('smsc.foo.net', port=>2552) or die; $resp_pdu = $smpp->submit_sm(destination_addr => '447799658372', data => 'test message') or die; *** Typical server, run from inetd: *** See test.pl for good templates with all official parameters, but beware that the actual parameter values are ficticious as is the flow of the dialog. MULTIPART MESSAGEReportedly (Zeus Panchenko) multipart messages can be gotten to work withwhile (length ($msgtext)) { if ($multimsg_maxparts) { @udh_ar = map { sprintf "%x", $_ } $origref, $multimsg_maxparts, $multimsg_curpart; $udh = pack("hhhhhh",0x05, 0x00, 0x03 , @udh_ar); $resp_pdu = $smpp->submit_sm(destination_addr => $phone, ... short_message => $udh . $msgtext, ); ... } } #4#cut =head1 VERSION 4.0 SUPPORT Net::SMPP was originally written for version 3.4 of SMPP protocol. I have since then gotten specifications for an earlier protocol, the version 4.0 (Logical, eh? (pun intended)). In my understanding the relevant differences are as follows (n.b. (ok) marks difference that has already been implemented): 1. A reserved (always 0x00000000) field in message header (v4 p. 21) (ok) 2. Connection can not be opened in transceiver mode (this module will not enforce this restriction) (ok) 3. Command versioning. Version 0x01 == V4 (v4 p. 22) (ok) 4. Support for extended facilities has to be requested during bind (ok) 5. bind_* PDUs have facilities_mask field (v4 p. 25) (ok) 6. bind_*_resp PDUs have facilities_mask field (v4 p. 27) (ok) 7. outbind lacks system ID field (v4 p.30, v3.4 p. 54) (ok) 8. submit_sm lacks service_type and adds message_class (v4 p. 34, v3.4 p. 59) (ok) 9. submit_sm: telematic_interworking == protocol_id (ok) 10. submit_sm: starting from number of destinations and destination address the message format is substantially different. Actually the message format is somewhat similar to v3.4 submit_multi. (ok) 11. submit_sm: validity period encoded as an integer relative offset (was absolute time as C string) (ok) 12. submit_sm: replace_if_present flag missing (ok) 13. submit_sm: sm_length field is 2 octets (was one) (ok) 14. submit_sm_resp is completely different, but actually equal to v3.4 submit_multi_resp (v4 p. 37, v3.4 pp. 67,75) (ok) 15. submit_sm vs submit_multi: lacks service_type, adds message_class (ok) 16. submit_sm vs submit_multi: number_of_dests increased from 1 byte to 4 (ok) 17. submit_sm vs submit_multi: esm_class lacking, adds messaging_mode and msg_reference (ok) 18. submit_sm vs submit_multi: telematic_interworking == protocol_id (ok) 19. submit_sm vs submit_multi: replace_if_present missing (ok) 20. submit_sm vs submit_multi: sm_length is 2 bytes (was one) (ok) 21. submit_sm vs submit_multi: lacks dest_flag and distribution_list_name (ok) 22. deliver_sm: lacks service_type (ok) 23. deliver_sm: lacks esm_class, adds msg_reference and message_class (ok) 24. deliver_sm: telematic_interworking == protocol_id (ok) 25. deliver_sm: priority_level == priority_flag (ok) 26. deliver_sm: submit_time_stamp == schedule_delivery_time (ok) 27. deliver_sm: lacks validity_period, registered_delivery, and replace_if_present_flag (ok) 28. deliver_sm: lacks sm_default_msg_id (ok) 29. deliver_sm: sm_length is now 2 bytes (was one) (ok) 30. deliver_sm_resp: lacks message_id (v3.4 has the field, but its unused) (ok) 31. New command: delivery_receipt (ok) 32. New response: delivery_receipt_resp (ok) 33. query_sm: dest_addr_* fields added (v4 p. 46, v3.4 p. 95) (ok) 34. query_sm_resp: error_code renamed to network_error_code and increased in size from one to 4 bytes (ok) 35. cancel_sm: service_type renamed to message_class, also type changed (ok) 36. replace_sm: added dest_addr_* fields (ok) 37. replace_sm: data type of validity_period changed (ok) 38. replace_sm: added data_coding field (ok) 39. replace_sm: sm_length field increased from one to two bytes (ok) 40. In v3.4 command code 0x0009 means bind_transceiver, in v4.0 this very same code means delivery_receipt (bummer) (ok) 41. In v3.4 enquire_link is 0x0015 where as in v4 it is 0x000a (ok) To create version 4 connection, you must specify smpp_version => 0x40 and you should not bind as transceiver as that is not supported by the specification. As v3.4 specification seems more mature, I recommend that where attributes have been renamed between v4 and v3.4 you stick to using v3.4 names. I have tried to provide compatibility code whenever possible. #4#end MISC. NOTESUnless you wrote your program to be multithreaded or multiprocess, everything will happen in one thread of execution. Thus if you get unbind while doing something else (e.g. checking your spool directory), it stays in operating system level buffers until you actually call read_pdu(). Knowing about unbind or not is of little use. You can write your program to assume the network traffic arrives only exactly when you call read_pdu().Regarding the unbind, it is normally handled by a dispatch table automatically if you use wait_pdu() to receive your traffic. But if you created your own dispatch table, you will have to add it there yourself. If you are calling read_pdu() then you have to handle it yourslef. Even if you are using the supplied table, you may want to double check - there could be a bug. One more thing: if your problem is knowing whether wait_pdu() or read_pdu() would block, then you have two possible solutions: 1. use select(2) systemcall to determine for the socket is ready for reading 2. structure your program as several processes (e.g. one for sending and one for receiving) so that you can afford to block The above two tricks are not specific to this module. Consult any standard text book on TCP/IP network programming. ERRORSPlease consult "status_code" table in the beginning of the source code or SMPP specification section 5.1.3, table 5-2, pp.112-114.EXPORTNone by default.TESTS / WHAT IS KNOWN TO WORKInteroperates with itself.TO DO AND BUGS
AUTHOR AND COPYRIGHTSampo Kellomaki <sampo@symlabs.com>Net::SMPP is copyright (c) 2001-2010 by Sampo Kellomaki, All rights reserved. Portions copyright (c) 2001-2005 by Symlabs, All rights reserved. You may use and distribute Net::SMPP under same terms as perl itself. NET::SMPP COMES WITH ABSOLUTELY NO WARRANTY. PLUGThis work was sponsored by Symlabs, the LDAP and directory experts (www.symlabs.com).SEE ALSO
Visit the GSP FreeBSD Man Page Interface. |