comparison rfcal/cmu200/socket.c @ 195:db9ee7745cdd

fc-cmu200d: socket handling skeleton added
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 24 Apr 2017 01:43:02 +0000
parents
children 47d56330609d
comparison
equal deleted inserted replaced
194:31d43f0e469a 195:db9ee7745cdd
1 /*
2 * This module handles the local UNIX domain socket interface for fc-cmu200d.
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
14 int listener, activesock;
15
16 extern char *bind_socket_pathname;
17
18 create_listener_socket()
19 {
20 /* local socket binding voodoo copied from osmocon */
21 struct sockaddr_un local;
22 unsigned int namelen;
23 int rc;
24
25 listener = socket(AF_UNIX, SOCK_STREAM, 0);
26 if (listener < 0) {
27 perror("socket(AF_UNIX, SOCK_STREAM, 0)");
28 exit(1);
29 }
30
31 local.sun_family = AF_UNIX;
32 strncpy(local.sun_path, bind_socket_pathname, sizeof(local.sun_path));
33 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
34 unlink(local.sun_path);
35
36 /* we use the same magic that X11 uses in Xtranssock.c for
37 * calculating the proper length of the sockaddr */
38 #if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
39 local.sun_len = strlen(local.sun_path);
40 #endif
41 #if defined(BSD44SOCKETS) || defined(SUN_LEN)
42 namelen = SUN_LEN(&local);
43 #else
44 namelen = strlen(local.sun_path) +
45 offsetof(struct sockaddr_un, sun_path) + 1;
46 #endif
47
48 rc = bind(listener, (struct sockaddr *) &local, namelen);
49 if (rc != 0) {
50 perror("bind on local socket");
51 exit(1);
52 }
53 rc = listen(listener, 1);
54 if (rc != 0) {
55 perror("listen");
56 exit(1);
57 }
58 return(0);
59 }
60
61 get_socket_connection()
62 {
63 struct sockaddr_un un_addr;
64 socklen_t len;
65
66 len = sizeof(un_addr);
67 activesock = accept(listener, (struct sockaddr *) &un_addr, &len);
68 if (activesock < 0) {
69 perror("socket accept");
70 exit(1);
71 }
72 printf("Accepted local socket connection\n");
73 return(0);
74 }
75
76 send_socket_response(str)
77 char *str;
78 {
79 write(activesock, str, strlen(str));
80 }