annotate rtp-mgr/ctrl_sock.c @ 6:191d58f5c24f

librtpalloc: port the simple client
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 27 May 2024 21:10:01 +0000
parents 247f4bbde24c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * In this module we implement the logic of listening on the RTP
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * allocator control socket and accepting control connections.
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/stat.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/socket.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <sys/un.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <netinet/in.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdio.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdint.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <stdlib.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <string.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <strings.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include <syslog.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 #include <unistd.h>
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 #include "struct.h"
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 #include "select.h"
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 static const char ctrl_socket_pathname[] = "/var/gsm/rtp_alloc_socket";
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 extern void ctrl_message_handler();
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 void
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 ctrlsock_accept_handler(listener_fd)
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 {
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 struct sockaddr_un sa;
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 socklen_t sa_len;
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 int conn_fd;
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 sa_len = sizeof sa;
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 conn_fd = accept(listener_fd, (struct sockaddr *) &sa, &sa_len);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 if (conn_fd < 0) {
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 syslog(LOG_CRIT, "accept on UNIX socket: %m");
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 exit(1);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 }
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 update_max_fd(conn_fd);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 FD_SET(conn_fd, &select_for_read);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 select_handlers[conn_fd] = ctrl_message_handler;
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 syslog(LOG_DEBUG, "accepted ctrl connection");
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 create_ctrl_socket()
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 {
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 struct sockaddr_un sa;
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 unsigned sa_len;
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 int fd, rc;
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 if (fd < 0) {
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 syslog(LOG_CRIT, "socket(AF_UNIX, SOCK_SEQPACKET, 0): %m");
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 return(-1);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 }
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 unlink(ctrl_socket_pathname);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 fill_sockaddr_un(ctrl_socket_pathname, &sa, &sa_len);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 rc = bind(fd, (struct sockaddr *) &sa, sa_len);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 if (rc < 0) {
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 syslog(LOG_ERR, "bind to %s: %m", ctrl_socket_pathname);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 return(-1);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 }
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 rc = listen(fd, 3);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (rc < 0) {
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 syslog(LOG_CRIT, "listen on UNIX socket: %m");
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 return(-1);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 chmod(ctrl_socket_pathname, 0775);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 update_max_fd(fd);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 FD_SET(fd, &select_for_read);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 select_handlers[fd] = ctrlsock_accept_handler;
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 return(0);
247f4bbde24c rtp-mgr: daemon ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }