annotate smpp-trx-sa/localsock.c @ 246:74dae755f85a

themwi-test-mtc: convert to libnumdb2
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 14 Aug 2023 22:37:11 -0800
parents 9d6e8d99d2b1
children
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 local socket part of smpp-trx-sa.
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
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 #include <sys/types.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/socket.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/un.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <unistd.h>
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 int localsock;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 void
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 create_local_socket(pathname)
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 char *pathname;
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 struct sockaddr_un sa;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 unsigned sa_len;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 int rc;
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 localsock = socket(AF_UNIX, SOCK_DGRAM, 0);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 if (localsock < 0) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 perror("socket(AF_UNIX, SOCK_DGRAM, 0)");
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 exit(1);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 unlink(pathname);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 fill_sockaddr_un(pathname, &sa, &sa_len);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 rc = bind(localsock, (struct sockaddr *) &sa, sa_len);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 if (rc < 0) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 fprintf(stderr, "bind error on %s\n", pathname);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 perror("bind");
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 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 void
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 localsock_select_handler()
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 u_char buf[0x10000];
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 int cc;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 unsigned pdu_len;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 cc = recv(localsock, buf, sizeof buf, 0);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (cc <= 0) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 perror("read from local socket");
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 log_fatal_error("error reading from local socket");
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 exit(1);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (cc < 16) {
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 inv_pdu: log_fatal_error("Received invalid PDU on local socket");
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 return;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (buf[0] || buf[1])
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 goto inv_pdu;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 pdu_len = (buf[2] << 8) | buf[3];
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 if (pdu_len != cc)
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 goto inv_pdu;
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 send_pdu_from_localsock(buf, pdu_len);
9d6e8d99d2b1 smpp-trx-sa: new program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 }