summaryrefslogtreecommitdiff
path: root/src/lib/sockets.c
diff options
context:
space:
mode:
authorSander Vrijders <[email protected]>2016-02-23 17:04:49 +0100
committerSander Vrijders <[email protected]>2016-02-23 17:04:49 +0100
commita3eb060b8d4419c1af85d3acf081f45cdc27708d (patch)
treed24203b64c5d229be028b6e04ab1a2958624bf0d /src/lib/sockets.c
parentd773fdaad545c122ff81acb57080c8baf46f3735 (diff)
downloadouroboros-a3eb060b8d4419c1af85d3acf081f45cdc27708d.tar.gz
ouroboros-a3eb060b8d4419c1af85d3acf081f45cdc27708d.zip
lib: Initial messages for the IRM
This provides the initial messages to be passed between the irmd and libouroboros-irm.
Diffstat (limited to 'src/lib/sockets.c')
-rw-r--r--src/lib/sockets.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/lib/sockets.c b/src/lib/sockets.c
new file mode 100644
index 00000000..8917f55a
--- /dev/null
+++ b/src/lib/sockets.c
@@ -0,0 +1,94 @@
+/*
+ * Ouroboros - Copyright (C) 2016
+ *
+ * The sockets layer to communicate between daemons
+ *
+ * Sander Vrijders <[email protected]>
+ *
+ * This program 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.
+ *
+ * This program 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#define OUROBOROS_PREFIX "libouroboros-sockets"
+
+#include <ouroboros/logs.h>
+#include <ouroboros/common.h>
+#include <ouroboros/sockets.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <sys/stat.h>
+#include <string.h>
+
+int client_socket_open(char * file_name)
+{
+ int sockfd;
+ struct sockaddr_un serv_addr;
+
+ sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (sockfd < 0) {
+ LOG_ERR("Failed to open socket");
+ return -1;
+ }
+
+ serv_addr.sun_family = AF_UNIX;
+ sprintf(serv_addr.sun_path, file_name);
+
+ if (connect(sockfd,
+ (struct sockaddr *) &serv_addr,
+ sizeof(serv_addr))) {
+ LOG_ERR("Failed to connect to server");
+ return -1;
+ }
+
+ return sockfd;
+}
+
+int server_socket_open(char * file_name)
+{
+ int sockfd;
+ struct sockaddr_un serv_addr;
+ struct stat sb;
+
+ if (!stat(file_name, &sb)) {
+ /* File exists */
+ if (unlink(file_name)) {
+ LOG_ERR("Failed to unlink filename: %s",
+ strerror(errno));
+ return -1;
+ }
+ }
+
+ sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (sockfd < 0) {
+ LOG_ERR("Failed to open socket");
+ return -1;
+ }
+
+ serv_addr.sun_family = AF_UNIX;
+ sprintf(serv_addr.sun_path, file_name);
+
+ if (bind(sockfd,
+ (struct sockaddr *) &serv_addr,
+ sizeof(serv_addr))) {
+ LOG_ERR("Failed to bind socket");
+ return -1;
+ }
+
+ if (listen(sockfd, 0)) {
+ LOG_ERR("Failed to listen to socket");
+ return -1;
+ }
+
+ return sockfd;
+}