annotate rfcal/cmu200/socket.c @ 196:47d56330609d

fc-cmu200d: skeleton complete, ready to start adding meat
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 24 Apr 2017 02:17:04 +0000
parents db9ee7745cdd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
195
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module handles the local UNIX domain socket interface for fc-cmu200d.
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/socket.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/un.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdio.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <unistd.h>
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 int listener, activesock;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 extern char *bind_socket_pathname;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 create_listener_socket()
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 /* local socket binding voodoo copied from osmocon */
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 struct sockaddr_un local;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 unsigned int namelen;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 int rc;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 listener = socket(AF_UNIX, SOCK_STREAM, 0);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (listener < 0) {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 perror("socket(AF_UNIX, SOCK_STREAM, 0)");
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 exit(1);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 }
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 local.sun_family = AF_UNIX;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 strncpy(local.sun_path, bind_socket_pathname, sizeof(local.sun_path));
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 unlink(local.sun_path);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 /* we use the same magic that X11 uses in Xtranssock.c for
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 * calculating the proper length of the sockaddr */
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 #if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 local.sun_len = strlen(local.sun_path);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 #endif
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 #if defined(BSD44SOCKETS) || defined(SUN_LEN)
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 namelen = SUN_LEN(&local);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 #else
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 namelen = strlen(local.sun_path) +
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 offsetof(struct sockaddr_un, sun_path) + 1;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 #endif
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 rc = bind(listener, (struct sockaddr *) &local, namelen);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (rc != 0) {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 perror("bind on local socket");
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 exit(1);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 rc = listen(listener, 1);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 if (rc != 0) {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 perror("listen");
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 exit(1);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 }
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 return(0);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 get_socket_connection()
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 struct sockaddr_un un_addr;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 socklen_t len;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 len = sizeof(un_addr);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 activesock = accept(listener, (struct sockaddr *) &un_addr, &len);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 if (activesock < 0) {
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 perror("socket accept");
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 exit(1);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 printf("Accepted local socket connection\n");
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 return(0);
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 }
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 send_socket_response(str)
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 char *str;
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 {
196
47d56330609d fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents: 195
diff changeset
79 printf("Msg to client: %s", str);
195
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 write(activesock, str, strlen(str));
db9ee7745cdd fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }