XRootD
XProtocol.cc
Go to the documentation of this file.
1 /******************************************************************************/
2 /* */
3 /* X P r o t o c o l . c c */
4 /* */
5 /* (c) 2016 by the Board of Trustees of the Leland Stanford, Jr., University */
6 /* Produced by Andrew Hanushevsky for Stanford University under contract */
7 /* DE-AC02-76-SFO0515 with the Department of Energy */
8 /* */
9 /* This file is part of the XRootD software suite. */
10 /* */
11 /* XRootD is free software: you can redistribute it and/or modify it under */
12 /* the terms of the GNU Lesser General Public License as published by the */
13 /* Free Software Foundation, either version 3 of the License, or (at your */
14 /* option) any later version. */
15 /* */
16 /* The XRootD protocol definition, documented in this file, is distributed */
17 /* under a modified BSD license and may be freely used to reimplement it. */
18 /* Any references to "source" in this license refers to this file or any */
19 /* other file that specifically contains the following license. */
20 /* */
21 /* Redistribution and use in source and binary forms, with or without */
22 /* modification, are permitted provided that the following conditions */
23 /* are met: */
24 /* */
25 /* 1. Redistributions of source code must retain the above copyright notice, */
26 /* this list of conditions and the following disclaimer. */
27 /* */
28 /* 2. Redistributions in binary form must reproduce the above copyright */
29 /* notice, this list of conditions and the following disclaimer in the */
30 /* documentation and/or other materials provided with the distribution. */
31 /* */
32 /* 3. Neither the name of the copyright holder nor the names of its */
33 /* contributors may be used to endorse or promote products derived from */
34 /* this software without specific prior written permission. */
35 /* */
36 /* 4. Derived software may not use the name XRootD or cmsd (regardless of */
37 /* capitilization) in association with the derived work if the protocol */
38 /* documented in this file is changed in any way. */
39 /* */
40 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
41 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
42 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR */
43 /* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
44 /* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
45 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
46 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
47 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY */
48 /* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
49 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE */
50 /* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
51 /******************************************************************************/
52 
53 #include <cinttypes>
54 #include <netinet/in.h>
55 #include <sys/types.h>
56 #include <cstring>
57 #include <cstdlib>
58 #include <cstdio>
59 #include <sys/stat.h>
60 #include <unistd.h>
61 
62 #include "XProtocol/XProtocol.hh"
63 
64 /******************************************************************************/
65 /* G l o b a l s */
66 /******************************************************************************/
67 
68 namespace
69 {
70 const char *errNames[kXR_ERRFENCE-kXR_ArgInvalid] =
71  {"Invalid argument", // kXR_ArgInvalid = 3000,
72  "Missing argument", // kXR_ArgMissing
73  "Argument is too long", // kXR_ArgTooLong
74  "File or object is locked", // kXR_FileLocked
75  "File or object not open", // kXR_FileNotOpen
76  "Filesystem error", // kXR_FSError
77  "Invalid request", // kXR_InvalidRequest
78  "I/O error", // kXR_IOError
79  "Insufficient memory", // kXR_NoMemory
80  "Insufficient space", // kXR_NoSpace
81  "Request not authorized", // kXR_NotAuthorized
82  "File or object not found", // kXR_NotFound
83  "Internal server error", // kXR_ServerError
84  "Unsupported request", // kXR_Unsupported
85  "No servers available", // kXR_noserver
86  "Target is not a file", // kXR_NotFile
87  "Target is a directory", // kXR_isDirectory
88  "Request cancelled", // kXR_Cancelled
89  "Target exists", // kXR_ItExists
90  "Checksum is invalid", // kXR_ChkSumErr
91  "Request in progress", // kXR_inProgress
92  "Quota exceeded", // kXR_overQuota
93  "Invalid signature", // kXR_SigVerErr
94  "Decryption failed", // kXR_DecryptErr
95  "Server is overloaded", // kXR_Overloaded
96  "Filesystem is read only", // kXR_fsReadOnly
97  "Invalid payload format", // kXR_BadPayload
98  "File attribute not found", // kXR_AttrNotFound
99  "Operation requires TLS", // kXR_TLSRequired
100  "No new servers for replica", // kXR_noReplicas
101  "Authentication failed", // kXR_AuthFailed
102  "Request is not possible", // kXR_Impossible
103  "Conflicting request", // kXR_Conflict
104  "Too many errors", // kXR_TooManyErrs
105  "Request timed out", // kXR_ReqTimedOut
106  "Timer expired" // kXR_TimerExipred
107  };
108 
109 const char *reqNames[kXR_REQFENCE-kXR_auth] =
110  {"auth", "query", "chmod", "close",
111  "dirlist", "gpfile", "protocol", "login",
112  "mkdir", "mv", "open", "ping",
113  "chkpoint", "read", "rm", "rmdir",
114  "sync", "stat", "set", "write",
115  "fattr", "prepare", "statx", "endsess",
116  "bind", "readv", "pgwrite", "locate",
117  "truncate", "sigver", "pgread", "writev",
118  "clone"
119  };
120 
121 // Following value is used to determine if the error or request code is
122 // host byte or network byte order making use of the fact each starts at 3000
123 //
124 union Endianness {kXR_unt16 xyz; unsigned char Endian[2];} little = {1};
125 }
126 
127 /******************************************************************************/
128 /* e r r N a m e */
129 /******************************************************************************/
130 
131 const char *XProtocol::errName(kXR_int32 errCode)
132 {
133 // Mangle request code if the byte orderdoesn't match our host order
134 //
135  if ((errCode < 0 || errCode > kXR_ERRFENCE) && little.Endian[0])
136  errCode = ntohl(errCode);
137 
138 // Validate the request code
139 //
140  if (errCode < kXR_ArgInvalid || errCode >= kXR_ERRFENCE
141  || !errNames[errCode - kXR_ArgInvalid])
142  return "!undefined error";
143 
144 // Return the proper table
145 //
146  return errNames[errCode - kXR_ArgInvalid];
147 }
148 
149 /******************************************************************************/
150 /* r e q N a m e */
151 /******************************************************************************/
152 
153 const char *XProtocol::reqName(kXR_unt16 reqCode)
154 {
155 // Mangle request code if the byte orderdoesn't match our host order
156 //
157  if (reqCode > kXR_REQFENCE && little.Endian[0]) reqCode = ntohs(reqCode);
158 
159 // Validate the request code
160 //
161  if (reqCode < kXR_auth || reqCode >= kXR_REQFENCE
162  || !reqNames[reqCode - kXR_auth])
163  return "!unknown";
164 
165 // Return the proper table
166 //
167  return reqNames[reqCode - kXR_auth];
168 }
169 
170 /******************************************************************************/
171 /* n v e c & v v e c o p e r a t i n s */
172 /******************************************************************************/
173 
174 // Add an attribute name to nvec (the buffer has to be sufficiently big)
175 //
176 char* ClientFattrRequest::NVecInsert( const char *name, char *buffer )
177 {
178  // set rc to 0
179  memset( buffer, 0, sizeof( kXR_unt16 ) );
180  buffer += sizeof( kXR_unt16 );
181  // copy attribute name including trailing null
182  size_t len = strlen( name );
183  memcpy( buffer, name, len + 1 );
184  buffer += len + 1;
185 
186  // return memory that comes right after newly inserted nvec record
187  return buffer;
188 }
189 
190 // Add an attribute name to vvec (the buffer has to be sufficiently big)
191 //
192 char* ClientFattrRequest::VVecInsert( const char *value, char *buffer )
193 {
194  // copy value size
195  kXR_int32 len = strlen( value );
196  kXR_int32 lendat = htonl( len );
197  memcpy( buffer, &lendat, sizeof( kXR_int32 ) );
198  buffer += sizeof( kXR_int32 );
199  // copy value itself
200  memcpy( buffer, value, len );
201  buffer += len;
202 
203  // return memory that comes right after newly inserted vvec entry
204  return buffer;
205 }
206 
207 // Read error code from nvec
208 //
209 char* ClientFattrRequest::NVecRead( char* buffer, kXR_unt16 &rc )
210  {
211  memcpy(&rc, buffer, sizeof(kXR_unt16));
212  rc = htons( rc );
213  buffer += sizeof( kXR_unt16 );
214  return buffer;
215  }
216 
217 // Read attribute name from nvec
218 //
219 char* ClientFattrRequest::NVecRead( char* buffer, char *&name )
220 {
221  name = strdup( buffer );
222  buffer += strlen( name ) + 1;
223  return buffer;
224 }
225 
226 // Read value length from vvec
227 //
228 char* ClientFattrRequest::VVecRead( char* buffer, kXR_int32 &len )
229 {
230  memcpy(&len, buffer, sizeof(kXR_int32));
231  len = htonl( len );
232  buffer += sizeof( kXR_int32 );
233  return buffer;
234 }
235 
236 // Read attribute value from vvec
237 //
238 char* ClientFattrRequest::VVecRead( char* buffer, kXR_int32 len, char *&value )
239 {
240  value = reinterpret_cast<char*>( malloc( len + 1 ) );
241  strncpy( value, buffer, len );
242  value[len] = 0;
243  buffer += len;
244  return buffer;
245 }
@ kXR_ArgInvalid
Definition: XProtocol.hh:1032
@ kXR_ERRFENCE
Definition: XProtocol.hh:1068
@ kXR_REQFENCE
Definition: XProtocol.hh:146
@ kXR_auth
Definition: XProtocol.hh:113
int kXR_int32
Definition: XPtypes.hh:89
unsigned short kXR_unt16
Definition: XPtypes.hh:67
static const char * reqName(kXR_unt16 reqCode)
Definition: XProtocol.cc:153
static const char * errName(kXR_int32 errCode)
Definition: XProtocol.cc:131
static char * VVecInsert(const char *value, char *buffer)
Definition: XProtocol.cc:192
static char * NVecRead(char *buffer, kXR_unt16 &rc)
Definition: XProtocol.cc:209
static char * VVecRead(char *buffer, kXR_int32 &len)
Definition: XProtocol.cc:228
static char * NVecInsert(const char *name, char *buffer)
Definition: XProtocol.cc:176