include/smb_macros.h

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    SMB parameters and setup
00004    Copyright (C) Andrew Tridgell 1992-1999
00005    Copyright (C) John H Terpstra 1996-1999
00006    Copyright (C) Luke Kenneth Casson Leighton 1996-1999
00007    Copyright (C) Paul Ashton 1998 - 1999
00008    
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2 of the License, or
00012    (at your option) any later version.
00013    
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018    
00019    You should have received a copy of the GNU General Public License
00020    along with this program; if not, write to the Free Software
00021    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00022 */
00023 
00024 #ifndef _SMB_MACROS_H
00025 #define _SMB_MACROS_H
00026 
00027 /* Misc bit macros */
00028 #define BOOLSTR(b) ((b) ? "Yes" : "No")
00029 #define BITSETW(ptr,bit) ((SVAL(ptr,0) & (1<<(bit)))!=0)
00030 
00031 /* for readability... */
00032 #define IS_DOS_READONLY(test_mode) (((test_mode) & aRONLY) != 0)
00033 #define IS_DOS_DIR(test_mode)      (((test_mode) & aDIR) != 0)
00034 #define IS_DOS_ARCHIVE(test_mode)  (((test_mode) & aARCH) != 0)
00035 #define IS_DOS_SYSTEM(test_mode)   (((test_mode) & aSYSTEM) != 0)
00036 #define IS_DOS_HIDDEN(test_mode)   (((test_mode) & aHIDDEN) != 0)
00037 
00038 #ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */
00039 
00040 /**
00041  * Free memory if the pointer and zero the pointer.
00042  *
00043  * @note You are explicitly allowed to pass NULL pointers -- they will
00044  * always be ignored.
00045  **/
00046 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
00047 #endif
00048 
00049 /* assert macros */
00050 #ifdef DEVELOPER
00051 #define SMB_ASSERT(b) ( (b) ? (void)0 : \
00052         (DEBUG(0,("PANIC: assert failed at %s(%d)\n", \
00053                  __FILE__, __LINE__)), smb_panic("assert failed")))
00054 #else
00055 /* redefine the assert macro for non-developer builds */
00056 #define SMB_ASSERT(b) ( (b) ? (void)0 : \
00057         (DEBUG(0,("PANIC: assert failed at %s(%d)\n", __FILE__, __LINE__))))
00058 #endif
00059 
00060 #define SMB_WARN(condition, message) \
00061     ((condition) ? (void)0 : \
00062      DEBUG(0, ("WARNING: %s: %s\n", #condition, message)))
00063 
00064 #define SMB_ASSERT_ARRAY(a,n) SMB_ASSERT((sizeof(a)/sizeof((a)[0])) >= (n))
00065 
00066 /* these are useful macros for checking validity of handles */
00067 #define IS_IPC(conn)       ((conn) && (conn)->ipc)
00068 #define IS_PRINT(conn)       ((conn) && (conn)->printer)
00069 /* you must add the following extern declaration to files using this macro
00070  * (do not add it to the macro as that causes nested extern declaration warnings)
00071  * extern struct current_user current_user;
00072  */
00073 #define FSP_BELONGS_CONN(fsp,conn) do {\
00074                         if (!((fsp) && (conn) && ((conn)==(fsp)->conn) && (current_user.vuid==(fsp)->vuid))) \
00075                                 return ERROR_NT(NT_STATUS_INVALID_HANDLE); \
00076                         } while(0)
00077 
00078 #define FNUM_OK(fsp,c) ((fsp) && !(fsp)->is_directory && (c)==(fsp)->conn && current_user.vuid==(fsp)->vuid)
00079 
00080 /* you must add the following extern declaration to files using this macro
00081  * (do not add it to the macro as that causes nested extern declaration warnings)
00082  * extern struct current_user current_user;
00083  */
00084 #define CHECK_FSP(fsp,conn) do {\
00085                         if (!(fsp) || !(conn)) \
00086                                 return ERROR_NT(NT_STATUS_INVALID_HANDLE); \
00087                         else if (((conn) != (fsp)->conn) || current_user.vuid != (fsp)->vuid) \
00088                                 return ERROR_NT(NT_STATUS_INVALID_HANDLE); \
00089                         else if ((fsp)->is_directory) \
00090                                 return ERROR_NT(NT_STATUS_INVALID_DEVICE_REQUEST); \
00091                         else if ((fsp)->fh->fd == -1) \
00092                                 return ERROR_NT(NT_STATUS_ACCESS_DENIED); \
00093                         (fsp)->num_smb_operations++;\
00094                         } while(0)
00095 
00096 #define CHECK_READ(fsp,inbuf) (((fsp)->fh->fd != -1) && ((fsp)->can_read || \
00097                         ((SVAL((inbuf),smb_flg2) & FLAGS2_READ_PERMIT_EXECUTE) && \
00098                          (fsp->access_mask & FILE_EXECUTE))))
00099 
00100 #define CHECK_WRITE(fsp) ((fsp)->can_write && ((fsp)->fh->fd != -1))
00101 
00102 #define ERROR_WAS_LOCK_DENIED(status) (NT_STATUS_EQUAL((status), NT_STATUS_LOCK_NOT_GRANTED) || \
00103                                 NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT) )
00104 
00105 /* the service number for the [globals] defaults */ 
00106 #define GLOBAL_SECTION_SNUM     (-1)
00107 /* translates a connection number into a service number */
00108 #define SNUM(conn)              ((conn)?(conn)->params->service:GLOBAL_SECTION_SNUM)
00109 
00110 
00111 /* access various service details */
00112 #define SERVICE(snum)      (lp_servicename(snum))
00113 #define PRINTERNAME(snum)  (lp_printername(snum))
00114 #define CAN_WRITE(conn)    (!conn->read_only)
00115 #define VALID_SNUM(snum)   (lp_snum_ok(snum))
00116 #define GUEST_OK(snum)     (VALID_SNUM(snum) && lp_guest_ok(snum))
00117 #define GUEST_ONLY(snum)   (VALID_SNUM(snum) && lp_guest_only(snum))
00118 #define CAN_SETDIR(snum)   (!lp_no_set_dir(snum))
00119 #define CAN_PRINT(conn)    ((conn) && lp_print_ok(SNUM(conn)))
00120 #define MAP_HIDDEN(conn)   ((conn) && lp_map_hidden(SNUM(conn)))
00121 #define MAP_SYSTEM(conn)   ((conn) && lp_map_system(SNUM(conn)))
00122 #define MAP_ARCHIVE(conn)   ((conn) && lp_map_archive(SNUM(conn)))
00123 #define IS_HIDDEN_PATH(conn,path)  ((conn) && is_in_path((path),(conn)->hide_list,(conn)->case_sensitive))
00124 #define IS_VETO_PATH(conn,path)  ((conn) && is_in_path((path),(conn)->veto_list,(conn)->case_sensitive))
00125 #define IS_VETO_OPLOCK_PATH(conn,path)  ((conn) && is_in_path((path),(conn)->veto_oplock_list,(conn)->case_sensitive))
00126 
00127 /* 
00128  * Used by the stat cache code to check if a returned
00129  * stat structure is valid.
00130  */
00131 
00132 #define VALID_STAT(st) ((st).st_nlink != 0)  
00133 #define VALID_STAT_OF_DIR(st) (VALID_STAT(st) && S_ISDIR((st).st_mode))
00134 #define SET_STAT_INVALID(st) ((st).st_nlink = 0)
00135 
00136 #ifndef MIN
00137 #define MIN(a,b) ((a)<(b)?(a):(b))
00138 #endif
00139 #ifndef MAX
00140 #define MAX(a,b) ((a)>(b)?(a):(b))
00141 #endif
00142 
00143 #ifndef ABS
00144 #define ABS(a) ((a)>0?(a):(-(a)))
00145 #endif
00146 
00147 /* Macros to get at offsets within smb_lkrng and smb_unlkrng
00148    structures. We cannot define these as actual structures
00149    due to possible differences in structure packing
00150    on different machines/compilers. */
00151 
00152 #define SMB_LPID_OFFSET(indx) (10 * (indx))
00153 #define SMB_LKOFF_OFFSET(indx) ( 2 + (10 * (indx)))
00154 #define SMB_LKLEN_OFFSET(indx) ( 6 + (10 * (indx)))
00155 #define SMB_LARGE_LPID_OFFSET(indx) (20 * (indx))
00156 #define SMB_LARGE_LKOFF_OFFSET_HIGH(indx) (4 + (20 * (indx)))
00157 #define SMB_LARGE_LKOFF_OFFSET_LOW(indx) (8 + (20 * (indx)))
00158 #define SMB_LARGE_LKLEN_OFFSET_HIGH(indx) (12 + (20 * (indx)))
00159 #define SMB_LARGE_LKLEN_OFFSET_LOW(indx) (16 + (20 * (indx)))
00160 
00161 /* Macro to test if an error has been cached for this fnum */
00162 #define HAS_CACHED_ERROR(fsp) ((fsp)->wbmpx_ptr && \
00163                 (fsp)->wbmpx_ptr->wr_discard)
00164 /* Macro to turn the cached error into an error packet */
00165 #define CACHED_ERROR(fsp) cached_error_packet(outbuf,fsp,__LINE__,__FILE__)
00166 
00167 #define ERROR_DOS(class,code) error_packet(outbuf,class,code,NT_STATUS_OK,__LINE__,__FILE__)
00168 #define ERROR_NT(status) error_packet(outbuf,0,0,status,__LINE__,__FILE__)
00169 #define ERROR_OPEN(status) error_open(outbuf,status,__LINE__,__FILE__)
00170 #define ERROR_FORCE_NT(status) error_packet(outbuf,-1,-1,status,__LINE__,__FILE__)
00171 #define ERROR_BOTH(status,class,code) error_packet(outbuf,class,code,status,__LINE__,__FILE__)
00172 
00173 /* this is how errors are generated */
00174 #define UNIXERROR(defclass,deferror) unix_error_packet(outbuf,defclass,deferror,NT_STATUS_OK,__LINE__,__FILE__)
00175 
00176 /* these are the datagram types */
00177 #define DGRAM_DIRECT_UNIQUE 0x10
00178 
00179 #define SMB_ROUNDUP(x,r) ( ((x)%(r)) ? ( (((x)+(r))/(r))*(r) ) : (x))
00180 
00181 /* Extra macros added by Ying Chen at IBM - speed increase by inlining. */
00182 #define smb_buf(buf) (((char *)(buf)) + smb_size + CVAL(buf,smb_wct)*2)
00183 #define smb_buflen(buf) (SVAL(buf,smb_vwv0 + (int)CVAL(buf, smb_wct)*2))
00184 
00185 /* the remaining number of bytes in smb buffer 'buf' from pointer 'p'. */
00186 #define smb_bufrem(buf, p) (smb_buflen(buf)-PTR_DIFF(p, smb_buf(buf)))
00187 
00188 /* Note that chain_size must be available as an extern int to this macro. */
00189 #define smb_offset(p,buf) (PTR_DIFF(p,buf+4) + chain_size)
00190 
00191 #define smb_len(buf) (PVAL(buf,3)|(PVAL(buf,2)<<8)|((PVAL(buf,1)&1)<<16))
00192 #define _smb_setlen(buf,len) do { buf[0] = 0; buf[1] = (len&0x10000)>>16; \
00193         buf[2] = (len&0xFF00)>>8; buf[3] = len&0xFF; } while (0)
00194 
00195 /*******************************************************************
00196 find the difference in milliseconds between two struct timeval
00197 values
00198 ********************************************************************/
00199 
00200 #define TvalDiff(tvalold,tvalnew) \
00201   (((tvalnew)->tv_sec - (tvalold)->tv_sec)*1000 +  \
00202          ((int)(tvalnew)->tv_usec - (int)(tvalold)->tv_usec)/1000)
00203 
00204 /****************************************************************************
00205 true if two IP addresses are equal
00206 ****************************************************************************/
00207 
00208 #define ip_equal(ip1,ip2) ((ip1).s_addr == (ip2).s_addr)
00209 #define ip_service_equal(ip1,ip2) ( ((ip1).ip.s_addr == (ip2).ip.s_addr) && ((ip1).port == (ip2).port) )
00210 
00211 /*****************************************************************
00212  splits out the last subkey of a key
00213  *****************************************************************/  
00214 
00215 #define reg_get_subkey(full_keyname, key_name, subkey_name) \
00216         split_at_last_component(full_keyname, key_name, '\\', subkey_name)
00217 
00218 /****************************************************************************
00219  Return True if the offset is at zero.
00220 ****************************************************************************/
00221 
00222 #define dptr_zero(buf) (IVAL(buf,1) == 0)
00223 
00224 /*******************************************************************
00225 copy an IP address from one buffer to another
00226 ********************************************************************/
00227 
00228 #define putip(dest,src) memcpy(dest,src,4)
00229 
00230 /*******************************************************************
00231  Return True if a server has CIFS UNIX capabilities.
00232 ********************************************************************/
00233 
00234 #define SERVER_HAS_UNIX_CIFS(c) ((c)->capabilities & CAP_UNIX)
00235 
00236 /****************************************************************************
00237  Make a filename into unix format.
00238 ****************************************************************************/
00239 
00240 #define IS_DIRECTORY_SEP(c) ((c) == '\\' || (c) == '/')
00241 #define unix_format(fname) string_replace(fname,'\\','/')
00242 #define unix_format_w(fname) string_replace_w(fname, UCS2_CHAR('\\'), UCS2_CHAR('/'))
00243 
00244 /****************************************************************************
00245  Make a file into DOS format.
00246 ****************************************************************************/
00247 
00248 #define dos_format(fname) string_replace(fname,'/','\\')
00249 
00250 /*****************************************************************************
00251  Check to see if we are a DC for this domain
00252 *****************************************************************************/
00253 
00254 #define IS_DC  (lp_server_role()==ROLE_DOMAIN_PDC || lp_server_role()==ROLE_DOMAIN_BDC) 
00255 
00256 /*****************************************************************************
00257  Safe allocation macros.
00258 *****************************************************************************/
00259 
00260 #define SMB_MALLOC_ARRAY(type,count) (type *)malloc_array(sizeof(type),(count))
00261 #define SMB_MEMALIGN_ARRAY(type,align,count) (type *)memalign_array(sizeof(type),align,(count))
00262 #define SMB_REALLOC(p,s) Realloc((p),(s),True)  /* Always frees p on error or s == 0 */
00263 #define SMB_REALLOC_KEEP_OLD_ON_ERROR(p,s) Realloc((p),(s),False) /* Never frees p on error or s == 0 */
00264 #define SMB_REALLOC_ARRAY(p,type,count) (type *)realloc_array((p),sizeof(type),(count),True) /* Always frees p on error or s == 0 */
00265 #define SMB_REALLOC_ARRAY_KEEP_OLD_ON_ERROR(p,type,count) (type *)realloc_array((p),sizeof(type),(count),False) /* Never frees p on error or s == 0 */
00266 #define SMB_CALLOC_ARRAY(type,count) (type *)calloc_array(sizeof(type),(count))
00267 #define SMB_XMALLOC_P(type) (type *)smb_xmalloc_array(sizeof(type),1)
00268 #define SMB_XMALLOC_ARRAY(type,count) (type *)smb_xmalloc_array(sizeof(type),(count))
00269 
00270 /* limiting size of ipc replies */
00271 #define SMB_REALLOC_LIMIT(ptr,size) (char *)SMB_REALLOC(ptr,MAX((size),4*1024))
00272 
00273 /* The new talloc is paranoid malloc checker safe. */
00274 
00275 #define TALLOC(ctx, size) talloc_zeronull(ctx, size, __location__)
00276 #define TALLOC_P(ctx, type) (type *)talloc_zeronull(ctx, sizeof(type), #type)
00277 #define TALLOC_ARRAY(ctx, type, count) (type *)_talloc_array_zeronull(ctx, sizeof(type), count, #type)
00278 #define TALLOC_MEMDUP(ctx, ptr, size) _talloc_memdup_zeronull(ctx, ptr, size, __location__)
00279 #define TALLOC_ZERO(ctx, size) _talloc_zero_zeronull(ctx, size, __location__)
00280 #define TALLOC_ZERO_P(ctx, type) (type *)_talloc_zero_zeronull(ctx, sizeof(type), #type)
00281 #define TALLOC_ZERO_ARRAY(ctx, type, count) (type *)_talloc_zero_array_zeronull(ctx, sizeof(type), count, #type)
00282 #define TALLOC_REALLOC(ctx, ptr, count) _talloc_realloc(ctx, ptr, count, __location__)
00283 #define TALLOC_REALLOC_ARRAY(ctx, ptr, type, count) (type *)_talloc_realloc_array(ctx, ptr, sizeof(type), count, #type)
00284 #define talloc_destroy(ctx) talloc_free(ctx)
00285 #define TALLOC_FREE(ctx) do { if ((ctx) != NULL) {talloc_free(ctx); ctx=NULL;} } while(0)
00286 #define TALLOC_SIZE(ctx, size) talloc_zeronull(ctx, size, __location__)
00287 #define TALLOC_ZERO_SIZE(ctx, size) _talloc_zero_zeronull(ctx, size, __location__)
00288 
00289 /* only define PARANOID_MALLOC_CHECKER with --enable-developer and not compiling
00290    the smbmount utils */
00291 
00292 #if defined(DEVELOPER) && !defined(SMBMOUNT_MALLOC)
00293 #  define PARANOID_MALLOC_CHECKER 1
00294 #endif
00295 
00296 #if defined(PARANOID_MALLOC_CHECKER)
00297 
00298 #define PRS_ALLOC_MEM(ps, type, count) (type *)prs_alloc_mem_((ps),sizeof(type),(count))
00299 
00300 /* Get medieval on our ass about malloc.... */
00301 
00302 /* Restrictions on malloc/realloc/calloc. */
00303 #ifdef malloc
00304 #undef malloc
00305 #endif
00306 #define malloc(s) __ERROR_DONT_USE_MALLOC_DIRECTLY
00307 
00308 #ifdef realloc
00309 #undef realloc
00310 #endif
00311 #define realloc(p,s) __ERROR_DONT_USE_REALLOC_DIRECTLY
00312 
00313 #ifdef calloc
00314 #undef calloc
00315 #endif
00316 #define calloc(n,s) __ERROR_DONT_USE_CALLOC_DIRECTLY
00317 
00318 #ifdef strndup
00319 #undef strndup
00320 #endif
00321 #define strndup(s,n) __ERROR_DONT_USE_STRNDUP_DIRECTLY
00322 
00323 #ifdef strdup
00324 #undef strdup
00325 #endif
00326 #define strdup(s) __ERROR_DONT_USE_STRDUP_DIRECTLY
00327 
00328 #define SMB_MALLOC(s) malloc_(s)
00329 #define SMB_MALLOC_P(type) (type *)malloc_(sizeof(type))
00330 
00331 #define SMB_STRDUP(s) smb_xstrdup(s)
00332 #define SMB_STRNDUP(s,n) smb_xstrndup(s,n)
00333 
00334 #else
00335 
00336 #define PRS_ALLOC_MEM(ps, type, count) (type *)prs_alloc_mem((ps),sizeof(type),(count))
00337 
00338 /* Regular malloc code. */
00339 
00340 #define SMB_MALLOC(s) malloc(s)
00341 #define SMB_MALLOC_P(type) (type *)malloc(sizeof(type))
00342 
00343 #define SMB_STRDUP(s) strdup(s)
00344 #define SMB_STRNDUP(s,n) strndup(s,n)
00345 
00346 #endif
00347 
00348 #define ADD_TO_ARRAY(mem_ctx, type, elem, array, num) \
00349 do { \
00350         *(array) = ((mem_ctx) != NULL) ? \
00351                 TALLOC_REALLOC_ARRAY(mem_ctx, (*(array)), type, (*(num))+1) : \
00352                 SMB_REALLOC_ARRAY((*(array)), type, (*(num))+1); \
00353         SMB_ASSERT((*(array)) != NULL); \
00354         (*(array))[*(num)] = (elem); \
00355         (*(num)) += 1; \
00356 } while (0)
00357 
00358 #define ADD_TO_LARGE_ARRAY(mem_ctx, type, elem, array, num, size) \
00359         add_to_large_array((mem_ctx), sizeof(type), &(elem), (void *)(array), (num), (size));
00360 
00361 #ifndef ISDOT
00362 #define ISDOT(p) (*(p) == '.' && *((p) + 1) == '\0')
00363 #endif /* ISDOT */
00364 
00365 #ifndef ISDOTDOT
00366 #define ISDOTDOT(p) (*(p) == '.' && *((p) + 1) == '.' && *((p) + 2) == '\0')
00367 #endif /* ISDOTDOT */
00368 
00369 #endif /* _SMB_MACROS_H */

Sambaに対してSat Aug 29 21:22:57 2009に生成されました。  doxygen 1.4.7