Browse code

- renamed: openserunix -> kamunix

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@4560 689a6050-402a-0410-94f2-e92a70836424

Klaus Darilion authored on 05/08/2008 14:19:32
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,22 @@
1
+# $Id$
2
+#
3
+#  kamunix Makefile
4
+# 
5
+
6
+include ../../Makefile.defs
7
+
8
+auto_gen=
9
+NAME=kamunix
10
+
11
+
12
+include ../../Makefile.sources
13
+
14
+# if you want to tune or reset flags
15
+#DEFS:= 
16
+#LDFLAGS:= 
17
+#LIBS:= 
18
+
19
+include ../../Makefile.rules
20
+
21
+modules:
22
+
0 23
new file mode 100644
... ...
@@ -0,0 +1,57 @@
1
+.\" $Id$
2
+.TH kamunix 8 21.06.2006 kamailio "Kamailio" 
3
+.\" Process with
4
+.\" groff -man -Tascii kamunix.8 
5
+.\"
6
+.SH NAME
7
+kamunix \- Kamailio UNIX socket wrapper
8
+.SH SYNOPSIS
9
+.B kamunix
10
+.BI path_to_socket
11
+
12
+.SH DESCRIPTION
13
+.B kamunix
14
+is a wrapper for sending external commands
15
+.B to Kamailio SIP server
16
+via UNIX sockets interface.
17
+.br
18
+This is a binary alternative to the textual FIFO interface.
19
+.PP
20
+.B kamunix
21
+reads from standard input one
22
+.B Kamailio
23
+command along with its paramters (if any) and prints the response 
24
+to standard output.
25
+
26
+.SH PARAMETERS
27
+.TP 3
28
+.B path_to_socket
29
+full path of the UNIX socket file used by Kamailio to receive the
30
+external commands
31
+
32
+.SH EXAMPLES
33
+.PP
34
+An Kamailio commands consists in one ore more lines: first contains the
35
+command name enclosed between ":", the following lines containing the 
36
+command parameters, one per line.
37
+.PP
38
+echo ":uptime:" | kamunix /tmp/kamailio.sock
39
+
40
+
41
+.SH AUTHORS
42
+
43
+see 
44
+.B /usr/share/doc/kamailio/AUTHORS
45
+
46
+.SH SEE ALSO
47
+.BR kamailio(8), kamailio.cfg(5), kamctl(8)
48
+.PP
49
+Full documentation on Kamailio is available at
50
+.I http://www.kamailio.org/.
51
+.PP
52
+Mailing lists:
53
+.nf 
54
+users@lists.kamailio.org - Kamailio user community
55
+.nf 
56
+devel@lists.kamailio.org - Kamailio development, new features and unstable version
57
+
0 58
new file mode 100644
... ...
@@ -0,0 +1,132 @@
1
+/*
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2004 FhG FOKUS
5
+ *
6
+ * This file is part of Kamailio, a free SIP server.
7
+ *
8
+ * Kamailio is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * Kamailio is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License 
19
+ * along with this program; if not, write to the Free Software 
20
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
+ *
22
+ */
23
+
24
+#include <stdio.h>
25
+#include <string.h>
26
+#include <unistd.h>
27
+#include <stdlib.h>
28
+#include <sys/stat.h>
29
+#include <sys/types.h>
30
+#include <sys/socket.h>
31
+#include <sys/un.h>
32
+#include <errno.h>
33
+
34
+
35
+/* AF_LOCAL is not defined on solaris */
36
+#if !defined(AF_LOCAL)
37
+#define AF_LOCAL AF_UNIX
38
+#endif
39
+#if !defined(PF_LOCAL)
40
+#define PF_LOCAL PF_UNIX
41
+#endif
42
+
43
+
44
+/* solaris doesn't have SUN_LEN */
45
+#ifndef SUN_LEN
46
+#define SUN_LEN(sa)	 ( strlen((sa)->sun_path) + \
47
+					 (size_t)(((struct sockaddr_un*)0)->sun_path) )
48
+#endif
49
+
50
+
51
+#define BUF_SIZE 65536
52
+#define DEFAULT_TIMEOUT 5
53
+
54
+int main(int argc, char** argv)
55
+{
56
+	int sock, len;
57
+	socklen_t from_len;
58
+	struct sockaddr_un from, to;
59
+	char name[256];
60
+	static char buffer[BUF_SIZE];
61
+	char *chroot_dir;
62
+
63
+	if (argc != 2) {
64
+		printf("Usage: %s <path_to_socket>\n", argv[0]);
65
+		return 1;
66
+	}
67
+
68
+	sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
69
+	if (sock == -1) {
70
+		fprintf(stderr, "Error while opening socket: %s\n", strerror(errno));
71
+		return -1;
72
+	}
73
+	
74
+	memset(&from, 0, sizeof(from));
75
+	from.sun_family = PF_LOCAL;
76
+
77
+	chroot_dir = getenv("CHROOT_DIR");
78
+	if (chroot_dir == NULL)
79
+		chroot_dir = "";
80
+	sprintf(name, "%s/tmp/Kamailio.%d.XXXXXX", chroot_dir, getpid());
81
+	umask(0); /* set mode to 0666 for when Kamailio is running as non-root user and kamctl is running as root */
82
+
83
+	if (mkstemp(name) == -1) {
84
+		fprintf(stderr, "Error in mkstemp with name=%s: %s\n", name, strerror(errno));
85
+		return -2;
86
+	}
87
+	if (unlink(name) == -1) {
88
+		fprintf(stderr, "Error in unlink of %s: %s\n", name, strerror(errno));
89
+		return -2;
90
+	}
91
+	strncpy(from.sun_path, name, strlen(name));
92
+
93
+	if (bind(sock, (struct sockaddr*)&from, SUN_LEN(&from)) == -1) {
94
+		fprintf(stderr, "Error in bind: %s\n", strerror(errno));
95
+		goto err;
96
+	}
97
+
98
+	memset(&to, 0, sizeof(to));
99
+	to.sun_family = PF_LOCAL;
100
+	strncpy(to.sun_path, argv[1], sizeof(to.sun_path) - 1);
101
+	
102
+	len = fread(buffer, 1, BUF_SIZE, stdin);
103
+
104
+	if (len) {
105
+		if (sendto(sock, buffer, len, 0, (struct sockaddr*)&to, SUN_LEN(&to)) == -1) {
106
+			fprintf(stderr, "Error in sendto: %s\n", strerror(errno));
107
+		        goto err;
108
+		}
109
+		from_len = sizeof(from);
110
+		len = recvfrom(sock, buffer, BUF_SIZE, 0, (struct sockaddr*)&from, &from_len);
111
+		if (len == -1) {
112
+			fprintf(stderr, "Error in recvfrom: %s\n", strerror(errno));
113
+			goto err;
114
+		}
115
+
116
+		fprintf(stdout, "%.*s", len, buffer);
117
+	} else {
118
+		fprintf(stderr, "Nothing to send\n");
119
+		goto err;
120
+	}
121
+
122
+	close(sock);
123
+	if (unlink(name) == -1)
124
+		fprintf(stderr, "Error in unlink of %s: %s\n", name, strerror(errno));
125
+	return 0;
126
+
127
+ err:
128
+	close(sock);
129
+	if (unlink(name) == -1)
130
+		fprintf(stderr, "Error in unlink of %s: %s\n", name, strerror(errno));
131
+	return -1;
132
+}