daemon.c File Reference

commotiond - an embedded C daemon for managing mesh networks. More...

#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <limits.h>
#include "config.h"
#include "debug.h"
#include "cmd.h"
#include "util.h"
#include "loop.h"
#include "plugin.h"
#include "process.h"
#include "profile.h"
#include "socket.h"
#include "msg.h"
#include "iface.h"
#include "id.h"
#include "obj.h"
#include "list.h"
#include "tree.h"

Macros

#define REQUEST_MAX   1024
 
#define RESPONSE_MAX   1024
 

Functions

 SCHEMA (default)
 
 SCHEMA (global)
 
 CMD (help)
 
 CMD (profiles)
 
 CMD (up)
 
 CMD (down)
 
 CMD (status)
 
 CMD (state)
 
 CMD (nodeid)
 
 CMD (genip)
 
 CMD (genbssid)
 
 CMD (set)
 
 CMD (get)
 
 CMD (save)
 
 CMD (new)
 
 CMD (delete)
 
int dispatcher_cb (co_obj_t *self, co_obj_t *fd)
 sends/receives socket messages More...
 
int main (int argc, char *argv[])
 Creates sockets for event loop, daemon and dispatcher. Starts/stops event loop.
 

Variables

co_socket_t unix_socket_proto
 

Detailed Description

commotiond - an embedded C daemon for managing mesh networks.

Author
Josh King (jheretic), jking.nosp@m.@cha.nosp@m.mbana.nosp@m..net

Function Documentation

int dispatcher_cb ( co_obj_t self,
co_obj_t fd 
)

sends/receives socket messages

Parameters
selfpointer to dispatcher socket struct
fdfile descriptor object of connected socket

References co_cmd_exec(), co_list_element(), co_list_import(), co_response_alloc(), and co_tree_insert().

Referenced by main().

707  {
708  CHECK(IS_SOCK(self),"Not a socket.");
709  co_socket_t *sock = (co_socket_t*)self;
710  char reqbuf[REQUEST_MAX];
711  memset(reqbuf, '\0', sizeof(reqbuf));
712  ssize_t reqlen = 0;
713  char respbuf[RESPONSE_MAX];
714  memset(respbuf, '\0', sizeof(respbuf));
715  size_t resplen = 0;
716  co_obj_t *request = NULL;
717  uint8_t *type = NULL;
718  uint32_t *id = NULL;
719  co_obj_t *nil = co_nil_create(0);
720 
721  /* Incoming message on socket */
722  reqlen = sock->receive((co_obj_t*)sock, fd, reqbuf, sizeof(reqbuf));
723  DEBUG("Received %d bytes.", (int)reqlen);
724  if(reqlen == 0) {
725  INFO("Received connection.");
726  return 1;
727  }
728  if (reqlen < 0) {
729  INFO("Connection recvd() -1");
730  sock->hangup((co_obj_t*)sock, fd);
731  return 1;
732  }
733  /* If it's a commotion message type, parse the header, target and payload */
734  CHECK(co_list_import(&request, reqbuf, reqlen) > 0, "Failed to import request.");
735  co_obj_data((char **)&type, co_list_element(request, 0));
736  CHECK(*type == 0, "Not a valid request.");
737  CHECK(co_obj_data((char **)&id, co_list_element(request, 1)) == sizeof(uint32_t), "Not a valid request ID.");
738  co_obj_t *ret = NULL;
739  if(co_cmd_exec(co_list_element(request, 2), &ret, co_list_element(request, 3)))
740  {
741  resplen = co_response_alloc(respbuf, sizeof(respbuf), *id, nil, ret);
742  sock->send(fd, respbuf, resplen);
743  }
744  else
745  {
746  if(ret == NULL)
747  {
748  ret = co_tree16_create();
749  co_tree_insert(ret, "error", sizeof("error"), co_str8_create("Incorrect command.", sizeof("Incorrect command."), 0));
750  }
751  resplen = co_response_alloc(respbuf, sizeof(respbuf), *id, ret, nil);
752  sock->send(fd, respbuf, resplen);
753  }
754 
755  co_obj_free(nil);
756  co_obj_free(request);
757  co_obj_free(ret);
758  return 1;
759 error:
760  co_obj_free(nil);
761  co_obj_free(request);
762  co_obj_free(ret);
763  return 1;
764 }
int co_cmd_exec(co_obj_t *key, co_obj_t **output, co_obj_t *param)
executes a command by running the function linked to in the command struct
Definition: cmd.c:109
Definition: obj.h:131
Definition: socket.h:65
size_t co_response_alloc(char *output, const size_t olen, const uint32_t id, const co_obj_t *error, co_obj_t *result)
allocate response
Definition: msg.c:141
size_t co_list_import(co_obj_t **list, const char *input, const size_t ilen)
import list from raw representation
Definition: list.c:562
int co_tree_insert(co_obj_t *root, const char *key, const size_t klen, co_obj_t *value)
insert object into given tree and associate with key
Definition: tree.c:332
co_obj_t * co_list_element(co_obj_t *list, const unsigned int index)
return item at specified position in list
Definition: list.c:483