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