Page Menu
Home
ClusterLabs Projects
Search
Configure Global Search
Log In
Files
F7989024
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
25 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/config.c b/src/config.c
index e70c6a7..2e33d79 100644
--- a/src/config.c
+++ b/src/config.c
@@ -1,359 +1,361 @@
/*
* Copyright (C) 2011 Jiaju Zhang <jjzhang@suse.de>
* Copyright (C) 2013 Philipp Marek <philipp.marek@linbit.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "booth.h"
#include "config.h"
#include "ticket.h"
#include "log.h"
static int ticket_size = 0;
static int ticket_realloc(void)
{
void *p;
booth_conf = realloc(booth_conf, sizeof(struct booth_config)
+ (ticket_size + TICKET_ALLOC)
* sizeof(struct ticket_config));
if (!booth_conf) {
log_error("can't alloc more booth config");
return -ENOMEM;
}
p = (char *) booth_conf + sizeof(struct booth_config)
+ ticket_size * sizeof(struct ticket_config);
memset(p, 0, TICKET_ALLOC * sizeof(struct ticket_config));
ticket_size += TICKET_ALLOC;
return 0;
}
int add_node(char *address, int type);
int add_node(char *addr_string, int type)
{
int rv;
struct booth_node *node;
rv = 1;
if (booth_conf->node_count == MAX_NODES) {
log_error("too many nodes");
goto out;
}
if (strlen(addr_string)+1 >= sizeof(booth_conf->node[0].addr_string)) {
log_error("node address \"%s\" too long", addr_string);
goto out;
}
node = booth_conf->node+booth_conf->node_count;
node->family = BOOTH_PROTO_FAMILY;
node->type = type;
node->nodeid = booth_conf->node_count;
strcpy(node->addr_string, addr_string);
node->tcp_fd = -1;
booth_conf->node_count++;
memset(&node->sa6, 0, sizeof(node->sa6));
if (node->family == AF_INET) {
if (inet_pton(AF_INET,
node->addr_string,
&node->sa4.sin_addr) < 0)
goto addr_bad;
node->sa4.sin_family = node->family;
node->sa4.sin_port = htons(booth_conf->port);
- node->addrlen = sizeof(struct in_addr);
+ node->saddrlen = sizeof(node->sa4);
+ node->addrlen = sizeof(node->sa4.sin_addr);
} else if (node->family == AF_INET6) {
if (inet_pton(AF_INET6,
node->addr_string,
&node->sa6.sin6_addr) < 0)
goto addr_bad;
node->sa6.sin6_family = node->family;
node->sa6.sin6_flowinfo = 0;
node->sa6.sin6_port = htons(booth_conf->port);
- node->addrlen = sizeof(struct in6_addr);
+ node->saddrlen = sizeof(node->sa6);
+ node->addrlen = sizeof(node->sa6.sin6_addr);
} else {
log_error("invalid INET family");
goto out;
}
rv = 0;
out:
return rv;
addr_bad:
log_error("Address string \"%s\" is bad", node->addr_string);
goto out;
}
int read_config(const char *path)
{
char line[1024];
FILE *fp;
char *s, *key, *val, *expiry, *weight, *c;
int in_quotes, got_equals, got_quotes, i;
int lineno = 0;
int got_transport = 0;
fp = fopen(path, "r");
if (!fp) {
log_error("failed to open %s: %s", path, strerror(errno));
return -1;
}
booth_conf = malloc(sizeof(struct booth_config)
+ TICKET_ALLOC * sizeof(struct ticket_config));
if (!booth_conf) {
log_error("failed to alloc memory for booth config");
return -ENOMEM;
}
memset(booth_conf, 0, sizeof(struct booth_config)
+ TICKET_ALLOC * sizeof(struct ticket_config));
ticket_size = TICKET_ALLOC;
while (fgets(line, sizeof(line), fp)) {
lineno++;
s = line;
while (*s == ' ')
s++;
if (*s == '#' || *s == '\n')
continue;
if (*s == '-' || *s == '.' || *s =='/'
|| *s == '+' || *s == '(' || *s == ')'
|| *s == ':' || *s == ',' || *s == '@'
|| *s == '=' || *s == '"') {
log_error("invalid key name in config file "
"('%c', line %d char %ld)", *s, lineno, (long)(s - line));
goto out;
}
key = s; /* will point to the key on the left hand side */
val = NULL; /* will point to the value on the right hand side */
in_quotes = 0; /* true iff we're inside a double-quoted string */
got_equals = 0; /* true iff we're on the RHS of the = assignment */
got_quotes = 0; /* true iff the RHS is quoted */
while (*s != '\n' && *s != '\0') {
if (!(*s >='a' && *s <= 'z')
&& !(*s >= 'A' && *s <= 'Z')
&& !(*s >= '0' && *s <= '9')
&& !(*s == '_')
&& !(*s == '-')
&& !(*s == '.')
&& !(*s == '/')
&& !(*s == ' ')
&& !(*s == '+')
&& !(*s == '(')
&& !(*s == ')')
&& !(*s == ':')
&& !(*s == ';')
&& !(*s == ',')
&& !(*s == '@')
&& !(*s == '=')
&& !(*s == '"')) {
log_error("invalid character ('%c', line %d char %ld)"
" in config file", *s, lineno, (long)(s - line));
goto out;
}
if (*s == '=' && !got_equals) {
got_equals = 1;
*s = '\0';
val = s + 1;
} else if ((*s == '=' || *s == '_' || *s == '-' || *s == '.')
&& got_equals && !in_quotes) {
log_error("invalid config file format: unquoted '%c' "
"(line %d char %ld)", *s, lineno, (long)(s - line));
goto out;
} else if ((*s == '/' || *s == '+'
|| *s == '(' || *s == ')' || *s == ':'
|| *s == ',' || *s == '@') && !in_quotes) {
log_error("invalid config file format: unquoted '%c' "
"(line %d char %ld)", *s, lineno, (long)(s - line));
goto out;
} else if ((*s == ' ')
&& !in_quotes && !got_quotes) {
log_error("invalid config file format: unquoted whitespace "
"(line %d char %ld)", lineno, (long)(s - line));
goto out;
} else if (*s == '"' && !got_equals) {
log_error("invalid config file format: unexpected quotes "
"(line %d char %ld)", lineno, (long)(s - line));
goto out;
} else if (*s == '"' && !in_quotes) {
in_quotes = 1;
if (val) {
val++;
got_quotes = 1;
}
} else if (*s == '"' && in_quotes) {
in_quotes = 0;
*s = '\0';
}
s++;
}
if (!got_equals) {
log_error("invalid config file format: missing '=' (lineno %d)",
lineno);
goto out;
}
if (in_quotes) {
log_error("invalid config file format: unterminated quotes (lineno %d)",
lineno);
goto out;
}
if (!got_quotes)
*s = '\0';
if (strlen(key) > BOOTH_NAME_LEN
|| strlen(val) > BOOTH_NAME_LEN) {
log_error("key/value too long");
goto out;
}
if (!strcmp(key, "transport")) {
if (!strcmp(val, "UDP"))
booth_conf->proto = UDP;
else if (!strcmp(val, "SCTP"))
booth_conf->proto = SCTP;
else {
log_error("invalid transport protocol");
goto out;
}
got_transport = 1;
}
if (!strcmp(key, "port"))
booth_conf->port = atoi(val);
if (!strcmp(key, "site")) {
if (add_node(val, SITE))
goto out;
}
if (!strcmp(key, "arbitrator")) {
if (add_node(val, ARBITRATOR))
goto out;
}
if (!strcmp(key, "ticket")) {
int count = booth_conf->ticket_count;
if (booth_conf->ticket_count == ticket_size) {
if (ticket_realloc() < 0)
goto out;
}
expiry = index(val, ';');
weight = rindex(val, ';');
if (!expiry) {
strcpy(booth_conf->ticket[count].name, val);
booth_conf->ticket[count].expiry = DEFAULT_TICKET_EXPIRY;
log_info("expire is not set in %s."
" Set the default value %ds.",
booth_conf->ticket[count].name,
DEFAULT_TICKET_EXPIRY);
}
else if (expiry && expiry == weight) {
*expiry++ = '\0';
while (*expiry == ' ')
expiry++;
strcpy(booth_conf->ticket[count].name, val);
booth_conf->ticket[count].expiry = atoi(expiry);
} else {
*expiry++ = '\0';
*weight++ = '\0';
while (*expiry == ' ')
expiry++;
while (*weight == ' ')
weight++;
strcpy(booth_conf->ticket[count].name, val);
booth_conf->ticket[count].expiry = atoi(expiry);
i = 0;
while ((c = index(weight, ','))) {
*c++ = '\0';
booth_conf->ticket[count].weight[i++]
= atoi(weight);
while (*c == ' ')
c++;
weight = c;
if (i == MAX_NODES) {
log_error("too many weights");
break;
}
}
}
booth_conf->ticket_count++;
}
}
if (!got_transport) {
log_error("config file was missing transport line");
goto out;
}
return 0;
out:
free(booth_conf);
return -1;
}
int check_config(int type)
{
// int i;
if (!booth_conf)
return -1;
/* for (i = 0; i < booth_conf->node_count; i++) {
if (booth_conf->node[i].local && booth_conf->node[i].type ==
type)
return 0;
}
return -1;*/
return 0;
}
int find_site_in_config(unsigned char *site, struct booth_node **node)
{
struct booth_node *n;
int i;
if (!booth_conf)
return 0;
for (i = 0; i < booth_conf->node_count; i++) {
n = booth_conf->node + i;
if (n->type == SITE &&
strcmp(n->addr_string, site) == 0) {
*node = n;
return 1;
}
}
return 0;
}
diff --git a/src/transport.c b/src/transport.c
index 7e9e35e..7771c7c 100644
--- a/src/transport.c
+++ b/src/transport.c
@@ -1,637 +1,637 @@
/*
* Copyright (C) 2011 Jiaju Zhang <jjzhang@suse.de>
* Copyright (C) 2013 Philipp Marek <philipp.marek@linbit.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include <stdlib.h>
#include <net/if.h>
#include <asm/types.h>
#include <linux/rtnetlink.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <poll.h>
#include "list.h"
#include "booth.h"
#include "log.h"
#include "config.h"
#include "paxos_lease.h"
#include "transport.h"
#define BOOTH_IPADDR_LEN (sizeof(struct in6_addr))
#define NETLINK_BUFSIZE 16384
#define SOCKET_BUFFER_SIZE 160000
#define FRAME_SIZE_MAX 10000
extern struct client *client;
extern struct pollfd *pollfd;
static struct booth_node local;
struct tcp_conn {
int s;
struct sockaddr to;
struct list_head list;
};
static LIST_HEAD(tcp);
struct udp_context {
int s;
struct iovec iov_recv;
char iov_buffer[FRAME_SIZE_MAX];
} udp;
static int (*deliver_fn) (void *msg, int msglen);
static void parse_rtattr(struct rtattr *tb[],
int max, struct rtattr *rta, int len)
{
while (RTA_OK(rta, len)) {
if (rta->rta_type <= max)
tb[rta->rta_type] = rta;
rta = RTA_NEXT(rta,len);
}
}
static int find_address(unsigned char ipaddr[BOOTH_IPADDR_LEN],
int family, int prefixlen,
int fuzzy_allowed,
struct booth_node **me)
{
int i;
struct booth_node *node;
int bytes, bits_left, mask;
unsigned char node_bits, ip_bits;
uint8_t *n_a;
bytes = prefixlen / 8;
bits_left = prefixlen % 8;
/* One bit left to check means ignore 7 lowest bits. */
mask = ~( (1 << (8 - bits_left)) -1);
for (i = 0; i < booth_conf->node_count; i++) {
node = booth_conf->node + i;
if (family != node->family)
continue;
n_a = node_to_addr_pointer(node);
if (memcmp(ipaddr, n_a, node->addrlen) == 0) {
found:
*me = node;
return 1;
}
if (!fuzzy_allowed)
continue;
// assert(bytes <= node->addrlen);
//#include <stdio.h>
// printf("node->addr %s, fam %d, prefix %d; %llx vs %llx, bytes %d\n", node->addr, node->family, prefixlen, *((long long*)&node->in6), *((long long*)ipaddr), bytes);
/* Check prefix, whole bytes */
if (memcmp(ipaddr, n_a, bytes) != 0)
continue;
//printf("bits %d\n", bits_left);
if (!bits_left)
goto found;
node_bits = n_a[bytes];
ip_bits = ipaddr[bytes];
//printf("nodebits %x ip %x mask %x\n", node_bits, ip_bits, mask);
if (((node_bits ^ ip_bits) & mask) == 0)
goto found;
}
return 0;
}
int find_myself(struct booth_node **me, int fuzzy_allowed)
{
int fd;
struct sockaddr_nl nladdr;
unsigned char ipaddr[BOOTH_IPADDR_LEN];
static char rcvbuf[NETLINK_BUFSIZE];
struct {
struct nlmsghdr nlh;
struct rtgenmsg g;
} req;
*me = NULL;
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (fd < 0) {
log_error("failed to create netlink socket");
return 0;
}
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf));
memset(&nladdr, 0, sizeof(nladdr));
nladdr.nl_family = AF_NETLINK;
memset(&req, 0, sizeof(req));
req.nlh.nlmsg_len = sizeof(req);
req.nlh.nlmsg_type = RTM_GETADDR;
req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST;
req.nlh.nlmsg_pid = 0;
req.nlh.nlmsg_seq = 1;
req.g.rtgen_family = AF_INET;
if (sendto(fd, (void *)&req, sizeof(req), 0,
(struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) {
close(fd);
log_error("failed to send data to netlink socket");
return 0;
}
while (1) {
int status;
struct nlmsghdr *h;
struct iovec iov = { rcvbuf, sizeof(rcvbuf) };
struct msghdr msg = {
(void *)&nladdr, sizeof(nladdr),
&iov, 1,
NULL, 0,
0
};
status = recvmsg(fd, &msg, 0);
if (!status) {
close(fd);
log_error("failed to recvmsg from netlink socket");
return 0;
}
h = (struct nlmsghdr *)rcvbuf;
if (h->nlmsg_type == NLMSG_DONE)
break;
if (h->nlmsg_type == NLMSG_ERROR) {
close(fd);
log_error("netlink socket recvmsg error");
return 0;
}
while (NLMSG_OK(h, status)) {
if (h->nlmsg_type == RTM_NEWADDR) {
struct ifaddrmsg *ifa = NLMSG_DATA(h);
struct rtattr *tb[IFA_MAX+1];
int len = h->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifa));
memset(tb, 0, sizeof(tb));
parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len);
memset(ipaddr, 0, BOOTH_IPADDR_LEN);
memcpy(ipaddr, RTA_DATA(tb[IFA_ADDRESS]),
BOOTH_IPADDR_LEN);
if (find_address(ipaddr,
ifa->ifa_family, ifa->ifa_prefixlen,
fuzzy_allowed, me))
goto out;
}
h = NLMSG_NEXT(h, status);
}
}
out:
close(fd);
return *me != NULL;
}
static int load_myid(void)
{
struct booth_node *me;
if (find_myself(&me, 0)) {
me->local = 1;
if (!local.family)
memcpy(&local, me, sizeof(struct booth_node));
return me->nodeid;
}
return -1;
}
static int booth_get_myid(void)
{
if (local.local)
return local.nodeid;
else
return -1;
}
static void process_dead(int ci)
{
struct tcp_conn *conn, *safe;
list_for_each_entry_safe(conn, safe, &tcp, list) {
if (conn->s == client[ci].fd) {
list_del(&conn->list);
free(conn);
break;
}
}
close(client[ci].fd);
client[ci].workfn = NULL;
client[ci].fd = -1;
pollfd[ci].fd = -1;
}
static void process_tcp_listener(int ci)
{
int fd, i, one = 1;
socklen_t addrlen = sizeof(struct sockaddr);
struct sockaddr addr;
struct tcp_conn *conn;
fd = accept(client[ci].fd, &addr, &addrlen);
if (fd < 0) {
log_error("process_tcp_listener: accept error %d %d",
fd, errno);
return;
}
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one));
conn = malloc(sizeof(struct tcp_conn));
if (!conn) {
log_error("failed to alloc mem");
return;
}
memset(conn, 0, sizeof(struct tcp_conn));
conn->s = fd;
memcpy(&conn->to, &addr, sizeof(struct sockaddr));
list_add_tail(&conn->list, &tcp);
i = client_add(fd, process_connection, process_dead);
log_debug("client connection %d fd %d", i, fd);
}
static int setup_tcp_listener(void)
{
int s, rv;
s = socket(local.family, SOCK_STREAM, 0);
if (s == -1) {
log_error("failed to create tcp socket %s", strerror(errno));
return s;
}
- rv = bind(s, &local.sa6, local.addrlen);
+ rv = bind(s, &local.sa6, local.saddrlen);
if (rv == -1) {
log_error("failed to bind socket %s", strerror(errno));
return rv;
}
rv = listen(s, 5);
if (rv == -1) {
log_error("failed to listen on socket %s", strerror(errno));
return rv;
}
return s;
}
static int booth_tcp_init(void * unused __attribute__((unused)))
{
int rv;
if (!local.local)
return -1;
rv = setup_tcp_listener();
if (rv < 0)
return rv;
client_add(rv, process_tcp_listener, NULL);
return 0;
}
static int connect_nonb(int sockfd, const struct sockaddr *saptr,
socklen_t salen, int sec)
{
int flags, n, error;
socklen_t len;
fd_set rset, wset;
struct timeval tval;
flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
error = 0;
if ( (n = connect(sockfd, saptr, salen)) < 0)
if (errno != EINPROGRESS)
return -1;
if (n == 0)
goto done; /* connect completed immediately */
FD_ZERO(&rset);
FD_SET(sockfd, &rset);
wset = rset;
tval.tv_sec = sec;
tval.tv_usec = 0;
if ((n = select(sockfd + 1, &rset, &wset, NULL,
sec ? &tval : NULL)) == 0) {
/* leave outside function to close */
/* timeout */
/* close(sockfd); */
errno = ETIMEDOUT;
return -1;
}
if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) {
len = sizeof(error);
if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
return -1; /* Solaris pending error */
} else {
log_error("select error: sockfd not set");
return -1;
}
done:
fcntl(sockfd, F_SETFL, flags); /* restore file status flags */
if (error) {
/* leave outside function to close */
/* close(sockfd); */
errno = error;
return -1;
}
return 0;
}
int booth_tcp_open(struct booth_node *to)
{
int s, rv;
if (to->tcp_fd >= 0)
goto found;
s = socket(to->family, SOCK_STREAM, 0);
if (s == -1) {
log_error("cannot create socket of family %d", to->family);
return -1;
}
- rv = connect_nonb(s, (struct sockaddr *)&to->sa6, to->addrlen, 10);
+ rv = connect_nonb(s, (struct sockaddr *)&to->sa6, to->saddrlen, 10);
if (rv == -1) {
if( errno == ETIMEDOUT)
log_error("connection to %s timeout", to->addr_string);
else
log_error("connection to %s error %s", to->addr_string,
strerror(errno));
goto error;
}
to->tcp_fd = s;
found:
return 1;
error:
if (s >= 0)
close(s);
return -1;
}
int booth_tcp_send(struct booth_node *to, void *buf, int len)
{
return do_write(to->tcp_fd, buf, len);
}
static int booth_tcp_recv(struct booth_node *from, void *buf, int len)
{
return do_read(from->tcp_fd, buf, len);
}
static int booth_tcp_close(struct booth_node *to)
{
if (to && to->tcp_fd >= 0) {
close(to->tcp_fd);
to->tcp_fd = -1;
}
return 0;
}
static int booth_tcp_exit(void)
{
return 0;
}
static int setup_udp_server(void)
{
int rv;
unsigned int recvbuf_size;
udp.s = socket(local.family, SOCK_DGRAM, 0);
if (udp.s == -1) {
log_error("failed to create udp socket %s", strerror(errno));
return -1;
}
rv = fcntl(udp.s, F_SETFL, O_NONBLOCK);
if (rv == -1) {
log_error("failed to set non-blocking operation "
"on udp socket: %s", strerror(errno));
close(udp.s);
return -1;
}
rv = bind(udp.s, (struct sockaddr *)&local.sa6, local.addrlen);
if (rv == -1) {
log_error("failed to bind socket %s", strerror(errno));
close(udp.s);
return -1;
}
recvbuf_size = SOCKET_BUFFER_SIZE;
rv = setsockopt(udp.s, SOL_SOCKET, SO_RCVBUF,
&recvbuf_size, sizeof(recvbuf_size));
if (rv == -1) {
log_error("failed to set recvbuf size");
close(udp.s);
return -1;
}
return udp.s;
}
static void process_recv(int ci)
{
struct msghdr msg_recv;
struct sockaddr_storage system_from;
int received;
unsigned char *msg_offset;
msg_recv.msg_name = &system_from;
msg_recv.msg_namelen = sizeof (struct sockaddr_storage);
msg_recv.msg_iov = &udp.iov_recv;
msg_recv.msg_iovlen = 1;
msg_recv.msg_control = 0;
msg_recv.msg_controllen = 0;
msg_recv.msg_flags = 0;
received = recvmsg(client[ci].fd, &msg_recv,
MSG_NOSIGNAL | MSG_DONTWAIT);
if (received == -1)
return;
msg_offset = udp.iov_recv.iov_base;
deliver_fn(msg_offset, received);
}
static int booth_udp_init(void *f)
{
int myid = -1;
memset(&local, 0, sizeof(struct booth_node));
myid = load_myid();
if (myid < 0) {
log_error("can't find myself in config file");
return -1;
}
memset(&udp, 0, sizeof(struct udp_context));
udp.iov_recv.iov_base = udp.iov_buffer;
udp.iov_recv.iov_len = FRAME_SIZE_MAX;
udp.s = setup_udp_server();
if (udp.s == -1)
return -1;
deliver_fn = f;
client_add(udp.s, process_recv, NULL);
return 0;
}
static int booth_udp_send(struct booth_node *to, void *buf, int len)
{
struct msghdr msg;
struct iovec iovec;
unsigned int iov_len;
int rv;
iovec.iov_base = (void *)buf;
iovec.iov_len = len;
iov_len = 1;
msg.msg_name = &to->sa6;
msg.msg_namelen = to->addrlen;
msg.msg_iov = (void *)&iovec;
msg.msg_iovlen = iov_len;
msg.msg_control = 0;
msg.msg_controllen = 0;
msg.msg_flags = 0;
rv = sendmsg(udp.s, &msg, MSG_NOSIGNAL);
if (rv < 0)
return rv;
return 0;
}
static int booth_udp_broadcast(void *buf, int len)
{
int i;
if (!booth_conf || !booth_conf->node_count)
return -1;
for (i = 0; i < booth_conf->node_count; i++)
booth_udp_send(booth_conf->node+i, buf, len);
return 0;
}
static int booth_udp_exit(void)
{
return 0;
}
/* SCTP transport layer has not been developed yet */
static int booth_sctp_init(void *f __attribute__((unused)))
{
return 0;
}
static int booth_sctp_send(struct booth_node * to __attribute__((unused)),
void *buf __attribute__((unused)),
int len __attribute__((unused)))
{
return 0;
}
static int booth_sctp_broadcast(void *buf __attribute__((unused)),
int len __attribute__((unused)))
{
return 0;
}
static int booth_sctp_exit(void)
{
return 0;
}
const struct booth_transport booth_transport[TRANSPORT_ENTRIES] = {
[TCP] = {
.name = "TCP",
.init = booth_tcp_init,
.get_myid = booth_get_myid,
.open = booth_tcp_open,
.send = booth_tcp_send,
.recv = booth_tcp_recv,
.close = booth_tcp_close,
.exit = booth_tcp_exit
},
[UDP] = {
.name = "UDP",
.init = booth_udp_init,
.get_myid = booth_get_myid,
.send = booth_udp_send,
.broadcast = booth_udp_broadcast,
.exit = booth_udp_exit
},
[SCTP] = {
.name = "SCTP",
.init = booth_sctp_init,
.get_myid = booth_get_myid,
.send = booth_sctp_send,
.broadcast = booth_sctp_broadcast,
.exit = booth_sctp_exit
}
};
const struct booth_transport *local_transport = booth_transport+TCP;
diff --git a/src/transport.h b/src/transport.h
index 94d2c77..332a3af 100644
--- a/src/transport.h
+++ b/src/transport.h
@@ -1,82 +1,83 @@
/*
* Copyright (C) 2011 Jiaju Zhang <jjzhang@suse.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _TRANSPORT_H
#define _TRANSPORT_H
#include "booth.h"
struct booth_node {
int nodeid;
int type;
int local;
char addr_string[BOOTH_NAME_LEN];
int tcp_fd;
unsigned short family;
union {
struct sockaddr_in sa4;
struct sockaddr_in6 sa6;
};
+ int saddrlen;
int addrlen;
} __attribute__((packed));
typedef enum {
TCP = 0,
UDP = 1,
SCTP = 2,
TRANSPORT_ENTRIES = 3,
} transport_layer_t;
typedef enum {
ARBITRATOR = 1,
SITE,
CLIENT,
} node_type_t;
struct booth_transport {
const char *name;
int (*init) (void *);
int (*get_myid) (void);
int (*open) (struct booth_node *);
int (*send) (struct booth_node *, void *, int);
int (*recv) (struct booth_node *, void *, int);
int (*broadcast) (void *, int);
int (*close) (struct booth_node *);
int (*exit) (void);
};
const struct booth_transport booth_transport[TRANSPORT_ENTRIES];
int find_myself(struct booth_node **me, int fuzzy_allowed);
int booth_tcp_open(struct booth_node *to);
int booth_tcp_send(struct booth_node *to, void *buf, int len);
inline static void * node_to_addr_pointer(struct booth_node *node) {
switch (node->family) {
case AF_INET: return &node->sa4.sin_addr;
case AF_INET6: return &node->sa6.sin6_addr;
}
return NULL;
}
extern const struct booth_transport *local_transport;
#endif /* _TRANSPORT_H */
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Oct 25, 2:10 AM (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2619963
Default Alt Text
(25 KB)
Attached To
Mode
rB Booth
Attached
Detach File
Event Timeline
Log In to Comment