Date: Mon, 23 Jul 2007 16:47:22 GMT From: Matus Harvan <mharvan@FreeBSD.org> To: Perforce Change Reviews <perforce@FreeBSD.org> Subject: PERFORCE change 123975 for review Message-ID: <200707231647.l6NGlM5P092929@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=123975 Change 123975 by mharvan@mharvan_twoflower on 2007/07/23 16:46:34 plugins can choose whether the daemon should prepend a fragmentation header the tcp plugin does framing on its own Affected files ... .. //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_icmp.c#3 edit .. //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_tcp.c#5 edit .. //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_udp.c#4 edit .. //depot/projects/soc2007/mharvan-mtund/mtund.src/tunneld.c#10 edit .. //depot/projects/soc2007/mharvan-mtund/mtund.src/tunneld.h#5 edit Differences ... ==== //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_icmp.c#3 (text+ko) ==== @@ -113,6 +113,7 @@ int plugin_register(plugint* pl) { pl->name = "icmp"; + pl->need_frag = 1; pl->initialize = plugin_initialize; pl->deinitialize = plugin_deinitialize; pl->send = plugin_send; @@ -185,7 +186,7 @@ if (data->server) { #ifdef __FreeBSD__ - //system("sysctl net.ipv4.icmp_echo_ignore_all=0"); + system("sysctl net.ipv4.icmp_echo_ignore_all=0"); #else system("sysctl net.ipv4.icmp_echo_ignore_all=0"); #endif ==== //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_tcp.c#5 (text+ko) ==== @@ -15,6 +15,8 @@ #include "tunneld.h" #include "plugin.h" +#define min(a,b) ( (a>b) ? b : a ) + /* * Establish a connected UDP endpoint. First get the list of potential * network layer addresses and transport layer port numbers. Iterate @@ -188,6 +190,7 @@ int plugin_register(plugint* pl) { pl->name = "tcp"; + pl->need_frag = 0; pl->initialize = plugin_initialize; pl->deinitialize = plugin_deinitialize; pl->send = plugin_send; @@ -247,7 +250,9 @@ plugin_tcp_datat *data = (plugin_tcp_datat*) pl->data; int n = 0; char packet[PACKETLEN]; + char *packet_p = packet; int new_fd = -1; + int len; //fprintf(stderr, "state: %d\n", data->state); if (! (data->state == PLUGIN_STATE_CONNECTED @@ -272,19 +277,42 @@ //report_plugin_error(pl, PLUGIN_ERROR_SUCCESS); } } else { - //n = read(data->fd, packet, sizeof(packet)); - n = recv(fd, packet, sizeof(packet),0); - //fprintf(stderr, "n: %d\n", n); - if (n <= 0) { - /* client disconnected */ - unregister_select_fd(fd); - data->state = PLUGIN_STATE_DISCONNECTED; - report_plugin_error(pl, PLUGIN_ERROR_RECEIVE); - } else if (n > 0) { + /* get length of the next packet */ + n = recv(fd, &len, sizeof(len),0); + if (n <= 0) goto recv_error; + + if (len <= sizeof(packet)) { + while (len>0) { + n = recv(fd, packet_p, len,0); + if (n <= 0) { + goto recv_error; + } else { + len -= n; + packet_p += n; + } + } process_data_from_plugin(pl, packet, n); + //report_plugin_error(pl, PLUGIN_ERROR_SUCCESS); + /* packet too large */ + } else { + fprintf(stderr, "client sent a too large packet (len: %d)\n", + len); + while(len) { + n = recv(fd, packet, min(len, sizeof(packet)),0); + if (n <= 0) { + goto recv_error; + } else { + len -= n; + } + } } + return; + recv_error: + /* client disconnected */ + unregister_select_fd(fd); + data->state = PLUGIN_STATE_DISCONNECTED; + report_plugin_error(pl, PLUGIN_ERROR_RECEIVE); } - //report_plugin_error(pl, PLUGIN_ERROR_SUCCESS); } /* @@ -294,11 +322,15 @@ int plugin_send(plugint *pl, char *data, int len) { plugin_tcp_datat *datapl = (plugin_tcp_datat*) pl->data; int n = 0; + if (datapl->state != PLUGIN_STATE_CONNECTED) { fprintf(stderr, "no client connected yet, discarding data\n"); return 0; } else { //n = write(datapl->fd, data, len); + // TODO: we should use on buffer only as the current + // approach generates two packets + n = send(datapl->fd, &len, sizeof(len), 0); n = send(datapl->fd, data, len, 0); //fprintf(stderr, "plugin_send: fd: %d\n", datapl->fd); //fprintf(stderr, "plugin_send: write returned %d\n", n); ==== //depot/projects/soc2007/mharvan-mtund/mtund.src/plugin_udp.c#4 (text+ko) ==== @@ -129,6 +129,7 @@ int plugin_register(plugint* pl) { pl->name = "udp"; + pl->need_frag = 1; pl->initialize = plugin_initialize; pl->deinitialize = plugin_deinitialize; pl->send = plugin_send; ==== //depot/projects/soc2007/mharvan-mtund/mtund.src/tunneld.c#10 (text+ko) ==== @@ -32,7 +32,7 @@ #define min(a,b) ( (a>b) ? b : a ) -/* max transfered unit - capsuled packet size */ +/* max transfered unit - encapsulated packet size */ //const int mtu = 1400; #define MTU 1500 /* how many pings can fail before the plugin is declared broken */ @@ -262,7 +262,8 @@ { u_int8_t dispatch = *data; frag_hdr_t *frag_hdr = NULL; - frag_llist_t *p, *q, **pp; + frag_llist_t *p; /* pointer to frag info about the processed fragment */ + frag_llist_t **pp; /* previous item before p */ struct timeval tv; int i; int dgram_reassembled; @@ -288,7 +289,7 @@ fprintf(stderr, "got a frag header: id %d, size %d, off %d, len %d\n", frag_hdr->id, frag_hdr->size, frag_hdr->offset, len); - /* search frag info in list */ + /* check if reassembly for this fragment has already started */ pp = &frag_list; for(p = frag_list; p; p = p->next) { if (p->id == frag_hdr->id && @@ -300,7 +301,7 @@ /* found in list */ if (p) { fprintf(stderr, "found frag info in list\n"); - /* fragment info not found in list, start a new reassembly */ + /* fragment info not found in list, start a new reassembly */ } else { fprintf(stderr, "frag info NOT found in list\n"); /* allocate memory */ @@ -349,6 +350,7 @@ } } else { fprintf(stderr, "fragment outside of packet payload\n"); + return; } /* check if the complete packet has been reassembled */ @@ -357,23 +359,13 @@ for(i=0; i < p->size/8 && dgram_reassembled; i++) { if (p->bitmap[i] != 0xff) { dgram_reassembled = 0; -/* printf("dgram_reassembled = 0 (bitmap[%d]: 0x%x)\n", */ -/* i, p->bitmap[i]); */ } } for(i=0; i < p->size%8 && dgram_reassembled; i++) { if (! (p->bitmap[p->size/8] & (1 << i))) { dgram_reassembled=0; -/* printf("dgram_reassembled = 0 (bitmap<<%d)\n", i); */ -/* printf("bitmap[%d]: 0x%x)\n", */ -/* p->size/8, p->bitmap[p->size/8]); */ } } -/* printf("bitmap: 0x"); */ -/* for(i=0; i < p->size/8 + 1; i++) { */ -/* printf("%x",p->bitmap[i]); */ -/* } */ -/* printf("\n"); */ /* packet completely reassembled */ if (dgram_reassembled) { fprintf(stderr, "frag reassembly: packet complete\n"); @@ -381,11 +373,10 @@ tun_send(p->buf, p->size); /* remove fragment info from linked list */ - q = p->next; + *pp = p->next; free(p->buf); free(p->bitmap); free(p); - *pp = q; } break; case DISPATCH_ECHO_REQUEST: @@ -419,8 +410,11 @@ if (current_pl == NULL) { fprintf(stderr, "no plugin connected yet, discarding tun data\n"); report_plugin_error(NULL, PLUGIN_ERROR_BOOTSTRAP); - } else { -#ifdef NO_FRAG + return; + } + + /* no need to add the fragmentation header */ + if (! current_pl->need_frag) { *ldata = DISPATCH_DATA; memcpy(ldata+1, data, min(sizeof(ldata)-1, len)); n = current_pl->send(current_pl, ldata, min(sizeof(ldata), len+1)); @@ -429,7 +423,8 @@ fprintf(stderr, "process_data_from_tun: plugind sent less " "bytes (%d) than requested (%d)\n", n, len); } -#else /* NO_FRAG */ + /* add the fragmentation header */ + } else { // mtu = current_pl->mtu; /* prepare the frag header */ @@ -467,7 +462,6 @@ frag_hdr.offset += n; len -= n; } -#endif /* NO_FRAG */ } } @@ -603,19 +597,22 @@ /* setup the tun interface */ if (server) { - #ifdef __FreeBSD__ +#ifdef __FreeBSD__ ssystem("ifconfig tun0 mtu 1400 192.168.0.1 192.168.0.2"); - #endif +#endif +#ifdef __linux + ssystem("ifconfig tun0 mtu 1400 192.168.0.1"); + ssystem("route add 192.168.0.2 tun0"); +#endif } else { - /* FreeBSD */ - #ifdef __FreeBSD__ +#ifdef __FreeBSD__ ssystem("ifconfig tun0 mtu 1400 192.168.0.2 192.168.0.1"); - #else - /* Linux */ +#endif +#ifdef __linux ssystem("ifconfig tun0 mtu 1400 192.168.0.2"); ssystem("route add 192.168.0.1 tun0"); - #endif +#endif } signal(SIGHUP, sigcb); @@ -635,8 +632,8 @@ plugins->name = "tcp_2222"; load_plugin("./plugin_tcp.so"); plugins->name = "tcp_3333"; - load_plugin("./plugin_icmp.so"); - plugins->name = "icmp"; + // load_plugin("./plugin_icmp.so"); + // plugins->name = "icmp"; if (server) { /* initialize all plugins */ ==== //depot/projects/soc2007/mharvan-mtund/mtund.src/tunneld.h#5 (text+ko) ==== @@ -44,6 +44,8 @@ int (*send)(struct _plugint*, char*, int); void (*receive)(int fd, short ev_type, void *arg); /* select fired on some fd - check for data */ void* data; + int need_frag; + int mtu; struct _plugint *next; } plugint;
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200707231647.l6NGlM5P092929>