浏览代码

Merge 'utils/kamunix' of kamailio svn into sip-router/utils

* 'utils/kamunix' of kamailio svn:
  - fix another linking error on solaris
  - fix linking errors on solaris, this needs lresolv, reported
  - don't link with unnecessary libs, related to bug #1855859
  - renamed: openserunix -> kamunix
Jan Janak 16 年之前
父节点
当前提交
8c187e76a2
共有 3 个文件被更改,包括 205 次插入0 次删除
  1. 16 0
      utils/kamunix/Makefile
  2. 57 0
      utils/kamunix/kamunix.8
  3. 132 0
      utils/kamunix/kamunix.c

+ 16 - 0
utils/kamunix/Makefile

@@ -0,0 +1,16 @@
+# $Id$
+#
+#  kamunix Makefile
+#
+
+include ../../Makefile.defs
+
+auto_gen=
+NAME=kamunix
+
+include ../../Makefile.sources
+
+include ../../Makefile.rules
+
+modules:
+

+ 57 - 0
utils/kamunix/kamunix.8

@@ -0,0 +1,57 @@
+.\" $Id$
+.TH kamunix 8 21.06.2006 kamailio "Kamailio" 
+.\" Process with
+.\" groff -man -Tascii kamunix.8 
+.\"
+.SH NAME
+kamunix \- Kamailio UNIX socket wrapper
+.SH SYNOPSIS
+.B kamunix
+.BI path_to_socket
+
+.SH DESCRIPTION
+.B kamunix
+is a wrapper for sending external commands
+.B to Kamailio SIP server
+via UNIX sockets interface.
+.br
+This is a binary alternative to the textual FIFO interface.
+.PP
+.B kamunix
+reads from standard input one
+.B Kamailio
+command along with its paramters (if any) and prints the response 
+to standard output.
+
+.SH PARAMETERS
+.TP 3
+.B path_to_socket
+full path of the UNIX socket file used by Kamailio to receive the
+external commands
+
+.SH EXAMPLES
+.PP
+An Kamailio commands consists in one ore more lines: first contains the
+command name enclosed between ":", the following lines containing the 
+command parameters, one per line.
+.PP
+echo ":uptime:" | kamunix /tmp/kamailio.sock
+
+
+.SH AUTHORS
+
+see 
+.B /usr/share/doc/kamailio/AUTHORS
+
+.SH SEE ALSO
+.BR kamailio(8), kamailio.cfg(5), kamctl(8)
+.PP
+Full documentation on Kamailio is available at
+.I http://www.kamailio.org/.
+.PP
+Mailing lists:
+.nf 
[email protected] - Kamailio user community
+.nf 
[email protected] - Kamailio development, new features and unstable version
+

+ 132 - 0
utils/kamunix/kamunix.c

@@ -0,0 +1,132 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2004 FhG FOKUS
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Kamailio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * Kamailio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License 
+ * along with this program; if not, write to the Free Software 
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <errno.h>
+
+
+/* AF_LOCAL is not defined on solaris */
+#if !defined(AF_LOCAL)
+#define AF_LOCAL AF_UNIX
+#endif
+#if !defined(PF_LOCAL)
+#define PF_LOCAL PF_UNIX
+#endif
+
+
+/* solaris doesn't have SUN_LEN */
+#ifndef SUN_LEN
+#define SUN_LEN(sa)	 ( strlen((sa)->sun_path) + \
+					 (size_t)(((struct sockaddr_un*)0)->sun_path) )
+#endif
+
+
+#define BUF_SIZE 65536
+#define DEFAULT_TIMEOUT 5
+
+int main(int argc, char** argv)
+{
+	int sock, len;
+	socklen_t from_len;
+	struct sockaddr_un from, to;
+	char name[256];
+	static char buffer[BUF_SIZE];
+	char *chroot_dir;
+
+	if (argc != 2) {
+		printf("Usage: %s <path_to_socket>\n", argv[0]);
+		return 1;
+	}
+
+	sock = socket(PF_LOCAL, SOCK_DGRAM, 0);
+	if (sock == -1) {
+		fprintf(stderr, "Error while opening socket: %s\n", strerror(errno));
+		return -1;
+	}
+	
+	memset(&from, 0, sizeof(from));
+	from.sun_family = PF_LOCAL;
+
+	chroot_dir = getenv("CHROOT_DIR");
+	if (chroot_dir == NULL)
+		chroot_dir = "";
+	sprintf(name, "%s/tmp/Kamailio.%d.XXXXXX", chroot_dir, getpid());
+	umask(0); /* set mode to 0666 for when Kamailio is running as non-root user and kamctl is running as root */
+
+	if (mkstemp(name) == -1) {
+		fprintf(stderr, "Error in mkstemp with name=%s: %s\n", name, strerror(errno));
+		return -2;
+	}
+	if (unlink(name) == -1) {
+		fprintf(stderr, "Error in unlink of %s: %s\n", name, strerror(errno));
+		return -2;
+	}
+	strncpy(from.sun_path, name, strlen(name));
+
+	if (bind(sock, (struct sockaddr*)&from, SUN_LEN(&from)) == -1) {
+		fprintf(stderr, "Error in bind: %s\n", strerror(errno));
+		goto err;
+	}
+
+	memset(&to, 0, sizeof(to));
+	to.sun_family = PF_LOCAL;
+	strncpy(to.sun_path, argv[1], sizeof(to.sun_path) - 1);
+	
+	len = fread(buffer, 1, BUF_SIZE, stdin);
+
+	if (len) {
+		if (sendto(sock, buffer, len, 0, (struct sockaddr*)&to, SUN_LEN(&to)) == -1) {
+			fprintf(stderr, "Error in sendto: %s\n", strerror(errno));
+		        goto err;
+		}
+		from_len = sizeof(from);
+		len = recvfrom(sock, buffer, BUF_SIZE, 0, (struct sockaddr*)&from, &from_len);
+		if (len == -1) {
+			fprintf(stderr, "Error in recvfrom: %s\n", strerror(errno));
+			goto err;
+		}
+
+		fprintf(stdout, "%.*s", len, buffer);
+	} else {
+		fprintf(stderr, "Nothing to send\n");
+		goto err;
+	}
+
+	close(sock);
+	if (unlink(name) == -1)
+		fprintf(stderr, "Error in unlink of %s: %s\n", name, strerror(errno));
+	return 0;
+
+ err:
+	close(sock);
+	if (unlink(name) == -1)
+		fprintf(stderr, "Error in unlink of %s: %s\n", name, strerror(errno));
+	return -1;
+}