1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

added separate tructures for local and remote addresses

git-svn-id: https://zerberus.eonerc.rwth-aachen.de:8443/svn/s2ss/trunk@9 8ec27952-4edc-4aab-86aa-e87bb2611832
This commit is contained in:
Steffen Vogel 2014-06-05 09:34:35 +00:00
parent bfacfbd34a
commit a27d709f07
2 changed files with 62 additions and 36 deletions

View file

@ -28,10 +28,16 @@ struct node
{
/// The socket descriptor
int sd;
// Local address of the socket
struct sockaddr_in local;
// Remote address of the socket
struct sockaddr_in addr;
struct sockaddr_in remote;
/// The type of this node
enum node_type type;
/// A short identifier of the node
char *name;
};
@ -45,13 +51,13 @@ struct msg; /* forward decl */
*
* @param name An acroynm, describing the node
* @param type The type of a node (SERVER, SIMULATOR, WORKSTATION)
* @param addr A string containing the node address
* @param port The UDP port of the node
* @param local A string specifying the local ip:port
* @param remote A string specifying the remote ip:port
* @return
* - 0 on success
* - otherwise on error occured
*/
struct node* node_create(const char *name, enum node_type type, const char *addr, int port);
struct node* node_create(const char *name, enum node_type type, const char *local, const char *remote);
/**
* @brief Delete a node created by node_create()

View file

@ -23,8 +23,9 @@
#include "msg.h"
#include "node.h"
struct node* node_create(const char *name, enum node_type type, const char *addr, int port)
struct node* node_create(const char *name, enum node_type type, const char *local, const char *remote)
{
int ret;
struct node *n = malloc(sizeof(struct node));
if (!n)
return NULL;
@ -34,6 +35,47 @@ struct node* node_create(const char *name, enum node_type type, const char *addr
n->name = strdup(name);
n->type = type;
resolve(local, &n->local);
resolve(remote, &n->remote);
/* create and connect socket */
n->sd = socket(AF_INET, SOCK_DGRAM, 0);
if (n->sd < 0) {
node_destroy(n);
print(FATAL, "Failed to create socket: %s", strerror(errno));
return NULL;
}
ret = bind(n->sd, &n->local, sizeof(struct sockaddr_in));
if (ret < 0) {
node_destroy(n);
print(FATAL, "Failed to bind socket: %s", strerror(errno));
return NULL;
}
ret = connect(n->sd, &n->remote, sizeof(struct sockaddr_in));
if (ret < 0) {
node_destroy(n);
print(FATAL, "Failed to connect socket: %s", strerror(errno));
return NULL;
}
print(DEBUG, "We listen for node %s at %s:%u", name, inet_ntoa(n->local.sin_addr), ntohs(n->local.sin_port));
print(DEBUG, "We sent to node %s at %s:%u", name, inet_ntoa(n->remote.sin_addr), ntohs(n->remote.sin_port));
return n;
}
int resolve(const char *addr, struct sockaddr_in *sa)
{
/* split host:port */
char *host;
char *port;
if (sscanf(addr, "%m[^:]:%ms", &host, &port) != 2) {
print(FATAL, "Invalid address format: %s", addr);
}
/* get ip */
struct addrinfo *result;
struct addrinfo hint = {
@ -42,43 +84,21 @@ struct node* node_create(const char *name, enum node_type type, const char *addr
.ai_protocol = 0
};
int ret = getaddrinfo(addr, NULL, &hint, &result);
int ret = getaddrinfo(host, port, &hint, &result);
if (ret) {
print(ERROR, "Failed to get address for node %s: %s", name, gai_strerror(ret));
return NULL;
print(FATAL, "Failed to get address for node %s: %s", addr, gai_strerror(ret));
return -EINVAL;
}
memcpy(&n->addr, result->ai_addr, sizeof(struct sockaddr_in));
n->addr.sin_family = AF_INET;
n->addr.sin_port = htons(port);
memcpy(sa, result->ai_addr, sizeof(struct sockaddr_in));
sa->sin_family = AF_INET;
sa->sin_port = htons(atoi(port));
freeaddrinfo(result);
free(host);
free(port);
print(DEBUG, "Node %s is reachable at %s:%u", name, inet_ntoa(n->addr.sin_addr), ntohs(n->addr.sin_port));
/* create and connect socket */
n->sd = socket(AF_INET, SOCK_DGRAM, 0);
if (n->sd < 0) {
print(ERROR, "failed to create socket: %s", strerror(errno));
node_destroy(n);
return NULL;
}
/*ret = connect(n->sd, &n->addr, sizeof(struct sockaddr_in));
if (ret < 0) {
print(ERROR, "Failed to connect socket: %s", strerror(errno));
node_destroy(n);
return NULL;
}*/
ret = bind(n->sd, &n->addr, sizeof(struct sockaddr_in));
if (ret < 0) {
print(ERROR, "Failed to bind socket: %s", strerror(errno));
node_destroy(n);
return NULL;
}
return n;
return 0;
}
void node_destroy(struct node* n)