comparison lcdtest/connect.c @ 28:de3d3cfcbb35

lcdtest: lcdphone program put together, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 27 May 2018 22:26:58 +0000
parents
children
comparison
equal deleted inserted replaced
27:4b7cac119fb5 28:de3d3cfcbb35
1 /*
2 * Connecting to an already running rvinterf process
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 <rvinterf/localsock.h>
14 #include "exitcodes.h"
15
16 char *socket_pathname = "/tmp/rvinterf_socket";
17 int sock;
18
19 connect_local_socket()
20 {
21 /* local socket binding voodoo copied from osmocon */
22 struct sockaddr_un local;
23 unsigned int namelen;
24 int rc;
25
26 sock = socket(AF_UNIX, SOCK_STREAM, 0);
27 if (sock < 0) {
28 perror("socket(AF_UNIX, SOCK_STREAM, 0)");
29 exit(ERROR_UNIX);
30 }
31
32 local.sun_family = AF_UNIX;
33 strncpy(local.sun_path, socket_pathname, sizeof(local.sun_path));
34 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
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 = connect(sock, (struct sockaddr *) &local, namelen);
49 if (rc != 0) {
50 perror(socket_pathname);
51 exit(ERROR_RVINTERF);
52 }
53
54 return(0);
55 }