FreeCalypso > hg > themwi-system-sw
annotate smpp-trx-sa/main.c @ 264:cdc807117841
*/Makefile: change INSTBIN to /opt/themwi/bin (was /usr/local/bin)
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 05 Nov 2023 16:07:14 -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 C file is the main module for smpp-trx-sa - a standalone bind_trx |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * client (ESME role) that writes all incoming messages into a log file |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 * and also allows outgoing messages to be sent via a UNIX domain local |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 * socket (datagram). |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 */ |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <sys/types.h> |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <sys/socket.h> |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <sys/errno.h> |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 #include <netinet/in.h> |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 #include <arpa/inet.h> |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 #include <stdio.h> |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 #include <stdlib.h> |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 #include <string.h> |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 #include <strings.h> |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 #include <time.h> |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 #include <unistd.h> |
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 extern int tcpsock, localsock; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 FILE *logF; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 time_t curtime; |
261
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
24 char fmt_time[32]; |
222
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 int init_done; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 |
261
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
27 static void |
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
28 format_time() |
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
29 { |
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
30 struct tm *tm; |
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
31 |
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
32 tm = gmtime(&curtime); |
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
33 sprintf(fmt_time, "%d-%02d-%02dT%02d:%02d:%02dZ", |
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
34 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, |
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
35 tm->tm_hour, tm->tm_min, tm->tm_sec); |
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
36 } |
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
37 |
222
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 main(argc, argv) |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 char **argv; |
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 struct sockaddr_in server_sin; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 fd_set fds; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 int max_fd, rc; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 |
263
1bf989f60aa3
smpp-trx-sa: log times of enquire_link packets
Mychaela Falconia <falcon@freecalypso.org>
parents:
261
diff
changeset
|
45 if (argc < 6 || argc > 7) { |
222
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 fprintf(stderr, |
263
1bf989f60aa3
smpp-trx-sa: log times of enquire_link packets
Mychaela Falconia <falcon@freecalypso.org>
parents:
261
diff
changeset
|
47 "usage: %s server-ip system-id password log-file socket-pathname [enq-link]\n", |
222
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 argv[0]); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 exit(1); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 } |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 server_sin.sin_family = AF_INET; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 server_sin.sin_addr.s_addr = inet_addr(argv[1]); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 if (server_sin.sin_addr.s_addr == INADDR_NONE) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 fprintf(stderr, "error: invalid IP address argument \"%s\"\n", |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 argv[1]); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 exit(1); |
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 server_sin.sin_port = htons(2775); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 if (strlen(argv[2]) > 15) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 fprintf(stderr, "error: system-id string is too long\n"); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 exit(1); |
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 if (strlen(argv[3]) > 8) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 fprintf(stderr, "error: password string is too long\n"); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 exit(1); |
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 logF = fopen(argv[4], "a"); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 if (!logF) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 perror(argv[4]); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 exit(1); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 } |
263
1bf989f60aa3
smpp-trx-sa: log times of enquire_link packets
Mychaela Falconia <falcon@freecalypso.org>
parents:
261
diff
changeset
|
72 if (argv[6]) |
1bf989f60aa3
smpp-trx-sa: log times of enquire_link packets
Mychaela Falconia <falcon@freecalypso.org>
parents:
261
diff
changeset
|
73 log_enquire_link_open(argv[6]); |
222
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 create_local_socket(argv[5]); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 max_fd = localsock; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 open_tcp_conn(&server_sin); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 if (tcpsock > max_fd) |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 max_fd = tcpsock; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 time(&curtime); |
261
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
80 format_time(); |
222
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 send_bind_req(argv[2], argv[3]); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 init_done = 1; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 /* main select loop */ |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 for (;;) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 FD_ZERO(&fds); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 FD_SET(tcpsock, &fds); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 FD_SET(localsock, &fds); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 rc = select(max_fd+1, &fds, 0, 0, 0); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 if (rc < 0) { |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 if (errno == EINTR) |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 continue; |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 perror("select"); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 log_fatal_error("select syscall error"); |
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 time(&curtime); |
261
a375639e4190
smpp-trx-sa: refactor time formatting code
Mychaela Falconia <falcon@freecalypso.org>
parents:
222
diff
changeset
|
97 format_time(); |
222
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 if (FD_ISSET(tcpsock, &fds)) |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 tcpsock_select_handler(); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 if (FD_ISSET(localsock, &fds)) |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 localsock_select_handler(); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 fflush(logF); |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 } |
9d6e8d99d2b1
smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 } |