From: Ronnie Sahlberg Date: Tue, 4 Oct 2011 01:31:56 +0000 (+1100) Subject: Fragment reassembly. Add reassembly of fragmented RPC PDUs X-Git-Tag: upstream/1.9.6^2~292 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d678b73e4d21fb34f302a65b8bcc340c2accdb96;p=deb_libnfs.git Fragment reassembly. Add reassembly of fragmented RPC PDUs --- diff --git a/include/libnfs-private.h b/include/libnfs-private.h index 85cc8f9..a8f5d26 100644 --- a/include/libnfs-private.h +++ b/include/libnfs-private.h @@ -17,6 +17,12 @@ #include #include +struct rpc_fragment { + struct rpc_fragment *next; + size_t size; + char *data; +}; + struct rpc_context { int fd; int is_connected; @@ -49,6 +55,9 @@ struct rpc_context { /* track the address we connect to so we can auto-reconnect on session failure */ struct sockaddr_storage s; int auto_reconnect; + + /* fragment reassembly */ + struct rpc_fragment *fragments; }; struct rpc_pdu { @@ -92,3 +101,6 @@ struct sockaddr *rpc_get_recv_sockaddr(struct rpc_context *rpc); void rpc_set_autoreconnect(struct rpc_context *rpc); void rpc_unset_autoreconnect(struct rpc_context *rpc); +int rpc_add_fragment(struct rpc_context *rpc, char *data, size_t size); +void rpc_free_all_fragments(struct rpc_context *rpc); + diff --git a/lib/init.c b/lib/init.c index 45aa78d..93c3745 100644 --- a/lib/init.c +++ b/lib/init.c @@ -120,6 +120,44 @@ void rpc_error_all_pdus(struct rpc_context *rpc, char *error) } } +static void rpc_free_fragment(struct rpc_fragment *fragment) +{ + if (fragment->data != NULL) { + free(fragment->data); + } + free(fragment); +} + +void rpc_free_all_fragments(struct rpc_context *rpc) +{ + while (rpc->fragments != NULL) { + struct rpc_fragment *fragment = rpc->fragments; + + SLIST_REMOVE(&rpc->fragments, fragment); + rpc_free_fragment(fragment); + } +} + +int rpc_add_fragment(struct rpc_context *rpc, char *data, size_t size) +{ + struct rpc_fragment *fragment; + + fragment = malloc(sizeof(struct rpc_fragment)); + if (fragment == NULL) { + return -1; + } + + fragment->size = size; + fragment->data = malloc(fragment->size); + if(fragment->data == NULL) { + free(fragment); + return -1; + } + + memcpy(fragment->data, data, fragment->size); + SLIST_ADD_END(&rpc->fragments, fragment); + return 0; +} void rpc_destroy_context(struct rpc_context *rpc) { @@ -136,6 +174,8 @@ void rpc_destroy_context(struct rpc_context *rpc) rpc_free_pdu(rpc, pdu); } + rpc_free_all_fragments(rpc); + auth_destroy(rpc->auth); rpc->auth =NULL; diff --git a/lib/pdu.c b/lib/pdu.c index d7426ce..8cfae61 100644 --- a/lib/pdu.c +++ b/lib/pdu.c @@ -141,11 +141,6 @@ int rpc_get_pdu_size(char *buf) size = ntohl(*(uint32_t *)buf); - if ((size & 0x80000000) == 0) { - /* cant handle oncrpc fragments */ - return -1; - } - return (size & 0x7fffffff) + 4; } @@ -214,8 +209,9 @@ int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) { struct rpc_pdu *pdu; XDR xdr; - int pos, recordmarker; + int pos, recordmarker = 0; unsigned int xid; + char *reasbuf = NULL; memset(&xdr, 0, sizeof(XDR)); @@ -226,11 +222,50 @@ int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) xdr_destroy(&xdr); return -1; } + if (!(recordmarker&0x80000000)) { + xdr_destroy(&xdr); + if (rpc_add_fragment(rpc, buf+4, size-4) != 0) { + rpc_set_error(rpc, "Failed to queue fragment for reassembly."); + return -1; + } + return 0; + } } + + /* reassembly */ + if (recordmarker != 0 && rpc->fragments != NULL) { + struct rpc_fragment *fragment; + size_t total = size - 4; + char *ptr; + + xdr_destroy(&xdr); + for (fragment = rpc->fragments; fragment; fragment = fragment->next) { + total += fragment->size; + } + + reasbuf = malloc(total); + if (reasbuf == NULL) { + rpc_set_error(rpc, "Failed to reassemble PDU"); + rpc_free_all_fragments(rpc); + return -1; + } + ptr = reasbuf; + for (fragment = rpc->fragments; fragment; fragment = fragment->next) { + memcpy(ptr, fragment->data, fragment->size); + ptr += fragment->size; + } + memcpy(ptr, buf + 4, size - 4); + xdrmem_create(&xdr, reasbuf, total, XDR_DECODE); + rpc_free_all_fragments(rpc); + } + pos = xdr_getpos(&xdr); if (xdr_int(&xdr, (int *)&xid) == 0) { rpc_set_error(rpc, "xdr_int reading xid failed"); xdr_destroy(&xdr); + if (reasbuf != NULL) { + free(reasbuf); + } return -1; } xdr_setpos(&xdr, pos); @@ -249,10 +284,16 @@ int rpc_process_pdu(struct rpc_context *rpc, char *buf, int size) if (rpc->is_udp == 0 || rpc->is_broadcast == 0) { rpc_free_pdu(rpc, pdu); } + if (reasbuf != NULL) { + free(reasbuf); + } return 0; } rpc_set_error(rpc, "No matching pdu found for xid:%d", xid); xdr_destroy(&xdr); + if (reasbuf != NULL) { + free(reasbuf); + } return -1; }