comparison utils/smpp-test1.c @ 220:c798a1762c7c

smpp-test1: take strings on the command line, construct bind req
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 01 Aug 2023 23:29:38 -0800
parents 9ba474e918c0
children
comparison
equal deleted inserted replaced
219:9ba474e918c0 220:c798a1762c7c
12 #include <stdlib.h> 12 #include <stdlib.h>
13 #include <string.h> 13 #include <string.h>
14 #include <strings.h> 14 #include <strings.h>
15 #include <unistd.h> 15 #include <unistd.h>
16 16
17 static const u_char bind_req[] = {
18 0x00, 0x00, 0x00, 0x23, /* command_length */
19 0x00, 0x00, 0x00, 0x09, /* command_id: bind_transceiver */
20 0x00, 0x00, 0x00, 0x00, /* command_status */
21 0x00, 0x00, 0x00, 0x01, /* sequence_number */
22 'G','S','M','-','S','M','S','C',0, /* system_id */
23 0, /* password: empty string */
24 'S','M','P','P',0, /* system_type */
25 0x34, /* interface_version */
26 0, 0, 0 /* empty address params */
27 };
28
29 static int tcpsock; 17 static int tcpsock;
30 static struct sockaddr_in server_sin; 18 static struct sockaddr_in server_sin;
19 static u_char bind_req[64];
20 static unsigned bind_req_len;
21 static char system_id[16], password[9];
31 static u_char rx_hdr[16]; 22 static u_char rx_hdr[16];
32 static unsigned rx_pkt_len; 23 static unsigned rx_pkt_len;
24
25 static void
26 construct_bind_req()
27 {
28 u_char *dp;
29 unsigned slen;
30
31 dp = bind_req + 4; /* length will be filled last */
32 /* command_id */
33 *dp++ = 0;
34 *dp++ = 0;
35 *dp++ = 0;
36 *dp++ = 0x09; /* bind_transceiver */
37 /* empty command_status */
38 *dp++ = 0;
39 *dp++ = 0;
40 *dp++ = 0;
41 *dp++ = 0;
42 /* sequence_number */
43 *dp++ = 0;
44 *dp++ = 0;
45 *dp++ = 0;
46 *dp++ = 1;
47 /* system_id */
48 slen = strlen(system_id) + 1;
49 bcopy(system_id, dp, slen);
50 dp += slen;
51 /* password */
52 slen = strlen(password) + 1;
53 bcopy(password, dp, slen);
54 dp += slen;
55 /* system_type */
56 strcpy(dp, "SMPP");
57 dp += 5;
58 /* interface_version */
59 *dp++ = 0x34;
60 /* addr_ton */
61 *dp++ = 0;
62 /* addr_npi */
63 *dp++ = 0;
64 /* address_range */
65 *dp++ = 0;
66 bind_req_len = dp - bind_req;
67 bind_req[0] = bind_req_len >> 24;
68 bind_req[1] = bind_req_len >> 16;
69 bind_req[2] = bind_req_len >> 8;
70 bind_req[3] = bind_req_len;
71 }
72
73 static void
74 print_bind_req()
75 {
76 unsigned off, chunk;
77 int i, c;
78
79 printf("Constructed bind request of %u bytes\n", bind_req_len);
80 for (off = 0; off < bind_req_len; off += chunk) {
81 chunk = bind_req_len - off;
82 if (chunk > 16)
83 chunk = 16;
84 printf("%02X: ", off);
85 for (i = 0; i < 16; i++) {
86 if (i < chunk)
87 printf("%02X ", bind_req[off + i]);
88 else
89 fputs(" ", stdout);
90 if (i == 7 || i == 15)
91 putchar(' ');
92 }
93 for (i = 0; i < chunk; i++) {
94 c = bind_req[off + i];
95 if (c < ' ' || c > '~')
96 c = '.';
97 putchar(c);
98 }
99 putchar('\n');
100 }
101 }
33 102
34 static void 103 static void
35 init_stage() 104 init_stage()
36 { 105 {
37 int rc; 106 int rc;
40 sizeof(struct sockaddr_in)); 109 sizeof(struct sockaddr_in));
41 if (rc < 0) { 110 if (rc < 0) {
42 perror("connect"); 111 perror("connect");
43 exit(1); 112 exit(1);
44 } 113 }
45 rc = write(tcpsock, bind_req, sizeof bind_req); 114 rc = write(tcpsock, bind_req, bind_req_len);
46 if (rc != sizeof bind_req) { 115 if (rc != bind_req_len) {
47 perror("write"); 116 perror("write");
48 exit(1); 117 exit(1);
49 } 118 }
50 } 119 }
51 120
125 } 194 }
126 195
127 main(argc, argv) 196 main(argc, argv)
128 char **argv; 197 char **argv;
129 { 198 {
130 if (argc != 2) { 199 if (argc < 3 || argc > 4) {
131 fprintf(stderr, "usage: %s server-ip\n", argv[0]); 200 fprintf(stderr, "usage: %s server-ip system-id [password]\n",
201 argv[0]);
132 exit(1); 202 exit(1);
133 } 203 }
134 server_sin.sin_family = AF_INET; 204 server_sin.sin_family = AF_INET;
135 server_sin.sin_addr.s_addr = inet_addr(argv[1]); 205 server_sin.sin_addr.s_addr = inet_addr(argv[1]);
136 if (server_sin.sin_addr.s_addr == INADDR_NONE) { 206 if (server_sin.sin_addr.s_addr == INADDR_NONE) {
137 fprintf(stderr, "error: invalid IP address argument \"%s\"\n", 207 fprintf(stderr, "error: invalid IP address argument \"%s\"\n",
138 argv[1]); 208 argv[1]);
139 exit(1); 209 exit(1);
140 } 210 }
141 server_sin.sin_port = htons(2775); 211 server_sin.sin_port = htons(2775);
212 if (strlen(argv[2]) > 15) {
213 fprintf(stderr, "error: system-id string is too long\n");
214 exit(1);
215 }
216 strcpy(system_id, argv[2]);
217 if (argv[3]) {
218 if (strlen(argv[3]) > 8) {
219 fprintf(stderr, "error: password string is too long\n");
220 exit(1);
221 }
222 strcpy(password, argv[3]);
223 }
224 construct_bind_req();
225 setlinebuf(stdout);
226 print_bind_req();
142 tcpsock = socket(AF_INET, SOCK_STREAM, 0); 227 tcpsock = socket(AF_INET, SOCK_STREAM, 0);
143 if (tcpsock < 0) { 228 if (tcpsock < 0) {
144 perror("socket"); 229 perror("socket");
145 exit(1); 230 exit(1);
146 } 231 }
147 setlinebuf(stdout);
148 init_stage(); 232 init_stage();
149 for (;;) { 233 for (;;) {
150 rx_bytes(rx_hdr, 16); 234 rx_bytes(rx_hdr, 16);
151 print_hdr(); 235 print_hdr();
152 preen_rx_len(); 236 preen_rx_len();