summaryrefslogtreecommitdiff
path: root/src/lib/ipcp.c
diff options
context:
space:
mode:
authorDimitri Staessens <[email protected]>2016-03-17 15:38:05 +0100
committerDimitri Staessens <[email protected]>2016-03-17 15:38:05 +0100
commitd2bffe6cc47af18099d33b82920df0bb027da207 (patch)
tree54bc0a9cfb51c1759bb2056961c555fcc9979da7 /src/lib/ipcp.c
parent80679807f5ed13fd0a214bc6cd4e1c182d87bfae (diff)
parent02681c391cbc1cc08f2dea5d8499f301d9321147 (diff)
downloadouroboros-d2bffe6cc47af18099d33b82920df0bb027da207.tar.gz
ouroboros-d2bffe6cc47af18099d33b82920df0bb027da207.zip
Merged in sandervrijders/ouroboros/be (pull request #35)
lib: Add communication with IPCP daemon
Diffstat (limited to 'src/lib/ipcp.c')
-rw-r--r--src/lib/ipcp.c122
1 files changed, 114 insertions, 8 deletions
diff --git a/src/lib/ipcp.c b/src/lib/ipcp.c
index 294d518c..6bc3c75f 100644
--- a/src/lib/ipcp.c
+++ b/src/lib/ipcp.c
@@ -31,6 +31,7 @@
#include <ouroboros/logs.h>
#include <ouroboros/config.h>
#include <ouroboros/utils.h>
+#include <ouroboros/sockets.h>
#include <stdlib.h>
#include <string.h>
@@ -38,6 +39,45 @@
#include <sys/types.h>
#include <sys/wait.h>
+static int send_ipcp_msg(pid_t pid,
+ struct ipcp_msg * msg)
+{
+ int sockfd = 0;
+ buffer_t * buf = NULL;
+ char * sock_path;
+
+ sock_path = ipcp_sock_path(pid);
+ if (sock_path == NULL)
+ return -1;
+
+ sockfd = client_socket_open(sock_path);
+ if (sockfd < 0) {
+ free(sock_path);
+ return -1;
+ }
+
+ buf = serialize_ipcp_msg(msg);
+ if (buf == NULL) {
+ free(sock_path);
+ close(sockfd);
+ return -1;
+ }
+
+ if (write(sockfd, buf->data, buf->size) == -1) {
+ free(sock_path);
+ free(buf->data);
+ free(buf);
+ close(sockfd);
+ return -1;
+ }
+
+ free(buf->data);
+ free(buf);
+
+ close(sockfd);
+ return 0;
+}
+
pid_t ipcp_create(rina_name_t name,
char * ipcp_type)
{
@@ -48,6 +88,9 @@ pid_t ipcp_create(rina_name_t name,
char * ipcp_dir = "bin/ipcpd";
char * full_name = NULL;
+ if (ipcp_type == NULL)
+ return -1;
+
pid = fork();
if (pid == -1) {
LOG_ERR("Failed to fork");
@@ -61,14 +104,15 @@ pid_t ipcp_create(rina_name_t name,
api_id = malloc(n_digits(name.api_id) + 1);
if (!api_id) {
LOG_ERR("Failed to malloc");
- exit(-1);
+ exit(EXIT_FAILURE);
}
sprintf(api_id, "%d", name.api_id);
aei_id = malloc(n_digits(name.aei_id) + 1);
if (!aei_id) {
LOG_ERR("Failed to malloc");
- exit(-1);
+ free(api_id);
+ exit(EXIT_FAILURE);
}
sprintf(aei_id, "%d", name.aei_id);
@@ -78,7 +122,9 @@ pid_t ipcp_create(rina_name_t name,
full_name = malloc(len);
if (!full_name) {
LOG_ERR("Failed to malloc");
- exit(-1);
+ free(api_id);
+ free(aei_id);
+ exit(EXIT_FAILURE);
}
strcpy(full_name, INSTALL_DIR);
@@ -97,7 +143,10 @@ pid_t ipcp_create(rina_name_t name,
LOG_DBG("%s", strerror(errno));
LOG_ERR("Failed to load IPCP daemon");
LOG_ERR("Make sure to run the installed version");
- exit(-1);
+ free(api_id);
+ free(aei_id);
+ free(full_name);
+ exit(EXIT_FAILURE);
}
int ipcp_destroy(pid_t pid)
@@ -121,20 +170,58 @@ int ipcp_reg(pid_t pid,
char ** difs,
size_t difs_size)
{
- return -1;
+ struct ipcp_msg msg;
+
+ if (difs == NULL)
+ return -1;
+
+ msg.code = IPCP_REG;
+ msg.difs = difs;
+ msg.difs_size = difs_size;
+
+ if (send_ipcp_msg(pid, &msg)) {
+ LOG_ERR("Failed to send message to daemon");
+ return -1;
+ }
+
+ return 0;
}
int ipcp_unreg(pid_t pid,
char ** difs,
size_t difs_size)
{
- return -1;
+ struct ipcp_msg msg;
+
+ if (difs == NULL)
+ return -1;
+
+ msg.code = IPCP_UNREG;
+ msg.difs = difs;
+ msg.difs_size = difs_size;
+
+ if (send_ipcp_msg(pid, &msg)) {
+ LOG_ERR("Failed to send message to daemon");
+ return -1;
+ }
+
+ return 0;
}
int ipcp_bootstrap(pid_t pid,
struct dif_config conf)
{
- return -1;
+ struct ipcp_msg msg;
+
+ msg.code = IPCP_BOOTSTRAP;
+ msg.conf = &conf;
+
+ if (send_ipcp_msg(pid, &msg)) {
+ LOG_ERR("Failed to send message to daemon");
+ return -1;
+ }
+
+ return 0;
}
int ipcp_enroll(pid_t pid,
@@ -143,5 +230,24 @@ int ipcp_enroll(pid_t pid,
char ** n_1_difs,
ssize_t n_1_difs_size)
{
- return -1;
+ struct ipcp_msg msg;
+
+ if (n_1_difs == NULL)
+ return -1;
+
+ if (dif_name == NULL)
+ return -1;
+
+ msg.code = IPCP_ENROLL;
+ msg.dif_name = dif_name;
+ msg.member = &member;
+ msg.difs = n_1_difs;
+ msg.difs_size = n_1_difs_size;
+
+ if (send_ipcp_msg(pid, &msg)) {
+ LOG_ERR("Failed to send message to daemon");
+ return -1;
+ }
+
+ return 0;
}