X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=lib%2Finit.c;h=bbcfb86fa52047a6c8730ef439341d594555eecb;hb=12106f49081f61243c1b8dc74d2b716c14105f0b;hp=45aa78da9f295acb92c5da2d333a53744801b170;hpb=a8a1b85846aab042cd410bf59caf617a0951380e;p=deb_libnfs.git diff --git a/lib/init.c b/lib/init.c index 45aa78d..bbcfb86 100644 --- a/lib/init.c +++ b/lib/init.c @@ -24,9 +24,9 @@ #include #include #include -#include -#include +#include #include "slist.h" +#include "libnfs-zdr.h" #include "libnfs.h" #include "libnfs-raw.h" #include "libnfs-private.h" @@ -41,6 +41,7 @@ struct rpc_context *rpc_init_context(void) } memset(rpc, 0, sizeof(struct rpc_context)); + rpc->magic = RPC_CONTEXT_MAGIC; rpc->encodebuflen = 65536; rpc->encodebuf = malloc(rpc->encodebuflen); if (rpc->encodebuf == NULL) { @@ -79,10 +80,12 @@ struct rpc_context *rpc_init_udp_context(void) void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (rpc->auth != NULL) { auth_destroy(rpc->auth); } - rpc->auth = (AUTH *)auth; + rpc->auth = auth; } @@ -90,6 +93,8 @@ void rpc_set_error(struct rpc_context *rpc, char *error_string, ...) { va_list ap; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + if (rpc->error_string != NULL) { free(rpc->error_string); } @@ -101,6 +106,8 @@ void rpc_set_error(struct rpc_context *rpc, char *error_string, ...) char *rpc_get_error(struct rpc_context *rpc) { + assert(rpc->magic == RPC_CONTEXT_MAGIC); + return rpc->error_string; } @@ -108,6 +115,8 @@ void rpc_error_all_pdus(struct rpc_context *rpc, char *error) { struct rpc_pdu *pdu; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + while((pdu = rpc->outqueue) != NULL) { pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); SLIST_REMOVE(&rpc->outqueue, pdu); @@ -120,11 +129,55 @@ 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) +{ + assert(rpc->magic == RPC_CONTEXT_MAGIC); + + 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, uint64_t size) +{ + struct rpc_fragment *fragment; + + assert(rpc->magic == RPC_CONTEXT_MAGIC); + + 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) { struct rpc_pdu *pdu; + assert(rpc->magic == RPC_CONTEXT_MAGIC); + while((pdu = rpc->outqueue) != NULL) { pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data); SLIST_REMOVE(&rpc->outqueue, pdu); @@ -136,6 +189,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; @@ -162,6 +217,7 @@ void rpc_destroy_context(struct rpc_context *rpc) rpc->udp_dest = NULL; } + rpc->magic = 0; free(rpc); }