PERFORCE change 182273 for review
Ana Kukec
anchie at FreeBSD.org
Wed Aug 11 21:54:02 UTC 2010
http://p4web.freebsd.org/@@182273?ac=10
Change 182273 by anchie at anchie_malimis on 2010/08/11 21:53:22
Reorganization of sendd due to the cleanup of unnecessary duplicate
code (e.g. removing OS_SND_freebsd lines). Aiming for keeping sendd
code as close as possible to the original DoCoMo's code.
Affected files ...
.. //depot/projects/soc2009/anchie_send/send_0.2/sendd/addr.c#3 edit
.. //depot/projects/soc2009/anchie_send/send_0.2/sendd/net.c#30 edit
.. //depot/projects/soc2009/anchie_send/send_0.2/sendd/os-freebsd/addr.c#4 edit
.. //depot/projects/soc2009/anchie_send/send_0.2/sendd/os-freebsd/snd_freebsd.c#5 edit
.. //depot/projects/soc2009/anchie_send/send_0.2/sendd/os-freebsd/snd_freebsd.h#4 edit
.. //depot/projects/soc2009/anchie_send/send_0.2/sendd/os/addr.c#1 add
.. //depot/projects/soc2009/anchie_send/send_0.2/sendd/os/snd_freebsd.h#1 add
.. //depot/projects/soc2009/anchie_send/send_0.2/sendd/os_specific.h#7 edit
.. //depot/projects/soc2009/anchie_send/send_0.2/sendd/proto.c#14 edit
.. //depot/projects/soc2009/anchie_send/send_0.2/sendd/sendd.c#13 edit
Differences ...
==== //depot/projects/soc2009/anchie_send/send_0.2/sendd/addr.c#3 (text+ko) ====
@@ -281,14 +281,9 @@
return (0);
}
- /*
if (os_specific_handle_iface(entry->intf_name, ifidx) < 0) {
return (-1);
}
- */
- if (snd_handle_iface(entry->intf_name, ifidx) < 0) {
- return (-1);
- }
intf_cfg_addr(&entry->intf_addr, ifidx);
==== //depot/projects/soc2009/anchie_send/send_0.2/sendd/net.c#30 (text+ko) ====
@@ -72,26 +72,6 @@
static char abuf[INET6_ADDRSTRLEN];
#endif
-#ifdef SND_OS_freebsd
-/* Per-interface info */
-struct snd_ifinfo {
- struct list_head list;
- char name[32];
- int ifidx;
- int snds;
-};
-static DEFINE_LIST_HEAD(ifaces);
-
-/* Data packet meta data */
-struct snd_packet_info {
- struct snd_ifinfo *ifinfo;
- int in;
- int ifidx;
-};
-
-static void snd_sock_read(struct snd_ifinfo *p);
-#endif
-
/* TODO: dynamically size according to MTU */
struct sbuff *
snd_get_buf(void)
@@ -105,99 +85,7 @@
return (b);
}
-#ifdef SND_MAX_PKT
-void
-snd_dispatch_fds(fd_set *fds)
-{
- struct snd_ifinfo *p;
-
- list_for_each_entry(p, &ifaces, list) {
- if (FD_ISSET(sndsock, fds)) {
- snd_sock_read(p);
- }
- }
-}
-
-int
-snd_handle_iface(const char *ifname, int ifidx)
-{
- applog(LOG_ERR, "snd_handle_iface");
- struct snd_ifinfo *p;
-
- if ((p = malloc(sizeof (*p))) == NULL) {
- APPLOG_NOMEM();
- return (-1);
- }
- memset(p, 0, sizeof (*p));
-
- snprintf(p->name, sizeof (p->name), "%s", ifname);
- DBG(&dbg_snd, "snd_handle_iface -> p->name = %s", p->name);
- p->ifidx = ifidx;
- DBG(&dbg_snd, "snd_handle_iface -> p->ifidx = %d", ifidx);
- p->snds = -1;
- list_add_tail(&p->list, &ifaces);
-
- return (0);
-}
-
void
-os_specific_deliver_pkt(void *p, struct sbuff *b, int drop, int changed)
-{
- struct snd_packet_info *pi;
- struct sockaddr_send sendsrc;
-
- if (drop) {
- snd_put_buf(b);
- return;
- }
-
- pi = (struct snd_packet_info *)(b->head);
- bzero(&sendsrc, sizeof(sendsrc));
- sendsrc.send_len = sizeof(sendsrc);
- sendsrc.send_family = AF_INET6;
- sendsrc.send_direction = pi->in;
- sendsrc.send_ifidx = pi->ifinfo->ifidx;
-
- DBG(&dbg_snd, "Sending %d bytes:\n", b->len);
- if (sendto(sndsock, b->data, b->len, 0, (struct sockaddr *)&sendsrc,
- sizeof(sendsrc)) < 0) {
- DBG(&dbg_snd, "Failed to send SEND message back to kernel.");
- perror("Failed");
- snd_put_buf(b);
- return;
- }
-
- snd_put_buf(b);
- return;
-}
-
-
-void
-snd_add_fds(fd_set *fds, int *maxfd, int sock)
-{
- struct snd_ifinfo *p;
-
- list_for_each_entry(p, &ifaces, list) {
- p->snds = sock;
- FD_SET(p->snds, fds);
- *maxfd = sendd_max(*maxfd, p->snds);
- }
-}
-#endif
-
-int
-freebsd_snd_init(void)
-{
-
- if (list_empty(&ifaces)) {
- applog(LOG_ERR, "SEND must be active on at least one iface");
- return (-1);
- }
-
- return (0);
-}
-
-void
snd_put_buf(struct sbuff *b)
{
sbuff_free(b);
@@ -238,57 +126,6 @@
return (0);
}
-#ifdef SND_OS_freebsd
-static void
-snd_sock_read(struct snd_ifinfo *p)
-{
- struct sockaddr_send sendsrc;
- socklen_t len;
- struct sbuff *b;
- struct snd_packet_info *pi;
- int n;
-
- if ((b = snd_get_buf()) == NULL) {
- goto done;
- }
-
- pi = sbuff_data(b);
- sbuff_advance(b, sizeof (*pi));
-
- len = sizeof(sendsrc);
- bzero(&sendsrc, sizeof(sendsrc));
- n = recvfrom(sndsock, b->data, b->rem, 0, (struct sockaddr *)&sendsrc, &len);
- if (n < 0) {
- applog(LOG_ERR, "%s: read: %s", __func__, strerror(errno));
- goto done;
- } else
- DBG(&dbg_snd, "%d bytes received on send socket. (%d)", n, b->rem);
-
- b->len = n;
-
- switch(sendsrc.send_direction) {
- case SND_IN:
- applog(LOG_ERR, "Direction: SND_IN");
- pi->ifinfo = p;
- pi->in = 1;
- snd_recv_pkt(b, p->ifidx, SND_IN, pi);
- break;
- case SND_OUT:
- applog(LOG_ERR, "Direction: SND_OUT");
- pi->ifinfo = p;
- pi->in = 0;
- snd_recv_pkt(b, p->ifidx, SND_OUT, pi);
- break;
- default:
- applog(LOG_ERR, "Unknown SEND pkt header: unknown direction.");
- }
-
-done:
- /* ToDo: Free memory! */
- snd_put_buf(b);
-}
-#endif
-
/*
* TODO: Linux is not yet up-to-date with rfc3542, specifically in that
* it uses the socket option IPV6_PKTINFO instead of IPV6_RECVPKTINFO.
@@ -348,11 +185,7 @@
}
int
-#ifdef SND_OS_freebsd
-snd_net_init(int *icmp6socket, int *sndsocket)
-#else
snd_net_init(void)
-#endif
{
int v;
struct icmp6_filter filter;
@@ -367,25 +200,11 @@
}
#endif
-#ifdef SND_OS_freebsd
-#define IPPROTO_SEND 259
- if ((*sndsocket = socket(PF_INET6, SOCK_RAW, IPPROTO_SEND)) < 0) {
- applog(LOG_ERR, "[%s:%d]: socket: %s", __func__, __LINE__,
- strerror(errno));
- return(-1);
- } else {
- applog(LOG_ERR, "%s: SEND socket created.", __FUNCTION__);
- }
-#endif
- if ((*icmp6socket = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
+ if ((icmp6sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
applog(LOG_ERR, "%s: socket: %s", __FUNCTION__,
strerror(errno));
return (-1);
}
-#ifdef SND_OS_freebsd
- icmp6sock = *icmp6socket;
- sndsock = *sndsocket;
-#endif
v = 255;
if (setsockopt(icmp6sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &v,
sizeof (v)) < 0) {
@@ -413,9 +232,5 @@
return (-1);
}
-#ifdef SND_OS_freebsd
- return (0);
-#else
return(icmp6sock);
-#endif
}
==== //depot/projects/soc2009/anchie_send/send_0.2/sendd/os-freebsd/addr.c#4 (text+ko) ====
==== //depot/projects/soc2009/anchie_send/send_0.2/sendd/os-freebsd/snd_freebsd.c#5 (text+ko) ====
@@ -30,12 +30,60 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
*/
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/mbuf.h>
+#include <sys/socket.h>
+
+#include <arpa/inet.h>
+
+#include <net/if.h>
+#include <net/route.h>
+
+#include <netinet/in.h>
+
+#include <netinet6/send.h>
+
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+
+#include "../sendd_local.h"
+#include "../os_specific.h"
+#include "../snd_proto.h"
+#include "../dbg.h"
+#include "snd_freebsd.h"
+
#include <applog.h>
-#include <list.h>
+#include <sbuff.h>
+static int icmp6sock = -1;
+static int sndsock = -1;
+
+#ifdef DEBUG
+static struct dlog_desc dbg = {
+ .desc = "net",
+ .ctx = SENDD_NAME
+};
+static char abuf[INET6_ADDRSTRLEN];
+#endif
+
+/* Per-interface info */
+struct snd_ifinfo {
+ struct list_head list;
+ char name[32];
+ int ifidx;
+ int snds;
+};
+static DEFINE_LIST_HEAD(ifaces);
-#include "config.h"
-#include "snd_freebsd.h"
-#include "../os_specific.h"
+/* Data packet meta data */
+struct snd_packet_info {
+ struct snd_ifinfo *ifinfo;
+ int in;
+ int ifidx;
+};
extern int linux_rand_init(void);
extern void linux_rand_fini(void);
@@ -46,6 +94,26 @@
if (linux_rand_init() < 0 || freebsd_snd_init() < 0) {
return (-1);
}
+
+ return (0);
+}
+
+int
+freebsd_snd_init(void)
+{
+ if (list_empty(&ifaces)) {
+ applog(LOG_ERR, "SEND must be active on at least one iface");
+ return (-1);
+ }
+#define IPPROTO_SEND 259
+ if ((sndsock = socket(PF_INET6, SOCK_RAW, IPPROTO_SEND)) < 0) {
+ applog(LOG_ERR, "[%s:%d]: socket: %s", __func__, __LINE__,
+ strerror(errno));
+ return(-1);
+ } else {
+ applog(LOG_ERR, "%s: SEND socket created: fd=%d", __func__, sndsock);
+ }
+
return (0);
}
@@ -53,4 +121,131 @@
os_specific_fini(void)
{
linux_rand_fini();
+ close(sndsock);
+}
+
+int
+os_specific_handle_iface(const char *ifname, int ifidx)
+{
+ struct snd_ifinfo *p;
+
+ if ((p = malloc(sizeof (*p))) == NULL) {
+ APPLOG_NOMEM();
+ return (-1);
+ }
+ memset(p, 0, sizeof (*p));
+
+ snprintf(p->name, sizeof (p->name), "%s", ifname);
+ DBG(&dbg_snd, "os_specific_handle_iface -> p->name = %s", p->name);
+ p->ifidx = ifidx;
+ DBG(&dbg_snd, "os_specific_handle_iface -> p->ifidx = %d", ifidx);
+ p->snds = -1;
+ list_add_tail(&p->list, &ifaces);
+
+ return (0);
+}
+
+static void
+snd_sock_read(struct snd_ifinfo *p)
+{
+ struct sockaddr_send sendsrc;
+ socklen_t len;
+ struct sbuff *b;
+ struct snd_packet_info *pi;
+ int n;
+
+ if ((b = snd_get_buf()) == NULL) {
+ goto done;
+ }
+
+ pi = sbuff_data(b);
+ sbuff_advance(b, sizeof (*pi));
+
+ len = sizeof(sendsrc);
+ bzero(&sendsrc, sizeof(sendsrc));
+ n = recvfrom(sndsock, b->data, b->rem, 0, (struct sockaddr *)&sendsrc, &len);
+ if (n < 0) {
+ applog(LOG_ERR, "%s: read: %s", __func__, strerror(errno));
+ goto done;
+ } else
+ DBG(&dbg_snd, "%d bytes received on send socket. (%d)", n, b->rem);
+
+ b->len = n;
+
+ switch(sendsrc.send_direction) {
+ case SND_IN:
+ applog(LOG_ERR, "Direction: SND_IN");
+ pi->ifinfo = p;
+ pi->in = 1;
+ snd_recv_pkt(b, p->ifidx, SND_IN, pi);
+ break;
+ case SND_OUT:
+ applog(LOG_ERR, "Direction: SND_OUT");
+ pi->ifinfo = p;
+ pi->in = 0;
+ snd_recv_pkt(b, p->ifidx, SND_OUT, pi);
+ break;
+ default:
+ applog(LOG_ERR, "Unknown SEND pkt header: unknown direction.");
+ }
+
+done:
+ /* ToDo: Free memory! */
+ snd_put_buf(b);
+ return;
+}
+
+void
+os_specific_add_fds(fd_set *fds, int *maxfd)
+{
+ struct snd_ifinfo *p;
+
+ list_for_each_entry(p, &ifaces, list) {
+ p->snds = sndsock;
+ FD_SET(p->snds, fds);
+ *maxfd = sendd_max(*maxfd, p->snds);
+ }
+}
+
+void
+os_specific_dispatch_fds(fd_set *fds)
+{
+ struct snd_ifinfo *p;
+
+ list_for_each_entry(p, &ifaces, list) {
+ if (FD_ISSET(sndsock, fds)) {
+ snd_sock_read(p);
+ }
+ }
+}
+
+void
+os_specific_deliver_pkt(void *p, struct sbuff *b, int drop, int changed)
+{
+ struct snd_packet_info *pi;
+ struct sockaddr_send sendsrc;
+
+ if (drop) {
+ snd_put_buf(b);
+ return;
+ }
+
+ pi = (struct snd_packet_info *)(b->head);
+ bzero(&sendsrc, sizeof(sendsrc));
+ sendsrc.send_len = sizeof(sendsrc);
+ sendsrc.send_family = AF_INET6;
+ sendsrc.send_direction = pi->in;
+ sendsrc.send_ifidx = pi->ifinfo->ifidx;
+
+ DBG(&dbg_snd, "Sending %d bytes.\n", b->len);
+ if (sendto(sndsock, b->data, b->len, 0, (struct sockaddr *)&sendsrc,
+ sizeof(sendsrc)) < 0) {
+ DBG(&dbg_snd, "Failed to send SEND message back to kernel.");
+ perror("Failed");
+ snd_put_buf(b);
+ return;
+ }
+
+ snd_put_buf(b);
+ return;
}
==== //depot/projects/soc2009/anchie_send/send_0.2/sendd/os-freebsd/snd_freebsd.h#4 (text+ko) ====
==== //depot/projects/soc2009/anchie_send/send_0.2/sendd/os_specific.h#7 (text+ko) ====
@@ -38,9 +38,9 @@
extern void snd_recv_pkt(struct sbuff *, int, int, void *);
+extern void os_specific_add_fds(fd_set *, int *);
extern int os_specific_add_addr(struct in6_addr *, int, int, uint32_t,
uint32_t);
-extern void os_specific_add_fds(fd_set *, int *);
extern int os_specific_del_addr(struct in6_addr *, int, int);
extern void os_specific_deliver_pkt(void *, struct sbuff *, int, int);
extern void os_specific_dispatch_fds(fd_set *);
@@ -49,10 +49,6 @@
extern void os_specific_fini(void);
extern int os_specific_init(void);
-extern void snd_add_fds(fd_set *, int *, int);
-extern void snd_dispatch_fds(fd_set *);
-extern int snd_handle_iface(const char *, int);
-
static __inline__ int
sendd_max(int a, int b)
{
==== //depot/projects/soc2009/anchie_send/send_0.2/sendd/proto.c#14 (text+ko) ====
@@ -384,11 +384,7 @@
DBG(&dbg, "%s pkt (%d bytes)", drop ? "dropping" : "delivering",
pi->b->len);
-#ifdef SND_OS_freebsd
- snd_deliver_pkt(pi->os_pkt, pi->b, drop, 1);
-#else
os_specific_deliver_pkt(pi->os_pkt, pi->b, drop, 1);
-#endif
free(pi);
}
@@ -433,11 +429,7 @@
snd_put_cga_params(pi->params);
if (sig) free(sig);
pi->b->data = pi->start;
-#ifdef SND_OS_freebsd
- snd_deliver_pkt(pi->os_pkt, pi->b, drop, 1);
-#else
os_specific_deliver_pkt(pi->os_pkt, pi->b, drop, 1);
-#endif
free(pi);
}
@@ -663,11 +655,7 @@
DBG(&dbg, "%s pkt (%d bytes)", drop ? "dropping" : "delivering",
pi->b->len);
-#ifdef SND_OS_freebsd
- snd_deliver_pkt(pi->os_pkt, pi->b, drop, 1);
-#else
os_specific_deliver_pkt(pi->os_pkt, pi->b, drop, 1);
-#endif
free(pi);
}
@@ -793,11 +781,7 @@
drop = 1;
done:
b->data = start;
-#ifdef SND_OS_freebsd
- snd_deliver_pkt(pkt, b, drop, changed);
-#else
os_specific_deliver_pkt(pkt, b, drop, changed);
-#endif
}
int
==== //depot/projects/soc2009/anchie_send/send_0.2/sendd/sendd.c#13 (text+ko) ====
@@ -78,11 +78,7 @@
}
static int
-#ifdef SND_OS_freebsd
-do_select(int icmps, int snds, int ctlfd)
-#else
do_select(int icmps, int ctlfd)
-#endif
{
fd_set fds[1];
int maxfd = -1;
@@ -91,20 +87,13 @@
if (cfd != -1) maxfd = cfd;
maxfd = sendd_max(icmps, maxfd);
maxfd = sendd_max(ctlfd, maxfd);
-#ifdef SND_OS_freebsd
- maxfd = sendd_max(snds, maxfd);
-#endif
+
for (;;) {
FD_ZERO(fds);
if (cfd != -1) FD_SET(cfd, fds);
FD_SET(icmps, fds);
FD_SET(ctlfd, fds);
-#ifdef SND_OS_freebsd
- snd_add_fds(fds, &maxfd, snds);
-#else
os_specific_add_fds(fds, &maxfd);
-#endif
- DBG(&snd_dbg, "after snd_add_fds");
tv = get_next_wait(tvb);
if (select(maxfd + 1, fds, NULL, NULL, tv) < 0) {
@@ -131,11 +120,7 @@
if (FD_ISSET(ctlfd, fds)) {
snd_ctl_read(ctlfd);
}
-#ifdef SND_OS_freebsd
- snd_dispatch_fds(fds);
-#else
os_specific_dispatch_fds(fds);
-#endif
snd_replace_non_cga_linklocals();
}
}
@@ -183,14 +168,12 @@
main(int argc, char **argv)
{
int r, c, icmps, ctlfd, do_daemon = 1;
+ char *cfile = SNDD_CONF_FILE;
#ifdef SND_OS_freebsd
int snds;
+ icmps = snds = (int *) malloc(sizeof(int));
#endif
- char *cfile = SNDD_CONF_FILE;
-#ifdef SND_OS_freebsd
- icmps = snds = (int *) malloc(sizeof(int));
-#endif
#ifdef DEBUG
if (applog_open(L_STDERR, SENDD_NAME) < 0) {
exit(1);
@@ -262,13 +245,7 @@
snd_ssl_init() < 0 ||
snd_cga_init() < 0 ||
snd_params_init() < 0 ||
-#ifdef SND_OS_freebsd
- snd_net_init(&icmps, &snds) < 0 ||
- icmps < 0 ||
- snds < 0 ||
-#else
(icmps = snd_net_init()) < 0 ||
-#endif
snd_init_cert() < 0 ||
snd_pkixip_config() < 0 ||
snd_proto_init() < 0 ||
@@ -296,11 +273,7 @@
cfd = 0;
}
#endif
-#ifdef SND_OS_freebsd
- r = do_select(icmps, snds, ctlfd);
-#else
r = do_select(icmps, ctlfd);
-#endif
snd_cleanup();
exit(r);
More information about the p4-projects
mailing list