comparison smpp-trx-sa/tcpconn.c @ 222:9d6e8d99d2b1

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