Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:27:52

0001 #ifndef __XPROTOCOL_H
0002 #define __XPROTOCOL_H
0003 /******************************************************************************/
0004 /*                                                                            */
0005 /*                          X P r o t o c o l . h h                           */
0006 /*                                                                            */
0007 /* (c) 2012 by the Board of Trustees of the Leland Stanford, Jr., University  */
0008 /*                            All Rights Reserved                             */
0009 /*   Produced by Andrew Hanushevsky for Stanford University under contract    */
0010 /*              DE-AC02-76-SFO0515 with the Department of Energy              */
0011 /*                                                                            */
0012 /* This file is part of the XRootD software suite.                            */
0013 /*                                                                            */
0014 /* XRootD is free software: you can redistribute it and/or modify it under    */
0015 /* the terms of the GNU Lesser General Public License as published by the     */
0016 /* Free Software Foundation, either version 3 of the License, or (at your     */
0017 /* option) any later version.                                                 */
0018 /*                                                                            */
0019 /* The XRoot protocol definition, documented in this file, is distributed     */
0020 /* under a modified BSD license and may be freely used to reimplement it.     */
0021 /* Any references to "source" in this license refers to this file or any      */
0022 /* other file that specifically contains the following license.               */
0023 /*                                                                            */
0024 /* Redistribution and use in source and binary forms, with or without         */
0025 /* modification, are permitted provided that the following conditions         */
0026 /* are met:                                                                   */
0027 /*                                                                            */
0028 /* 1. Redistributions of source code must retain the above copyright notice,  */
0029 /*    this list of conditions and the following disclaimer.                   */
0030 /*                                                                            */
0031 /* 2. Redistributions in binary form must reproduce the above copyright       */
0032 /*    notice, this list of conditions and the following disclaimer in the     */
0033 /*    documentation and/or other materials provided with the distribution.    */
0034 /*                                                                            */
0035 /* 3. Neither the name of the copyright holder nor the names of its           */
0036 /*    contributors may be used to endorse or promote products derived from    */
0037 /*    this software without specific prior written permission.                */
0038 /*                                                                            */
0039 /* 4. Derived software may not use the name XRootD or cmsd (regardless of     */
0040 /*    capitilization) in association with the derived work if the protocol    */
0041 /*    documented in this file is changed in any way.                          */
0042 /*                                                                            */
0043 /*    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS     */
0044 /*    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT       */
0045 /*    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR   */
0046 /*    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT    */
0047 /*    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  */
0048 /*    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT        */
0049 /*    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,   */
0050 /*    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY   */
0051 /*    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT     */
0052 /*    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   */
0053 /******************************************************************************/
0054 
0055 #ifdef __CINT__
0056 #define __attribute__(x)
0057 #endif
0058 
0059 #include "XProtocol/XPtypes.hh"
0060 
0061 /******************************************************************************/
0062 /*          P r o t o c o l   V e r s i o n   D e f i n i t i o n s           */
0063 /******************************************************************************/
0064   
0065 // The following is the binary representation of the protocol version here.
0066 // Protocol version is repesented as three base10 digits x.y.z with x having no
0067 // upper limit (i.e. n.9.9 + 1 -> n+1.0.0). The kXR_PROTSIGNVERSION defines the
0068 // protocol version where request signing became available.
0069 //
0070 #define kXR_PROTOCOLVERSION  0x00000511
0071 #define kXR_PROTXATTVERSION  0x00000500
0072 #define kXR_PROTTLSVERSION   0x00000500
0073 #define kXR_PROTPGRWVERSION  0x00000511
0074 #define kXR_PROTSIGNVERSION  0x00000310
0075 #define kXR_PROTOCOLVSTRING "5.1.0"
0076 
0077 /******************************************************************************/
0078 /*               C l i e n t - S e r v e r   H a n d s h a k e                */
0079 /******************************************************************************/
0080 
0081 // The fields to be sent as initial handshake
0082 //
0083 struct ClientInitHandShake {
0084    kXR_int32 first;
0085    kXR_int32 second;
0086    kXR_int32 third;
0087    kXR_int32 fourth;
0088    kXR_int32 fifth;
0089 };
0090 
0091 // The body received after the first handshake's header
0092 //
0093 struct ServerInitHandShake {
0094    kXR_int32 msglen;
0095    kXR_int32 protover;
0096    kXR_int32 msgval;
0097 };
0098   
0099 /******************************************************************************/
0100 /*                       C l i e n t   R e q u e s t s                        */
0101 /******************************************************************************/
0102 
0103 // G.Ganis: All the following structures never need padding bytes:
0104 //          no need of packing options like __attribute__((packed))
0105 //
0106 // All binary data is sent in network byte order.
0107 
0108 // Client request codes
0109 // 
0110 enum XRequestTypes {
0111    kXR_1stRequest= 3000,
0112    kXR_auth    =   3000,
0113    kXR_query,   // 3001
0114    kXR_chmod,   // 3002
0115    kXR_close,   // 3003
0116    kXR_dirlist, // 3004
0117    kXR_gpfile,  // 3005 was kXR_getfile
0118    kXR_protocol,// 3006
0119    kXR_login,   // 3007
0120    kXR_mkdir,   // 3008
0121    kXR_mv,      // 3009
0122    kXR_open,    // 3010
0123    kXR_ping,    // 3011
0124    kXR_chkpoint,// 3012 was kXR_putfile
0125    kXR_read,    // 3013
0126    kXR_rm,      // 3014
0127    kXR_rmdir,   // 3015
0128    kXR_sync,    // 3016
0129    kXR_stat,    // 3017
0130    kXR_set,     // 3018
0131    kXR_write,   // 3019
0132    kXR_fattr,   // 3020 was kXR_admin
0133    kXR_prepare, // 3021
0134    kXR_statx,   // 3022
0135    kXR_endsess, // 3023
0136    kXR_bind,    // 3024
0137    kXR_readv,   // 3025
0138    kXR_pgwrite, // 3026 was kXR_verifyw
0139    kXR_locate,  // 3027
0140    kXR_truncate,// 3028
0141    kXR_sigver,  // 3029
0142    kXR_pgread,  // 3030 was kXR_decrypt
0143    kXR_writev,  // 3031
0144    kXR_REQFENCE // Always last valid request code +1
0145 };
0146 
0147 // Virtual client request codes
0148 //
0149 enum XVirtRequestTypes {
0150    kXR_virtReadv = 2000
0151 };
0152 
0153 // All client requests use a header with the following format
0154 //
0155 struct ClientRequestHdr {
0156    kXR_char  streamid[2];
0157    kXR_unt16 requestid;
0158    kXR_char  body[16];
0159    kXR_int32 dlen;
0160 };
0161 
0162 /******************************************************************************/
0163 /*                      k X R _ a u t h   R e q u e s t                       */
0164 /******************************************************************************/
0165   
0166 struct ClientAuthRequest {
0167    kXR_char  streamid[2];
0168    kXR_unt16 requestid;
0169    kXR_char  reserved[12];
0170    kXR_char  credtype[4];
0171    kXR_int32 dlen;
0172 };
0173 
0174 /******************************************************************************/
0175 /*                      k X R _ b i n d   R e q u e s t                       */
0176 /******************************************************************************/
0177   
0178 struct ClientBindRequest {
0179    kXR_char  streamid[2];
0180    kXR_unt16 requestid;
0181    kXR_char  sessid[16];
0182    kXR_int32 dlen;
0183 };
0184 
0185 /******************************************************************************/
0186 /*                     k X R _ c h m o d   R e q u e s t                      */
0187 /******************************************************************************/
0188   
0189 struct ClientChmodRequest {
0190    kXR_char  streamid[2];
0191    kXR_unt16 requestid;
0192    kXR_char  reserved[14];
0193    kXR_unt16 mode;        // See XOpenRequestMode
0194    kXR_int32 dlen;
0195 };
0196 
0197 /******************************************************************************/
0198 /*                  k X R _ c h k p o i n t   R e q u e s t                   */
0199 /******************************************************************************/
0200   
0201 struct ClientChkPointRequest {
0202    kXR_char  streamid[2];
0203    kXR_unt16 requestid;
0204    kXR_char  fhandle[4];    // For Create, Delete, Query, or Restore
0205    kXR_char  reserved[11];
0206    kXR_char  opcode;        // One of kXR_ckpxxxx actions
0207    kXR_int32 dlen;
0208 };
0209 
0210 // Actions
0211 //
0212 static const int kXR_ckpBegin   = 0;  // Begin    checkpoint
0213 static const int kXR_ckpCommit  = 1;  // Commit   changes
0214 static const int kXR_ckpQuery   = 2;  // Query    checkpoint limits
0215 static const int kXR_ckpRollback= 3;  // Rollback changes
0216 static const int kXR_ckpXeq     = 4;  // Execute trunc, write, or writev
0217 
0218 // The minimum size of a checkpoint data limit
0219 //
0220 static const int kXR_ckpMinMax  = 104857604;  // 10 MB
0221   
0222 /******************************************************************************/
0223 /*                     k X R _ c l o s e   R e q u e s t                      */
0224 /******************************************************************************/
0225   
0226 struct ClientCloseRequest {
0227    kXR_char  streamid[2];
0228    kXR_unt16 requestid;
0229    kXR_char  fhandle[4];
0230    kXR_char  reserved[12];
0231    kXR_int32 dlen;
0232 };
0233 
0234 /******************************************************************************/
0235 /*                   k X R _ d i r l i s t   R e q u e s t                    */
0236 /******************************************************************************/
0237 
0238 enum XDirlistRequestOption {
0239    kXR_online = 1,
0240    kXR_dstat  = 2,
0241    kXR_dcksm  = 4    // dcksm implies dstat irrespective of dstat setting
0242 };
0243   
0244 struct ClientDirlistRequest {
0245    kXR_char  streamid[2];
0246    kXR_unt16 requestid;
0247    kXR_char  reserved[15];
0248    kXR_char  options[1];     // See XDirlistRequestOption enum
0249    kXR_int32 dlen;
0250 };
0251 
0252 /******************************************************************************/
0253 /*                   k X R _ e n d s e s s   R e q u e s t                    */
0254 /******************************************************************************/
0255   
0256 struct ClientEndsessRequest {
0257    kXR_char  streamid[2];
0258    kXR_unt16 requestid;
0259    kXR_char  sessid[16];
0260    kXR_int32 dlen;
0261 };
0262 
0263 /******************************************************************************/
0264 /*                     k X R _ f a t t r   R e q u e s t                      */
0265 /******************************************************************************/
0266 
0267 // kXR_fattr subcodes
0268 //
0269 enum xfaSubCode {
0270    kXR_fattrDel    = 0,
0271    kXR_fattrGet    = 1,
0272    kXR_fattrList   = 2,
0273    kXR_fattrSet    = 3,
0274    kXR_fatrrMaxSC  = 3     // Highest valid subcode
0275 };
0276 
0277 // kXR_fattr limits
0278 //
0279 enum xfaLimits {
0280    kXR_faMaxVars = 16,     // Maximum variables per request
0281    kXR_faMaxNlen = 248,    // Maximum length of variable name
0282    kXR_faMaxVlen = 65536   // Maximum length of variable value
0283 };
0284   
0285 struct ClientFattrRequest {
0286    kXR_char  streamid[2];
0287    kXR_unt16 requestid;
0288    kXR_char  fhandle[4];
0289    kXR_char  subcode;      // See xfaSubCode enum
0290    kXR_char  numattr;
0291    kXR_char  options;      // See valid options below
0292    kXR_char  reserved[9];
0293    kXR_int32 dlen;
0294 
0295 // Valid options:
0296 //
0297    static const int isNew = 0x01; // For set,  the variable must not exist
0298    static const int aData = 0x10; // For list, return attribute value
0299 
0300 // Add an attribute name to nvec (the buffer has to be sufficiently big)
0301 //
0302    static char* NVecInsert( const char *name,  char *buffer );
0303 
0304 // Add an attribute name to vvec (the buffer has to be sufficiently big)
0305 //
0306    static char* VVecInsert( const char *value, char *buffer );
0307 
0308 // Read error code from nvec
0309 //
0310    static char* NVecRead( char* buffer, kXR_unt16 &rc );
0311 
0312 // Read attribute name from nvec, should be deallocated with free()
0313 //
0314    static char* NVecRead( char* buffer, char *&name );
0315 
0316 // Read value length from vvec
0317 //
0318    static char* VVecRead( char* buffer, kXR_int32 &len );
0319 
0320 // Read attribute value from vvec, should be deallocated with free()
0321 //
0322    static char* VVecRead( char* buffer, kXR_int32 len, char *&value );
0323 
0324 };
0325   
0326 /******************************************************************************/
0327 /*                    k X R _ g p f i l e   R e q u e s t                     */
0328 /******************************************************************************/
0329   
0330 struct ClientGPfileRequest { // ??? This is all wrong; correct when implemented
0331    kXR_char  streamid[2];
0332    kXR_unt16 requestid;      // kXR_gpfile
0333    kXR_int32 options;
0334    kXR_char reserved[8];
0335    kXR_int32 buffsz;
0336    kXR_int32  dlen;
0337 };
0338 
0339 /******************************************************************************/
0340 /*                    k X R _ l o c a t e   R e q u e s t                     */
0341 /******************************************************************************/
0342   
0343 struct ClientLocateRequest {
0344    kXR_char  streamid[2];
0345    kXR_unt16 requestid;
0346    kXR_unt16 options;     // See XOpenRequestOption enum tagged for locate
0347    kXR_char  reserved[14];
0348    kXR_int32 dlen;
0349 };
0350 
0351 /******************************************************************************/
0352 /*                     k X R _ l o g i n   R e q u e s t                      */
0353 /******************************************************************************/
0354 
0355 // this is a bitmask
0356 enum XLoginAbility {
0357    kXR_nothing    =   0,
0358    kXR_fullurl    =   1,
0359    kXR_multipr    =   3,
0360    kXR_readrdok   =   4,
0361    kXR_hasipv64   =   8,
0362    kXR_onlyprv4   =  16,
0363    kXR_onlyprv6   =  32,
0364    kXR_lclfile    =  64,
0365    kXR_redirflags = 128
0366 };
0367 
0368 // this iss a bitmask
0369 enum XLoginAbility2 {
0370    kXR_empty   = 0,
0371    kXR_ecredir = 1
0372 };
0373 
0374 // this is a bitmask (note that XLoginVersion resides in lower bits)
0375 enum XLoginCapVer {
0376    kXR_lcvnone = 0,
0377    kXR_vermask = 63,
0378    kXR_asyncap = 128
0379 };
0380 
0381 // this is a single number that is or'd into capver as the version
0382 //
0383 enum XLoginVersion {
0384    kXR_ver000 = 0,  // Old clients predating history
0385    kXR_ver001 = 1,  // Generally implemented 2005 protocol
0386    kXR_ver002 = 2,  // Same as 1 but adds asyncresp recognition
0387    kXR_ver003 = 3,  // The 2011-2012 rewritten client
0388    kXR_ver004 = 4,  // The 2016 sign-capable   client
0389    kXR_ver005 = 5   // The 2019 TLS-capable    client
0390 };
0391   
0392 struct ClientLoginRequest {
0393    kXR_char  streamid[2];
0394    kXR_unt16 requestid;
0395    kXR_int32 pid;
0396    kXR_char username[8];
0397    kXR_char ability2;      // See XLoginAbility2 enum flags
0398    kXR_char ability;       // See XLoginAbility  enum flags
0399    kXR_char capver[1];     // See XLoginCapVer   enum flags
0400    kXR_char reserved2;
0401    kXR_int32  dlen;
0402 };
0403 
0404 /******************************************************************************/
0405 /*                     k X R _ m k d i r   R e q u e s t                      */
0406 /******************************************************************************/
0407 
0408 enum XMkdirOptions {
0409    kXR_mknone  = 0,
0410    kXR_mkdirpath  = 1
0411 };
0412   
0413 struct ClientMkdirRequest {
0414    kXR_char  streamid[2];
0415    kXR_unt16 requestid;
0416    kXR_char  options[1];
0417    kXR_char  reserved[13];
0418    kXR_unt16 mode;          // See XOpenRequestMode
0419    kXR_int32 dlen;
0420 };
0421 
0422 /******************************************************************************/
0423 /*                        k X R _ m v   R e q u e s t                         */
0424 /******************************************************************************/
0425   
0426 struct ClientMvRequest {
0427    kXR_char  streamid[2];
0428    kXR_unt16 requestid;
0429    kXR_char  reserved[14];
0430    kXR_int16 arg1len;
0431    kXR_int32 dlen;
0432 };
0433 
0434 /******************************************************************************/
0435 /*                      k X R _ o p e n   R e q u e s t                       */
0436 /******************************************************************************/
0437 
0438 // OPEN MODE FOR A REMOTE FILE
0439 enum XOpenRequestMode {
0440    kXR_ur = 0x100,
0441    kXR_uw = 0x080,
0442    kXR_ux = 0x040,
0443    kXR_gr = 0x020,
0444    kXR_gw = 0x010,
0445    kXR_gx = 0x008,
0446    kXR_or = 0x004,
0447    kXR_ow = 0x002,
0448    kXR_ox = 0x001
0449 };
0450 
0451 enum XOpenRequestOption {
0452    kXR_compress = 0x0001, //     1   // also locate (return unique hosts)
0453    kXR_delete   = 0x0002, //     2
0454    kXR_force    = 0x0004, //     4
0455    kXR_new      = 0x0008, //     8
0456    kXR_open_read= 0x0010, //    16
0457    kXR_open_updt= 0x0020, //    32
0458    kXR_async    = 0x0040, //    64
0459    kXR_refresh  = 0x0080, //   128   // also locate
0460    kXR_mkpath   = 0x0100, //   256
0461    kXR_prefname = 0x0100, //   256   // only locate
0462    kXR_open_apnd= 0x0200, //   512
0463    kXR_retstat  = 0x0400, //  1024
0464    kXR_4dirlist = 0x0400, //  1024   // for locate intending a dirlist
0465    kXR_replica  = 0x0800, //  2048
0466    kXR_posc     = 0x1000, //  4096
0467    kXR_nowait   = 0x2000, //  8192   // also locate
0468    kXR_seqio    = 0x4000, // 16384
0469    kXR_open_wrto= 0x8000  // 32768
0470 };
0471 
0472 enum XOpenRequestOption2 {
0473    kXR_dup      = 0x0001, //     1
0474    kXR_samefs   = 0x0002  //     2
0475 };
0476   
0477 struct ClientOpenRequest {
0478    kXR_char  streamid[2];
0479    kXR_unt16 requestid;
0480    kXR_unt16 mode;
0481    kXR_unt16 options;
0482    kXR_char  reserved[12];
0483    kXR_int32  dlen;
0484 };
0485 
0486 /******************************************************************************/
0487 /*                    k X R _ p g r e a d   R e q u e s t                     */
0488 /******************************************************************************/
0489   
0490 // The page size for pgread and pgwrite and the maximum transmission size
0491 //
0492 namespace XrdProto  // Always use this namespace for new additions
0493 {
0494 static const int kXR_pgPageSZ = 4096;     // Length of a page
0495 static const int kXR_pgPageBL = 12;       // log2(page length)
0496 static const int kXR_pgUnitSZ = kXR_pgPageSZ + sizeof(kXR_unt32);
0497 static const int kXR_pgMaxEpr = 128;      // Max checksum errs per request
0498 static const int kXR_pgMaxEos = 256;      // Max checksum errs outstanding
0499 
0500 // kXR_pgread/write options
0501 //
0502 static const kXR_char kXR_AnyPath = 0xff; // In pathid
0503 static const int      kXR_pgRetry = 0x01; // In reqflags
0504 }
0505   
0506 struct ClientPgReadRequest {
0507    kXR_char  streamid[2];
0508    kXR_unt16 requestid;
0509    kXR_char  fhandle[4];
0510    kXR_int64 offset;
0511    kXR_int32 rlen;
0512    kXR_int32 dlen;     // Request data length must be 0 unless args present
0513 };
0514 
0515 struct ClientPgReadReqArgs {
0516    kXR_char  pathid;   // Request data length must be 1
0517    kXR_char  reqflags; // Request data length must be 2
0518 };
0519 
0520 namespace
0521 {
0522 }
0523 
0524 /******************************************************************************/
0525 /*                   k X R _ p r w r i t e   R e q u e s t                    */
0526 /******************************************************************************/
0527   
0528 struct ClientPgWriteRequest {
0529    kXR_char  streamid[2];
0530    kXR_unt16 requestid;
0531    kXR_char  fhandle[4];
0532    kXR_int64 offset;
0533    kXR_char  pathid;
0534    kXR_char  reqflags;
0535    kXR_char  reserved[2];
0536    kXR_int32 dlen;
0537 // kXR_char  data[dlen];
0538 };
0539 
0540 /******************************************************************************/
0541 /*                      k X R _ p i n g   R e q u e s t                       */
0542 /******************************************************************************/
0543   
0544 struct ClientPingRequest {
0545    kXR_char  streamid[2];
0546    kXR_unt16 requestid;
0547    kXR_char  reserved[16];
0548    kXR_int32 dlen;
0549 };
0550 
0551 /******************************************************************************/
0552 /*                  k X R _ p r o t o c o l   R e q u e s t                   */
0553 /******************************************************************************/
0554   
0555 struct ClientProtocolRequest {
0556    kXR_char  streamid[2];
0557    kXR_unt16 requestid;
0558    kXR_int32 clientpv;      // 2.9.7 or higher
0559    kXR_char  flags;         // 3.1.0 or higher
0560    kXR_char  expect;        // 4.0.0 or higher
0561    kXR_char  reserved[10];
0562    kXR_int32 dlen;
0563 
0564 enum RequestFlags {
0565    kXR_secreqs  = 0x01,  // Options: Return security requirements
0566    kXR_ableTLS  = 0x02,  // Options: Client is TLS capable
0567    kXR_wantTLS  = 0x04,  // Options: Change connection to use TLS
0568    kXR_bifreqs  = 0x08   // Options: Return bind interface requirements
0569 };
0570 
0571 enum ExpectFlags {
0572    kXR_ExpMask   = 0x0f, // Isolate the relevant expect enumeration value
0573    kXR_ExpNone   = 0x00,
0574    kXR_ExpBind   = 0x01,
0575    kXR_ExpGPF    = 0x02,
0576    kXR_ExpLogin  = 0x03,
0577    kXR_ExpTPC    = 0x04,
0578    kXR_ExpGPFA   = 0x08
0579 };
0580 };
0581 
0582 /******************************************************************************/
0583 /*                   k X R _ p r e p a r e   R e q u e s t                    */
0584 /******************************************************************************/
0585 
0586 enum XPrepRequestOption {
0587    kXR_cancel = 1,
0588    kXR_notify = 2,
0589    kXR_noerrs = 4,
0590    kXR_stage  = 8,
0591    kXR_wmode  = 16,
0592    kXR_coloc  = 32,
0593    kXR_fresh  = 64,
0594    kXR_usetcp = 128,
0595 
0596    kXR_evict  = 0x0001 // optionsX: file no longer useful
0597 };
0598   
0599 struct ClientPrepareRequest {
0600    kXR_char  streamid[2];
0601    kXR_unt16 requestid;
0602    kXR_char  options;
0603    kXR_char  prty;
0604    kXR_unt16 port;          // 2.9.9 or higher
0605    kXR_unt16 optionX;       // Extended options
0606    kXR_char  reserved[10];
0607    kXR_int32 dlen;
0608 };
0609 
0610 /******************************************************************************/
0611 /*                     k X R _ q u e r y   R e q u e s t                      */
0612 /******************************************************************************/
0613 
0614 enum XQueryType {
0615    kXR_QStats = 1,
0616    kXR_QPrep  = 2,
0617    kXR_Qcksum = 3,
0618    kXR_Qxattr = 4,
0619    kXR_Qspace = 5,
0620    kXR_Qckscan= 6,
0621    kXR_Qconfig= 7,
0622    kXR_Qvisa  = 8,
0623    kXR_Qopaque=16,
0624    kXR_Qopaquf=32,
0625    kXR_Qopaqug=64
0626 };
0627   
0628 struct ClientQueryRequest {
0629    kXR_char  streamid[2];
0630    kXR_unt16 requestid;
0631    kXR_unt16 infotype;    // See XQueryType enum
0632    kXR_char  reserved1[2];
0633    kXR_char  fhandle[4];
0634    kXR_char  reserved2[8];
0635    kXR_int32 dlen;
0636 };
0637 
0638 /******************************************************************************/
0639 /*                      k X R _ r e a d   R e q u e s t                       */
0640 /******************************************************************************/
0641   
0642 struct ClientReadRequest {
0643    kXR_char  streamid[2];
0644    kXR_unt16 requestid;
0645    kXR_char  fhandle[4];
0646    kXR_int64 offset;
0647    kXR_int32 rlen;
0648    kXR_int32 dlen;
0649 // Optionally followed by read_args
0650 };
0651 
0652 struct read_args {
0653    kXR_char  pathid;
0654    kXR_char  reserved[7];
0655 // This struct may be followed by an array of readahead_list
0656 };
0657 
0658 struct readahead_list {
0659    kXR_char  fhandle[4];
0660    kXR_int32 rlen;
0661    kXR_int64 offset;
0662 };
0663 
0664 /******************************************************************************/
0665 /*                     k X R _ r e a d v   R e q u e s t                      */
0666 /******************************************************************************/
0667   
0668 struct ClientReadVRequest {
0669    kXR_char  streamid[2];
0670    kXR_unt16 requestid;
0671    kXR_char  reserved[15];
0672    kXR_char  pathid;
0673    kXR_int32 dlen;
0674 // This struct followed by the read_list
0675 };
0676 
0677 namespace XrdProto  // Always use this namespace for new additions
0678 {
0679 struct read_list {
0680    kXR_char  fhandle[4];
0681    kXR_int32 rlen;
0682    kXR_int64 offset;
0683 };
0684 static const int rlItemLen = sizeof(read_list);
0685 static const int maxRvecln = 16384;
0686 static const int maxRvecsz = maxRvecln/rlItemLen;
0687 }
0688 
0689 /******************************************************************************/
0690 /*                        k X R _ r m   R e q u e s t                         */
0691 /******************************************************************************/
0692   
0693 struct ClientRmRequest {
0694    kXR_char  streamid[2];
0695    kXR_unt16 requestid;
0696    kXR_char  reserved[16];
0697    kXR_int32 dlen;
0698 };
0699 
0700 /******************************************************************************/
0701 /*                     k X R _ r m d i r   R e q u e s t                      */
0702 /******************************************************************************/
0703   
0704 struct ClientRmdirRequest {
0705    kXR_char  streamid[2];
0706    kXR_unt16 requestid;
0707    kXR_char  reserved[16];
0708    kXR_int32 dlen;
0709 };
0710 
0711 /******************************************************************************/
0712 /*                       k X R _ s e t   R e q u e s t                        */
0713 /******************************************************************************/
0714   
0715 struct ClientSetRequest {
0716    kXR_char  streamid[2];
0717    kXR_unt16 requestid;
0718    kXR_char reserved[15];
0719    kXR_char  modifier;  // For security purposes, should be zero
0720    kXR_int32  dlen;
0721 };
0722 
0723 /******************************************************************************/
0724 /*                    k X R _ s i g v e r   R e q u e s t                     */
0725 /******************************************************************************/
0726 
0727 // Cryptography used for kXR_sigver SigverRequest::crypto
0728 enum XSecCrypto {
0729    kXR_SHA256   = 0x01,   // Hash used
0730    kXR_HashMask = 0x0f,   // Mak to extract the hash type
0731    kXR_rsaKey   = 0x80    // The rsa key was used
0732 };
0733 
0734 // Flags for kXR_sigver
0735 enum XSecFlags {
0736    kXR_nodata   = 1  // Request payload was not hashed
0737 };
0738 
0739 // Version number
0740 enum XSecVersion {
0741    kXR_Ver_00 = 0
0742 };
0743 
0744 struct ClientSigverRequest {
0745    kXR_char  streamid[2];
0746    kXR_unt16 requestid;
0747    kXR_unt16 expectrid; // Request code of subsequent request
0748    kXR_char  version;   // Security version being used (see XSecVersion)
0749    kXR_char  flags;     // One or more flags defined in enum (see XSecFlags)
0750    kXR_unt64 seqno;     // Monotonically increasing number (part of hash)
0751    kXR_char  crypto;    // Cryptography used (see XSecCrypto)
0752    kXR_char  rsvd2[3];
0753    kXR_int32 dlen;
0754 };
0755 
0756 /******************************************************************************/
0757 /*                      k X R _ s t a t   R e q u e s t                       */
0758 /******************************************************************************/
0759 
0760 enum XStatRequestOption {
0761    kXR_vfs    = 1
0762 };
0763   
0764 struct ClientStatRequest {
0765    kXR_char  streamid[2];
0766    kXR_unt16 requestid;
0767    kXR_char  options;    // See XStatRequestOption
0768    kXR_char  reserved[11];
0769    kXR_char  fhandle[4];
0770    kXR_int32 dlen;
0771 };
0772 
0773 /******************************************************************************/
0774 /*                      k X R _ s y n c   R e q u e s t                       */
0775 /******************************************************************************/
0776   
0777 struct ClientSyncRequest {
0778    kXR_char  streamid[2];
0779    kXR_unt16 requestid;
0780    kXR_char  fhandle[4];
0781    kXR_char  reserved[12];
0782    kXR_int32 dlen;
0783 };
0784 
0785 /******************************************************************************/
0786 /*                  k X R _ t r u n c a t e   R e q u e s t                   */
0787 /******************************************************************************/
0788   
0789 struct ClientTruncateRequest {
0790    kXR_char  streamid[2];
0791    kXR_unt16 requestid;
0792    kXR_char  fhandle[4];
0793    kXR_int64 offset;
0794    kXR_char  reserved[4];
0795    kXR_int32 dlen;
0796 };
0797 
0798 /******************************************************************************/
0799 /*                     k X R _ w r i t e   R e q u e s t                      */
0800 /******************************************************************************/
0801   
0802 struct ClientWriteRequest {
0803    kXR_char  streamid[2];
0804    kXR_unt16 requestid;
0805    kXR_char  fhandle[4];
0806    kXR_int64 offset;
0807    kXR_char  pathid;
0808    kXR_char  reserved[3];
0809    kXR_int32 dlen;
0810 };
0811 
0812 /******************************************************************************/
0813 /*                    k X R _ w r i t e v   R e q u e s t                     */
0814 /******************************************************************************/
0815   
0816 struct ClientWriteVRequest {
0817    kXR_char  streamid[2];
0818    kXR_unt16 requestid;
0819    kXR_char  options;  // See static const ints below
0820    kXR_char  reserved[15];
0821    kXR_int32 dlen;
0822 // This struct followed by the write_list
0823 
0824    static const kXR_int32 doSync = 0x01;
0825 };
0826 
0827 namespace XrdProto  // Always use this namespace for new additions
0828 {
0829 struct write_list {
0830    kXR_char fhandle[4];
0831    kXR_int32 wlen;
0832    kXR_int64 offset;
0833 };
0834 static const int wlItemLen = sizeof(write_list);
0835 static const int maxWvecln = 16384;
0836 static const int maxWvecsz = maxWvecln/wlItemLen;
0837 }
0838 
0839 /******************************************************************************/
0840 /*          U n i o n   o f   a l l   C l i e n t   R e q u e s t s           */
0841 /******************************************************************************/
0842   
0843 typedef union {
0844    struct ClientRequestHdr header;
0845    struct ClientAuthRequest auth;
0846    struct ClientBindRequest bind;
0847    struct ClientChkPointRequest chkpoint;
0848    struct ClientChmodRequest chmod;
0849    struct ClientCloseRequest close;
0850    struct ClientDirlistRequest dirlist;
0851    struct ClientEndsessRequest endsess;
0852    struct ClientFattrRequest fattr;
0853    struct ClientGPfileRequest gpfile;
0854    struct ClientLocateRequest locate;
0855    struct ClientLoginRequest login;
0856    struct ClientMkdirRequest mkdir;
0857    struct ClientMvRequest mv;
0858    struct ClientOpenRequest open;
0859    struct ClientPgReadRequest pgread;
0860    struct ClientPgWriteRequest pgwrite;
0861    struct ClientPingRequest ping;
0862    struct ClientPrepareRequest prepare;
0863    struct ClientProtocolRequest protocol;
0864    struct ClientQueryRequest query;
0865    struct ClientReadRequest read;
0866    struct ClientReadVRequest readv;
0867    struct ClientRmRequest rm;
0868    struct ClientRmdirRequest rmdir;
0869    struct ClientSetRequest set;
0870    struct ClientSigverRequest sigver;
0871    struct ClientStatRequest stat;
0872    struct ClientSyncRequest sync;
0873    struct ClientTruncateRequest truncate;
0874    struct ClientWriteRequest write;
0875    struct ClientWriteVRequest writev;
0876 } ClientRequest;
0877 
0878 typedef union {
0879    struct ClientRequestHdr header;
0880    struct ClientSigverRequest sigver;
0881 } SecurityRequest;
0882 
0883 /******************************************************************************/
0884 /*                      S e r v e r   R e s p o n s e s                       */
0885 /******************************************************************************/
0886 
0887 // Nice header for the server response.
0888 // Note that the protocol specifies these values to be in network
0889 // byte order when sent
0890 //
0891 // G.Ganis: The following structures never need padding bytes:
0892 //          no need of packing options
0893 
0894 // Server response codes
0895 //
0896 enum XResponseType {
0897    kXR_ok      =   0,
0898    kXR_oksofar =   4000,
0899    kXR_attn,    // 4001
0900    kXR_authmore,// 4002
0901    kXR_error,   // 4003
0902    kXR_redirect,// 4004
0903    kXR_wait,    // 4005
0904    kXR_waitresp,// 4006
0905    kXR_status,  // 4007
0906    kXR_noResponsesYet = 10000
0907 };
0908 
0909 // All serer responses start with the same header
0910 //
0911 struct ServerResponseHeader {
0912    kXR_char  streamid[2];
0913    kXR_unt16 status;
0914    kXR_int32 dlen;
0915 };
0916 
0917 // This is a bit of wierdness held over from the very old days, sigh.
0918 //
0919 struct ServerResponseBody_Buffer {
0920    char data[4096];
0921 };
0922 
0923 /******************************************************************************/
0924 /*                     k X R _ a t t n   R e s p o n s e                      */
0925 /******************************************************************************/
0926 
0927 enum XActionCode {
0928    kXR_asyncab =     5000, // No longer supported
0929    kXR_asyncdi,   // 5001     No longer supported
0930    kXR_asyncms =     5002,
0931    kXR_asyncrd,   // 5003     No longer supported
0932    kXR_asyncwt,   // 5004     No longer supported
0933    kXR_asyncav,   // 5005     No longer supported
0934    kXR_asynunav,  // 5006     No longer supported
0935    kXR_asyncgo,   // 5007     No longer supported
0936    kXR_asynresp=     5008
0937 };
0938   
0939 struct ServerResponseBody_Attn {
0940    kXR_int32 actnum;      // See XActionCode enum
0941    char      parms[4096]; // Should be sufficient for every use
0942 };
0943 
0944 struct ServerResponseBody_Attn_asyncms  {
0945    kXR_int32            actnum;       // XActionCode::kXR_asyncms
0946    char                 reserved[4];
0947    ServerResponseHeader resphdr;
0948    char                 respdata[4096];
0949 };
0950 
0951 struct ServerResponseBody_Attn_asynresp {
0952    kXR_int32            actnum;       // XActionCode::kXR_asynresp
0953    char                 reserved[4];
0954    ServerResponseHeader resphdr;
0955    char                 respdata[4096];
0956 };
0957 
0958 /******************************************************************************/
0959 /*                 k X R _ a u t h m o r e   R e s p o n s e                  */
0960 /******************************************************************************/
0961   
0962 struct ServerResponseBody_Authmore {
0963    char data[4096];
0964 };
0965 
0966 /******************************************************************************/
0967 /*                     k X R _ b i n d   R e s p o n s e                      */
0968 /******************************************************************************/
0969   
0970 struct ServerResponseBody_Bind {
0971     kXR_char substreamid;
0972 };
0973 
0974 /******************************************************************************/
0975 /*                 k X R _ c h k p o i n t   R e s p o n s e                  */
0976 /******************************************************************************/
0977   
0978 struct ServerResponseBody_ChkPoint { // Only for kXR_ckpQMax
0979     kXR_unt32 maxCkpSize;  // Maximum number of bytes including overhead
0980     kXR_unt32 useCkpSize;  // The number of bytes already being used
0981 };
0982   
0983 /******************************************************************************/
0984 /*                    k X R _ e r r o r   R e s p o n s e                     */
0985 /******************************************************************************/
0986 
0987 enum XErrorCode {
0988    kXR_ArgInvalid =        3000,
0989    kXR_ArgMissing,      // 3001
0990    kXR_ArgTooLong,      // 3002
0991    kXR_FileLocked,      // 3003
0992    kXR_FileNotOpen,     // 3004
0993    kXR_FSError,         // 3005
0994    kXR_InvalidRequest,  // 3006
0995    kXR_IOError,         // 3007
0996    kXR_NoMemory,        // 3008
0997    kXR_NoSpace,         // 3009
0998    kXR_NotAuthorized,   // 3010
0999    kXR_NotFound,        // 3011
1000    kXR_ServerError,     // 3012
1001    kXR_Unsupported,     // 3013
1002    kXR_noserver,        // 3014
1003    kXR_NotFile,         // 3015
1004    kXR_isDirectory,     // 3016
1005    kXR_Cancelled,       // 3017
1006    kXR_ItExists,        // 3018
1007    kXR_ChkSumErr,       // 3019
1008    kXR_inProgress,      // 3020
1009    kXR_overQuota,       // 3021
1010    kXR_SigVerErr,       // 3022
1011    kXR_DecryptErr,      // 3023
1012    kXR_Overloaded,      // 3024
1013    kXR_fsReadOnly,      // 3025
1014    kXR_BadPayload,      // 3026
1015    kXR_AttrNotFound,    // 3027
1016    kXR_TLSRequired,     // 3028
1017    kXR_noReplicas,      // 3029
1018    kXR_AuthFailed,      // 3030
1019    kXR_Impossible,      // 3031
1020    kXR_Conflict,        // 3032
1021    kXR_TooManyErrs,     // 3033
1022    kXR_ReqTimedOut,     // 3034
1023    kXR_TimerExpired,    // 3035
1024    kXR_ERRFENCE,        // Always last valid errcode + 1
1025    kXR_noErrorYet = 10000
1026 };
1027   
1028 struct ServerResponseBody_Error {
1029    kXR_int32 errnum;       // See XErrorCode enu
1030    char      errmsg[4096]; // Should be sufficient for every use
1031 };
1032 
1033 /******************************************************************************/
1034 /*                    k X R _ l o g i n   R e s p o n s e                     */
1035 /******************************************************************************/
1036   
1037 struct ServerResponseBody_Login {
1038    kXR_char  sessid[16];
1039    kXR_char  sec[4096]; // Should be sufficient for every use
1040 };
1041 
1042 /******************************************************************************/
1043 /*                     k X R _ o p e n   R e s p o n s e                      */
1044 /******************************************************************************/
1045 
1046 struct ServerResponseBody_Open {
1047    kXR_char  fhandle[4];
1048    kXR_int32 cpsize;    // cpsize & cptype returned if kXR_compress *or*
1049    kXR_char  cptype[4]; // kXR_retstat is specified
1050 }; // info will follow if kXR_retstat is specified
1051   
1052 /******************************************************************************/
1053 /*                   k X R _ p g r e a d   R e s p o n s e                    */
1054 /******************************************************************************/
1055 
1056 struct ServerResponseBody_pgRead {
1057    kXR_int64 offset;    // info[]: File offset of data that follows
1058 // kXR_char  data[dlen];
1059 };
1060 
1061 /******************************************************************************/
1062 /*                  k X R _ p g w r i t e   R e s p o n s e                   */
1063 /******************************************************************************/
1064   
1065 struct ServerResponseBody_pgWrite {
1066    kXR_int64 offset;                // info[]: File offset of data written
1067 };
1068 
1069 
1070 // The following structure is appended to ServerResponseBody_pgWrite if one or
1071 // more checksum errors occurred and need to be retransmitted.
1072 //
1073 struct ServerResponseBody_pgWrCSE {
1074    kXR_unt32 cseCRC;                // crc32c of all following bits
1075    kXR_int16 dlFirst;               // Data length at first offset in list
1076    kXR_int16 dlLast;                // Data length at last  offset in list
1077 // kXR_int64 bof[(dlen-8)/8];       // List of offsets of pages in error
1078 };
1079 
1080 /******************************************************************************/
1081 /*                 k X R _ p r o t o c o l   R e s p o n s e                  */
1082 /******************************************************************************/
1083 
1084 // The following information is returned in the response body when kXR_bifreqs
1085 // is set in ClientProtocolRequest::flags. Note that the size of bifInfo is
1086 // is variable. This response will not be returned if there are no bif's.
1087 // Note: This structure is null byte padded to be a multiple of 8 bytes!
1088 //
1089 struct ServerResponseBifs_Protocol {
1090    kXR_char  theTag;      // Always the character 'B' to identify struct
1091    kXR_char  rsvd;        // Reserved for the future (always 0 for now)
1092    kXR_unt16 bifILen;     // Length of bifInfo including null bytes.
1093 // kXR_char  bifInfo[bifILen];
1094 };
1095   
1096 // The following information is returned in the response body when kXR_secreqs
1097 // is set in ClientProtocolRequest::flags. Note that the size of secvec is
1098 // defined by secvsz and will not be present when secvsz == 0.
1099 //
1100 struct ServerResponseSVec_Protocol {
1101    kXR_char  reqindx;     // Request index
1102    kXR_char  reqsreq;     // Request signing requirement
1103 };
1104 
1105 struct ServerResponseReqs_Protocol {
1106    kXR_char  theTag;      // Always the character 'S' to identify struct
1107    kXR_char  rsvd;        // Reserved for the future (always 0 for now)
1108    kXR_char  secver;      // Security version
1109    kXR_char  secopt;      // Security options
1110    kXR_char  seclvl;      // Security level when secvsz == 0
1111    kXR_char  secvsz;      // Number of items in secvec (i.e. its length/2)
1112    ServerResponseSVec_Protocol secvec;
1113 };
1114 
1115 
1116 namespace XrdProto
1117 {
1118 typedef struct ServerResponseBifs_Protocol bifReqs;
1119 typedef struct ServerResponseReqs_Protocol secReqs;
1120 }
1121 
1122 // Options reflected in protocol response ServerResponseReqs_Protocol::secopt
1123 //
1124 #define kXR_secOData 0x01
1125 #define kXR_secOFrce 0x02
1126 
1127 // Security level definitions (these are predefined but can be over-ridden)
1128 //
1129 #define kXR_secNone       0
1130 #define kXR_secCompatible 1
1131 #define kXR_secStandard   2
1132 #define kXR_secIntense    3
1133 #define kXR_secPedantic   4
1134 
1135 // Requirements one of which set in each ServerResponseReqs_Protocol::secvec
1136 //
1137 #define kXR_signIgnore    0
1138 #define kXR_signLikely    1
1139 #define kXR_signNeeded    2
1140 
1141 // Version used for kXR_sigver and is set in SigverRequest::version,
1142 // ServerResponseReqs_Protocol::secver
1143 //
1144 #define kXR_secver_0  0
1145   
1146 // KINDS of SERVERS (no longer used by new clients)
1147 //
1148 #define kXR_DataServer 1
1149 #define kXR_LBalServer 0
1150 
1151 // The below are defined for protocol version 2.9.7 or higher
1152 // These are the flag values in the kXR_protool response
1153 //
1154 #define kXR_isManager     0x00000002
1155 #define kXR_isServer      0x00000001
1156 #define kXR_attrMeta      0x00000100
1157 #define kXR_attrProxy     0x00000200
1158 #define kXR_attrSuper     0x00000400
1159 #define kXR_attrVirtRdr   0x00000800
1160 
1161 // Virtual options set on redirect
1162 //
1163 #define kXR_recoverWrts   0x00001000
1164 #define kXR_collapseRedir 0x00002000
1165 #define kXR_ecRedir       0x00004000
1166 
1167 // Things the server supports
1168 //
1169 #define kXR_anongpf       0x00800000
1170 #define kXR_supgpf        0x00400000
1171 #define kXR_suppgrw       0x00200000
1172 #define kXR_supposc       0x00100000
1173 
1174 // TLS requirements
1175 //
1176 #define kXR_haveTLS       0x80000000
1177 #define kXR_gotoTLS       0x40000000
1178 #define kXR_tlsAny        0x1f000000
1179 #define kXR_tlsData       0x01000000
1180 #define kXR_tlsGPF        0x02000000
1181 #define kXR_tlsLogin      0x04000000
1182 #define kXR_tlsSess       0x08000000
1183 #define kXR_tlsTPC        0x10000000
1184 #define kXR_tlsGPFA       0x20000000
1185 
1186 // Body for the kXR_protocol response... useful
1187 //
1188 struct ServerResponseBody_Protocol {
1189    kXR_int32 pval;
1190    kXR_int32 flags;
1191    ServerResponseReqs_Protocol secreq; // Only for V3.1.0+ && if requested
1192 };
1193 
1194 // Handy definition of the size of the protocol response when the security
1195 // information is not present.
1196 //
1197 #define kXR_ShortProtRespLen sizeof(ServerResponseBody_Protocol)-\
1198                              sizeof(ServerResponseReqs_Protocol)
1199 
1200 /******************************************************************************/
1201 /*                 k X R _ r e d i r e c t   R e s p o n s e                  */
1202 /******************************************************************************/
1203   
1204 struct ServerResponseBody_Redirect {
1205    kXR_int32 port;
1206    char host[4096]; // Should be sufficient for every use
1207 };
1208 
1209 /******************************************************************************/
1210 /*                     k X R _ s t a t   R e s p o n s e                      */
1211 /******************************************************************************/
1212 
1213 // The following bits are encoded in the "flags" token in the response
1214 //
1215 enum XStatRespFlags {
1216    kXR_file    = 0,
1217    kXR_xset    = 1,
1218    kXR_isDir   = 2,
1219    kXR_other   = 4,
1220    kXR_offline = 8,
1221    kXR_readable=16,
1222    kXR_writable=32,
1223    kXR_poscpend=64,
1224    kXR_bkpexist=128
1225 };
1226   
1227 /******************************************************************************/
1228 /*                   k X R _ s t a t u s   R e s p o n s e                    */
1229 /******************************************************************************/
1230 
1231 struct ServerResponseBody_Status { // Always preceeded by ServerResponseHeader
1232    kXR_unt32 crc32c;      // IETF RFC 7143 standard
1233    kXR_char  streamID[2]; // Identical to streamid[2]  in ServerResponseHeader
1234    kXR_char  requestid;   // requestcode - kXR_1stRequest
1235    kXR_char  resptype;    // See RespType enum below
1236    kXR_char  reserved[4];
1237    kXR_int32 dlen;
1238 // kXR_char  info[ServerResponseHeader::dlen-sizeof(ServerResponseBody_Status)];
1239 // kXR_char  data[dlen];
1240 };
1241 
1242 namespace XrdProto
1243 {
1244 enum RespType {
1245 
1246    kXR_FinalResult   = 0x00,
1247    kXR_PartialResult = 0x01,
1248    kXR_ProgressInfo  = 0x02
1249 };
1250 
1251    // This is the minimum size of ServerResponseHeader::dlen for kXR_status
1252    //
1253    static const int kXR_statusBodyLen = sizeof(ServerResponseBody_Status);
1254 }
1255 
1256 struct ServerResponseStatus {
1257    struct ServerResponseHeader      hdr;
1258    struct ServerResponseBody_Status bdy;
1259 };
1260   
1261 /******************************************************************************/
1262 /*                     k X R _ w a i t   R e s p o n s e                      */
1263 /******************************************************************************/
1264   
1265 struct ServerResponseBody_Wait {
1266    kXR_int32 seconds;
1267    char infomsg[4096]; // Should be sufficient for every use
1268 };
1269 
1270 /******************************************************************************/
1271 /*                 k X R _ w a i t r e s p   R e s p o n s e                  */
1272 /******************************************************************************/
1273   
1274 struct ServerResponseBody_Waitresp {
1275    kXR_int32 seconds;
1276 };
1277 
1278 /******************************************************************************/
1279 /*         U n i o n   o f   a l l   S e r v e r   R e s p o n s e s          */
1280 /******************************************************************************/
1281   
1282 struct ServerResponse
1283 {
1284   ServerResponseHeader hdr;
1285   union
1286   {
1287     ServerResponseBody_Attn     attn;
1288     ServerResponseBody_Authmore authmore;
1289     ServerResponseBody_Bind     bind;
1290     ServerResponseBody_Buffer   buffer;
1291     ServerResponseBody_Error    error;
1292     ServerResponseBody_Login    login;
1293     ServerResponseBody_Protocol protocol;
1294     ServerResponseBody_Redirect redirect;
1295     ServerResponseBody_Status   status;
1296     ServerResponseBody_Wait     wait;
1297     ServerResponseBody_Waitresp waitresp;
1298   } body;
1299 };
1300 
1301 // The pgread and pgwrite do not fit the union above because they are composed
1302 // of three structs not two as all the above. So, we define the exceptions here.
1303 //
1304 struct ServerResponseV2
1305 {
1306   ServerResponseStatus status; // status.bdy and status.hdr
1307   union
1308   {
1309     ServerResponseBody_pgRead   pgread;
1310     ServerResponseBody_pgWrite  pgwrite;
1311   } info;
1312 };
1313 
1314 struct ALIGN_CHECK {char chkszreq[25-sizeof(ClientRequest)];
1315    char chkszrsp[ 9-sizeof(ServerResponseHeader)];
1316 };
1317 
1318 /******************************************************************************/
1319 /*                   X P r o t o c o l   U t i l i t i e s                    */
1320 /******************************************************************************/
1321 
1322 #include <cerrno>
1323 #if defined(WIN32)
1324 #if !defined(ENOTBLK)
1325 #  define ENOTBLK 15
1326 #endif
1327 #if !defined(ETXTBSY)
1328 #define ETXTBSY 26
1329 #endif
1330 #if !defined(ENOBUFS)
1331 #define ENOBUFS 105
1332 #endif
1333 #if !defined(ENETUNREACH)
1334 #define ENETUNREACH 114
1335 #endif
1336 #endif
1337 
1338 #ifndef ENOATTR
1339 #define ENOATTR ENODATA
1340 #endif
1341 
1342 #ifndef EBADRQC
1343 #define EBADRQC EBADRPC
1344 #endif
1345 
1346 #ifndef EAUTH
1347 #define EAUTH EBADE
1348 #endif
1349 
1350 struct stat;
1351   
1352 class XProtocol
1353 {
1354 public:
1355 
1356 // mapError() is the official mapping from errno to xroot protocol error.
1357 //
1358 static int mapError(int rc)
1359       {if (rc < 0) rc = -rc;
1360        switch(rc)
1361           {case ENOENT:        return kXR_NotFound;
1362            case EINVAL:        return kXR_ArgInvalid;
1363            case EPERM:         return kXR_NotAuthorized;
1364            case EACCES:        return kXR_NotAuthorized;
1365            case EIO:           return kXR_IOError;
1366            case ENOMEM:        return kXR_NoMemory;
1367            case ENOBUFS:       return kXR_NoMemory;
1368            case ENOSPC:        return kXR_NoSpace;
1369            case ENAMETOOLONG:  return kXR_ArgTooLong;
1370            case ENETUNREACH:   return kXR_noserver;
1371            case ENOTBLK:       return kXR_NotFile;
1372            case ENOTSUP:       return kXR_Unsupported;
1373            case EISDIR:        return kXR_isDirectory;
1374            case ENOTEMPTY: [[fallthrough]];
1375            // In the case one tries to delete a non-empty directory
1376            // we have decided that until the next major release
1377            // the kXR_ItExists flag will be returned
1378            case EEXIST:
1379                 return kXR_ItExists;
1380            case EBADRQC:       return kXR_InvalidRequest;
1381            case ETXTBSY:       return kXR_inProgress;
1382            case ENODEV:        return kXR_FSError;
1383            case EFAULT:        return kXR_ServerError;
1384            case EDOM:          return kXR_ChkSumErr;
1385            case EDQUOT:        return kXR_overQuota;
1386            case EILSEQ:        return kXR_SigVerErr;
1387            case ERANGE:        return kXR_DecryptErr;
1388            case EUSERS:        return kXR_Overloaded;
1389            case EROFS:         return kXR_fsReadOnly;
1390            case ENOATTR:       return kXR_AttrNotFound;
1391            case EPROTOTYPE:    return kXR_TLSRequired;
1392            case EADDRNOTAVAIL: return kXR_noReplicas;
1393            case EAUTH:         return kXR_AuthFailed;
1394            case EIDRM:         return kXR_Impossible;
1395            case ENOTTY:        return kXR_Conflict;
1396            case ETOOMANYREFS:  return kXR_TooManyErrs;
1397            case ETIMEDOUT:     return kXR_ReqTimedOut;
1398            case EBADF:         return kXR_FileNotOpen;
1399            case ECANCELED:     return kXR_Cancelled;
1400            case ETIME:         return kXR_TimerExpired;
1401            default:            return kXR_FSError;
1402           }
1403       }
1404   
1405 static int toErrno( int xerr )
1406 {
1407     switch(xerr)
1408        {case kXR_ArgInvalid:    return EINVAL;
1409         case kXR_ArgMissing:    return EINVAL;
1410         case kXR_ArgTooLong:    return ENAMETOOLONG;
1411         case kXR_FileLocked:    return EDEADLK;
1412         case kXR_FileNotOpen:   return EBADF;
1413         case kXR_FSError:       return ENODEV;
1414         case kXR_InvalidRequest:return EBADRQC;
1415         case kXR_IOError:       return EIO;
1416         case kXR_NoMemory:      return ENOMEM;
1417         case kXR_NoSpace:       return ENOSPC;
1418         case kXR_NotAuthorized: return EACCES;
1419         case kXR_NotFound:      return ENOENT;
1420         case kXR_ServerError:   return EFAULT;
1421         case kXR_Unsupported:   return ENOTSUP;
1422         case kXR_noserver:      return EHOSTUNREACH;
1423         case kXR_NotFile:       return ENOTBLK;
1424         case kXR_isDirectory:   return EISDIR;
1425         case kXR_Cancelled:     return ECANCELED;
1426         case kXR_ItExists:      return EEXIST;
1427         case kXR_ChkSumErr:     return EDOM;
1428         case kXR_inProgress:    return EINPROGRESS;
1429         case kXR_overQuota:     return EDQUOT;
1430         case kXR_SigVerErr:     return EILSEQ;
1431         case kXR_DecryptErr:    return ERANGE;
1432         case kXR_Overloaded:    return EUSERS;
1433         case kXR_fsReadOnly:    return EROFS;
1434         case kXR_BadPayload:    return EINVAL;
1435         case kXR_AttrNotFound:  return ENOATTR;
1436         case kXR_TLSRequired:   return EPROTOTYPE;
1437         case kXR_noReplicas:    return EADDRNOTAVAIL;
1438         case kXR_AuthFailed:    return EAUTH;
1439         case kXR_Impossible:    return EIDRM;
1440         case kXR_Conflict:      return ENOTTY;
1441         case kXR_TooManyErrs:   return ETOOMANYREFS;
1442         case kXR_ReqTimedOut:   return ETIMEDOUT;
1443         case kXR_TimerExpired:  return ETIME;  // Used for 504 Gateway timeout in proxy
1444         default:                return ENOMSG;
1445        }
1446 }
1447 
1448 static const char *errName(kXR_int32 errCode);
1449 
1450 static const char *reqName(kXR_unt16 reqCode);
1451 
1452 /******************************************************************************/
1453 /*                  O b s o l e t e   D e f i n i t i o n s                   */
1454 /******************************************************************************/
1455 
1456 struct ServerResponseBody_Attn_asyncdi { // No longer supported
1457    kXR_int32 actnum;
1458    kXR_int32 wsec;
1459    kXR_int32 msec;
1460 };
1461 
1462 struct ServerResponseBody_Attn_asyncrd { // No longer supported
1463    kXR_int32 actnum;
1464    kXR_int32 port;
1465    char host[4092];
1466 };
1467 
1468 struct ServerResponseBody_Attn_asyncwt { // No longer supported
1469    kXR_int32 actnum;
1470    kXR_int32 wsec;
1471 };
1472 
1473 // Kind of error inside a XTNetFile's routine (temporary)
1474 //
1475 enum XReqErrorType {
1476    kGENERICERR = 0,    // Generic error
1477    kREAD,              // Error while reading from stream
1478    kWRITE,             // Error while writing to stream
1479    kREDIRCONNECT,      // Error redirecting to a given host
1480    kOK,                // Everything seems ok
1481    kNOMORESTREAMS      // No more available stream IDs for
1482                        // async processing
1483 };
1484 
1485 typedef kXR_int32 ServerResponseType;
1486 
1487 #define kXR_maxReqRetry 10
1488 
1489 }; // XProtocol
1490 #endif