From a0fbfe1c6d6f42b4c2704c882beda1c4cafe5016 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 24 Oct 2017 11:27:44 +0200 Subject: [SERVER] Fix types or add explicit casts everywhere we might have type conversion problems --- src/serialize.c | 4 +-- src/serialize.h | 2 +- src/server/altservers.c | 4 +-- src/server/globals.h | 8 ++--- src/server/helper.c | 4 +-- src/server/image.c | 85 ++++++++++++++++++++++++++----------------------- src/server/integrity.c | 2 +- src/server/net.c | 57 +++++++++++++++++---------------- src/server/rpc.c | 4 +-- src/server/server.c | 2 +- src/server/uplink.c | 10 +++--- src/shared/fdsignal.c | 1 + src/shared/log.c | 20 ++++++------ src/shared/protocol.h | 4 +-- src/shared/sockhelper.c | 19 ++++++----- src/shared/sockhelper.h | 4 +-- 16 files changed, 120 insertions(+), 110 deletions(-) diff --git a/src/serialize.c b/src/serialize.c index c8457e8..0bc0dcd 100644 --- a/src/serialize.c +++ b/src/serialize.c @@ -78,7 +78,7 @@ void serializer_put_string(serialized_buffer_t *buffer, const char *value) buffer->buffer_pointer += len; } -ssize_t serializer_get_written_length(serialized_buffer_t *buffer) +uint32_t serializer_get_written_length(serialized_buffer_t *buffer) { - return buffer->buffer_pointer - buffer->buffer; + return (uint32_t)( buffer->buffer_pointer - buffer->buffer ); } diff --git a/src/serialize.h b/src/serialize.h index 63b6de9..1b73531 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -15,7 +15,7 @@ void serializer_reset_read(serialized_buffer_t *buffer, size_t data_len); void serializer_reset_write(serialized_buffer_t *buffer); -ssize_t serializer_get_written_length(serialized_buffer_t *buffer); +uint32_t serializer_get_written_length(serialized_buffer_t *buffer); // diff --git a/src/server/altservers.c b/src/server/altservers.c index f9dec4b..2accb96 100644 --- a/src/server/altservers.c +++ b/src/server/altservers.c @@ -469,9 +469,9 @@ static void *altservers_main(void *data UNUSED) // Penaltize rtt if this was a cycle; this will treat this server with lower priority // in the near future too, so we prevent alternating between two servers that are both // part of a cycle and have the lowest latency. - const unsigned int rtt = (end.tv_sec - start.tv_sec) * 1000000 + const unsigned int rtt = (unsigned int)((end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000 - + ( (isCurrent && uplink->cycleDetected) ? 1000000 : 0 ); // µs + + ( (isCurrent && uplink->cycleDetected) ? 1000000 : 0 )); // µs unsigned int avg = altservers_updateRtt( &servers[itAlt], rtt ); // If a cycle was detected, or we lost connection to the current (last) server, penaltize it one time if ( ( uplink->cycleDetected || uplink->fd == -1 ) && isCurrent ) avg = (avg * 2) + 50000; diff --git a/src/server/globals.h b/src/server/globals.h index 31ac841..798f4ee 100644 --- a/src/server/globals.h +++ b/src/server/globals.h @@ -76,8 +76,8 @@ typedef struct { char comment[COMMENT_LENGTH]; dnbd3_host_t host; - int rtt[SERVER_RTT_PROBES]; - int rttIndex; + unsigned int rtt[SERVER_RTT_PROBES]; + unsigned int rttIndex; bool isPrivate, isClientOnly; ticks lastFail; int numFails; @@ -112,11 +112,11 @@ struct _dnbd3_image uint32_t masterCrc32; // CRC-32 of the crc-32 list int readFd; // used to read the image. Used from multiple threads, so use atomic operations (pread et al) int cacheFd; // used to write to the image, in case it is relayed. ONLY USE FROM UPLINK THREAD! - int rid; // revision of image int completenessEstimate; // Completeness estimate in percent int users; // clients currently using this image int id; // Unique ID of this image. Only unique in the context of this running instance of DNBD3-Server bool working; // true if image exists and completeness is == 100% or a working upstream proxy is connected + uint16_t rid; // revision of image pthread_spinlock_t lock; }; @@ -125,7 +125,7 @@ struct _dnbd3_client #define HOSTNAMELEN (48) uint64_t bytesSent; // Byte counter for this client. Use statsLock when accessing dnbd3_image_t *image; - uint32_t tmpBytesSent; // Temporary byte counter that gets added to the global counter periodically. Use statsLock when accessing + size_t tmpBytesSent; // Temporary byte counter that gets added to the global counter periodically. Use statsLock when accessing int sock; bool isServer; // true if a server in proxy mode, false if real client dnbd3_host_t host; diff --git a/src/server/helper.c b/src/server/helper.c index 96647e1..1a67a95 100644 --- a/src/server/helper.c +++ b/src/server/helper.c @@ -87,11 +87,11 @@ bool host_to_string(const dnbd3_host_t *host, char *target, size_t targetlen) if ( targetlen < 10 ) return false; if ( host->type == AF_INET6 ) { *target++ = '['; - inet_ntop( AF_INET6, host->addr, target, targetlen - 10 ); + inet_ntop( AF_INET6, host->addr, target, (socklen_t)targetlen - 10 ); target += strlen( target ); *target++ = ']'; } else if ( host->type == AF_INET ) { - inet_ntop( AF_INET, host->addr, target, targetlen - 8 ); + inet_ntop( AF_INET, host->addr, target, (socklen_t)targetlen - 8 ); target += strlen( target ); } else { snprintf( target, targetlen, "", (int)host->type ); diff --git a/src/server/image.c b/src/server/image.c index 47bcb0b..b2a89ca 100644 --- a/src/server/image.c +++ b/src/server/image.c @@ -48,7 +48,7 @@ static bool image_load_all_internal(char *base, char *path); static bool image_addToList(dnbd3_image_t *image); static bool image_load(char *base, char *path, int withUplink); static bool image_clone(int sock, char *name, uint16_t revision, uint64_t imageSize); -static bool image_calcBlockCrc32(const int fd, const int block, const uint64_t realFilesize, uint32_t *crc); +static bool image_calcBlockCrc32(const int fd, const size_t block, const uint64_t realFilesize, uint32_t *crc); static bool image_ensureDiskSpace(uint64_t size); static uint8_t* image_loadCacheMap(const char * const imagePath, const int64_t fileSize); @@ -92,7 +92,7 @@ bool image_isComplete(dnbd3_image_t *image) last_byte = 0xFF; } else { for (j = 0; j < blocks_in_last_byte; ++j) - last_byte |= (1 << j); + last_byte |= (uint8_t)(1 << j); } complete = ((image->cache_map[map_len_bytes - 1] & last_byte) == last_byte); } @@ -121,14 +121,14 @@ void image_updateCachemap(dnbd3_image_t *image, uint64_t start, uint64_t end, co return; } while ( pos < end ) { - const int map_y = pos >> 15; - const int map_x = (pos >> 12) & 7; // mod 8 - const uint8_t bit_mask = 1 << map_x; + const size_t map_y = (int)( pos >> 15 ); + const int map_x = (int)( (pos >> 12) & 7 ); // mod 8 + const int bit_mask = 1 << map_x; if ( set ) { if ( (image->cache_map[map_y] & bit_mask) == 0 ) dirty = true; - image->cache_map[map_y] |= bit_mask; + image->cache_map[map_y] |= (uint8_t)bit_mask; } else { - image->cache_map[map_y] &= ~bit_mask; + image->cache_map[map_y] &= (uint8_t)~bit_mask; } pos += DNBD3_BLOCK_SIZE; } @@ -142,7 +142,7 @@ void image_updateCachemap(dnbd3_image_t *image, uint64_t start, uint64_t end, co pos = start; while ( pos < end ) { if ( image->cache_map == NULL ) break; - const int block = pos / HASH_BLOCK_SIZE; + const int block = (int)( pos / HASH_BLOCK_SIZE ); if ( image_isHashBlockComplete( image->cache_map, block, image->virtualFilesize ) ) { spin_unlock( &image->lock ); integrity_check( image, block ); @@ -269,7 +269,7 @@ dnbd3_image_t* image_get(char *name, uint16_t revision, bool checkIfWorking) const char *removingText = _removeMissingImages ? ", removing from list" : ""; dnbd3_image_t *candidate = NULL; // Simple sanity check - const int slen = strlen( name ); + const size_t slen = strlen( name ); if ( slen == 0 || name[slen - 1] == '/' || name[0] == '/' ) return NULL ; // Go through array spin_lock( &imageListLock ); @@ -666,7 +666,7 @@ static bool image_isHashBlockComplete(const uint8_t * const cacheMap, const uint if ( end <= realFilesize ) { // Trivial case: block in question is not the last block (well, or image size is multiple of HASH_BLOCK_SIZE) const int startCacheIndex = (int)( ( block * HASH_BLOCK_SIZE ) / ( DNBD3_BLOCK_SIZE * 8 ) ); - const int endCacheIndex = startCacheIndex + ( HASH_BLOCK_SIZE / ( DNBD3_BLOCK_SIZE * 8 ) ); + const int endCacheIndex = startCacheIndex + (int)( HASH_BLOCK_SIZE / ( DNBD3_BLOCK_SIZE * 8 ) ); for ( int i = startCacheIndex; i < endCacheIndex; ++i ) { if ( cacheMap[i] != 0xff ) { return false; @@ -675,8 +675,8 @@ static bool image_isHashBlockComplete(const uint8_t * const cacheMap, const uint } else { // Special case: Checking last block, which is smaller than HASH_BLOCK_SIZE for (uint64_t mapPos = block * HASH_BLOCK_SIZE; mapPos < realFilesize; mapPos += DNBD3_BLOCK_SIZE ) { - const int map_y = mapPos >> 15; - const int map_x = (mapPos >> 12) & 7; // mod 8 + const size_t map_y = mapPos >> 15; + const int map_x = (int)( (mapPos >> 12) & 7 ); // mod 8 const int mask = 1 << map_x; if ( (cacheMap[map_y] & mask) == 0 ) return false; } @@ -694,7 +694,7 @@ static bool image_load_all_internal(char *base, char *path) assert( path != NULL ); assert( *path == '/' ); struct dirent entry, *entryPtr; - const int pathLen = strlen( path ); + const size_t pathLen = strlen( path ); char subpath[PATHLEN]; struct stat st; DIR * const dir = opendir( path ); @@ -764,7 +764,7 @@ static bool image_addToList(dnbd3_image_t *image) */ static bool image_load(char *base, char *path, int withUplink) { - int i, revision = -1; + int revision = -1; struct stat st; uint8_t *cache_map = NULL; uint32_t *crc32list = NULL; @@ -780,7 +780,7 @@ static bool image_load(char *base, char *path, int withUplink) char *lastSlash = strrchr( path, '/' ); char *fileName = lastSlash + 1; char imgName[strlen( path )]; - const int fileNameLen = strlen( fileName ); + const size_t fileNameLen = strlen( fileName ); // Copy virtual path (relative path in "base") char * const virtBase = path + strlen( base ) + 1; @@ -791,19 +791,23 @@ static bool image_load(char *base, char *path, int withUplink) } *dst = '\0'; - // Parse file name for revision - // Try to parse *.r syntax - for (i = fileNameLen - 1; i > 1; --i) { - if ( fileName[i] < '0' || fileName[i] > '9' ) break; - } - if ( i != fileNameLen - 1 && fileName[i] == 'r' && fileName[i - 1] == '.' ) { - revision = atoi( fileName + i + 1 ); - src = fileName; - while ( src < fileName + i - 1 ) { - *dst++ = *src++; + do { + // Parse file name for revision + // Try to parse *.r syntax + size_t i; + for (i = fileNameLen - 1; i > 1; --i) { + if ( fileName[i] < '0' || fileName[i] > '9' ) break; } - *dst = '\0'; - } + if ( i != fileNameLen - 1 && fileName[i] == 'r' && fileName[i - 1] == '.' ) { + revision = atoi( fileName + i + 1 ); + src = fileName; + while ( src < fileName + i - 1 ) { + *dst++ = *src++; + } + *dst = '\0'; + } + } while (0); + // Legacy mode enabled and no rid extracted from filename? if ( _vmdkLegacyMode && revision == -1 ) { // Yes, simply append full file name and set rid to 1 @@ -817,7 +821,7 @@ static bool image_load(char *base, char *path, int withUplink) } // Get pointer to already existing image if possible - existing = image_get( imgName, revision, true ); + existing = image_get( imgName, (uint16_t)revision, true ); // ### Now load the actual image related data ### fdImage = open( path, O_RDONLY ); @@ -902,16 +906,16 @@ static bool image_load(char *base, char *path, int withUplink) image->uplink = NULL; image->realFilesize = realFilesize; image->virtualFilesize = virtualFilesize; - image->rid = revision; + image->rid = (uint16_t)revision; image->users = 0; image->readFd = -1; image->cacheFd = -1; image->working = (image->cache_map == NULL ); spin_init( &image->lock, PTHREAD_PROCESS_PRIVATE ); - int offset; + int32_t offset; if ( stat( path, &st ) == 0 ) { // Negatively offset atime by file modification time - offset = st.st_mtime - time( NULL ); + offset = (int32_t)( st.st_mtime - time( NULL ) ); if ( offset > 0 ) offset = 0; } else { offset = 0; @@ -1590,35 +1594,36 @@ bool image_checkBlocksCrc32(const int fd, uint32_t *crc32list, const int *blocks /** * Calc CRC-32 of block. Value is returned as little endian. */ -static bool image_calcBlockCrc32(const int fd, const int block, const uint64_t realFilesize, uint32_t *crc) +static bool image_calcBlockCrc32(const int fd, const size_t block, const uint64_t realFilesize, uint32_t *crc) { char buffer[40000]; - *crc = crc32( 0L, Z_NULL, 0 ); - int bytes = 0; // How many bytes to read from the input file - const int bytesFromFile = MIN( HASH_BLOCK_SIZE, realFilesize - ( (int64_t)block * HASH_BLOCK_SIZE) ); + const size_t bytesFromFile = MIN( HASH_BLOCK_SIZE, realFilesize - ( block * HASH_BLOCK_SIZE) ); // Determine how many bytes we had to read if the file size were a multiple of 4k // This might be the same value if the real file's size is a multiple of 4k - const int64_t vbs = ( ( realFilesize + ( DNBD3_BLOCK_SIZE - 1 ) ) & ~( DNBD3_BLOCK_SIZE - 1 ) ) - ( (int64_t)block * HASH_BLOCK_SIZE); - const int virtualBytesFromFile = (int)MIN( HASH_BLOCK_SIZE, vbs ); + const size_t vbs = ( ( realFilesize + ( DNBD3_BLOCK_SIZE - 1 ) ) & ~( DNBD3_BLOCK_SIZE - 1 ) ) - ( block * HASH_BLOCK_SIZE ); + const size_t virtualBytesFromFile = MIN( HASH_BLOCK_SIZE, vbs ); const off_t readPos = (int64_t)block * HASH_BLOCK_SIZE; + size_t bytes = 0; + assert( vbs >= bytesFromFile ); + *crc = crc32( 0L, Z_NULL, 0 ); // Calculate the crc32 by reading data from the file while ( bytes < bytesFromFile ) { const int n = MIN( (int)sizeof(buffer), bytesFromFile - bytes ); - const int r = pread( fd, buffer, n, readPos + bytes ); + const ssize_t r = pread( fd, buffer, n, readPos + bytes ); if ( r <= 0 ) { logadd( LOG_WARNING, "CRC: Read error (errno=%d)", errno ); return false; } *crc = crc32( *crc, (Bytef*)buffer, r ); - bytes += r; + bytes += (size_t)r; } // If the virtual file size is different, keep going using nullbytes if ( bytesFromFile < virtualBytesFromFile ) { memset( buffer, 0, sizeof(buffer) ); bytes = virtualBytesFromFile - bytesFromFile; while ( bytes != 0 ) { - const int len = MIN( (int)sizeof(buffer), bytes ); + const size_t len = MIN( sizeof(buffer), bytes ); *crc = crc32( *crc, (Bytef*)buffer, len ); bytes -= len; } diff --git a/src/server/integrity.c b/src/server/integrity.c index 1216947..e07a076 100644 --- a/src/server/integrity.c +++ b/src/server/integrity.c @@ -102,7 +102,7 @@ static void* integrity_main(void * data UNUSED) // Setting nice of this thread - this is not POSIX conforming, so check if other platforms support this. // POSIX says that setpriority() should set the nice value of all threads belonging to the current process, // but on linux you can do this per thread. - pid_t tid = syscall( SYS_gettid ); + pid_t tid = (pid_t)syscall( SYS_gettid ); setpriority( PRIO_PROCESS, tid, 10 ); #endif pthread_mutex_lock( &integrityQueueLock ); diff --git a/src/server/net.c b/src/server/net.c index 0c1a11f..09121c3 100644 --- a/src/server/net.c +++ b/src/server/net.c @@ -68,7 +68,7 @@ void net_updateGlobalSentStatsFromClient(dnbd3_client_t * const client) static inline bool recv_request_header(int sock, dnbd3_request_t *request) { - int ret, fails = 0; + ssize_t ret, fails = 0; // Read request header from socket while ( ( ret = recv( sock, request, sizeof(*request), MSG_WAITALL ) ) != sizeof(*request) ) { if ( errno == EINTR && ++fails < 10 ) continue; @@ -165,33 +165,34 @@ void* net_handleNewConnection(void *clientPtr) { dnbd3_client_t * const client = (dnbd3_client_t *)clientPtr; dnbd3_request_t request; - int ret; // Await data from client. Since this is a fresh connection, we expect data right away sock_setTimeout( client->sock, _clientTimeout ); - ret = recv( client->sock, &request, sizeof(request), MSG_WAITALL ); - // Let's see if this looks like an HTTP request - if ( ret > 5 && request.magic != dnbd3_packet_magic - && ( strncmp( (char*)&request, "GET ", 4 ) == 0 || strncmp( (char*)&request, "POST ", 5 ) == 0 ) ) { - rpc_sendStatsJson( client->sock, &client->host, &request, (size_t)ret ); - goto fail_preadd; - } + do { + const int ret = (int)recv( client->sock, &request, sizeof(request), MSG_WAITALL ); + // Let's see if this looks like an HTTP request + if ( ret > 5 && request.magic != dnbd3_packet_magic + && ( strncmp( (char*)&request, "GET ", 4 ) == 0 || strncmp( (char*)&request, "POST ", 5 ) == 0 ) ) { + rpc_sendStatsJson( client->sock, &client->host, &request, ret ); + goto fail_preadd; + } - // It's expected to be a real dnbd3 client - // Check request for validity - if ( ret != sizeof(request) ) { - logadd( LOG_DEBUG1, "Error receiving request: Could not read message header (%d/%d, e=%d)", ret, (int)sizeof(request), errno ); - goto fail_preadd; - } - if ( request.magic != dnbd3_packet_magic ) { - logadd( LOG_DEBUG1, "Magic in client handshake incorrect" ); - goto fail_preadd; - } - fixup_request( request ); - if ( request.cmd != CMD_SELECT_IMAGE ) { - logadd( LOG_WARNING, "Client sent != CMD_SELECT_IMAGE in handshake (got cmd=%d, size=%d), dropping client.", (int)request.cmd, (int)request.size ); - goto fail_preadd; - } + // It's expected to be a real dnbd3 client + // Check request for validity + if ( ret != sizeof(request) ) { + logadd( LOG_DEBUG1, "Error receiving request: Could not read message header (%d/%d, e=%d)", (int)ret, (int)sizeof(request), errno ); + goto fail_preadd; + } + if ( request.magic != dnbd3_packet_magic ) { + logadd( LOG_DEBUG1, "Magic in client handshake incorrect" ); + goto fail_preadd; + } + fixup_request( request ); + if ( request.cmd != CMD_SELECT_IMAGE ) { + logadd( LOG_WARNING, "Client sent != CMD_SELECT_IMAGE in handshake (got cmd=%d, size=%d), dropping client.", (int)request.cmd, (int)request.size ); + goto fail_preadd; + } + } while (0); // Fully init client struct spin_init( &client->lock, PTHREAD_PROCESS_PRIVATE ); spin_init( &client->statsLock, PTHREAD_PROCESS_PRIVATE ); @@ -341,7 +342,7 @@ void* net_handleNewConnection(void *clientPtr) pos = start; do { const int map_x = (pos >> 12) & 7; // mod 8 - const uint8_t bit_mask = 1 << map_x; + const uint8_t bit_mask = (uint8_t)( 1 << map_x ); if ( (image->cache_map[firstByteInMap] & bit_mask) == 0 ) { isCached = false; break; @@ -355,7 +356,7 @@ void* net_handleNewConnection(void *clientPtr) while ( pos < end ) { assert( lastByteInMap == (pos >> 15) ); const int map_x = (pos >> 12) & 7; // mod 8 - const uint8_t bit_mask = 1 << map_x; + const uint8_t bit_mask = (uint8_t)( 1 << map_x ); if ( (image->cache_map[lastByteInMap] & bit_mask) == 0 ) { isCached = false; break; @@ -472,7 +473,7 @@ void* net_handleNewConnection(void *clientPtr) // Build list of known working alt servers num = altservers_getMatching( &client->host, server_list, NUMBER_SERVERS ); reply.cmd = CMD_GET_SERVERS; - reply.size = num * sizeof(dnbd3_server_entry_t); + reply.size = (uint32_t)( num * sizeof(dnbd3_server_entry_t) ); pthread_mutex_lock( &client->sendMutex ); send_reply( client->sock, &reply, server_list ); pthread_mutex_unlock( &client->sendMutex ); @@ -506,7 +507,7 @@ set_name: ; reply.size = 0; send_reply( client->sock, &reply, NULL ); } else { - const int size = reply.size = (IMGSIZE_TO_HASHBLOCKS(image->realFilesize) + 1) * sizeof(uint32_t); + const uint32_t size = reply.size = (uint32_t)( (IMGSIZE_TO_HASHBLOCKS(image->realFilesize) + 1) * sizeof(uint32_t) ); send_reply( client->sock, &reply, NULL ); send( client->sock, &image->masterCrc32, sizeof(uint32_t), MSG_MORE ); send( client->sock, image->crc32, size - sizeof(uint32_t), 0 ); diff --git a/src/server/rpc.c b/src/server/rpc.c index 7f57b0a..7b558ce 100644 --- a/src/server/rpc.c +++ b/src/server/rpc.c @@ -250,7 +250,7 @@ static void addacl(int argc, char **argv, void *data UNUSED) } } memcpy( aclRules[aclCount].host, host.addr, 16 ); - aclRules[aclCount].bytes = bits / 8; + aclRules[aclCount].bytes = (int)( bits / 8 ); aclRules[aclCount].bitMask = 0; aclRules[aclCount].permissions = mask; bits %= 8; @@ -258,7 +258,7 @@ static void addacl(int argc, char **argv, void *data UNUSED) for (long int i = 0; i < bits; ++i) { aclRules[aclCount].bitMask = ( aclRules[aclCount].bitMask >> 1 ) | 0x80; } - aclRules[aclCount].host[aclRules[aclCount].bytes] &= aclRules[aclCount].bitMask; + aclRules[aclCount].host[aclRules[aclCount].bytes] &= (uint8_t)aclRules[aclCount].bitMask; } // We now have .bytes set to the number of bytes to memcmp. // In case we have an odd bitmask, .bitMask will be != 0, so when comparing, diff --git a/src/server/server.c b/src/server/server.c index 6cbea3a..1e63dcc 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -277,7 +277,7 @@ int main(int argc, char *argv[]) logadd( LOG_ERROR, "Didnt get a poll list!" ); exit( EXIT_FAILURE ); } - if ( !sock_listen( listeners, bindAddress, _listenPort ) ) { + if ( !sock_listen( listeners, bindAddress, (uint16_t)_listenPort ) ) { logadd( LOG_ERROR, "Could not listen on any local interface." ); exit( EXIT_FAILURE ); } diff --git a/src/server/uplink.c b/src/server/uplink.c index cb5f426..2b50e8f 100644 --- a/src/server/uplink.c +++ b/src/server/uplink.c @@ -331,7 +331,7 @@ static void* uplink_mainloop(void *data) // poll() do { declare_now; - waitTime = timing_diffMs( &now, &nextAltCheck ); + waitTime = (int)timing_diffMs( &now, &nextAltCheck ); } while(0); if ( waitTime < 1500 ) waitTime = 1500; if ( waitTime > 5000 ) waitTime = 5000; @@ -474,10 +474,10 @@ static void uplink_sendRequests(dnbd3_connection_t *link, bool newOnly) //logadd( LOG_DEBUG2 %p] Sending slot %d, now %d, handle %" PRIu64 ", Range: %" PRIu64 "-%" PRIu64 "\n", (void*)link, j, link->queue[j].status, link->queue[j].handle, link->queue[j].from, link->queue[j, ".to ); link->queue[j].status = ULR_PENDING; const uint64_t offset = link->queue[j].from; - const uint32_t size = link->queue[j].to - link->queue[j].from; + const uint32_t size = (uint32_t)( link->queue[j].to - link->queue[j].from ); uint8_t hops = link->queue[j].hopCount; spin_unlock( &link->queueLock ); - if ( hops < 200 ) hops += 1; + if ( hops < 200 ) ++hops; const int ret = dnbd3_get_block( link->fd, offset, size, offset, COND_HOPCOUNT( link->version, hops ) ); if ( !ret ) { // Non-critical - if the connection dropped or the server was changed @@ -525,7 +525,7 @@ static void uplink_sendReplicationRequest(dnbd3_connection_t *link) spin_unlock( &image->lock ); // Unlocked - do not break or continue here... const uint64_t offset = link->replicationHandle = (uint64_t)i * (uint64_t)requestBlockSize; - const uint32_t size = MIN( image->realFilesize - offset, requestBlockSize ); + const uint32_t size = (uint32_t)MIN( image->realFilesize - offset, requestBlockSize ); if ( !dnbd3_get_block( link->fd, offset, size, link->replicationHandle, COND_HOPCOUNT( link->version, 1 ) ) ) { logadd( LOG_DEBUG1, "Error sending background replication request to uplink server!\n" ); return; @@ -627,7 +627,7 @@ static void uplink_handleReceive(dnbd3_connection_t *link) dnbd3_client_t * const client = req->client; outReply.cmd = CMD_GET_BLOCK; outReply.handle = req->handle; - outReply.size = req->to - req->from; + outReply.size = (uint32_t)( req->to - req->from ); iov[0].iov_base = &outReply; iov[0].iov_len = sizeof outReply; iov[1].iov_base = link->recvBuffer + (req->from - start); diff --git a/src/shared/fdsignal.c b/src/shared/fdsignal.c index 5e81284..5e5cf7f 100644 --- a/src/shared/fdsignal.c +++ b/src/shared/fdsignal.c @@ -7,6 +7,7 @@ //#warning "Using pointer-packing pipe based signalling" #include "fdsignal.inc/pipe64.c" #else +_Static_assert( sizeof(int) != 4 || sizeof(void*) != 8, "Something's goofy, fix preprocessor check above!" ); //#warning "Using fallback pipe based signalling" #include "fdsignal.inc/pipe_malloc.c" #endif diff --git a/src/shared/log.c b/src/shared/log.c index afafc6f..0b385c5 100644 --- a/src/shared/log.c +++ b/src/shared/log.c @@ -89,26 +89,26 @@ void logadd(const logmask_t mask, const char *fmt, ...) ret = vsnprintf( buffer + offset, LINE_LEN - offset, fmt, ap ); va_end( ap ); if ( ret < 0 ) return; - ret += offset; - if ( ret + 1 >= LINE_LEN ) { + offset += ret; + if ( offset + 1 >= LINE_LEN ) { buffer[LINE_LEN-2] = '\0'; - ret = LINE_LEN - 2; + offset = LINE_LEN - 2; } - if ( ret > 0 && buffer[ret-1] != '\n' ) { - buffer[ret++] = '\n'; - buffer[ret] = '\0'; + if ( buffer[offset-1] != '\n' ) { + buffer[offset++] = '\n'; + buffer[offset] = '\0'; } if ( maskFile & mask ) { pthread_mutex_lock( &logLock ); if ( logFd >= 0 ) { - int done = 0; - while (done < ret ) { - const int wr = write( logFd, buffer + done, ret - done ); + size_t done = 0; + while (done < offset ) { + const ssize_t wr = write( logFd, buffer + done, ret - done ); if ( wr < 0 ) { printf( "Logging to file failed! (errno=%d)\n", errno ); break; } - done += wr; + done += (size_t)wr; } } pthread_mutex_unlock( &logLock ); diff --git a/src/shared/protocol.h b/src/shared/protocol.h index c3ccbc1..561e5a1 100644 --- a/src/shared/protocol.h +++ b/src/shared/protocol.h @@ -26,7 +26,7 @@ static inline int dnbd3_read_reply(int sock, dnbd3_reply_t *reply, bool wait) { - int ret = recv( sock, reply, sizeof(*reply), (wait ? MSG_WAITALL : MSG_DONTWAIT) | MSG_NOSIGNAL ); + ssize_t ret = recv( sock, reply, sizeof(*reply), (wait ? MSG_WAITALL : MSG_DONTWAIT) | MSG_NOSIGNAL ); if ( ret == 0 ) return REPLY_CLOSED; if ( ret < 0 ) { if ( errno == EAGAIN || errno == EWOULDBLOCK ) return REPLY_AGAIN; @@ -62,7 +62,7 @@ static inline bool dnbd3_select_image(int sock, const char *name, uint16_t rid, const ssize_t len = serializer_get_written_length( &serialized ); request.magic = dnbd3_packet_magic; request.cmd = CMD_SELECT_IMAGE; - request.size = len; + request.size = (uint32_t)len; #ifdef _DEBUG request.handle = 0; request.offset = 0; diff --git a/src/shared/sockhelper.c b/src/shared/sockhelper.c index e511f0f..118fbec 100644 --- a/src/shared/sockhelper.c +++ b/src/shared/sockhelper.c @@ -160,37 +160,39 @@ void sock_destroyPollList(poll_list_t *list) free( list ); } -int sock_printHost(const dnbd3_host_t * const host, char * const buffer, const int len) +size_t sock_printHost(const dnbd3_host_t * const host, char * const buffer, const size_t len) { // Worst case: Port 5 chars, ':' to separate ip and port 1 char, terminating null 1 char = 7, [] for IPv6 if ( len < 10 ) return 0; char *output = buffer; if ( host->type == AF_INET6 ) { *output++ = '['; - inet_ntop( AF_INET6, host->addr, output, len - 10 ); + inet_ntop( AF_INET6, host->addr, output, (socklen_t)( len - 10 ) ); output += strlen( output ); *output++ = ']'; } else if ( host->type == AF_INET ) { - inet_ntop( AF_INET, host->addr, output, len - 8 ); + inet_ntop( AF_INET, host->addr, output, (socklen_t)( len - 8 ) ); output += strlen( output ); } else { int ret = snprintf( output, len, "", (int)host->type ); - return MIN( ret, len-1 ); + if ( ret <= 0 ) return 0; + return MIN( (size_t)ret, len-1 ); } *output = '\0'; if ( host->port != 0 ) { // There are still at least 7 bytes left in the buffer, port is at most 5 bytes + ':' + '\0' = 7 int ret = snprintf( output, 7, ":%d", (int)ntohs( host->port ) ); + if ( ret < 0 ) ret = 0; output += MIN( ret, 6 ); } return output - buffer; } -int sock_printable(struct sockaddr *addr, socklen_t addrLen, char *output, int len) +size_t sock_printable(const struct sockaddr * const addr, const socklen_t addrLen, char *output, const size_t len) { char host[100], port[10]; int outlen = 0; - int ret = getnameinfo( addr, addrLen, host, 100, port, 10, NI_NUMERICHOST | NI_NUMERICSERV ); + int ret = getnameinfo( addr, addrLen, host, sizeof(host), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV ); if ( ret == 0 ) { if ( addr->sa_family == AF_INET ) { outlen = snprintf( output, len, "%s:%s", host, port ); @@ -198,7 +200,8 @@ int sock_printable(struct sockaddr *addr, socklen_t addrLen, char *output, int l outlen = snprintf( output, len, "[%s]:%s", host, port ); } } - return MIN( outlen, len-1 ); + if ( outlen <= 0 ) return 0; + return MIN( (size_t)outlen, len-1 ); } bool sock_listen(poll_list_t* list, char* bind_addr, uint16_t port) @@ -289,7 +292,7 @@ bool sock_append(poll_list_t *list, const int sock, bool wantRead, bool wantWrit { if ( sock == -1 || list->count >= MAXLISTEN ) return false; list->entry[list->count++].fd = sock; - list->entry[list->count++].events = ( wantRead ? POLLIN : 0 ) | ( wantWrite ? POLLOUT : 0 ) | POLLRDHUP; + list->entry[list->count++].events = (short)( ( wantRead ? POLLIN : 0 ) | ( wantWrite ? POLLOUT : 0 ) | POLLRDHUP ); list->count++; return true; } diff --git a/src/shared/sockhelper.h b/src/shared/sockhelper.h index 62603c5..abfcb9c 100644 --- a/src/shared/sockhelper.h +++ b/src/shared/sockhelper.h @@ -31,9 +31,9 @@ int sock_resolveToDnbd3Host(const char * const address, dnbd3_host_t * const des void sock_setTimeout(const int sockfd, const int milliseconds); -int sock_printHost(const dnbd3_host_t * const host, char *output, const int len); +size_t sock_printHost(const dnbd3_host_t * const host, char *output, const size_t len); -int sock_printable(struct sockaddr *addr, socklen_t addrLen, char *output, int len); +size_t sock_printable(const struct sockaddr * const addr, const socklen_t addrLen, char *output, const size_t len); /** * Create new poll list. -- cgit v1.2.3-55-g7522