2014-07-14 11:49:44 +00:00
|
|
|
/** Nodes.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-07-14 10:47:41 +00:00
|
|
|
#include "config.h"
|
2014-06-05 09:34:56 +00:00
|
|
|
#include "cfg.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
#include "utils.h"
|
|
|
|
#include "msg.h"
|
|
|
|
#include "node.h"
|
2014-06-05 09:35:39 +00:00
|
|
|
#include "if.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:56 +00:00
|
|
|
int node_connect(struct node *n)
|
|
|
|
{
|
2014-06-05 09:34:46 +00:00
|
|
|
/* Create socket */
|
2014-06-05 09:34:29 +00:00
|
|
|
n->sd = socket(AF_INET, SOCK_DGRAM, 0);
|
2014-06-05 09:34:56 +00:00
|
|
|
if (n->sd < 0)
|
2014-06-05 09:35:16 +00:00
|
|
|
perror("Failed to create socket");
|
2014-06-05 09:34:56 +00:00
|
|
|
|
|
|
|
/* Set socket options */
|
|
|
|
int prio = SOCKET_PRIO;
|
2014-06-05 09:35:39 +00:00
|
|
|
if (setsockopt(n->sd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)))
|
|
|
|
perror("Failed to set socket priority");
|
|
|
|
else
|
|
|
|
debug(4, "Set socket priority of node '%s' to %u", n->name, prio);
|
|
|
|
|
|
|
|
/* Set mark for outgoing packets */
|
|
|
|
if (setsockopt(n->sd, SOL_SOCKET, SO_MARK, &n->mark, sizeof(n->mark)))
|
|
|
|
perror("Failed to set mark for outgoing packets");
|
|
|
|
else
|
|
|
|
debug(4, "Set mark of outgoing packets of node '%s' to %u", n->name, n->mark);
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:46 +00:00
|
|
|
/* Bind socket for receiving */
|
2014-06-05 09:34:56 +00:00
|
|
|
if (bind(n->sd, (struct sockaddr *) &n->local, sizeof(struct sockaddr_in)))
|
2014-06-05 09:35:16 +00:00
|
|
|
perror("Failed to bind to socket");
|
2014-06-05 09:34:35 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:35:09 +00:00
|
|
|
int node_disconnect(struct node *n)
|
2014-06-05 09:34:29 +00:00
|
|
|
{
|
|
|
|
close(n->sd);
|
2014-06-25 01:53:37 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-10 18:47:25 +00:00
|
|
|
struct node* node_lookup_name(const char *str, struct node *nodes)
|
2014-06-05 09:34:29 +00:00
|
|
|
{
|
2014-06-10 18:47:25 +00:00
|
|
|
for (struct node *n = nodes; n; n = n->next) {
|
|
|
|
if (!strcmp(str, n->name)) {
|
|
|
|
return n;
|
2014-06-05 09:34:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-06-05 09:34:56 +00:00
|
|
|
return NULL;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|