PERFORCE change 125706 for review
Matus Harvan
mharvan at FreeBSD.org
Sun Aug 26 04:10:23 PDT 2007
http://perforce.freebsd.org/chv.cgi?CH=125706
Change 125706 by mharvan at mharvan_bike-planet on 2007/08/26 11:10:14
DNS plugin bugfixing
Affected files ...
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/mtund.c#14 edit
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_dns/dns.c#2 edit
.. //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_dns/plugin_dns.c#5 edit
Differences ...
==== //depot/projects/soc2007/mharvan-mtund/mtund.src/mtund.c#14 (text+ko) ====
@@ -826,7 +826,7 @@
case DISPATCH_ECHO_REQUEST:
debug("process_data_from_plugin(): ECHO_REQUEST\n");
debug("got echo request (plugin: %s)\n", pl->name);
-
+ debug("seq: %u\n", *(data+1));
/* pings are qualify for temporary connection status only */
*conn_flag = CONN_TEMP;
pl->conn_map(pl, *clid, CONN_TEMP);
@@ -858,7 +858,7 @@
if (server) {
if (*clid != 0)
set_client_pl(cl, pl);
- } else
+ } else /* client */
free(data);
break;
@@ -904,7 +904,7 @@
return;
}
- /* TODO reqid = *(data+1); */
+ //TODO reqid = *((long*)(data+1));
/* assign a new client ID */
for (i = 1; i < MAXCLIENTS && clients[i].used != 0; i++);
@@ -941,7 +941,7 @@
}
if (myclid != 0) { /* ID already assigned */
- pl->conn_map(pl, *clid, CONN_DISCARD);
+ //pl->conn_map(pl, *clid, CONN_DISCARD);
return;
}
@@ -1329,8 +1329,8 @@
/* pl = load_plugin("./plugin_udp_catchall.so"); */
/* pl->name = "udp_catchall"; */
/* } else { /\* client *\/ */
- pl = load_plugin("./plugin_udp.so");
- pl->name = "udp_1234";
+/* pl = load_plugin("./plugin_udp.so"); */
+/* pl->name = "udp_1234"; */
/* pl = load_plugin("./plugin_udp.so"); */
/* pl->name = "udp_1235"; */
/* /\* pl = load_plugin("./plugin_udp.so"); *\/ */
@@ -1340,8 +1340,8 @@
/* pl->name = "tcp_1234"; */
/* pl = load_plugin("./plugin_icmp.so"); */
/* pl->name = "icmp"; */
-/* pl = load_plugin("./plugin_dns/plugin_dns.so"); */
-/* pl->name = "dns_53"; */
+ pl = load_plugin("./plugin_dns/plugin_dns.so");
+ pl->name = "dns_53";
if (server) {
/* initialize all plugins */
==== //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_dns/dns.c#2 (text+ko) ====
@@ -105,7 +105,7 @@
HEADER *header;
short qdcount;
short ancount;
- char name[255];
+ char name[256];
uint32_t ttl;
short class;
short type;
@@ -167,8 +167,8 @@
return -1;
}
- readname(packet, packetlen, &data, name, sizeof(name) -1);
- name[256] = 0;
+ readname(packet, packetlen, &data, name, sizeof(name) - 2);
+ name[sizeof(name) - 1] = 0;
readshort(packet, &data, &type);
readshort(packet, &data, &class);
@@ -177,7 +177,7 @@
break;
}
- strncpy(q->name, name, 257);
+ strncpy(q->name, name, sizeof(name));
q->type = type;
q->id = id;
==== //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_dns/plugin_dns.c#5 (text+ko) ====
@@ -65,7 +65,9 @@
* how often should an empty request be sent to the server - This is
* useful when the server has data to send but the client doesn't.
*/
-#define PLUGIN_DNS_KEEP_ALIVE 1
+#define PLUGIN_DNS_KEEP_ALIVE_SEC 0
+#define PLUGIN_DNS_KEEP_ALIVE_USEC 500000
+
#define TOPDOMAIN "matustest.eecs.jacobs-university.de"
struct conn {
@@ -111,7 +113,6 @@
int n, fd = 0;
memset(&hints, 0, sizeof(hints));
- //hints.ai_family = AF_UNSPEC;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
@@ -159,7 +160,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_DGRAM;
@@ -192,24 +192,14 @@
return fd;
}
-static void
-dump_data(char *data, int len)
-{
- int i;
- printf("(%d bytes) ", len);
- for(i = 0; i < len; i++)
- printf("%02hhx ", *(data+i));
- printf("\n");
-}
-
/* register a timer event */
static void
register_timer_ev(struct event *ev)
{
struct timeval tv;
- tv.tv_sec=PLUGIN_DNS_KEEP_ALIVE;
- tv.tv_usec=0;
+ tv.tv_sec=PLUGIN_DNS_KEEP_ALIVE_SEC;
+ tv.tv_usec=PLUGIN_DNS_KEEP_ALIVE_USEC;
evtimer_del(ev);
evtimer_add(ev, &tv);
}
@@ -226,7 +216,7 @@
char tmpdata[4];
- /* DNS encoding/decoding seems broken for shorter payloads */
+ /* DNS encoding/decoding seems to be broken for too short payloads */
if (datalen < 3) {
memset(tmpdata, 0, sizeof(tmpdata));
memcpy(tmpdata, data, datalen);
@@ -234,8 +224,6 @@
datalen=sizeof(tmpdata);
}
-/* printf("dns_send(): datalen: %d\n", datalen); */
-/* dump_data(data, datalen); */
if (server) {
len = dns_encode(buf, sizeof(buf), &pldata->q, QR_ANSWER,
data, datalen);
@@ -264,6 +252,7 @@
conn_init(struct conn *conn)
{
conn->used = 0;
+ conn->clid = 0;
conn->queued_normal_data = NULL;
conn->queued_normal_data_len = 0;
@@ -290,8 +279,8 @@
if (server)
//pl->mtu = 1024;
pl->mtu = 512;
- else
- pl->mtu = 0;
+ else /* client */
+ pl->mtu = 0; /* always prepend the fragment header */
pl->initialize = plugin_initialize;
pl->deinitialize = plugin_deinitialize;
pl->is_ready_to_send = plugin_is_ready_to_send;
@@ -338,10 +327,12 @@
plugin_initialize(struct plugin *pl, char *host, char *port)
{
struct plugin_dns_data *data = (struct plugin_dns_data*) pl->data;
- int fd_flags;
+ int i;
- conn_init(data->conns);
+ for (i = 0; i < MAXCLIENTS; i++)
+ conn_init(&data->conns[i]);
data->conn = data->conns;
+ memset(&data->q, 0, sizeof(data->q));
if (server) {
data->fd = udp_open(port);
} else {
@@ -351,13 +342,6 @@
if (data->fd == -1)
return -1;
- /* non-blocking i/o */
- fd_flags = fcntl(data->fd, F_GETFL, 0);
- if (fd_flags == -1)
- errx(EX_OSFILE,
- "Failed to get flags from the dns socket fd\n");
- fcntl(data->fd, F_SETFL, fd_flags|O_NONBLOCK);
-
event_set(&data->ev, data->fd, EV_PERSIST | EV_READ,
plugin_receive, pl);
event_add(&data->ev, NULL);
@@ -366,8 +350,6 @@
data->state = PLUGIN_STATE_INITIALIZED;
} else { /* client */
data->state = PLUGIN_STATE_CONNECTED;
- event_set(&data->ev, data->fd, EV_PERSIST | EV_READ,
- plugin_receive, pl);
/* the client should send keep-alive request to the server */
evtimer_set(&data->timer_ev, plugin_dns_timer_ev_handler, pl);
@@ -392,31 +374,6 @@
data->state = PLUGIN_STATE_UNINITIALIZED;
}
-/* static int */
-/* send_dns_pkt(struct plugin *pl, struct conn *conn, char *data, int len) */
-/* { */
-/* struct plugin_dns_data *pldata = pl->data; */
-/* int n = 0; */
-
-/* printf("send_dns_pkt(): len: %d\n", len); */
-
-/* //[...] */
-
-/* if (server) */
-/* n = sendto(pldata->fd, data, len, 0, */
-/* (struct sockaddr*)&pldata->addr, pldata->addrlen); */
-/* else */
-/* n = send(pldata->fd, data, len, 0); */
-/* fprintf(stderr, "plugin_send: send returned %d\n", n); */
-
-/* /\* the client has to reset the timer for keep-alive requests *\/ */
-/* if (!server) { /\* client *\/ */
-/* register_timer_ev(&pldata->timer_ev); */
-/* } */
-
-/* return n; */
-/* } */
-
void
plugin_receive(int fd, short ev_type, void *arg)
{
@@ -433,8 +390,6 @@
int r;
char *domain;
- printf("plugin_receive(ev_type: 0x%x)\n", ev_type);
-
memset(&data->q, 0, sizeof(data->q));
/* read data from the socket */
@@ -442,8 +397,6 @@
r = recvfrom(data->fd, packet, sizeof(packet), 0,
(struct sockaddr *) &from, &fromlen);
if (r == -1) {
- if (errno == EAGAIN && !server)
- goto send_request;
warn("plugin_dns: plugin_receive: recvfrom() returned %d", r);
plugin_report(pl, 0, REPORT_ERROR_RECEIVE);
return;
@@ -457,7 +410,6 @@
dns_decode(buf, buflen, &data->q, QR_QUERY, packet, r);
domain = strstr(data->q.name, data->topdomain);
n = decode_data(buf, buflen, data->q.name, domain);
- printf("domain: %s, decoded: %s\n", domain, buf);
if (n <= 0)
return;
@@ -468,7 +420,7 @@
(struct sockaddr*)&from, fromlen);
/* use connection 0 as a temporary connection */
- data->conn = data->conns;
+ data->conn = &data->conns[0];
plugin_conn_close(pl, data->conn->clid);
conn_init(data->conn);
@@ -493,7 +445,7 @@
buflen = dns_decode(buf, sizeof(buf), &data->q, QR_ANSWER,
packet, r);
process_data_from_plugin(pl, buf, buflen, &clid, &conn_flag);
- send_request:
+
/*
* The client should send back an empty request if there was no
* data to send. This allows the server to send more data back.
@@ -533,14 +485,14 @@
/* send the queued data */
if (conn->queued_urgent_data != NULL ||
conn->queued_normal_data != NULL) {
- if (conn->queued_urgent_data != NULL) {
+ if (conn->queued_urgent_data != NULL) { /* urgent data */
dns_send(pl->data, conn,
conn->queued_urgent_data,
conn->queued_urgent_data_len);
free(conn->queued_urgent_data);
conn->queued_urgent_data = NULL;
conn->queued_urgent_data_len = 0;
- } else {
+ } else { /* normal data */
dns_send(pl->data, conn,
conn->queued_normal_data,
conn->queued_normal_data_len);
@@ -622,50 +574,48 @@
conn = conn_by_clid(pldata, clid);
- printf("plugin_dns: plugin_send(): clid: %hhu, data_type: %d\n",
- clid, data_type);
- printf("plugin_send(): urgent_data: %d, normal_data: %d\n",
- conn->queued_urgent_data_len, conn->queued_normal_data_len);
-
if (server) { /* server - queue the data */
+ /* locate the queue corresponding to the data type */
switch (data_type) {
case NORMAL_DATA:
queued_data = &conn->queued_normal_data;
queued_data_len = &conn->queued_normal_data_len;
- goto process_queued_data;
+ break;
case URGENT_DATA:
queued_data = &conn->queued_urgent_data;
queued_data_len = &conn->queued_urgent_data_len;
-
- process_queued_data:
- if (*queued_data == NULL) {
- *queued_data = malloc(len);
- if (*queued_data == NULL) {
- return SEND_ERROR_MALLOC;
- }
- memcpy(*queued_data, data, len);
- *queued_data_len = len;
- *consumed = len;
- return SEND_PKT_QUEUED;
- } else {
- *consumed = 0;
- return SEND_ERROR_QUEUE_FULL;
- }
+ break;
default:
errx(EX_SOFTWARE, "plugin_dns: plugin_send(): "
"invalid data_type 0x%x\n", data_type);
+ return (SEND_ERROR);
}
+
+ /* queue the data */
+ if (*queued_data == NULL) {
+ *queued_data = malloc(len);
+ if (*queued_data == NULL) {
+ return (SEND_ERROR_MALLOC);
+ }
+ memcpy(*queued_data, data, len);
+ *queued_data_len = len;
+ *consumed = len;
+ return (SEND_PKT_QUEUED);
+ } else {
+ *consumed = 0;
+ return (SEND_ERROR_QUEUE_FULL);
+ }
} else { /* client - send the data straight away */
pldata->q.id++;
*consumed = dns_send(pldata, pldata->conns, data, len);
if (*consumed > 0) {
conn->data_sent_after_last_receive = 1;
- return SEND_PKT_SENT;
+ return (SEND_PKT_SENT);
} else
- return SEND_ERROR;
+ return (SEND_ERROR);
}
}
More information about the p4-projects
mailing list