annotate smpp-trx-sa/tcpconn.c @ 247:c6a3fb913c53

top Makefile: mtctest changed to use libnumdb2
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 14 Aug 2023 22:39:28 -0800
parents 9d6e8d99d2b1
children 1bf989f60aa3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
222
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements the part of smpp-trx-sa that handles
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * the TCP connection to the SMPP server.
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/socket.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <netinet/in.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <arpa/inet.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdio.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <string.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <strings.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <unistd.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 extern FILE *logF;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 int tcpsock;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 static u_char rx_buf[0x10000];
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 static unsigned rx_accum, rx_pdu_len;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 static int rx_body;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 void
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 open_tcp_conn(server_sin)
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 struct sockaddr_in *server_sin;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 int rc;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 tcpsock = socket(AF_INET, SOCK_STREAM, 0);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (tcpsock < 0) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 perror("socket(AF_INET, SOCK_STREAM, 0)");
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 exit(1);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 rc = connect(tcpsock, (struct sockaddr *) server_sin,
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 sizeof(struct sockaddr_in));
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 if (rc < 0) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 perror("TCP connect");
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 exit(1);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 static void
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 got_full_pdu()
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 unsigned command_id;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 /* prepare for next PDU Rx */
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 rx_body = 0;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 rx_accum = 0;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 /* back to the one we just got */
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 command_id = (rx_buf[4] << 24) | (rx_buf[5] << 16) | (rx_buf[6] << 8) |
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 rx_buf[7];
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (command_id == 0x15 && rx_pdu_len == 16) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 send_enq_link_resp(rx_buf);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 return;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 log_rx_pdu(rx_buf, rx_pdu_len);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 if (command_id == 0x05 || command_id == 0x103)
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 send_message_resp(rx_buf);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 void
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 tcpsock_select_handler()
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 unsigned goal;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 int cc;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 if (rx_body)
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 goal = rx_pdu_len;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 else
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 goal = 16;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 cc = read(tcpsock, rx_buf + rx_accum, goal - rx_accum);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 if (cc < 0) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 perror("read from TCP socket");
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 log_fatal_error("error reading from TCP socket");
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 exit(1);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 if (cc == 0) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 log_fatal_error("Server closed TCP connection");
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 exit(1);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 rx_accum += cc;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 if (rx_accum < goal)
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 return;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 if (rx_body) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 got_full_pdu();
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 return;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 if (rx_buf[0] || rx_buf[1]) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 log_rx_pdu(rx_buf, 16);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 fprintf(logF, "Fatal error: length exceeds limit\n");
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 exit(1);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 rx_pdu_len = (rx_buf[2] << 8) | rx_buf[3];
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 if (rx_pdu_len < 16) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 log_rx_pdu(rx_buf, 16);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 fprintf(logF, "Fatal error: length below 16\n");
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 exit(1);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 if (rx_pdu_len == 16) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 got_full_pdu();
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 return;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 rx_body = 1;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 }