comparison rvinterf/libasync/init.c @ 335:40b8557b9d04

rvinterf suite: libasync factored out of fc-tmsh
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 22 Apr 2014 06:51:09 +0000
parents rvinterf/tmsh/init.c@c146f38d2b5f
children
comparison
equal deleted inserted replaced
334:73a2b359b3cd 335:40b8557b9d04
1 /*
2 * This module contains the common initialization code for fc-tmsh and g23sh.
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
16 extern char *socket_pathname;
17 extern 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(1);
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(1);
52 }
53
54 return(0);
55 }
56
57 send_init_command(cmdpkt, cmdlen)
58 u_char *cmdpkt;
59 {
60 u_char lenbuf[2];
61
62 lenbuf[0] = 0;
63 lenbuf[1] = cmdlen;
64 write(sock, lenbuf, 2);
65 write(sock, cmdpkt, cmdlen);
66 }