Commit | Line | Data |
---|---|---|
84004dbf RS |
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> | |
98f5fee8 M |
24 | #include <stdlib.h> |
25 | #include <rpc/rpc.h> | |
84004dbf RS |
26 | #include <rpc/xdr.h> |
27 | #include "slist.h" | |
28 | #include "libnfs.h" | |
29 | #include "libnfs-raw.h" | |
30 | #include "libnfs-private.h" | |
31 | ||
32 | struct 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 | printf("Failed to allocate rpc context\n"); | |
39 | return NULL; | |
40 | } | |
41 | bzero(rpc, sizeof(struct rpc_context)); | |
42 | ||
43 | rpc->encodebuflen = 65536; | |
44 | rpc->encodebuf = malloc(rpc->encodebuflen); | |
45 | if (rpc->encodebuf == NULL) { | |
46 | printf("Failed to allocate a buffer for rpc encoding\n"); | |
47 | free(rpc); | |
48 | return NULL; | |
49 | } | |
50 | ||
51 | rpc->auth = authunix_create_default(); | |
52 | if (rpc->auth == NULL) { | |
53 | printf("failed to create authunix\n"); | |
54 | free(rpc->encodebuf); | |
55 | free(rpc); | |
56 | return NULL; | |
57 | } | |
58 | rpc->xid = 1; | |
59 | rpc->fd = -1; | |
60 | ||
61 | return rpc; | |
62 | } | |
63 | ||
64 | ||
65 | void rpc_set_auth(struct rpc_context *rpc, struct AUTH *auth) | |
66 | { | |
67 | if (rpc->auth != NULL) { | |
68 | auth_destroy(rpc->auth); | |
69 | } | |
70 | rpc->auth = auth; | |
71 | } | |
72 | ||
73 | ||
74 | void rpc_set_error(struct rpc_context *rpc, char *error_string, ...) | |
75 | { | |
76 | va_list ap; | |
77 | char *str; | |
78 | ||
79 | if (rpc->error_string != NULL) { | |
80 | free(rpc->error_string); | |
81 | } | |
82 | va_start(ap, error_string); | |
83 | vasprintf(&str, error_string, ap); | |
84 | rpc->error_string = str; | |
85 | va_end(ap); | |
86 | } | |
87 | ||
88 | char *rpc_get_error(struct rpc_context *rpc) | |
89 | { | |
90 | return rpc->error_string; | |
91 | } | |
92 | ||
93 | void rpc_error_all_pdus(struct rpc_context *rpc, char *error) | |
94 | { | |
95 | struct rpc_pdu *pdu; | |
96 | ||
97 | while((pdu = rpc->outqueue) != NULL) { | |
98 | pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); | |
99 | SLIST_REMOVE(&rpc->outqueue, pdu); | |
100 | rpc_free_pdu(rpc, pdu); | |
101 | } | |
102 | while((pdu = rpc->waitpdu) != NULL) { | |
103 | pdu->cb(rpc, RPC_STATUS_ERROR, error, pdu->private_data); | |
104 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
105 | rpc_free_pdu(rpc, pdu); | |
106 | } | |
107 | } | |
108 | ||
109 | ||
110 | void rpc_destroy_context(struct rpc_context *rpc) | |
111 | { | |
112 | struct rpc_pdu *pdu; | |
113 | ||
114 | while((pdu = rpc->outqueue) != NULL) { | |
115 | pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data); | |
116 | SLIST_REMOVE(&rpc->outqueue, pdu); | |
117 | rpc_free_pdu(rpc, pdu); | |
118 | } | |
119 | while((pdu = rpc->waitpdu) != NULL) { | |
120 | pdu->cb(rpc, RPC_STATUS_CANCEL, NULL, pdu->private_data); | |
121 | SLIST_REMOVE(&rpc->waitpdu, pdu); | |
122 | rpc_free_pdu(rpc, pdu); | |
123 | } | |
124 | ||
125 | auth_destroy(rpc->auth); | |
126 | rpc->auth =NULL; | |
127 | ||
128 | if (rpc->fd != -1) { | |
129 | close(rpc->fd); | |
130 | } | |
131 | ||
132 | if (rpc->encodebuf != NULL) { | |
133 | free(rpc->encodebuf); | |
134 | rpc->encodebuf = NULL; | |
135 | } | |
136 | ||
137 | if (rpc->error_string != NULL) { | |
138 | free(rpc->error_string); | |
139 | rpc->error_string = NULL; | |
140 | } | |
141 | ||
142 | free(rpc); | |
143 | } | |
144 | ||
145 |