FreeCalypso > hg > themwi-system-sw
annotate smpp-trx-sa/tcpconn.c @ 268:d6630a2d6e80
doc: beginning of numbering plan documentation
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 13 Nov 2023 18:20:57 -0800 |
parents | 1bf989f60aa3 |
children |
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); |
263
1bf989f60aa3
smpp-trx-sa: log times of enquire_link packets
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
56 log_enquire_link_item(); |
222
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 return; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 } |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 log_rx_pdu(rx_buf, rx_pdu_len); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 if (command_id == 0x05 || command_id == 0x103) |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 send_message_resp(rx_buf); |
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 |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 void |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 tcpsock_select_handler() |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 unsigned goal; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 int cc; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 if (rx_body) |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 goal = rx_pdu_len; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 else |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 goal = 16; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 cc = read(tcpsock, rx_buf + rx_accum, goal - rx_accum); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 if (cc < 0) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 perror("read from TCP socket"); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 log_fatal_error("error reading from TCP socket"); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 exit(1); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 } |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 if (cc == 0) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 log_fatal_error("Server closed TCP connection"); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 exit(1); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 } |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 rx_accum += cc; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 if (rx_accum < goal) |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 return; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 if (rx_body) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 got_full_pdu(); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 return; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 } |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 if (rx_buf[0] || rx_buf[1]) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 log_rx_pdu(rx_buf, 16); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 fprintf(logF, "Fatal error: length exceeds limit\n"); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 exit(1); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 } |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 rx_pdu_len = (rx_buf[2] << 8) | rx_buf[3]; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 if (rx_pdu_len < 16) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 log_rx_pdu(rx_buf, 16); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 fprintf(logF, "Fatal error: length below 16\n"); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 exit(1); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 } |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 if (rx_pdu_len == 16) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 got_full_pdu(); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 return; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 } |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 rx_body = 1; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 } |