FreeCalypso > hg > themwi-system-sw
view utils/smpp-test1.c @ 240:c7a3ba56c636
themwi-short-dial: fix bug in printf arguments
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 14 Aug 2023 15:37:59 -0800 |
parents | c798a1762c7c |
children |
line wrap: on
line source
/* * This program connects to an SMPP server in the role of a client, * sends a bind_transceiver request, and then dumps everything * that comes back. */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <unistd.h> static int tcpsock; static struct sockaddr_in server_sin; static u_char bind_req[64]; static unsigned bind_req_len; static char system_id[16], password[9]; static u_char rx_hdr[16]; static unsigned rx_pkt_len; static void construct_bind_req() { u_char *dp; unsigned slen; dp = bind_req + 4; /* length will be filled last */ /* command_id */ *dp++ = 0; *dp++ = 0; *dp++ = 0; *dp++ = 0x09; /* bind_transceiver */ /* empty command_status */ *dp++ = 0; *dp++ = 0; *dp++ = 0; *dp++ = 0; /* sequence_number */ *dp++ = 0; *dp++ = 0; *dp++ = 0; *dp++ = 1; /* system_id */ slen = strlen(system_id) + 1; bcopy(system_id, dp, slen); dp += slen; /* password */ slen = strlen(password) + 1; bcopy(password, dp, slen); dp += slen; /* system_type */ strcpy(dp, "SMPP"); dp += 5; /* interface_version */ *dp++ = 0x34; /* addr_ton */ *dp++ = 0; /* addr_npi */ *dp++ = 0; /* address_range */ *dp++ = 0; bind_req_len = dp - bind_req; bind_req[0] = bind_req_len >> 24; bind_req[1] = bind_req_len >> 16; bind_req[2] = bind_req_len >> 8; bind_req[3] = bind_req_len; } static void print_bind_req() { unsigned off, chunk; int i, c; printf("Constructed bind request of %u bytes\n", bind_req_len); for (off = 0; off < bind_req_len; off += chunk) { chunk = bind_req_len - off; if (chunk > 16) chunk = 16; printf("%02X: ", off); for (i = 0; i < 16; i++) { if (i < chunk) printf("%02X ", bind_req[off + i]); else fputs(" ", stdout); if (i == 7 || i == 15) putchar(' '); } for (i = 0; i < chunk; i++) { c = bind_req[off + i]; if (c < ' ' || c > '~') c = '.'; putchar(c); } putchar('\n'); } } static void init_stage() { int rc; rc = connect(tcpsock, (struct sockaddr *) &server_sin, sizeof(struct sockaddr_in)); if (rc < 0) { perror("connect"); exit(1); } rc = write(tcpsock, bind_req, bind_req_len); if (rc != bind_req_len) { perror("write"); exit(1); } } static void rx_bytes(buf, need_len) u_char *buf; unsigned need_len; { int cc; unsigned remain; for (remain = need_len; remain; remain -= cc) { cc = read(tcpsock, buf, remain); if (cc <= 0) { perror("read"); exit(1); } } } static void print_hdr() { int i, j, pos; fputs("Got header:", stdout); pos = 0; for (i = 0; i < 4; i++) { putchar(' '); for (j = 0; j < 4; j++) printf("%02X", rx_hdr[pos++]); } putchar('\n'); } static void preen_rx_len() { rx_pkt_len = (rx_hdr[0] << 24) | (rx_hdr[1] << 16) | (rx_hdr[2] << 8) | rx_hdr[3]; printf("Rx packet length: %u bytes\n", rx_pkt_len); if (rx_pkt_len < 16) { printf("Error: packet length is too short\n"); exit(1); } } static void read_and_dump_body() { u_char buf[16]; unsigned offset, chunk; int i, c; for (offset = 16; offset < rx_pkt_len; offset += chunk) { chunk = rx_pkt_len - offset; if (chunk > 16) chunk = 16; rx_bytes(buf, chunk); printf("%08X: ", offset); for (i = 0; i < 16; i++) { if (i < chunk) printf("%02X ", buf[i]); else fputs(" ", stdout); if (i == 7 || i == 15) putchar(' '); } for (i = 0; i < chunk; i++) { c = buf[i]; if (c < ' ' || c > '~') c = '.'; putchar(c); } putchar('\n'); } } main(argc, argv) char **argv; { if (argc < 3 || argc > 4) { fprintf(stderr, "usage: %s server-ip system-id [password]\n", argv[0]); exit(1); } server_sin.sin_family = AF_INET; server_sin.sin_addr.s_addr = inet_addr(argv[1]); if (server_sin.sin_addr.s_addr == INADDR_NONE) { fprintf(stderr, "error: invalid IP address argument \"%s\"\n", argv[1]); exit(1); } server_sin.sin_port = htons(2775); if (strlen(argv[2]) > 15) { fprintf(stderr, "error: system-id string is too long\n"); exit(1); } strcpy(system_id, argv[2]); if (argv[3]) { if (strlen(argv[3]) > 8) { fprintf(stderr, "error: password string is too long\n"); exit(1); } strcpy(password, argv[3]); } construct_bind_req(); setlinebuf(stdout); print_bind_req(); tcpsock = socket(AF_INET, SOCK_STREAM, 0); if (tcpsock < 0) { perror("socket"); exit(1); } init_stage(); for (;;) { rx_bytes(rx_hdr, 16); print_hdr(); preen_rx_len(); read_and_dump_body(); } }