client.c
来自「非常好的dns解析软件」· C语言 代码 · 共 2,439 行 · 第 1/5 页
C
2,439 行
} client->message->rcode = dns_rcode_noerror; /* RFC1123 section 6.1.3.2 */ if ((client->attributes & NS_CLIENTATTR_MULTICAST) != 0) client->message->flags &= ~DNS_MESSAGEFLAG_RD; /* * Deal with EDNS. */ opt = dns_message_getopt(client->message); if (opt != NULL) { /* * Set the client's UDP buffer size. */ client->udpsize = opt->rdclass; /* * If the requested UDP buffer size is less than 512, * ignore it and use 512. */ if (client->udpsize < 512) client->udpsize = 512; /* * Get the flags out of the OPT record. */ client->extflags = (isc_uint16_t)(opt->ttl & 0xFFFF); /* * Do we understand this version of EDNS? * * XXXRTH need library support for this! */ client->ednsversion = (opt->ttl & 0x00FF0000) >> 16; if (client->ednsversion > 0) { result = client_addopt(client); if (result == ISC_R_SUCCESS) result = DNS_R_BADVERS; ns_client_error(client, result); goto cleanup; } /* * Create an OPT for our reply. */ result = client_addopt(client); if (result != ISC_R_SUCCESS) { ns_client_error(client, result); goto cleanup; } } if (client->message->rdclass == 0) { ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1), "message class could not be determined"); ns_client_dumpmessage(client, "message class could not be determined"); ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_FORMERR); goto cleanup; } /* * Determine the destination address. If the receiving interface is * bound to a specific address, we simply use it regardless of the * address family. All IPv4 queries should fall into this case. * Otherwise, if this is a TCP query, get the address from the * receiving socket (this needs a system call and can be heavy). * For IPv6 UDP queries, we get this from the pktinfo structure (if * supported). * If all the attempts fail (this can happen due to memory shortage, * etc), we regard this as an error for safety. */ if ((client->interface->flags & NS_INTERFACEFLAG_ANYADDR) == 0) isc_netaddr_fromsockaddr(&destaddr, &client->interface->addr); else { result = ISC_R_FAILURE; if (TCP_CLIENT(client)) { isc_sockaddr_t destsockaddr; result = isc_socket_getsockname(client->tcpsocket, &destsockaddr); if (result == ISC_R_SUCCESS) isc_netaddr_fromsockaddr(&destaddr, &destsockaddr); } if (result != ISC_R_SUCCESS && client->interface->addr.type.sa.sa_family == AF_INET6 && (client->attributes & NS_CLIENTATTR_PKTINFO) != 0) { isc_uint32_t zone = 0; /* * XXXJT technically, we should convert the receiving * interface ID to a proper scope zone ID. However, * due to the fact there is no standard API for this, * we only handle link-local addresses and use the * interface index as link ID. Despite the assumption, * it should cover most typical cases. */ if (IN6_IS_ADDR_LINKLOCAL(&client->pktinfo.ipi6_addr)) zone = (isc_uint32_t)client->pktinfo.ipi6_ifindex; isc_netaddr_fromin6(&destaddr, &client->pktinfo.ipi6_addr); isc_netaddr_setzone(&destaddr, zone); result = ISC_R_SUCCESS; } if (result != ISC_R_SUCCESS) { UNEXPECTED_ERROR(__FILE__, __LINE__, "failed to get request's " "destination: %s", isc_result_totext(result)); goto cleanup; } } /* * Find a view that matches the client's source address. */ for (view = ISC_LIST_HEAD(ns_g_server->viewlist); view != NULL; view = ISC_LIST_NEXT(view, link)) { if (client->message->rdclass == view->rdclass || client->message->rdclass == dns_rdataclass_any) { dns_name_t *tsig = NULL; sigresult = dns_message_rechecksig(client->message, view); if (sigresult == ISC_R_SUCCESS) tsig = client->message->tsigname; if (allowed(&netaddr, tsig, view->matchclients) && allowed(&destaddr, tsig, view->matchdestinations) && !((client->message->flags & DNS_MESSAGEFLAG_RD) == 0 && view->matchrecursiveonly)) { dns_view_attach(view, &client->view); break; } } } if (view == NULL) { char classname[DNS_RDATACLASS_FORMATSIZE]; /* * Do a dummy TSIG verification attempt so that the * response will have a TSIG if the query did, as * required by RFC2845. */ isc_buffer_t b; isc_region_t *r; dns_message_resetsig(client->message); r = dns_message_getrawmessage(client->message); isc_buffer_init(&b, r->base, r->length); isc_buffer_add(&b, r->length); (void)dns_tsig_verify(&b, client->message, NULL, NULL); dns_rdataclass_format(client->message->rdclass, classname, sizeof(classname)); ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(1), "no matching view in class '%s'", classname); ns_client_dumpmessage(client, "no matching view in class"); ns_client_error(client, notimp ? DNS_R_NOTIMP : DNS_R_REFUSED); goto cleanup; } ns_client_log(client, NS_LOGCATEGORY_CLIENT, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(5), "using view '%s'", view->name); /* * Check for a signature. We log bad signatures regardless of * whether they ultimately cause the request to be rejected or * not. We do not log the lack of a signature unless we are * debugging. */ client->signer = NULL; dns_name_init(&client->signername, NULL); result = dns_message_signer(client->message, &client->signername); if (result == ISC_R_SUCCESS) { ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3), "request has valid signature"); client->signer = &client->signername; } else if (result == ISC_R_NOTFOUND) { ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3), "request is not signed"); } else if (result == DNS_R_NOIDENTITY) { ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3), "request is signed by a nonauthoritative key"); } else { char tsigrcode[64]; isc_buffer_t b; dns_name_t *name = NULL; isc_buffer_init(&b, tsigrcode, sizeof(tsigrcode) - 1); RUNTIME_CHECK(dns_tsigrcode_totext(client->message->tsigstatus, &b) == ISC_R_SUCCESS); tsigrcode[isc_buffer_usedlength(&b)] = '\0'; /* There is a signature, but it is bad. */ if (dns_message_gettsig(client->message, &name) != NULL) { char namebuf[DNS_NAME_FORMATSIZE]; dns_name_format(name, namebuf, sizeof(namebuf)); ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_ERROR, "request has invalid signature: " "TSIG %s: %s (%s)", namebuf, isc_result_totext(result), tsigrcode); } else { ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_ERROR, "request has invalid signature: %s (%s)", isc_result_totext(result), tsigrcode); } /* * Accept update messages signed by unknown keys so that * update forwarding works transparently through slaves * that don't have all the same keys as the master. */ if (!(client->message->tsigstatus == dns_tsigerror_badkey && client->message->opcode == dns_opcode_update)) { ns_client_error(client, sigresult); goto cleanup; } } /* * Decide whether recursive service is available to this client. * We do this here rather than in the query code so that we can * set the RA bit correctly on all kinds of responses, not just * responses to ordinary queries. Note if you can't query the * cache there is no point in setting RA. */ ra = ISC_FALSE; if (client->view->resolver != NULL && client->view->recursion == ISC_TRUE && ns_client_checkaclsilent(client, client->view->recursionacl, ISC_TRUE) == ISC_R_SUCCESS && ns_client_checkaclsilent(client, client->view->queryacl, ISC_TRUE) == ISC_R_SUCCESS) ra = ISC_TRUE; if (ra == ISC_TRUE) client->attributes |= NS_CLIENTATTR_RA; ns_client_log(client, DNS_LOGCATEGORY_SECURITY, NS_LOGMODULE_CLIENT, ISC_LOG_DEBUG(3), ra ? "recursion available" : "recursion not available"); /* * Adjust maximum UDP response size for this client. */ if (client->udpsize > 512) { dns_peer_t *peer = NULL; isc_uint16_t udpsize = view->maxudp; (void) dns_peerlist_peerbyaddr(view->peers, &netaddr, &peer); if (peer != NULL) dns_peer_getmaxudp(peer, &udpsize); if (client->udpsize > udpsize) client->udpsize = udpsize; } /* * Dispatch the request. */ switch (client->message->opcode) { case dns_opcode_query: CTRACE("query"); ns_query_start(client); break; case dns_opcode_update: CTRACE("update"); ns_client_settimeout(client, 60); ns_update_start(client, sigresult); break; case dns_opcode_notify: CTRACE("notify"); ns_client_settimeout(client, 60); ns_notify_start(client); break; case dns_opcode_iquery: CTRACE("iquery"); ns_client_error(client, DNS_R_NOTIMP); break; default: CTRACE("unknown opcode"); ns_client_error(client, DNS_R_NOTIMP); } cleanup: return;}static voidclient_timeout(isc_task_t *task, isc_event_t *event) { ns_client_t *client; REQUIRE(event != NULL); REQUIRE(event->ev_type == ISC_TIMEREVENT_LIFE || event->ev_type == ISC_TIMEREVENT_IDLE); client = event->ev_arg; REQUIRE(NS_CLIENT_VALID(client)); REQUIRE(task == client->task); REQUIRE(client->timer != NULL); UNUSED(task); CTRACE("timeout"); isc_event_free(&event); if (client->shutdown != NULL) { (client->shutdown)(client->shutdown_arg, ISC_R_TIMEDOUT); client->shutdown = NULL; client->shutdown_arg = NULL; } if (client->newstate > NS_CLIENTSTATE_READY) client->newstate = NS_CLIENTSTATE_READY; (void)exit_check(client);}static isc_result_tget_clientmctx(ns_clientmgr_t *manager, isc_mem_t **mctxp) { isc_mem_t *clientmctx;#if NMCTXS > 0 isc_result_t result;#endif /* * Caller must be holding the manager lock. */#if NMCTXS > 0 INSIST(manager->nextmctx < NMCTXS); clientmctx = manager->mctxpool[manager->nextmctx]; if (clientmctx == NULL) { result = isc_mem_create(0, 0, &clientmctx); if (result != ISC_R_SUCCESS) return (result); manager->mctxpool[manager->nextmctx] = clientmctx; manager->nextmctx++; if (manager->nextmctx == NMCTXS) manager->nextmctx = 0; }#else clientmctx = manager->mctx;#endif isc_mem_attach(clientmctx, mctxp); return (ISC_R_SUCCESS);}static isc_result_tclient_create(ns_clientmgr_t *manager, ns_client_t **clientp) { ns_client_t *client; isc_result_t result; isc_mem_t *mctx = NULL; /* * Caller must be holding the manager lock. * * Note: creating a client does not add the client to the * manager's client list or set the client's manager pointer. * The caller is responsible for that. */ REQUIRE(clientp != NULL && *clientp == NULL); result = get_clientmctx(manager, &mctx); if (result != ISC_R_SUCCESS) return (result); client = isc_mem_get(mctx, sizeof(*client)); if (client == NULL) { isc_mem_detach(&mctx); return (ISC_R_NOMEMORY); } client->mctx = mctx; client->task = NULL; result = isc_task_create(manager->taskmgr, 0, &client->task); if (result != ISC_R_SUCCESS) goto cleanup_client; isc_task_setname(client->task, "client", client); client->timer = NULL; result = isc_timer_create(manager->timermgr, isc_timertype_inactive, NULL, NULL, client->task, client_timeout, client, &client->timer); if (result != ISC_R_SUCCESS) goto cleanup_task; client->timerset = ISC_FALSE; client->message = NULL; result = dns_message_create(client->mctx, DNS_MESSAGE_INTENTPARSE, &client->message); if (result != ISC_R_SUCCESS) goto cleanup_timer; /* XXXRTH Hardwired constants */ client->sendevent = (isc_socketevent_t *) isc_event_allocate(client->mctx, client, ISC_SOCKEVENT_SENDDONE, client_senddone, client, sizeof(isc_socketevent_t)); if (client->sendevent == NULL) { result = ISC_R_NOMEMORY; goto cleanup_message; } client->recvbuf = isc_mem_get(client->mctx, RECV_BUFFER_SIZE); if (client->recvbuf == NULL) { result = ISC_R_NOMEMORY; goto cleanup_sendevent; } client->recvevent = (isc_socketevent_t *) isc_event_allocate(client->mctx, client, ISC_SOCKEVENT_RECVDONE, client_request, client, sizeof(isc_socketevent_t)); if (client->recvevent == NULL) { result = ISC_R_NOMEMORY; goto cleanup_recvbuf; } client->magic = NS_CLIENT_MAGIC; client->manager = NULL; client->state = NS_CLIENTSTATE_INACTIVE; client->newstate = NS_CLIENTSTATE_MAX; client->naccepts = 0; client->nreads = 0; client->nsends = 0; client->nrecvs = 0; client->nupdates = 0; client->nctls = 0; client->references = 0; client->attributes = 0; client->view = NULL; client->dispatch = NULL; client->udpsocket = NULL; client->tcplistener = NULL; client->tcpsocket = NULL; client->tcpmsg_valid = ISC_FALSE; client->tcpbuf = NULL; client->opt = NULL; client->udpsize = 512; client->extflags = 0; client->ednsversion = -1; client->next = NULL; client->shutdown = NULL; client->shutdown_arg = NULL; dns_name_init(&client->signername, NULL); client->mortal = ISC_FALSE; client->tcpquota = NULL; client->recursionquota = NULL; client->interface = NULL; client->peeraddr_valid = ISC_FALSE; ISC_EVENT_INIT(&client->ctlevent, sizeof(client->ctlevent), 0, NULL, NS_EVENT_CLIENTCONTROL, client_start, client, client, NULL, NULL); /* * Initialize FORMERR cache to sentinel value that will not match * any actual FORMERR response. */ isc_sockaddr_any(&client->formerrcache.addr); client->formerrcache.time = 0; client->formerrcache.id = 0; ISC_LINK_INIT(client, link); client->list = NULL; /* * We call the init routines for the various kinds of client here, * after we have created an otherwise valid client, because some * of them call routines that REQUIRE(NS_CLIENT_VALID(client)). */ result = ns_query_init(client);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?