开发者

TFTP server written in C - Binary diff at the client end shows a difference but not always

开发者 https://www.devze.com 2023-04-10 07:03 出处:网络
I have written a tftp server in C . The tftp client I am using is the native linux client . The file transfer seems to happen correctly . The sent and received file sizes appear to be the same . But i

I have written a tftp server in C . The tftp client I am using is the native linux client . The file transfer seems to happen correctly . The sent and received file sizes appear to be the same . But if I do "cmp" on both the files there is a difference . I have attached the function which sends "512 bytes" of data or less to the client. I am not sure if there is any padding being introduced but I don't know where that might happen .

void send_next_data (int client_id)
{
int opcode = 0;
int block_id = 0;
unsigned long bytes_left = 0;
unsigned long offset = 0;
unsigned long file_length = 0;
char *ptr = buffer_send;
int total_bytes = 0;
int result;
int i;


opcode = 3;
client_list[client_id].prev_blockid += 1;
block_id = client_list[client_id].prev_blockid;

opcode = htons(opcode);
block_id = htons(block_id);
bzero(buffer_send,520);

memcpy(ptr, &opcode, sizeof(uint16_t));
ptr += sizeof(uint16_t);

memcpy(ptr, &block_id, sizeof(uint16_t));
ptr += sizeof(uint16_t);

total_bytes += 4;

offset = client_list[client_id].offset;
file_length =  client_list[client_id].file_length;

if(offset < file_length)
{
    bytes_left = file_length - offset;
    if(bytes_left > 512)
    {
        memcpy(ptr, &client_list[client_id].file[offset], 512);
        ptr += 512;
        total_bytes += 512;
        client_list[client_id].offset += 512;
    }
    else
    {
        memcpy(ptr, &client_list[client_id].file[offset], bytes_left);
        ptr += bytes_left;
        total_bytes += bytes_left;
        client_list[client_id].offset += bytes_left;
    }
}


result = sendto(socket_list[client_id], buffer_send, total_bytes, 0 , (struct sockaddr *)&client_list[client_id].clientAddr, client_list[client_id].clientAddrLen);
printf(" total_bytes sent = %d, result = %d , client id = %d, send on fd = %d\n", total_bytes, result, client_id, socket_list[client_id]);
    //  printf(" Error = %d\n", errno);
}

I copy the data from the binary file onto a buffer and then use offsets on this buffer to transfer 512 or less bytes of data . To test if the file has been copied to the buffer properly ,I wrote this buffer back to a "test" file and a cmp between the source and this "test" file says there is no difference.

fseek(fp, 0L, SEEK_END);
client_list[n].file_length = ftell(fp);
fseek(fp, 0L, SEEK_SET);

client_list[n].file = malloc((client_list[n].file_length+100) * sizeof(char));

bzero(client_list[n].file, client_list[n].file_length+100);
i=0;
    c = getc(fp) ;
while (!(feof(fp)))
{
   client_list[n].file[i] = c;
   c = getc(fp);
   i++;
}

printf(" Number of bytes read from file = %ld , size of file = %ld \n", i, client_list[n].file_length);

send_next_data(n);

fclose(fp);

fp = fopen("test", "wb");
fwrite(client_list[n].file, 1,client_list[n].file_length ,fp);
fclose(fp);

I generate binary files using time dd if=/dev/urandom of=random-file bs=1 count=xxxx and send it . For a file size of 779开发者_JAVA技巧40 bytes , cmp says that the first diff appears at byte 44393 . Any help would be appreciated .

0

精彩评论

暂无评论...
验证码 换一张
取 消