annotate utils/smpp-test1.c @ 218:211a043a385f

smpp-test1 program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 01 Aug 2023 22:38:04 -0800
parents
children 9ba474e918c0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
218
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program connects to an SMPP server in the role of a client,
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * sends a bind_transceiver request, and then dumps everything
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * that comes back.
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/socket.h>
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <netinet/in.h>
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <arpa/inet.h>
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdio.h>
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdlib.h>
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <string.h>
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <strings.h>
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <unistd.h>
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 static const u_char bind_req[] = {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 0x00, 0x00, 0x00, 0x17, /* command_length */
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 0x00, 0x00, 0x00, 0x09, /* command_id: bind_transceiver */
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 0x00, 0x00, 0x00, 0x00, /* command_status */
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 0x00, 0x00, 0x00, 0x00, /* sequence_number */
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 0, 0, 0, /* empty strings */
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 0x34, /* interface_version */
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 0, 0, 0 /* empty address params */
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 };
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 static int tcpsock;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 static struct sockaddr_in server_sin;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 static u_char rx_hdr[16];
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 static unsigned rx_pkt_len;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 static void
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 init_stage()
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 int rc;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 rc = connect(tcpsock, (struct sockaddr *) &server_sin,
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 sizeof(struct sockaddr_in));
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 if (rc < 0) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 perror("connect");
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 exit(1);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 rc = write(tcpsock, bind_req, sizeof bind_req);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (rc != sizeof bind_req) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 perror("write");
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 exit(1);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 static void
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 rx_bytes(buf, need_len)
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 u_char *buf;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 unsigned need_len;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 int cc;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 unsigned remain;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 for (remain = need_len; remain; remain -= cc) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 cc = read(tcpsock, buf, remain);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if (cc <= 0) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 perror("read");
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 exit(1);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 static void
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 print_hdr()
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 int i, j, pos;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 fputs("Got header:", stdout);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 pos = 0;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 for (i = 0; i < 4; i++) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 putchar(' ');
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 for (j = 0; j < 4; j++)
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 printf("%02X", rx_hdr[pos++]);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 putchar('\n');
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 static void
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 preen_rx_len()
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 rx_pkt_len = (rx_hdr[0] << 24) | (rx_hdr[1] << 16) | (rx_hdr[2] << 8) |
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 rx_hdr[3];
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 printf("Rx packet length: %u bytes\n", rx_pkt_len);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 if (rx_pkt_len < 16) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 printf("Error: packet length is too short\n");
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 exit(1);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 static void
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 read_and_dump_body()
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 u_char buf[16];
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 unsigned offset, chunk;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 int i, c;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 for (offset = 16; offset < rx_pkt_len; offset += chunk) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 chunk = rx_pkt_len - offset;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 if (chunk > 16)
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 chunk = 16;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 rx_bytes(buf, chunk);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 printf("%08X: ", offset);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 for (i = 0; i < 16; i++) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 if (i < chunk)
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 printf("%02X ", buf[i]);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 else
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 fputs(" ", stdout);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 if (i == 7 || i == 15)
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 putchar(' ');
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 for (i = 0; i < chunk; i++) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 c = buf[i];
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 if (c < ' ' || c > '~')
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 c = '.';
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 putchar(c);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 putchar('\n');
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 main(argc, argv)
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 char **argv;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 if (argc != 2) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 fprintf(stderr, "usage: %s server-ip\n", argv[0]);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 exit(1);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 server_sin.sin_family = AF_INET;
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 server_sin.sin_addr.s_addr = inet_addr(argv[1]);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 if (server_sin.sin_addr.s_addr == INADDR_NONE) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 fprintf(stderr, "error: invalid IP address argument \"%s\"\n",
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 argv[1]);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 exit(1);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 server_sin.sin_port = htons(2775);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 tcpsock = socket(AF_INET, SOCK_STREAM, 0);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 if (tcpsock < 0) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 perror("socket");
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 exit(1);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 setlinebuf(stdout);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 init_stage();
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 for (;;) {
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 rx_bytes(rx_hdr, 16);
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 print_hdr();
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 preen_rx_len();
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 read_and_dump_body();
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 }
211a043a385f smpp-test1 program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 }