PERFORCE change 125424 for review
Matus Harvan
mharvan at FreeBSD.org
Mon Aug 20 09:44:13 PDT 2007
http://perforce.freebsd.org/chv.cgi?CH=125424
Change 125424 by mharvan at mharvan_bike-planet on 2007/08/20 16:43:13
removed server from plugin_initialize() signature
TCP and UDP plugins cleanup
Affected files ...
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/mtund.c#12 edit
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/mtund.h#7 edit
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin.h#10 edit
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_dns/plugin_dns.c#4 edit
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_icmp.c#12 edit
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_tcp.c#16 edit
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_udp.c#12 edit
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_udp_catchall.c#5 edit
Differences ...
==== //depot/projects/soc2007/mharvan-mtund/mtund.src/mtund.c#12 (text+ko) ====
@@ -1211,8 +1211,8 @@
/* try to initialize plugin */
debug("initalizing plugin: %s...\n",
pl->name);
- if (((pl->initialize)(pl, server,
- host, pl->name+4)) == 0) {
+ if (((pl->initialize)(pl, host, pl->name+4))
+ == 0) {
mylog("initialized plugin %s\n",
pl->name);
set_client_pl(cl, pl);
@@ -1349,7 +1349,7 @@
/* initialize all plugins */
TAILQ_FOREACH(pl, &plugins, plugins) {
//(void) (pl->initialize)(server, host, port);
- (void) (pl->initialize)(pl, server, host, pl->name+4);
+ (void) (pl->initialize)(pl, host, pl->name+4);
// we should unload plugins which fail to initialize
}
} else { /* client */
==== //depot/projects/soc2007/mharvan-mtund/mtund.src/mtund.h#7 (text+ko) ====
@@ -48,7 +48,7 @@
/* structure representing a plugin in the main tunnel daemon */
struct plugin {
- int (*initialize)(struct plugin*, int, char*, char*);
+ int (*initialize)(struct plugin*, char*, char*);
void (*deinitialize)(struct plugin*);
void (*receive)(int fd, short ev_type, void *arg); /* select fired on some fd - check for data */
int (*send)(struct plugin*, uint8_t, char*, int, int, int*);
@@ -143,4 +143,9 @@
*/
int plugin_custom_send(struct plugin *pl, uint8_t clid, char *data, int len);
+/*
+ * debugging output
+ */
+int debug(const char *fmt, ...);
+
#endif
==== //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin.h#10 (text+ko) ====
@@ -72,7 +72,7 @@
* opened. For a client, success means a socket has been connected and
* in case of UDP handshake data exchanged.
*/
-int plugin_initialize(struct plugin *pl, const int server, char *host, char *port);
+int plugin_initialize(struct plugin *pl, char *host, char *port);
/*
* Deinitialize the plugin, close open sockets, unregister them with
==== //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_dns/plugin_dns.c#4 (text+ko) ====
@@ -1,7 +1,38 @@
+/*-
+ * Copyright (c) 2007 Matus Harvan
+ * All rights reserved
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * The name of the author may not be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
/*
* DNS encapsulation plugin
*
- * uses iodine source code
+ * calls functions from iodine source code to do the dns en/decoding
*/
#include <sys/types.h>
@@ -303,15 +334,8 @@
register_timer_ev(&data->timer_ev);
}
-/*
- * server: 0 - client, 1 - server
- *
- * Note that udp_connect wil not fail even if the remote endpoint does
- * not work. It will fail once we try to send data, so it may be wise
- * to first send some handshake data.
- */
int
-plugin_initialize(struct plugin *pl, const int server, char *host, char *port)
+plugin_initialize(struct plugin *pl, char *host, char *port)
{
struct plugin_dns_data *data = (struct plugin_dns_data*) pl->data;
int fd_flags;
==== //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_icmp.c#12 (text+ko) ====
@@ -1,3 +1,34 @@
+/*-
+ * Copyright (c) 2007 Matus Harvan
+ * All rights reserved
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * The name of the author may not be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
/*
* ICMP encapsulation plugin
*
@@ -199,15 +230,8 @@
register_timer_ev(&data->timer_ev);
}
-/*
- * server: 0 - client, 1 - server
- *
- * Note that udp_connect wil not fail even if the remote endpoint does
- * not work. It will fail once we try to send data, so it may be wise
- * to first send some handshake data.
- */
int
-plugin_initialize(struct plugin *pl, const int server, char *host, char *port)
+plugin_initialize(struct plugin *pl, char *host, char *port)
{
struct addrinfo hints, *ai;
int n;
==== //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_tcp.c#16 (text+ko) ====
@@ -1,3 +1,34 @@
+/*-
+ * Copyright (c) 2007 Matus Harvan
+ * All rights reserved
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * The name of the author may not be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
@@ -22,14 +53,9 @@
#define TEMP_CONN_TIMEOUT 5
#define USE_TCP_CATCHALL 1
-enum {
- CONN_STATUS_FREE, /* connection not in use */
- CONN_STATUS_TEMP, /* temporary connection */
- CONN_STATUS_PERM /* permanenet connection */
-};
-
#define min(a,b) ( (a>b) ? b : a )
+/* structure representing a connection, one per client */
struct conn {
int status;
uint8_t clid;
@@ -39,16 +65,28 @@
struct plugin_tcp_data *data;
};
+enum {
+ CONN_STATUS_FREE, /* connection not in use */
+ CONN_STATUS_TEMP, /* temporary connection */
+ CONN_STATUS_PERM /* permanenet connection */
+};
+
+/* plugin-specific data */
struct plugin_tcp_data {
int state; /* is a client connected? */
- //struct conn conn; /* server - bind fd, client - connect fd */
+ /* conns[0]: server - bind fd, client - connect fd */
struct conn conns[MAXCLIENTS]; /* server - accept fds */
struct conn tmpconns[MAXTMPCONNS]; /* server - accept fds */
- struct conn *conn;
+ struct conn *conn; /* most recently receiving connection */
struct plugin *pl;
};
void plugin_accept_new_conn(int fd, short ev_type, void *arg);
+void plugin_conn_timeout(int fd, short ev_type, void *arg);
+static void conn_discard(struct conn *conn);
+void plugin_conn_close(struct plugin *pl, uint8_t clid);
+static void conn_update_to_perm(struct plugin_tcp_data *data,
+ uint8_t clid, int fd);
/*
* Create a listening TCP endpoint. First get the list of potential
@@ -66,7 +104,6 @@
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
- //hints.ai_family = AF_UNSPEC;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
@@ -154,7 +191,6 @@
struct timeval tv;
memset(&hints, 0, sizeof(hints));
- //hints.ai_family = AF_UNSPEC;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
@@ -208,7 +244,14 @@
return close(fd);
}
+/* timer event handler function - used to timeout CONN_TYPE_TEMP connections */
+void
+plugin_conn_timeout(int fd, short ev_type, void *arg)
+{
+ conn_discard(arg);
+}
+/* close connection */
static void
conn_discard(struct conn *conn)
{
@@ -222,12 +265,77 @@
conn->status = CONN_STATUS_FREE;
}
+/* close connection - calls conn_discard() */
void
-plugin_conn_timeout(int fd, short ev_type, void *arg)
+plugin_conn_close(struct plugin *pl, uint8_t clid)
+{
+ struct plugin_tcp_data *data = pl->data;
+ if (server)
+ if (clid == 0)
+ conn_discard(data->conn);
+ else
+ conn_discard(&data->conns[clid]);
+ else
+ conn_discard(data->conns);
+}
+
+/* server only - update connection to CONN_TYPE_PERM */
+static void
+conn_update_to_perm(struct plugin_tcp_data *data, uint8_t clid, int fd)
{
- conn_discard(arg);
+ struct conn *conn = &data->conns[clid];
+
+ if (conn->clid == clid && conn->fd == fd)
+ return; /* nothing to do */
+
+ /* discard previous connection */
+ if (conn->status == CONN_STATUS_PERM)
+ conn_discard(conn);
+
+ /* set up the new connection */
+ conn->fd = fd;
+ conn->clid = clid;
+ event_set(&conn->ev, conn->fd, EV_PERSIST | EV_READ,
+ plugin_receive, conn);
+ event_add(&conn->ev, NULL);
+ evtimer_set(&conn->timer_ev, plugin_conn_timeout, conn);
}
+/* server only */
+void
+plugin_conn_map(struct plugin *pl, uint8_t clid, int conn_flag)
+{
+ struct plugin_tcp_data *data = pl->data;
+ struct conn *conn = data->conn;
+ int fd;
+
+ if (!server)
+ return;
+
+ /* update the connection status (server only) */
+ switch (conn_flag) {
+ case CONN_DISCARD:
+ conn_discard(conn);
+ return;
+
+ case CONN_TEMP:
+ break;
+
+ case CONN_PERM:
+ /* migrate a temporary connection to a permanent one */
+ fd = conn->fd;
+ if (conn->status == CONN_STATUS_TEMP) {
+ conn->status = CONN_STATUS_FREE;
+ evtimer_del(&conn->timer_ev);
+ event_del(&conn->ev);
+ conn->fd = -1;
+ }
+ conn_update_to_perm(data, clid, fd);
+ break;
+ }
+}
+
+
int
plugin_register(struct plugin* pl)
{
@@ -260,22 +368,15 @@
}
}
-/*
- * server: 0 - client, 1 - server
- *
- * Note that udp_connect wil not fail even if the remote endpoint does
- * not work. It will fail once we try to send data, so it may be wise
- * to first send some handshake data.
- */
int
-plugin_initialize(struct plugin *pl, const int server,
- char *host, char *port)
+plugin_initialize(struct plugin *pl, char *host, char *port)
{
struct plugin_tcp_data *data = (struct plugin_tcp_data*) pl->data;
int i;
data->pl = pl;
+ /* initialize conns and tmpconns */
for (i = 0; i < MAXCLIENTS; i++) {
data->conns[i].status = CONN_STATUS_FREE;
data->conns[i].fd = -1;
@@ -289,6 +390,7 @@
}
data->conn = NULL;
+ /* open the socket */
if (server) {
data->conns->fd = tcp_listen(port);
/* enable TCP_CATCHALL */
@@ -338,76 +440,8 @@
data->state = PLUGIN_STATE_UNINITIALIZED;
}
+/* accept a new connection on the listening fd - called by libevent */
void
-plugin_conn_close(struct plugin *pl, uint8_t clid)
-{
- struct plugin_tcp_data *data = pl->data;
- if (server)
- if (clid == 0)
- conn_discard(data->conn);
- else
- conn_discard(&data->conns[clid]);
- else
- conn_discard(data->conns);
-}
-
-/* server only */
-static void
-conn_update_to_perm(struct plugin_tcp_data *data, uint8_t clid, int fd)
-{
- struct conn *conn = &data->conns[clid];
-
- if (conn->clid == clid && conn->fd == fd)
- return; /* nothing to do */
-
- /* discard previous connection */
- if (conn->status == CONN_STATUS_PERM)
- conn_discard(conn);
-
- /* set up the new connection */
- conn->fd = fd;
- conn->clid = clid;
- event_set(&conn->ev, conn->fd, EV_PERSIST | EV_READ,
- plugin_receive, conn);
- event_add(&conn->ev, NULL);
- evtimer_set(&conn->timer_ev, plugin_conn_timeout, conn);
-}
-
-/* server only */
-void
-plugin_conn_map(struct plugin *pl, uint8_t clid, int conn_flag)
-{
- struct plugin_tcp_data *data = pl->data;
- struct conn *conn = data->conn;
- int fd;
-
- if (!server)
- return;
-
- /* update the connection status (server only) */
- switch (conn_flag) {
- case CONN_DISCARD:
- conn_discard(conn);
- return;
-
- case CONN_TEMP:
- break;
-
- case CONN_PERM:
- /* migrate a temporary connection to a permanent one */
- fd = conn->fd;
- if (conn->status == CONN_STATUS_TEMP) {
- conn->status = CONN_STATUS_FREE;
- evtimer_del(&conn->timer_ev);
- event_del(&conn->ev);
- conn->fd = -1;
- }
- conn_update_to_perm(data, clid, fd);
- break;
- }
-}
-
-void
plugin_accept_new_conn(int fd, short ev_type, void *arg)
{
struct plugin_tcp_data *data = ((struct conn *)arg)->data;
@@ -415,8 +449,8 @@
int i;
struct timeval tv;
- //assert(server);
- //assert(conn == data->conn);
+/* assert(server); */
+/* assert(conn == data->conn); */
/* find a free tmpcopnn to store the connection metadata */
for (i = 0; i < MAXTMPCONNS; i++)
@@ -448,7 +482,7 @@
evtimer_add(&conn->timer_ev, &tv);
data->conn = conn;
- //data->state = PLUGIN_STATE_CONNECTED;
+ /* data->state = PLUGIN_STATE_CONNECTED; */
}
}
@@ -464,7 +498,7 @@
uint8_t clid = 0;
int conn_flag;
- printf("plugin_tcp: plugin_receive(): fd: %d, conn: 0x%x, "
+ debug("plugin_tcp: plugin_receive(): fd: %d, conn: 0x%x, "
"conn->fd: %d\n", fd, (uint32_t)conn, conn->fd);
/* get length of the next packet */
@@ -565,4 +599,5 @@
plugin_custom_receive(struct plugin *pl, uint8_t clid,
char *data, int len)
{
+ /* nothing to do */
}
==== //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_udp.c#12 (text+ko) ====
@@ -1,24 +1,69 @@
-#include <stdio.h>
+/*-
+ * Copyright (c) 2007 Matus Harvan
+ * All rights reserved
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * The name of the author may not be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/queue.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <arpa/inet.h>
+#include <netdb.h>
+
+#include <err.h>
#include <errno.h>
#include <sys/time.h>
#include <event.h>
+#include <stdio.h>
#include <stdlib.h>
-#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <time.h>
+#include <unistd.h>
-#include <sys/queue.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-//#include <sys/select.h>
-#include <err.h>
-
#include "mtund.h"
#include "plugin.h"
+/* structure representing a connection, one per client */
+struct conn {
+ struct sockaddr_storage addr;
+ socklen_t addrlen;
+};
+
+/* plugin-specific data */
+struct plugin_udp_data {
+ int state;
+ int fd; /* udp socket to the other endpoint */
+ struct event ev; /* for monitoring fd */
+ struct conn conns[MAXCLIENTS]; /* 0 is last conn and client conn */
+};
+
/*
* Establish a connected UDP endpoint. First get the list of potential
* network layer addresses and transport layer port numbers. Iterate
@@ -27,56 +72,42 @@
* exists).
*/
-struct conn {
- struct sockaddr_storage addr;
- socklen_t addrlen;
-};
-
-struct plugin_udp_data {
- int fd; /* udp socket to the other endpoint */
- struct event ev;
- int state;
- struct conn conns[MAXCLIENTS]; /* 0 is last conn and client conn */
-};
-
static int
udp_connect(char *host, char *port)
{
- struct addrinfo hints, *ai_list, *ai;
- int n, fd = 0;
+ struct addrinfo hints, *ai_list, *ai;
+ int n, fd = 0;
- memset(&hints, 0, sizeof(hints));
- //hints.ai_family = AF_UNSPEC;
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_DGRAM;
-
- n = getaddrinfo(host, port, &hints, &ai_list);
- if (n) {
- fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(n));
- exit(EXIT_FAILURE);
- }
-
- for (ai = ai_list; ai; ai = ai->ai_next) {
- fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
- if (fd < 0) {
- continue;
- }
- if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0) {
- break;
- }
- close(fd);
- }
-
- freeaddrinfo(ai_list);
-
- if (ai == NULL) {
- fprintf(stderr, "socket or connect: failed for %s port %s\n",
- host, port);
-// exit(EXIT_FAILURE);
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_DGRAM;
+
+ n = getaddrinfo(host, port, &hints, &ai_list);
+ if (n) {
+ fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(n));
+ exit(EXIT_FAILURE);
+ }
+
+ for (ai = ai_list; ai; ai = ai->ai_next) {
+ fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (fd < 0) {
+ continue;
+ }
+ if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0) {
+ break;
+ }
+ close(fd);
+ }
+
+ freeaddrinfo(ai_list);
+
+ if (ai == NULL) {
+ fprintf(stderr, "socket or connect: failed for %s port %s\n",
+ host, port);
return -1;
- }
-
- return fd;
+ }
+
+ return fd;
}
/*
@@ -89,42 +120,41 @@
static int
udp_open(char *port)
{
- struct addrinfo hints, *ai_list, *ai;
- int n, fd = 0, on = 1;
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_flags = AI_PASSIVE;
- //hints.ai_family = AF_UNSPEC;
- hints.ai_family = AF_INET;
- hints.ai_socktype = SOCK_DGRAM;
-
- n = getaddrinfo(NULL, port, &hints, &ai_list);
- if (n) {
- fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(n));
- return -1;
- }
-
- for (ai = ai_list; ai; ai = ai->ai_next) {
- fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
- if (fd < 0) {
- continue;
- }
-
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
- if (bind(fd, ai->ai_addr, ai->ai_addrlen) == 0) {
- break;
- }
- close(fd);
- }
-
- freeaddrinfo(ai_list);
-
- if (ai == NULL) {
- fprintf(stderr, "bind failed for port %s\n", port);
- return -1;
- }
-
- return fd;
+ struct addrinfo hints, *ai_list, *ai;
+ int n, fd = 0, on = 1;
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_flags = AI_PASSIVE;
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_DGRAM;
+
+ n = getaddrinfo(NULL, port, &hints, &ai_list);
+ if (n) {
+ fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(n));
+ return -1;
+ }
+
+ for (ai = ai_list; ai; ai = ai->ai_next) {
+ fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (fd < 0) {
+ continue;
+ }
+
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+ if (bind(fd, ai->ai_addr, ai->ai_addrlen) == 0) {
+ break;
+ }
+ close(fd);
+ }
+
+ freeaddrinfo(ai_list);
+
+ if (ai == NULL) {
+ fprintf(stderr, "bind failed for port %s\n", port);
+ return -1;
+ }
+
+ return fd;
}
/*
@@ -139,27 +169,6 @@
}
-/* static struct client * */
-/* lookup_addr(struct plugin_udp_data *data, */
-/* struct sockaddr *addr, socklen_t addrlen) */
-/* { */
-/* struct client *p; */
-/* LIST_FOREACH(p, &data->clientl, clients) */
-/* if (memcmp(addr, &p->addr, addrlen) == 0) */
-/* return p; */
-/* return NULL; */
-/* } */
-
-/* static struct client * */
-/* lookup_clid(struct plugin_udp_data *data, clientid_t clid) */
-/* { */
-/* struct conn *p; */
-/* LIST_FOREACH(p, &data->conns, conns) */
-/* if (p->clid == clid) */
-/* return p; */
-/* return NULL; */
-/* } */
-
int plugin_register(struct plugin* pl) {
pl->name = "udp";
pl->mtu = 1400;
@@ -190,18 +199,9 @@
}
}
-/*
- * server: 0 - client, 1 - server
- *
- * Note that udp_connect wil not fail even if the remote endpoint does
- * not work. It will fail once we try to send data, so it may be wise
- * to first send some handshake data.
- */
int
-plugin_initialize(struct plugin *pl, const int server,
- char *host, char *port)
+plugin_initialize(struct plugin *pl, char *host, char *port)
{
- //int n = 0;
struct plugin_udp_data *data = (struct plugin_udp_data*)pl->data;
if (server == 1) {
@@ -242,7 +242,6 @@
int conn_flag;
uint8_t clid = 0;
- //memset(conn, 0, sizeof(*conn));
conn->addrlen = sizeof(conn->addr);
n = recvfrom(data->fd, packet, sizeof(packet), 0,
(struct sockaddr *) &conn->addr, &conn->addrlen);
@@ -270,11 +269,6 @@
}
process_data_from_plugin(pl, packet, n, &clid, &conn_flag);
-
-/* if (conn_flag == CONN_PERM && clid != 0) { */
-/* printf("plugin_send: CONN_PERM, clid: %hhd, updating...\n", clid); */
-/* memcpy(&data->conns[clid], conn, sizeof(*conn)); */
-/* } */
}
int
@@ -298,10 +292,8 @@
if (!server)
return;
- if (conn_flag == CONN_PERM && clid != 0) {
- printf("plugin_idp: conn_map(): CONN_PERM, clid: %hhd\n",clid);
+ if (conn_flag == CONN_PERM && clid != 0)
memcpy(&data->conns[clid], data->conns, sizeof(struct conn));
- }
}
int
@@ -312,7 +304,6 @@
int nwrite = 0;
struct conn *conn = &(datapl->conns[clid]);
- printf("plugin_send: clid: %hhd\n", clid);
if (server) {
nwrite = sendto(datapl->fd, data, len, 0,
(struct sockaddr*)&conn->addr,
@@ -326,14 +317,11 @@
}
} else { /* client */
if (datapl->state != PLUGIN_STATE_CONNECTED) {
- fprintf(stderr, "not connected yet, "
- "discarding data\n");
+ debug("not connected yet, discarding data\n");
return (SEND_ERROR);
} else {
*consumed = nwrite;
nwrite = send(datapl->fd, data, len, 0);
- fprintf(stderr, "plugin_send: send returned %d\n",
- nwrite);
}
return (SEND_PKT_SENT);
}
@@ -343,4 +331,5 @@
plugin_custom_receive(struct plugin *pl, uint8_t clid,
char *data, int len)
{
+ /* nothing to do */
}
==== //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_udp_catchall.c#5 (text+ko) ====
@@ -1,3 +1,34 @@
+/*-
+ * Copyright (c) 2007 Matus Harvan
+ * All rights reserved
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * The name of the author may not be used to endorse or promote
+ * products derived from this software without specific prior
+ * written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
#include <sys/types.h>
#include <sys/sysctl.h>
@@ -119,8 +150,7 @@
* to first send some handshake data.
*/
int
-plugin_initialize(struct plugin *pl, const int server,
- char *host, char *port)
+plugin_initialize(struct plugin *pl, char *host, char *port)
{
struct plugin_udpall_data *data = pl->data;
struct sockaddr_in sa;
More information about the p4-projects
mailing list