comparison rvinterf/lowlevel/localsock.c @ 177:fef035264dd4

rvinterf: beginning of local socket handling
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 23 Nov 2013 20:15:02 +0000
parents rvinterf/old/rvtdump_tx.c@f42854da4563
children 7ab6b29e76bb
comparison
equal deleted inserted replaced
176:7f727aaf5cd4 177:fef035264dd4
1 /*
2 * This rvinterf module handles the local UNIX domain socket interface
3 */
4
5 #include <sys/types.h>
6 #include <sys/socket.h>
7 #include <sys/un.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <strings.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include "../localsock.h"
14 #include "client.h"
15
16 static char sockpath[] = "/tmp/rvinterf_socket";
17
18 int listener;
19
20 extern struct client *client_head;
21 extern int max_fd;
22
23 create_listener_socket()
24 {
25 /* local socket binding voodoo copied from osmocon */
26 struct sockaddr_un local;
27 unsigned int namelen;
28 int rc;
29
30 listener = socket(AF_UNIX, SOCK_STREAM, 0);
31 if (listener < 0) {
32 perror("socket(AF_UNIX, SOCK_STREAM, 0)");
33 exit(1);
34 }
35
36 local.sun_family = AF_UNIX;
37 strncpy(local.sun_path, sockpath, sizeof(local.sun_path));
38 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
39 unlink(local.sun_path);
40
41 /* we use the same magic that X11 uses in Xtranssock.c for
42 * calculating the proper length of the sockaddr */
43 #if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
44 local.sun_len = strlen(local.sun_path);
45 #endif
46 #if defined(BSD44SOCKETS) || defined(SUN_LEN)
47 namelen = SUN_LEN(&local);
48 #else
49 namelen = strlen(local.sun_path) +
50 offsetof(struct sockaddr_un, sun_path) + 1;
51 #endif
52
53 rc = bind(listener, (struct sockaddr *) &local, namelen);
54 if (rc != 0) {
55 perror("bind on local socket");
56 exit(1);
57 }
58 rc = listen(listener, 3);
59 if (rc != 0) {
60 perror("listen");
61 exit(1);
62 }
63
64 if (listener > max_fd)
65 max_fd = listener;
66 return(0);
67 }
68
69 static void
70 prep_for_length_rx(cli)
71 struct client *cli;
72 {
73 cli->rx_state = 0;
74 cli->rx_ptr = cli->rx_buf;
75 cli->rx_left = 2;
76 }
77
78 static void
79 prep_for_message_rx(cli)
80 struct client *cli;
81 {
82 cli->rx_state = 1;
83 cli->rx_ptr = cli->rx_buf;
84 cli->rx_left = cli->rx_msglen;
85 }
86
87 handle_listener_select()
88 {
89 struct sockaddr_un un_addr;
90 socklen_t len;
91 int rc;
92 struct client *newcli;
93
94 len = sizeof(un_addr);
95 rc = accept(listener, (struct sockaddr *) &un_addr, &len);
96 if (rc < 0) {
97 perror("rvinterf: accept");
98 exit(1);
99 }
100 newcli = malloc(sizeof(struct client));
101 if (!newcli) {
102 perror("rvinterf: malloc for new client");
103 exit(1);
104 }
105 bzero(newcli, sizeof(struct client));
106 newcli->fd = rc;
107 newcli->next = client_head;
108 client_head = newcli;
109 prep_for_length_rx(newcli);
110 return(0);
111 }
112
113 void
114 handle_client_select(cli)
115 struct client *cli;
116 {
117 int cc;
118
119 cc = read(cli->fd, cli->rx_ptr, cli->rx_left);
120 if (cc <= 0) {
121 /* normal client exit condition */
122 close_socket: cli->rx_state = 2;
123 return;
124 }
125 cli->rx_ptr += cc;
126 cli->rx_left -= cc;
127 if (cli->rx_left)
128 return;
129 /* got the thing, process it */
130 if (cli->rx_state) {
131 prep_for_length_rx(cli);
132 /* process_msg_from_client(cli); */
133 } else {
134 cli->rx_msglen = *(u_short *)cli->rx_buf;
135 if (cli->rx_msglen < 1 || cli->rx_msglen > LOCALSOCK_MAX_MSG) {
136 /* TODO: report invalid length to the client */
137 goto close_socket;
138 }
139 prep_for_message_rx(cli);
140 }
141 }