我对 LinkedLists 的经验很少,并且无法弄清楚测试字符串是否在其中一个节点中的逻辑。整个程序正在等待客户端发送 DNS 查询,然后在无限循环中发送回响应。我想做的是:
确定 LinkedList 是否具有客户端请求的主机名。如果不存在,请将其添加到 LinkedList 并在执行查找后将答案保存到同一节点。如果它在那里,只需给客户我已经查找并存储在answer[]
.
这是一段简化的代码:
struct queryCache {
char* hostName;
uint8_t answer[UDP_RECV_SIZE];
struct queryCache* next;
};
struct queryCache* qcRoot;
int main (int argc, char** argv) {
// ...unrelated code
qcRoot = malloc(sizeof(struct queryCache));
qcRoot->hostName = 0;
qcRoot->next = 0;
while (1) {
// Wait for client with recvfrom()
char* cqHostName;
// Code that malloc()s and strcpy()s the client hostname into cqHostName
// Determine if cqHostName is in the cache
int hostNameInCache = 0;
struct queryCache* currQC = qcRoot;
while (currQC) {
if (!strcmp(currQC->hostName, cqHostName)) {
puts("In the cache");
hostNameInCache = 1;
break;
}
currQC = currQC->next;
}
// If cqHostName is not in the cache add its name
if (!hostNameInCache) {
currQC->hostName = malloc(strlen(cqHostName)+1);
strcpy(currQC->hostName, cqHostName);
printf("Added HOSTNAME: %s to the cache\n", cqHostName);
currQC->next = malloc(sizeof(struct queryCache));
currQC = currQC->next;
currQC->hostName = 0;
currQC->next = 0;
}
// Code that does a recursive DNS
// Code that will copy the response into the appropriate answer[] of the LinkedList
}
}
该程序似乎只是在第一个客户端请求后退出而没有给出错误。如果我删除 LinkedList 代码,它工作得很好,所以我很确定出了什么问题与我如何检查字符串是否在 LinkedList 中有关。