nfs.x: some rpcgen's can not handle multiple union choices for the same arm.
[deb_libnfs.git] / lib / init.c
... / ...
CommitLineData
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
16*/
17
18#define _GNU_SOURCE
19#include <stdio.h>
20#include <stdarg.h>
21#include <unistd.h>
22#include <string.h>
23#include <strings.h>
24#include <stdlib.h>
25#include <rpc/rpc.h>
26#include <rpc/xdr.h>
27#include "slist.h"
28#include "libnfs.h"
29#include "libnfs-raw.h"
30#include "libnfs-private.h"
31
32struct rpc_context *rpc_init_context(void)
33{
34 struct rpc_context *rpc;
35
36 rpc = malloc(sizeof(struct rpc_context));
37 if (rpc == NULL) {
38 return NULL;
39 }
40 bzero(rpc, sizeof(struct rpc_context));
41
42 rpc->encodebuflen = 65536;
43 rpc->encodebuf = malloc(rpc->encodebuflen);
44 if (rpc->encodebuf == NULL) {
45 free(rpc);
46 return NULL;
47 }
48
49 rpc->auth = authunix_create_default();
50 if (rpc->auth == NULL) {
51 free(rpc->encodebuf);
52 free(rpc);
53 return NULL;
54 }
55 rpc->xid = 1;
56 rpc->fd = -1;
57
58 return rpc;
59}
60
61
62struct rpc_context *rpc_init_udp_context(void)
63{
64 struct rpc_context *rpc;
65
66 rpc = rpc_init_context();
67 if (rpc != NULL) {
68 rpc->is_udp = 1;
69 }
70
71 return rpc;
72}
73
74void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth)
75{
76 if (rpc->auth != NULL) {
77 auth_destroy(rpc->auth);
78 }
79 rpc->auth = auth;
80}
81
82
83void rpc_set_error(struct rpc_context *rpc, char *error_string, ...)
84{
85 va_list ap;
86 char *str;
87
88 if (rpc->error_string != NULL) {
89 free(rpc->error_string);
90 }
91 va_start(ap, error_string);
92 vasprintf(&str, error_string, ap);
93 rpc->error_string = str;
94 va_end(ap);
95}
96
97char *rpc_get_error(struct rpc_context *rpc)
98{
99 return rpc->error_string;
100}
101
102void rpc_error_all_pdus(struct rpc_context *rpc, char *error)
103{
104 struct rpc_pdu *pdu;
105
106 while((pdu = rpc->outqueue) != NULL) {
107 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
108 SLIST_REMOVE(&rpc->outqueue, pdu);
109 rpc_free_pdu(rpc, pdu);
110 }
111 while((pdu = rpc->waitpdu) != NULL) {
112 pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data);
113 SLIST_REMOVE(&rpc->waitpdu, pdu);
114 rpc_free_pdu(rpc, pdu);
115 }
116}
117
118
119void rpc_destroy_context(struct rpc_context *rpc)
120{
121 struct rpc_pdu *pdu;
122
123 while((pdu = rpc->outqueue) != NULL) {
124 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
125 SLIST_REMOVE(&rpc->outqueue, pdu);
126 rpc_free_pdu(rpc, pdu);
127 }
128 while((pdu = rpc->waitpdu) != NULL) {
129 pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data);
130 SLIST_REMOVE(&rpc->waitpdu, pdu);
131 rpc_free_pdu(rpc, pdu);
132 }
133
134 auth_destroy(rpc->auth);
135 rpc->auth =NULL;
136
137 if (rpc->fd != -1) {
138 close(rpc->fd);
139 }
140
141 if (rpc->encodebuf != NULL) {
142 free(rpc->encodebuf);
143 rpc->encodebuf = NULL;
144 }
145
146 if (rpc->error_string != NULL) {
147 free(rpc->error_string);
148 rpc->error_string = NULL;
149 }
150
151 if (rpc->udp_dest != NULL) {
152 free(rpc->udp_dest);
153 rpc->udp_dest = NULL;
154 }
155
156 free(rpc);
157}
158
159