My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
socket_interface.c
Go to the documentation of this file.
1 /*
2 START OF LICENSE STUB
3  DeDOS: Declarative Dispersion-Oriented Software
4  Copyright (C) 2017 University of Pennsylvania, Georgetown University
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 END OF LICENSE STUB
19 */
20 #include "socket_interface.h"
21 #include "cli.h"
22 #include "logging.h"
23 
24 #include <stdlib.h>
25 #include <zmq.h>
26 
27 #define MAX_RECV_LEN 1024
28 
29 static void* socket_loop(void *v_port) {
30  int port = *(int*)v_port;
31  free(v_port);
32  void *context = zmq_ctx_new();
33  if (context == NULL) {
34  log_error("Error instantiating ZMQ context");
35  return NULL;
36  }
37  void *socket = zmq_socket(context, ZMQ_REP);
38  if (socket == NULL) {
39  log_error("Error instantiating ZMQ socket");
40  return NULL;
41  }
42 
43  int rtn;
44  if (port <= 0) {
45  rtn = zmq_bind(socket, "ipc:///tmp/dedosipc");
46  if (rtn != 0) {
47  log_perror("Error binding ZMQ socket to ipc");
48  return NULL;
49  } else {
50  log_info("Bound ipc socket");
51  }
52  } else {
53  char port_str[32];
54  sprintf(port_str, "tcp://*:%d", port);
55  rtn = zmq_bind(socket, port_str);
56  if (rtn != 0) {
57  log_perror("Error binding ZMQ socket to %s", port_str);
58  return NULL;
59  } else {
60  log_info("Bound control socket: %s", port_str);
61  }
62  }
63 
64  char buf[MAX_RECV_LEN];
65  while (1) {
66  rtn = zmq_recv(socket, buf, MAX_RECV_LEN, 0);
67  if (rtn < 0) {
68  log_perror("Error receiving ZMQ message");
69  return NULL;
70  }
71  buf[rtn] = '\0';
72 
73  log_info("Received over socket: %s", buf);
74 
75  int cmd_rtn = parse_cmd_action(buf);
76  char cmd_rtn_c[32];
77  sprintf(cmd_rtn_c, "%d", cmd_rtn);
78 
79  zmq_send(socket, cmd_rtn_c, strlen(cmd_rtn_c), 0);
80  }
81 }
82 
83 
84 int start_socket_interface_thread(pthread_t *thread, int port) {
85  int *p_port = malloc(sizeof(*p_port));
86  *p_port = port;
87  int err = pthread_create(thread, NULL, socket_loop, p_port);
88  if (err != 0) {
89  log_error("ERROR: can't create thread: %s", strerror(err));
90  } else {
91  log_info("Socket Control Thread created successfully");
92  }
93  return err;
94 }
#define log_info(fmt,...)
Definition: logging.h:88
#define log_perror(fmt,...)
Definition: logging.h:102
#define MAX_RECV_LEN
Logging of status messages to the terminal.
static void * socket_loop(void *v_port)
#define log_error(fmt,...)
Definition: logging.h:101
int start_socket_interface_thread(pthread_t *thread, int port)
int parse_cmd_action(char *cmd)
Definition: cli.c:506