#include <rpc/xdr.h>
#include <rpc/auth.h>
+struct rpc_fragment {
+ struct rpc_fragment *next;
+ size_t size;
+ char *data;
+};
+
struct rpc_context {
int fd;
int is_connected;
/* 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 {
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);
+
}
}
+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)
{
rpc_free_pdu(rpc, pdu);
}
+ rpc_free_all_fragments(rpc);
+
auth_destroy(rpc->auth);
rpc->auth =NULL;
size = ntohl(*(uint32_t *)buf);
- if ((size & 0x80000000) == 0) {
- /* cant handle oncrpc fragments */
- return -1;
- }
-
return (size & 0x7fffffff) + 4;
}
{
struct rpc_pdu *pdu;
XDR xdr;
- int pos, recordmarker;
+ int pos, recordmarker = 0;
unsigned int xid;
+ char *reasbuf = NULL;
memset(&xdr, 0, sizeof(XDR));
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);
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;
}