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  };
119 
120 // Following value is used to determine if the error or request code is
121 // host byte or network byte order making use of the fact each starts at 3000
122 //
123 union Endianness {kXR_unt16 xyz; unsigned char Endian[2];} little = {1};
124 }
125 
126 /******************************************************************************/
127 /* e r r N a m e */
128 /******************************************************************************/
129 
130 const char *XProtocol::errName(kXR_int32 errCode)
131 {
132 // Mangle request code if the byte orderdoesn't match our host order
133 //
134  if ((errCode < 0 || errCode > kXR_ERRFENCE) && little.Endian[0])
135  errCode = ntohl(errCode);
136 
137 // Validate the request code
138 //
139  if (errCode < kXR_ArgInvalid || errCode >= kXR_ERRFENCE)
140  return "!undefined error";
141 
142 // Return the proper table
143 //
144  return errNames[errCode - kXR_ArgInvalid];
145 }
146 
147 /******************************************************************************/
148 /* r e q N a m e */
149 /******************************************************************************/
150 
151 const char *XProtocol::reqName(kXR_unt16 reqCode)
152 {
153 // Mangle request code if the byte orderdoesn't match our host order
154 //
155  if (reqCode > kXR_REQFENCE && little.Endian[0]) reqCode = ntohs(reqCode);
156 
157 // Validate the request code
158 //
159  if (reqCode < kXR_auth || reqCode >= kXR_REQFENCE) return "!unknown";
160 
161 // Return the proper table
162 //
163  return reqNames[reqCode - kXR_auth];
164 }
165 
166 /******************************************************************************/
167 /* n v e c & v v e c o p e r a t i n s */
168 /******************************************************************************/
169 
170 // Add an attribute name to nvec (the buffer has to be sufficiently big)
171 //
172 char* ClientFattrRequest::NVecInsert( const char *name, char *buffer )
173 {
174  // set rc to 0
175  memset( buffer, 0, sizeof( kXR_unt16 ) );
176  buffer += sizeof( kXR_unt16 );
177  // copy attribute name including trailing null
178  size_t len = strlen( name );
179  memcpy( buffer, name, len + 1 );
180  buffer += len + 1;
181 
182  // return memory that comes right after newly inserted nvec record
183  return buffer;
184 }
185 
186 // Add an attribute name to vvec (the buffer has to be sufficiently big)
187 //
188 char* ClientFattrRequest::VVecInsert( const char *value, char *buffer )
189 {
190  // copy value size
191  kXR_int32 len = strlen( value );
192  kXR_int32 lendat = htonl( len );
193  memcpy( buffer, &lendat, sizeof( kXR_int32 ) );
194  buffer += sizeof( kXR_int32 );
195  // copy value itself
196  memcpy( buffer, value, len );
197  buffer += len;
198 
199  // return memory that comes right after newly inserted vvec entry
200  return buffer;
201 }
202 
203 // Read error code from nvec
204 //
205 char* ClientFattrRequest::NVecRead( char* buffer, kXR_unt16 &rc )
206  {
207  memcpy(&rc, buffer, sizeof(kXR_unt16));
208  rc = htons( rc );
209  buffer += sizeof( kXR_unt16 );
210  return buffer;
211  }
212 
213 // Read attribute name from nvec
214 //
215 char* ClientFattrRequest::NVecRead( char* buffer, char *&name )
216 {
217  name = strdup( buffer );
218  buffer += strlen( name ) + 1;
219  return buffer;
220 }
221 
222 // Read value length from vvec
223 //
224 char* ClientFattrRequest::VVecRead( char* buffer, kXR_int32 &len )
225 {
226  memcpy(&len, buffer, sizeof(kXR_int32));
227  len = htonl( len );
228  buffer += sizeof( kXR_int32 );
229  return buffer;
230 }
231 
232 // Read attribute value from vvec
233 //
234 char* ClientFattrRequest::VVecRead( char* buffer, kXR_int32 len, char *&value )
235 {
236  value = reinterpret_cast<char*>( malloc( len + 1 ) );
237  strncpy( value, buffer, len );
238  value[len] = 0;
239  buffer += len;
240  return buffer;
241 }
@ kXR_ArgInvalid
Definition: XProtocol.hh:990
@ kXR_ERRFENCE
Definition: XProtocol.hh:1026
@ kXR_REQFENCE
Definition: XProtocol.hh:144
@ kXR_auth
Definition: XProtocol.hh:112
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:151
static const char * errName(kXR_int32 errCode)
Definition: XProtocol.cc:130
static char * VVecInsert(const char *value, char *buffer)
Definition: XProtocol.cc:188
static char * NVecRead(char *buffer, kXR_unt16 &rc)
Definition: XProtocol.cc:205
static char * VVecRead(char *buffer, kXR_int32 &len)
Definition: XProtocol.cc:224
static char * NVecInsert(const char *name, char *buffer)
Definition: XProtocol.cc:172