comparison smpp-trx-sa/localsock.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
comparison
equal deleted inserted replaced
221:e1d7db9d734c 222:9d6e8d99d2b1
1 /*
2 * This module implements the local socket part of smpp-trx-sa.
3 */
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <sys/un.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 int localsock;
13
14 void
15 create_local_socket(pathname)
16 char *pathname;
17 {
18 struct sockaddr_un sa;
19 unsigned sa_len;
20 int rc;
21
22 localsock = socket(AF_UNIX, SOCK_DGRAM, 0);
23 if (localsock < 0) {
24 perror("socket(AF_UNIX, SOCK_DGRAM, 0)");
25 exit(1);
26 }
27 unlink(pathname);
28 fill_sockaddr_un(pathname, &sa, &sa_len);
29 rc = bind(localsock, (struct sockaddr *) &sa, sa_len);
30 if (rc < 0) {
31 fprintf(stderr, "bind error on %s\n", pathname);
32 perror("bind");
33 exit(1);
34 }
35 }
36
37 void
38 localsock_select_handler()
39 {
40 u_char buf[0x10000];
41 int cc;
42 unsigned pdu_len;
43
44 cc = recv(localsock, buf, sizeof buf, 0);
45 if (cc <= 0) {
46 perror("read from local socket");
47 log_fatal_error("error reading from local socket");
48 exit(1);
49 }
50 if (cc < 16) {
51 inv_pdu: log_fatal_error("Received invalid PDU on local socket");
52 return;
53 }
54 if (buf[0] || buf[1])
55 goto inv_pdu;
56 pdu_len = (buf[2] << 8) | buf[3];
57 if (pdu_len != cc)
58 goto inv_pdu;
59 send_pdu_from_localsock(buf, pdu_len);
60 }