我想知道如何安全地将一个未签名的char*数组写入fifo。我尝试过将数组转换为char*,但是我不认为内容是安全的,因为我得到的结果是不正确的。数组是一个花过滤器。
我要做的是按照原样将bloomfilter从子进程传输到父进程,这样父进程就可以具有相同的bloomfilter。
(来自Wiki: Bloom过滤器是一种空间高效的概率数据结构,用于测试一个元素是否是一个集合的成员。错误的阳性匹配是可能的,但假否定是不可能的-换句话说,查询返回“可能在集合”或“肯定不在集合”)。
子进程有一个列表,比如病毒列表,每个节点都有一个bloomfilter。父进程为分叉的每个子进程都有一个类,用于在每个病毒节点中存储花过滤器。
以下是我到目前为止在子进程中的代码:
void send_bloomfilters(int write_fd, int buf_size)
{
char buffer[5000]; // buffer used for concatination of bloomfilters
char temp[200];
VirusNode* current = virus_list.head;
strcpy(buffer, (char *)¤t->bf[0]); // coping the bloomfilter to buffer
while(current != NULL) // sending all bloomfilter in one buffer like so: bf1#bf2#bf3#...
{
sprintf(temp, "#%s", (char *)¤t->bf[0]);
strcat(buffer,temp);
current = current->next;
}
send_message(write_fd, buffer, buf_size); //finally sending the buffer to parent
}
// Sends <message> to file descriptor <fd> per <buf_size> characters
void send_message(int fd, char *message, int buf_size)
{
// writing the length of the message to be received
int length = strlen(message);
char buffer[10];
memset(buffer, 0, 10);
sprintf(buffer, "%d@", length);
write(fd, buffer, 9); // sending the number of bytes reader is about to read
int buffer_size = buf_size;
char * input_write = new char[length + 1];
strcpy(input_write,message); // coping the message we want to send
char * str = input_write; // pointer to the array
int bytes_written = 0, total_bytes = 0; // We might need to write less or more bytes
buffer_size = length < buffer_size ? length : buffer_size; // than <buf_size>
while(total_bytes < length)
{
str += bytes_written; // move str pointer
bytes_written = write(fd, str, buffer_size); //and write the next <buffer_size> characters
total_bytes += bytes_written; // adding them to the total amount of bytes written altogether
if((total_bytes + buffer_size) > length)
buffer_size = length - total_bytes; // reading exactly the amount that's left
}
delete [] input_write;
}下面是父进程的代码:
void receive_bloomfilters(Children *children_info, int num_children, int buf_size)
{
struct pollfd filedescs[num_children];
char * buf;
char * bloomfilter;
// while there are still monitors left to send a message
while(children_sent_remain(m_info, num_children)) //function checks how many children are left to read from
{
for (int i = 0; i < num_children; i++)
{
filedescs[i].fd = children_info[i].read_fd;
filedescs[i].events = POLLIN;
}
if(poll(filedescs,num_children,10) < 0 ) printf("poll blocked\n");
for (int i = 0; i < num_children; i++)
{
if((filedescs[i].revents & POLLIN))
{
if(filedescs[i].fd == children_info[i].read_fd)
{
buf = read_message(children_info[i].read_fd, buf_size);
VirusNode* current = m_info[i].virus_list->head;
bloomfilter = strtok (buf,"#");
while (bloomfilter != NULL && current != NULL)
{
current->InsertBloom(bloomfilter);
current = current->next;
bloomfilter = strtok (NULL, "#");
}
delete [] buf;
m_info[i].not_sent_yet = false; // flag=false now that we read the message from <i> child
}
}
}
}
}
void VirusNode::InsertBloom(char* bloomfilter) {
monitor_bloom->bf = (unsigned char*)bloomfilter;
}
// Reads a message from <fd> and returns it.
char *read_message(int read_end_fd, int buf_size)
{
char buffer[10];
int fifo_buffer_size = buf_size;
read(read_end_fd, buffer, 9);
char * tok = strtok(buffer, "@");
int length = atoi(tok); // how many characters will be received
char * input_read = new char[length + 1];
char * str = input_read;
int bytes_read = 0, total_bytes = 0; // We might need to read less or more bytes
fifo_buffer_size = length < fifo_buffer_size ? length : fifo_buffer_size; // than <buf_size>
while(total_bytes < length)
{
str += bytes_read; // move str pointer
bytes_read = read(read_end_fd, str, fifo_buffer_size); //and read the next <buf_size> characters
total_bytes += bytes_read; // adding them to the total amount of bytes read altogether
if((total_bytes + fifo_buffer_size) > length)
fifo_buffer_size = length - total_bytes; // reading exactly the amount that's left
}
input_read[length] = '\0';
return input_read;
}我是一个乞丐,学习编码,所以任何帮助都会很感激。我做错什么了吗?
发布于 2021-05-10 16:24:25
您正在使用str-函数来操作字节缓冲区。他们假设字符串是0结束的,所以如果过滤器中有0字节,就不能工作。
转到memcpy,realloc。或者更好一些--使用vectors。
https://stackoverflow.com/questions/67473641
复制相似问题