1 
2 /*
3  * Copyright (C) 2006 Raul Tremsal
4  * File  : esme.c
5  * Author: Raul Tremsal <ultraismo@yahoo.com>
6  *
7  * This file is part of libsmpp34 (c-open-smpp3.4 library).
8  *
9  * The libsmpp34 library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1 of the
12  * License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  */
24 
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <arpa/inet.h>
29 
30 
31 #ifdef __linux__
32 #include <stdint.h>
33 #endif
34 
35 #include "smpp34.h"
36 #include "smpp34_structs.h"
37 #include "smpp34_params.h"
38 
39 extern char *optarg;
40 char file_pdu[256];
41 
42 int ret = 0;
43 uint8_t bufPDU[2048];
44 int bufPDULen = 0;
45 uint8_t bPrint[2048];
46 
47 #define HELP_FORMAT " -f file [-h]\n" \
48 "    -f /path/to/file: binary file path.\n" \
49 "    -h : Help, show this message.\n"
50 
51 
work(uint32_t id,void * dst)52 int work( uint32_t id, void* dst )
53 {
54 
55     /* Print Buffer *******************************************************/
56     memset(bPrint, 0, sizeof(bPrint));
57     ret = smpp34_dumpBuf(bPrint, sizeof(bPrint), bufPDU, bufPDULen);
58     if( ret != 0 ){
59         printf("Error in smpp34_dumpBuf():%d:\n%s\n",
60                                             smpp34_errno, smpp34_strerror );
61         return( -1 );
62     };
63     printf("parse smpp34_dumpBuf()\n%s\n", smpp34_strerror);
64     printf("-----------------------------------------------------------\n");
65     printf("%s", bPrint);
66     printf("-----------------------------------------------------------\n");
67 
68     /* Copy PDU from Buffer ***********************************************/
69     ret = smpp34_unpack(id, (void*)dst, bufPDU, bufPDULen);
70     if( ret != 0 ){
71         printf("Error in smpp34_unpack():%d:\n%s\n",
72                                              smpp34_errno, smpp34_strerror);
73         return( -1 );
74     };
75     printf("parse smpp34_unpack()\n%s\n", smpp34_strerror);
76 
77     /* Print PDU **********************************************************/
78     memset(bPrint, 0, sizeof(bPrint));
79     ret = smpp34_dumpPdu(id, bPrint, sizeof(bPrint), (void*)dst );
80     if( ret != 0){
81         printf("Error in smpp34_dumpPdu():%d:\n%s\n",
82                                              smpp34_errno, smpp34_strerror);
83         return( -1 );
84     };
85     printf("parse smpp34_dumpPdu()\n%s\n", smpp34_strerror);
86     printf("-----------------------------------------------------------\n");
87     printf("%s\n", bPrint);
88     printf("-----------------------------------------------------------\n");
89 
90     return( 0 );
91 };
92 
main(int argc,char ** argv)93 int main( int argc, char **argv )
94 {
95     int co;
96     FILE *fd = NULL;
97     uint32_t tt, id;
98     tt = id = 0;
99 
100     while( (co = getopt(argc, argv, "f:h")) != EOF ){
101         switch( co ){
102         case 'f':
103             snprintf(file_pdu, sizeof(file_pdu), "%s", optarg);
104             break;
105         default:
106             printf("Error: unrecognized option\n");
107         case 'h':
108             printf("usage: %s %s\n", argv[0], HELP_FORMAT);
109             return( -1 );
110         };
111     };
112 
113     if( strcmp(file_pdu, "") == 0  ){ printf("Error in parameters\n");
114         printf("usage: %s %s\n", argv[0], HELP_FORMAT); return( -1 ); };
115 
116     /* Open File **********************************************************/
117 
118     if( (fd = fopen(file_pdu, "r")) == NULL ){
119         printf("Can't open file %s\n", file_pdu);
120         return( -1 );
121     };
122 
123     memset(bufPDU, 0, sizeof(bufPDU)); bufPDULen = 0;
124     /* char *fgets(char *s, int size, FILE *stream); */
125     while( !feof( fd ) ){
126         *(bufPDU + (bufPDULen++)) = (uint8_t)getc( fd );
127     };
128     fclose( fd ); bufPDULen--;
129 
130     memcpy(&tt, (bufPDU+4), 4); id = ntohl( tt );
131 
132     if( id == BIND_TRANSMITTER ){
133         bind_transmitter_t t1;
134         memset(&t1, 0, sizeof(bind_transmitter_t));
135         return( work( id, (void*)&t1 ) );
136     } else if( id == BIND_TRANSMITTER_RESP ){
137         bind_transmitter_resp_t t1;
138         memset(&t1, 0, sizeof(bind_transmitter_resp_t));
139         return( work( id, (void*)&t1 ) );
140     } else if( id == BIND_RECEIVER ){
141         bind_receiver_t t1;
142         memset(&t1, 0, sizeof(bind_receiver_t));
143         return( work( id, (void*)&t1 ) );
144     } else if( id == BIND_RECEIVER_RESP ){
145         bind_receiver_resp_t t1;
146         memset(&t1, 0, sizeof(bind_receiver_resp_t));
147         return( work( id, (void*)&t1 ) );
148     } else if( id == BIND_TRANSCEIVER ){
149         bind_transceiver_t t1;
150         memset(&t1, 0, sizeof(bind_transceiver_t));
151         return( work( id, (void*)&t1 ) );
152     } else if( id == BIND_TRANSCEIVER_RESP ){
153         bind_transceiver_resp_t t1;
154         memset(&t1, 0, sizeof(bind_transceiver_resp_t));
155         return( work( id, (void*)&t1 ) );
156     } else if( id == OUTBIND ){
157         outbind_t t1;
158         memset(&t1, 0, sizeof(outbind_t));
159         return( work( id, (void*)&t1 ) );
160     } else if( id == UNBIND ){
161         unbind_t t1;
162         memset(&t1, 0, sizeof(unbind_t));
163         return( work( id, (void*)&t1 ) );
164     } else if( id == UNBIND_RESP ){
165         unbind_resp_t t1;
166         memset(&t1, 0, sizeof(unbind_resp_t));
167         return( work( id, (void*)&t1 ) );
168     } else if( id == GENERIC_NACK ){
169         generic_nack_t t1;
170         memset(&t1, 0, sizeof(generic_nack_t));
171         return( work( id, (void*)&t1 ) );
172     } else if( id == SUBMIT_SM ){
173         submit_sm_t t1;
174         memset(&t1, 0, sizeof(submit_sm_t));
175         return( work( id, (void*)&t1 ) );
176     } else if( id == SUBMIT_SM_RESP ){
177         submit_sm_resp_t t1;
178         memset(&t1, 0, sizeof(submit_sm_resp_t));
179         return( work( id, (void*)&t1 ) );
180     } else if( id == SUBMIT_MULTI ){
181         submit_multi_t t1;
182         memset(&t1, 0, sizeof(submit_multi_t));
183         return( work( id, (void*)&t1 ) );
184     } else if( id == SUBMIT_MULTI_RESP ){
185         submit_multi_resp_t t1;
186         memset(&t1, 0, sizeof(submit_multi_resp_t));
187         return( work( id, (void*)&t1 ) );
188     } else if( id == DELIVER_SM ){
189         deliver_sm_t t1;
190         memset(&t1, 0, sizeof(deliver_sm_t));
191         return( work( id, (void*)&t1 ) );
192     } else if( id == DELIVER_SM_RESP ){
193         deliver_sm_resp_t t1;
194         memset(&t1, 0, sizeof(deliver_sm_resp_t));
195         return( work( id, (void*)&t1 ) );
196     } else if( id == DATA_SM ){
197         data_sm_t t1;
198         memset(&t1, 0, sizeof(data_sm_t));
199         return( work( id, (void*)&t1 ) );
200     } else if( id == DATA_SM_RESP ){
201         data_sm_resp_t t1;
202         memset(&t1, 0, sizeof(data_sm_resp_t));
203         return( work( id, (void*)&t1 ) );
204     } else if( id == QUERY_SM ){
205         query_sm_t t1;
206         memset(&t1, 0, sizeof(query_sm_t));
207         return( work( id, (void*)&t1 ) );
208     } else if( id == QUERY_SM_RESP ){
209         query_sm_resp_t t1;
210         memset(&t1, 0, sizeof(query_sm_resp_t));
211         return( work( id, (void*)&t1 ) );
212     } else if( id == CANCEL_SM ){
213         cancel_sm_t t1;
214         memset(&t1, 0, sizeof(cancel_sm_t));
215         return( work( id, (void*)&t1 ) );
216     } else if( id == CANCEL_SM_RESP ){
217         cancel_sm_resp_t t1;
218         memset(&t1, 0, sizeof(cancel_sm_resp_t));
219         return( work( id, (void*)&t1 ) );
220     } else if( id == REPLACE_SM ){
221         replace_sm_t t1;
222         memset(&t1, 0, sizeof(replace_sm_t));
223         return( work( id, (void*)&t1 ) );
224     } else if( id == REPLACE_SM_RESP ){
225         replace_sm_resp_t t1;
226         memset(&t1, 0, sizeof(replace_sm_resp_t));
227         return( work( id, (void*)&t1 ) );
228     } else if( id == ENQUIRE_LINK ){
229         enquire_link_t t1;
230         memset(&t1, 0, sizeof(enquire_link_t));
231         return( work( id, (void*)&t1 ) );
232     } else if( id == ENQUIRE_LINK_RESP ){
233         enquire_link_resp_t t1;
234         memset(&t1, 0, sizeof(enquire_link_resp_t));
235         return( work( id, (void*)&t1 ) );
236     } else if( id == ALERT_NOTIFICATION ){
237         alert_notification_t t1;
238         memset(&t1, 0, sizeof(alert_notification_t));
239         return( work( id, (void*)&t1 ) );
240     } else {
241         printf("Invalid SMPP PDU [%08X].\n", id);
242     };
243     return( -1 );
244 
245 };
246