13fe6e175a4d2487e173359fbd23e17ef3e95da9
[sfrench/samba-autobuild/.git] / source3 / librpc / ndr / util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libndr interface
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23
24 /*
25  * Convert an ndr error to string
26  */
27
28 const char *ndr_errstr(enum ndr_err_code err)
29 {
30         switch (err) {
31         case NDR_ERR_SUCCESS:
32                 return "NDR_ERR_SUCCESS";
33                 break;
34         case NDR_ERR_ARRAY_SIZE:
35                 return "NDR_ERR_ARRAY_SIZE";
36                 break;
37         case NDR_ERR_BAD_SWITCH:
38                 return "NDR_ERR_BAD_SWITCH";
39                 break;
40         case NDR_ERR_OFFSET:
41                 return "NDR_ERR_OFFSET";
42                 break;
43         case NDR_ERR_RELATIVE:
44                 return "NDR_ERR_RELATIVE";
45                 break;
46         case NDR_ERR_CHARCNV:
47                 return "NDR_ERR_CHARCNV";
48                 break;
49         case NDR_ERR_LENGTH:
50                 return "NDR_ERR_LENGTH";
51                 break;
52         case NDR_ERR_SUBCONTEXT:
53                 return "NDR_ERR_SUBCONTEXT";
54                 break;
55         case NDR_ERR_COMPRESSION:
56                 return "NDR_ERR_COMPRESSION";
57                 break;
58         case NDR_ERR_STRING:
59                 return "NDR_ERR_STRING";
60                 break;
61         case NDR_ERR_VALIDATE:
62                 return "NDR_ERR_VALIDATE";
63                 break;
64         case NDR_ERR_BUFSIZE:
65                 return "NDR_ERR_BUFSIZE";
66                 break;
67         case NDR_ERR_ALLOC:
68                 return "NDR_ERR_ALLOC";
69                 break;
70         case NDR_ERR_RANGE:
71                 return "NDR_ERR_RANGE";
72                 break;
73         case NDR_ERR_TOKEN:
74                 return "NDR_ERR_TOKEN";
75                 break;
76         case NDR_ERR_IPV4ADDRESS:
77                 return "NDR_ERR_IPV4ADDRESS";
78                 break;
79         case NDR_ERR_IPV6ADDRESS:
80                 return "NDR_ERR_IPV6ADDRESS";
81                 break;
82         case NDR_ERR_INVALID_POINTER:
83                 return "NDR_ERR_INVALID_POINTER";
84                 break;
85         case NDR_ERR_UNREAD_BYTES:
86                 return "NDR_ERR_UNREAD_BYTES";
87                 break;
88         case NDR_ERR_NDR64:
89                 return "NDR_ERR_NDR64";
90                 break;
91         }
92
93         return talloc_asprintf(talloc_tos(), "Unknown NDR error: %d", err);
94 }
95
96 enum ndr_err_code ndr_push_server_id(struct ndr_push *ndr, int ndr_flags, const struct server_id *r)
97 {
98         if (ndr_flags & NDR_SCALARS) {
99                 NDR_CHECK(ndr_push_align(ndr, 4));
100                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS,
101                                           (uint32_t)r->pid));
102 #ifdef CLUSTER_SUPPORT
103                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS,
104                                           (uint32_t)r->vnn));
105 #endif
106         }
107         if (ndr_flags & NDR_BUFFERS) {
108         }
109         return NDR_ERR_SUCCESS;
110 }
111
112 enum ndr_err_code ndr_pull_server_id(struct ndr_pull *ndr, int ndr_flags, struct server_id *r)
113 {
114         if (ndr_flags & NDR_SCALARS) {
115                 uint32_t pid;
116                 NDR_CHECK(ndr_pull_align(ndr, 4));
117                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &pid));
118 #ifdef CLUSTER_SUPPORT
119                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &r->vnn));
120 #endif
121                 r->pid = (pid_t)pid;
122         }
123         if (ndr_flags & NDR_BUFFERS) {
124         }
125         return NDR_ERR_SUCCESS;
126 }
127
128 void ndr_print_server_id(struct ndr_print *ndr, const char *name, const struct server_id *r)
129 {
130         ndr_print_struct(ndr, name, "server_id");
131         ndr->depth++;
132         ndr_print_uint32(ndr, "id", (uint32_t)r->pid);
133 #ifdef CLUSTER_SUPPORT
134         ndr_print_uint32(ndr, "vnn", (uint32_t)r->vnn);
135 #endif
136         ndr->depth--;
137 }
138
139 _PUBLIC_ void ndr_print_sockaddr_storage(struct ndr_print *ndr, const char *name, const struct sockaddr_storage *ss)
140 {
141         char addr[INET6_ADDRSTRLEN];
142         ndr->print(ndr, "%-25s: %s", name, print_sockaddr(addr, sizeof(addr), ss));
143 }