comparison rvinterf/libinterf/connect.c @ 419:67d683a87b1d

rvinterf client refactoring: libinterf created
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 27 Oct 2018 23:35:10 +0000
parents rvinterf/etmsync/connect.c@e7502631a0f9
children
comparison
equal deleted inserted replaced
418:cb3e8d080a23 419:67d683a87b1d
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 "pktmux.h"
14 #include "localsock.h"
15 #include "exitcodes.h"
16
17 char *socket_pathname = "/tmp/rvinterf_socket";
18 int sock;
19
20 connect_local_socket()
21 {
22 /* local socket binding voodoo copied from osmocon */
23 struct sockaddr_un local;
24 unsigned int namelen;
25 int rc;
26
27 sock = socket(AF_UNIX, SOCK_STREAM, 0);
28 if (sock < 0) {
29 perror("socket(AF_UNIX, SOCK_STREAM, 0)");
30 exit(ERROR_UNIX);
31 }
32
33 local.sun_family = AF_UNIX;
34 strncpy(local.sun_path, socket_pathname, sizeof(local.sun_path));
35 local.sun_path[sizeof(local.sun_path) - 1] = '\0';
36
37 /* we use the same magic that X11 uses in Xtranssock.c for
38 * calculating the proper length of the sockaddr */
39 #if defined(BSD44SOCKETS) || defined(__UNIXWARE__)
40 local.sun_len = strlen(local.sun_path);
41 #endif
42 #if defined(BSD44SOCKETS) || defined(SUN_LEN)
43 namelen = SUN_LEN(&local);
44 #else
45 namelen = strlen(local.sun_path) +
46 offsetof(struct sockaddr_un, sun_path) + 1;
47 #endif
48
49 rc = connect(sock, (struct sockaddr *) &local, namelen);
50 if (rc != 0) {
51 perror(socket_pathname);
52 exit(ERROR_RVINTERF);
53 }
54
55 return(0);
56 }