diff options
Diffstat (limited to 'src/charon/threads')
-rw-r--r-- | src/charon/threads/kernel_interface.c | 146 | ||||
-rw-r--r-- | src/charon/threads/receiver.c | 29 | ||||
-rw-r--r-- | src/charon/threads/scheduler.c | 26 | ||||
-rw-r--r-- | src/charon/threads/sender.c | 22 | ||||
-rwxr-xr-x | src/charon/threads/stroke_interface.c | 528 | ||||
-rw-r--r-- | src/charon/threads/stroke_interface.h | 15 | ||||
-rw-r--r-- | src/charon/threads/thread_pool.c | 67 | ||||
-rw-r--r-- | src/charon/threads/thread_pool.h | 13 |
8 files changed, 426 insertions, 420 deletions
diff --git a/src/charon/threads/kernel_interface.c b/src/charon/threads/kernel_interface.c index c74cf8f27..5c220b0c8 100644 --- a/src/charon/threads/kernel_interface.c +++ b/src/charon/threads/kernel_interface.c @@ -230,11 +230,6 @@ struct private_kernel_interface_t { * Condvar allows signaling of threads waiting for a reply. */ pthread_cond_t condvar; - - /** - * Logger for XFRM stuff - */ - logger_t *logger; }; @@ -362,15 +357,12 @@ static void receive_messages(private_kernel_interface_t *this) } if (reqid == 0) { - this->logger->log(this->logger, ERROR, - "Received a XFRM_MSG_ACQUIRE, but no reqid found"); + DBG1(SIG_DBG_KNL, "received a XFRM_MSG_ACQUIRE, but no reqid found"); } else { - this->logger->log(this->logger, CONTROL|LEVEL1, - "Received a XFRM_MSG_ACQUIRE"); - this->logger->log(this->logger, CONTROL, - "creating acquire job for CHILD_SA with reqid %d", + DBG2(SIG_DBG_KNL, "received a XFRM_MSG_ACQUIRE"); + DBG1(SIG_DBG_KNL, "creating acquire job for CHILD_SA with reqid %d", reqid); job = (job_t*)acquire_job_create(reqid); charon->job_queue->add(charon->job_queue, job); @@ -389,12 +381,10 @@ static void receive_messages(private_kernel_interface_t *this) spi = expire->state.id.spi; reqid = expire->state.reqid; - this->logger->log(this->logger, CONTROL|LEVEL1, - "Received a XFRM_MSG_EXPIRE"); - this->logger->log(this->logger, CONTROL, - "creating %s job for %s CHILD_SA 0x%x (reqid %d)", + DBG2(SIG_DBG_KNL, "received a XFRM_MSG_EXPIRE"); + DBG1(SIG_DBG_KNL, "creating %s job for %N CHILD_SA 0x%x (reqid %d)", expire->hard ? "delete" : "rekey", - mapping_find(protocol_id_m, protocol), ntohl(spi), + protocol_id_names, protocol, ntohl(spi), reqid); if (expire->hard) { @@ -457,7 +447,7 @@ static status_t get_spi(private_kernel_interface_t *this, memset(&request, 0, sizeof(request)); status_t status = SUCCESS; - this->logger->log(this->logger, CONTROL|LEVEL2, "getting spi"); + DBG2(SIG_DBG_KNL, "getting spi"); hdr = (struct nlmsghdr*)request; hdr->nlmsg_flags = NLM_F_REQUEST; @@ -476,29 +466,29 @@ static status_t get_spi(private_kernel_interface_t *this, if (send_message(this, hdr, &response) != SUCCESS) { - this->logger->log(this->logger, ERROR, "netlink communication failed"); + DBG1(SIG_DBG_KNL, "netlink communication failed"); return FAILED; } else if (response->nlmsg_type == NLMSG_ERROR) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_ALLOCSPI got an error: %s", + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_ALLOCSPI got an error: %s", strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); status = FAILED; } else if (response->nlmsg_type != XFRM_MSG_NEWSA) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_ALLOCSPI got a unknown reply"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_ALLOCSPI got a unknown reply"); status = FAILED; } else if (response->nlmsg_len < NLMSG_LENGTH(sizeof(struct xfrm_usersa_info))) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_ALLOCSPI got an invalid reply"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_ALLOCSPI got an invalid reply"); status = FAILED; } else { *spi = ((struct xfrm_usersa_info*)NLMSG_DATA(response))->id.spi; - this->logger->log(this->logger, CONTROL|LEVEL1, "SPI is 0x%x", *spi); + DBG2(SIG_DBG_KNL, "SPI is 0x%x", *spi); } free(response); @@ -526,7 +516,7 @@ static status_t add_sa(private_kernel_interface_t *this, memset(&request, 0, sizeof(request)); status_t status = SUCCESS; - this->logger->log(this->logger, CONTROL|LEVEL2, "adding SA"); + DBG2(SIG_DBG_KNL, "adding SA"); hdr = (struct nlmsghdr*)request; hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; @@ -561,12 +551,12 @@ static status_t add_sa(private_kernel_interface_t *this, alg_name = lookup_algorithm(encryption_algs, enc_alg, &key_size); if (alg_name == NULL) { - this->logger->log(this->logger, ERROR, "Algorithm %s not supported by kernel!", - mapping_find(encryption_algorithm_m, enc_alg->algorithm)); + DBG1(SIG_DBG_KNL, "algorithm %N not supported by kernel!", + encryption_algorithm_names, enc_alg->algorithm); return FAILED; } - this->logger->log(this->logger, CONTROL|LEVEL2, " using encryption algorithm %s with key size %d", - mapping_find(encryption_algorithm_m, enc_alg->algorithm), key_size); + DBG2(SIG_DBG_KNL, " using encryption algorithm %N with key size %d", + encryption_algorithm_names, enc_alg->algorithm, key_size); rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size); hdr->nlmsg_len += rthdr->rta_len; @@ -589,12 +579,12 @@ static status_t add_sa(private_kernel_interface_t *this, alg_name = lookup_algorithm(integrity_algs, int_alg, &key_size); if (alg_name == NULL) { - this->logger->log(this->logger, ERROR, "Algorithm %s not supported by kernel!", - mapping_find(integrity_algorithm_m, int_alg->algorithm)); + DBG1(SIG_DBG_KNL, "algorithm %N not supported by kernel!", + integrity_algorithm_names, int_alg->algorithm); return FAILED; } - this->logger->log(this->logger, CONTROL|LEVEL2, " using integrity algorithm %s with key size %d", - mapping_find(integrity_algorithm_m, int_alg->algorithm), key_size); + DBG2(SIG_DBG_KNL, " using integrity algorithm %N with key size %d", + integrity_algorithm_names, int_alg->algorithm, key_size); rthdr->rta_len = RTA_LENGTH(sizeof(struct xfrm_algo) + key_size); hdr->nlmsg_len += rthdr->rta_len; @@ -644,18 +634,18 @@ static status_t add_sa(private_kernel_interface_t *this, if (send_message(this, hdr, &response) != SUCCESS) { - this->logger->log(this->logger, ERROR, "netlink communication failed"); + DBG1(SIG_DBG_KNL, "netlink communication failed"); return FAILED; } else if (response->nlmsg_type != NLMSG_ERROR) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_NEWSA not acknowledged"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_NEWSA not acknowledged"); status = FAILED; } else if (((struct nlmsgerr*)NLMSG_DATA(response))->error) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_NEWSA got an error: %s", - strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_NEWSA got an error: %s", + strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); status = FAILED; } @@ -681,7 +671,7 @@ static status_t update_sa( memset(&request, 0, sizeof(request)); status_t status = SUCCESS; - this->logger->log(this->logger, CONTROL|LEVEL2, "getting SA"); + DBG2(SIG_DBG_KNL, "getting SA"); hdr = (struct nlmsghdr*)request; hdr->nlmsg_flags = NLM_F_REQUEST; @@ -696,30 +686,30 @@ static status_t update_sa( if (send_message(this, hdr, &update) != SUCCESS) { - this->logger->log(this->logger, ERROR, "netlink communication failed"); + DBG1(SIG_DBG_KNL, "netlink communication failed"); return FAILED; } else if (update->nlmsg_type == NLMSG_ERROR) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETSA got an error: %s", - strerror(-((struct nlmsgerr*)NLMSG_DATA(update))->error)); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_GETSA got an error: %s", + strerror(-((struct nlmsgerr*)NLMSG_DATA(update))->error)); free(update); return FAILED; } else if (update->nlmsg_type != XFRM_MSG_NEWSA) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETSA got a unknown reply"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_GETSA got a unknown reply"); free(update); return FAILED; } else if (update->nlmsg_len < NLMSG_LENGTH(sizeof(struct xfrm_usersa_info))) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETSA got an invalid reply"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_GETSA got an invalid reply"); free(update); return FAILED; } - this->logger->log(this->logger, CONTROL|LEVEL2, "updating SA"); + DBG2(SIG_DBG_KNL, "updating SA"); update->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; update->nlmsg_type = XFRM_MSG_UPDSA; @@ -731,7 +721,7 @@ static status_t update_sa( if (dst_changes & HOST_DIFF_ADDR) { - this->logger->log(this->logger, CONTROL|LEVEL2, "destination address changed! replacing SA"); + DBG2(SIG_DBG_KNL, "destination address changed! replacing SA"); update->nlmsg_type = XFRM_MSG_NEWSA; host2xfrm(new_dst, &sa->id.daddr); @@ -756,24 +746,24 @@ static status_t update_sa( if (send_message(this, update, &response) != SUCCESS) { - this->logger->log(this->logger, ERROR, "netlink communication failed"); + DBG1(SIG_DBG_KNL, "netlink communication failed"); free(update); return FAILED; } else if (response->nlmsg_type != NLMSG_ERROR) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_XXXSA not acknowledged"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_XXXSA not acknowledged"); status = FAILED; } else if (((struct nlmsgerr*)NLMSG_DATA(response))->error) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_XXXSA got an error: %s", - strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_XXXSA got an error: %s", + strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); status = FAILED; } else if (dst_changes & HOST_DIFF_ADDR) { - this->logger->log(this->logger, CONTROL|LEVEL2, "deleting old SA"); + DBG2(SIG_DBG_KNL, "deleting old SA"); status = this->public.del_sa(&this->public, dst, spi, protocol); } @@ -794,7 +784,7 @@ static status_t query_sa(private_kernel_interface_t *this, host_t *dst, struct xfrm_usersa_id *sa_id; struct xfrm_usersa_info *sa_info; - this->logger->log(this->logger, CONTROL|LEVEL2, "querying SA"); + DBG2(SIG_DBG_KNL, "querying SA"); memset(&request, 0, sizeof(request)); hdr = (struct nlmsghdr*)request; @@ -810,18 +800,18 @@ static status_t query_sa(private_kernel_interface_t *this, host_t *dst, if (send_message(this, hdr, &response) != SUCCESS) { - this->logger->log(this->logger, ERROR, "netlink communication failed"); + DBG1(SIG_DBG_KNL, "netlink communication failed"); return FAILED; } else if (response->nlmsg_type != XFRM_MSG_NEWSA) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETSA not acknowledged"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_GETSA not acknowledged"); free(response); return FAILED; } else if (response->nlmsg_len < NLMSG_LENGTH(sizeof(struct xfrm_usersa_info))) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETSA got an invalid reply"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_GETSA got an invalid reply"); free(response); return FAILED; } @@ -847,7 +837,7 @@ static status_t del_sa(private_kernel_interface_t *this, host_t *dst, memset(&request, 0, sizeof(request)); status_t status = SUCCESS; - this->logger->log(this->logger, CONTROL|LEVEL2, "deleting SA"); + DBG2(SIG_DBG_KNL, "deleting SA"); hdr = (struct nlmsghdr*)request; hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; @@ -862,17 +852,17 @@ static status_t del_sa(private_kernel_interface_t *this, host_t *dst, if (send_message(this, hdr, &response) != SUCCESS) { - this->logger->log(this->logger, ERROR, "netlink communication failed"); + DBG1(SIG_DBG_KNL, "netlink communication failed"); return FAILED; } else if (response->nlmsg_type != NLMSG_ERROR) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_DELSA not acknowledged"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_DELSA not acknowledged"); status = FAILED; } else if (((struct nlmsgerr*)NLMSG_DATA(response))->error) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_DELSA got an error: %s", + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_DELSA got an error: %s", strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); status = FAILED; } @@ -1010,8 +1000,7 @@ static status_t add_policy(private_kernel_interface_t *this, if (!update) { current->refcount++; - this->logger->log(this->logger, CONTROL|LEVEL1, - "policy already exists, increasing refcount"); + DBG2(SIG_DBG_KNL, "policy already exists, increasing refcount"); if (!high_prio) { /* if added policy is for a ROUTED child_sa, do not @@ -1033,7 +1022,7 @@ static status_t add_policy(private_kernel_interface_t *this, policy->refcount = 1; } - this->logger->log(this->logger, CONTROL|LEVEL2, "adding policy"); + DBG2(SIG_DBG_KNL, "adding policy"); memset(&request, 0, sizeof(request)); hdr = (struct nlmsghdr*)request; @@ -1087,20 +1076,18 @@ static status_t add_policy(private_kernel_interface_t *this, if (send_message(this, hdr, &response) != SUCCESS) { - this->logger->log(this->logger, ERROR, "netlink communication failed"); + DBG1(SIG_DBG_KNL, "netlink communication failed"); return FAILED; } else if (response->nlmsg_type != NLMSG_ERROR) { - this->logger->log(this->logger, ERROR, - "netlink request XFRM_MSG_UPDPOLICY not acknowledged"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_UPDPOLICY not acknowledged"); status = FAILED; } else if (((struct nlmsgerr*)NLMSG_DATA(response))->error) { - this->logger->log(this->logger, ERROR, - "netlink request XFRM_MSG_UPDPOLICY got an error: %s", - strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_UPDPOLICY got an error: %s", + strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); status = FAILED; } @@ -1125,7 +1112,7 @@ static status_t query_policy(private_kernel_interface_t *this, memset(&request, 0, sizeof(request)); status_t status = SUCCESS; - this->logger->log(this->logger, CONTROL|LEVEL2, "querying policy"); + DBG2(SIG_DBG_KNL, "querying policy"); hdr = (struct nlmsghdr*)request; hdr->nlmsg_flags = NLM_F_REQUEST; @@ -1138,25 +1125,25 @@ static status_t query_policy(private_kernel_interface_t *this, if (send_message(this, hdr, &response) != SUCCESS) { - this->logger->log(this->logger, ERROR, "netlink communication failed"); + DBG1(SIG_DBG_KNL, "netlink communication failed"); return FAILED; } else if (response->nlmsg_type == NLMSG_ERROR) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETPOLICY got an error: %s", - strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_GETPOLICY got an error: %s", + strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); free(response); return FAILED; } else if (response->nlmsg_type != XFRM_MSG_NEWPOLICY) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETPOLICY got an unknown reply"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_GETPOLICY got an unknown reply"); free(response); return FAILED; } else if (response->nlmsg_len < NLMSG_LENGTH(sizeof(struct xfrm_userpolicy_info))) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_GETPOLICY got an invalid reply"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_GETPOLICY got an invalid reply"); free(response); return FAILED; } @@ -1185,7 +1172,7 @@ static status_t del_policy(private_kernel_interface_t *this, iterator_t *iterator; status_t status = SUCCESS; - this->logger->log(this->logger, CONTROL|LEVEL2, "deleting policy"); + DBG2(SIG_DBG_KNL, "deleting policy"); /* create a policy */ memset(&policy, 0, sizeof(kernel_policy_t)); @@ -1204,8 +1191,7 @@ static status_t del_policy(private_kernel_interface_t *this, if (--to_delete->refcount > 0) { /* is used by more SAs, keep in kernel */ - this->logger->log(this->logger, CONTROL|LEVEL1, - "is used by other SAs, not removed"); + DBG2(SIG_DBG_KNL, "is used by other SAs, not removed"); iterator->destroy(iterator); pthread_mutex_unlock(&this->pol_mutex); return SUCCESS; @@ -1219,8 +1205,7 @@ static status_t del_policy(private_kernel_interface_t *this, pthread_mutex_unlock(&this->pol_mutex); if (!to_delete) { - this->logger->log(this->logger, CONTROL|LEVEL2, - "no such policy found"); + DBG1(SIG_DBG_KNL, "no such policy found"); return NOT_FOUND; } @@ -1239,18 +1224,18 @@ static status_t del_policy(private_kernel_interface_t *this, if (send_message(this, hdr, &response) != SUCCESS) { - this->logger->log(this->logger, ERROR, "netlink communication failed"); + DBG1(SIG_DBG_KNL, "netlink communication failed"); return FAILED; } else if (response->nlmsg_type != NLMSG_ERROR) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_DELPOLICY not acknowledged"); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_DELPOLICY not acknowledged"); status = FAILED; } else if (((struct nlmsgerr*)NLMSG_DATA(response))->error) { - this->logger->log(this->logger, ERROR, "netlink request XFRM_MSG_DELPOLICY got an error: %s", - strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); + DBG1(SIG_DBG_KNL, "netlink request XFRM_MSG_DELPOLICY got an error: %s", + strerror(-((struct nlmsgerr*)NLMSG_DATA(response))->error)); status = FAILED; } @@ -1294,7 +1279,6 @@ kernel_interface_t *kernel_interface_create() this->pid = getpid(); this->responses = linked_list_create(); this->policies = linked_list_create(); - this->logger = logger_manager->get_logger(logger_manager, XFRM); pthread_mutex_init(&(this->rep_mutex),NULL); pthread_mutex_init(&(this->pol_mutex),NULL); pthread_cond_init(&(this->condvar),NULL); diff --git a/src/charon/threads/receiver.c b/src/charon/threads/receiver.c index d60792be9..c2fc315a7 100644 --- a/src/charon/threads/receiver.c +++ b/src/charon/threads/receiver.c @@ -32,7 +32,6 @@ #include <queues/job_queue.h> #include <queues/jobs/job.h> #include <queues/jobs/incoming_packet_job.h> -#include <utils/logger_manager.h> typedef struct private_receiver_t private_receiver_t; @@ -50,11 +49,6 @@ struct private_receiver_t { * Assigned thread. */ pthread_t assigned_thread; - - /** - * A logger for the receiver_t object. - */ - logger_t *logger; }; /** @@ -68,20 +62,20 @@ static void receive_packets(private_receiver_t * this) /* cancellation disabled by default */ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); - this->logger->log(this->logger, CONTROL, "receiver thread running, thread_ID: %06u", (int)pthread_self()); + DBG1(SIG_DBG_NET, "receiver thread running, thread_ID: %06u", + (int)pthread_self()); - while (1) + while (TRUE) { while (charon->socket->receive(charon->socket,¤t_packet) == SUCCESS) { - this->logger->log(this->logger, CONTROL | LEVEL1, "Creating job from packet"); + DBG2(SIG_DBG_NET, "creating job from packet"); current_job = (job_t *) incoming_packet_job_create(current_packet); - + charon->job_queue->add(charon->job_queue,current_job); - } - /* bad bad, rebuild the socket ? */ - this->logger->log(this->logger, ERROR, "Receiving from socket failed!"); + /* bad bad, TODO: rebuild the socket ? */ + DBG1(SIG_DBG_NET, "receiving from socket failed!"); } } @@ -90,12 +84,8 @@ static void receive_packets(private_receiver_t * this) */ static void destroy(private_receiver_t *this) { - this->logger->log(this->logger, CONTROL | LEVEL1, "Going to terminate receiver thread"); pthread_cancel(this->assigned_thread); - pthread_join(this->assigned_thread, NULL); - this->logger->log(this->logger, CONTROL | LEVEL1, "Receiver thread terminated"); - free(this); } @@ -108,13 +98,10 @@ receiver_t * receiver_create() this->public.destroy = (void(*)(receiver_t*)) destroy; - this->logger = logger_manager->get_logger(logger_manager, RECEIVER); - if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))receive_packets, this) != 0) { - this->logger->log(this->logger, ERROR, "Receiver thread could not be started"); free(this); - charon->kill(charon, "Unable to create receiver thread"); + charon->kill(charon, "unable to create receiver thread"); } return &(this->public); diff --git a/src/charon/threads/scheduler.c b/src/charon/threads/scheduler.c index ba86c1b43..d62b779d6 100644 --- a/src/charon/threads/scheduler.c +++ b/src/charon/threads/scheduler.c @@ -28,7 +28,6 @@ #include <daemon.h> #include <definitions.h> -#include <utils/logger_manager.h> #include <queues/job_queue.h> @@ -47,11 +46,6 @@ struct private_scheduler_t { * Assigned thread. */ pthread_t assigned_thread; - - /** - * A logger. - */ - logger_t *logger; }; /** @@ -64,16 +58,17 @@ static void get_events(private_scheduler_t * this) /* cancellation disabled by default */ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); - this->logger->log(this->logger, CONTROL, "scheduler thread running, thread_ID: %06u", (int)pthread_self()); + DBG1(SIG_DBG_JOB, "scheduler thread running, thread_ID: %06u", + (int)pthread_self()); while (TRUE) { - this->logger->log(this->logger, CONTROL|LEVEL2, "waiting for next event..."); + DBG2(SIG_DBG_JOB, "waiting for next event..."); /* get a job, this block until one is available */ current_job = charon->event_queue->get(charon->event_queue); /* queue the job in the job queue, workers will eat them */ - this->logger->log(this->logger, CONTROL | LEVEL1, "got event, adding job %s to job-queue.", - mapping_find(job_type_m, current_job->get_type(current_job))); + DBG2(SIG_DBG_JOB, "got event, adding job %N to job-queue", + job_type_names, current_job->get_type(current_job)); charon->job_queue->add(charon->job_queue, current_job); } } @@ -83,12 +78,8 @@ static void get_events(private_scheduler_t * this) */ static void destroy(private_scheduler_t *this) { - this->logger->log(this->logger, CONTROL | LEVEL1, "going to terminate scheduler thread"); pthread_cancel(this->assigned_thread); - pthread_join(this->assigned_thread, NULL); - this->logger->log(this->logger, CONTROL | LEVEL1, "scheduler thread terminated"); - free(this); } @@ -98,18 +89,15 @@ static void destroy(private_scheduler_t *this) scheduler_t * scheduler_create() { private_scheduler_t *this = malloc_thing(private_scheduler_t); - - this->public.destroy = (void(*)(scheduler_t*)) destroy; - this->logger = logger_manager->get_logger(logger_manager, SCHEDULER); + this->public.destroy = (void(*)(scheduler_t*)) destroy; if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))get_events, this) != 0) { /* thread could not be created */ - this->logger->log(this->logger, ERROR, "scheduler thread could not be created!"); free(this); charon->kill(charon, "unable to create scheduler thread"); } - + return &(this->public); } diff --git a/src/charon/threads/sender.c b/src/charon/threads/sender.c index 9f16a1d2c..6df549e1d 100644 --- a/src/charon/threads/sender.c +++ b/src/charon/threads/sender.c @@ -30,7 +30,6 @@ #include <network/socket.h> #include <network/packet.h> #include <queues/send_queue.h> -#include <utils/logger_manager.h> typedef struct private_sender_t private_sender_t; @@ -48,11 +47,6 @@ struct private_sender_t { * Assigned thread. */ pthread_t assigned_thread; - - /** - * A logger for this sender_t object. - */ - logger_t *logger; }; @@ -67,16 +61,17 @@ static void send_packets(private_sender_t * this) /* cancellation disabled by default */ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); - this->logger->log(this->logger, CONTROL, "sender thread running, thread_ID: %06u", (int)pthread_self()); + DBG1(SIG_DBG_NET, "sender thread running, thread_ID: %06u", + (int)pthread_self()); while (TRUE) { current_packet = charon->send_queue->get(charon->send_queue); - this->logger->log(this->logger, CONTROL|LEVEL1, "Got a packet, sending it"); + DBG2(SIG_DBG_NET, "got a packet, sending it"); status = charon->socket->send(charon->socket, current_packet); if (status != SUCCESS) { - this->logger->log(this->logger, ERROR, "Sending packet failed"); + DBG1(SIG_DBG_NET, "sending packet failed"); } current_packet->destroy(current_packet); } @@ -87,12 +82,8 @@ static void send_packets(private_sender_t * this) */ static void destroy(private_sender_t *this) { - this->logger->log(this->logger, CONTROL | LEVEL1, "Going to terminate sender thread"); pthread_cancel(this->assigned_thread); - pthread_join(this->assigned_thread, NULL); - this->logger->log(this->logger, CONTROL | LEVEL1, "Sender thread terminated"); - free(this); } @@ -104,14 +95,11 @@ sender_t * sender_create() private_sender_t *this = malloc_thing(private_sender_t); this->public.destroy = (void(*)(sender_t*)) destroy; - - this->logger = logger_manager->get_logger(logger_manager, SENDER); if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))send_packets, this) != 0) { - this->logger->log(this->logger, ERROR, "Sender thread could not be created"); free(this); - charon->kill(charon, "Unable to create sender thread"); + charon->kill(charon, "unable to create sender thread"); } return &(this->public); diff --git a/src/charon/threads/stroke_interface.c b/src/charon/threads/stroke_interface.c index 35fcff4d2..988390ef5 100755 --- a/src/charon/threads/stroke_interface.c +++ b/src/charon/threads/stroke_interface.c @@ -33,10 +33,11 @@ #include "stroke_interface.h" -#include <stroke.h> #include <types.h> +#include <stroke.h> #include <daemon.h> #include <crypto/x509.h> +#include <crypto/crl.h> #include <queues/jobs/initiate_job.h> #include <queues/jobs/route_job.h> #include <utils/leak_detective.h> @@ -59,16 +60,11 @@ struct private_stroke_t { * Public part of stroke_t object. */ stroke_t public; - - /** - * Assigned logger_t object in charon. - */ - logger_t *logger; /** - * Logger which logs to stroke + * Output stream (stroke console) */ - logger_t *stroke_logger; + FILE *out; /** * Unix socket to listen for strokes @@ -76,14 +72,9 @@ struct private_stroke_t { int socket; /** - * Thread which reads from the ocket + * Thread which reads from the Socket */ pthread_t assigned_thread; - - /** - * Read from the socket and handle stroke messages - */ - void (*stroke_receive) (private_stroke_t *this); }; /** @@ -115,7 +106,7 @@ static void pop_string(stroke_msg_t *msg, char **string) /** * Load end entitity certificate */ -static x509_t* load_end_certificate(const char *filename, identification_t **idp, logger_t *logger) +static x509_t* load_end_certificate(const char *filename, identification_t **idp) { char path[PATH_BUF]; x509_t *cert; @@ -142,7 +133,7 @@ static x509_t* load_end_certificate(const char *filename, identification_t **idp if (ugh != NULL) { - logger->log(logger, ERROR, "warning: certificate %s", ugh); + DBG1(SIG_DBG_CFG, "warning: certificate %s", ugh); } if (!id->equals(id, subject) && !cert->equals_subjectAltName(cert, id)) { @@ -170,7 +161,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) host_t *my_host, *other_host, *my_subnet, *other_subnet; proposal_t *proposal; traffic_selector_t *my_ts, *other_ts; - + pop_string(msg, &msg->add_conn.name); pop_string(msg, &msg->add_conn.me.address); pop_string(msg, &msg->add_conn.other.address); @@ -187,29 +178,27 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) pop_string(msg, &msg->add_conn.algorithms.ike); pop_string(msg, &msg->add_conn.algorithms.esp); - this->logger->log(this->logger, CONTROL, - "received stroke: add connection \"%s\"", msg->add_conn.name); - - this->logger->log(this->logger, CONTROL|LEVEL2, "conn %s", msg->add_conn.name); - this->logger->log(this->logger, CONTROL|LEVEL2, " right=%s", msg->add_conn.me.address); - this->logger->log(this->logger, CONTROL|LEVEL2, " left=%s", msg->add_conn.other.address); - this->logger->log(this->logger, CONTROL|LEVEL2, " rightsubnet=%s", msg->add_conn.me.subnet); - this->logger->log(this->logger, CONTROL|LEVEL2, " leftsubnet=%s", msg->add_conn.other.subnet); - this->logger->log(this->logger, CONTROL|LEVEL2, " rightid=%s", msg->add_conn.me.id); - this->logger->log(this->logger, CONTROL|LEVEL2, " leftid=%s", msg->add_conn.other.id); - this->logger->log(this->logger, CONTROL|LEVEL2, " rightcert=%s", msg->add_conn.me.cert); - this->logger->log(this->logger, CONTROL|LEVEL2, " leftcert=%s", msg->add_conn.other.cert); - this->logger->log(this->logger, CONTROL|LEVEL2, " rightca=%s", msg->add_conn.me.ca); - this->logger->log(this->logger, CONTROL|LEVEL2, " leftca=%s", msg->add_conn.other.ca); - this->logger->log(this->logger, CONTROL|LEVEL2, " ike=%s", msg->add_conn.algorithms.ike); - this->logger->log(this->logger, CONTROL|LEVEL2, " esp=%s", msg->add_conn.algorithms.esp); + DBG1(SIG_DBG_CFG, "received stroke: add connection '%s'", msg->add_conn.name); + + DBG2(SIG_DBG_CFG, "conn %s", msg->add_conn.name); + DBG2(SIG_DBG_CFG, " right=%s", msg->add_conn.me.address); + DBG2(SIG_DBG_CFG, " left=%s", msg->add_conn.other.address); + DBG2(SIG_DBG_CFG, " rightsubnet=%s", msg->add_conn.me.subnet); + DBG2(SIG_DBG_CFG, " leftsubnet=%s", msg->add_conn.other.subnet); + DBG2(SIG_DBG_CFG, " rightid=%s", msg->add_conn.me.id); + DBG2(SIG_DBG_CFG, " leftid=%s", msg->add_conn.other.id); + DBG2(SIG_DBG_CFG, " rightcert=%s", msg->add_conn.me.cert); + DBG2(SIG_DBG_CFG, " leftcert=%s", msg->add_conn.other.cert); + DBG2(SIG_DBG_CFG, " rightca=%s", msg->add_conn.me.ca); + DBG2(SIG_DBG_CFG, " leftca=%s", msg->add_conn.other.ca); + DBG2(SIG_DBG_CFG, " ike=%s", msg->add_conn.algorithms.ike); + DBG2(SIG_DBG_CFG, " esp=%s", msg->add_conn.algorithms.esp); my_host = msg->add_conn.me.address? host_create_from_string(msg->add_conn.me.address, IKE_PORT) : NULL; if (my_host == NULL) { - this->stroke_logger->log(this->stroke_logger, ERROR, - "invalid host: %s", msg->add_conn.me.address); + DBG1(SIG_DBG_CFG, "invalid host: %s\n", msg->add_conn.me.address); return; } @@ -217,8 +206,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) host_create_from_string(msg->add_conn.other.address, IKE_PORT) : NULL; if (other_host == NULL) { - this->stroke_logger->log(this->stroke_logger, ERROR, - "invalid host: %s", msg->add_conn.other.address); + DBG1(SIG_DBG_CFG, "invalid host: %s\n", msg->add_conn.other.address); my_host->destroy(my_host); return; } @@ -228,8 +216,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) stroke_end_t tmp_end; host_t *tmp_host; - this->stroke_logger->log(this->stroke_logger, CONTROL|LEVEL1, - "left is other host, swapping ends"); + DBG2(SIG_DBG_CFG, "left is other host, swapping ends\n"); tmp_host = my_host; my_host = other_host; @@ -241,8 +228,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) } else if (!charon->socket->is_local_address(charon->socket, my_host, NULL)) { - this->stroke_logger->log(this->stroke_logger, ERROR, - "left nor right host is our side, aborting"); + DBG1(SIG_DBG_CFG, "left nor right host is our side, aborting\n"); goto destroy_hosts; } @@ -250,8 +236,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) msg->add_conn.me.id : msg->add_conn.me.address); if (my_id == NULL) { - this->stroke_logger->log(this->stroke_logger, ERROR, - "invalid id: %s", msg->add_conn.me.id); + DBG1(SIG_DBG_CFG, "invalid ID: %s\n", msg->add_conn.me.id); goto destroy_hosts; } @@ -259,8 +244,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) msg->add_conn.other.id : msg->add_conn.other.address); if (other_id == NULL) { - this->stroke_logger->log(this->stroke_logger, ERROR, - "invalid id: %s", msg->add_conn.other.id); + DBG1(SIG_DBG_CFG, "invalid ID: %s\n", msg->add_conn.other.id); my_id->destroy(my_id); goto destroy_hosts; } @@ -269,8 +253,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) msg->add_conn.me.subnet : msg->add_conn.me.address, IKE_PORT); if (my_subnet == NULL) { - this->stroke_logger->log(this->stroke_logger, ERROR, - "invalid subnet: %s", msg->add_conn.me.subnet); + DBG1(SIG_DBG_CFG, "invalid subnet: %s\n", msg->add_conn.me.subnet); goto destroy_ids; } @@ -278,8 +261,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) msg->add_conn.other.subnet : msg->add_conn.other.address, IKE_PORT); if (other_subnet == NULL) { - this->stroke_logger->log(this->stroke_logger, ERROR, - "invalid subnet: %s", msg->add_conn.me.subnet); + DBG1(SIG_DBG_CFG, "invalid subnet: %s\n", msg->add_conn.me.subnet); my_subnet->destroy(my_subnet); goto destroy_ids; } @@ -318,7 +300,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) } if (msg->add_conn.me.cert) { - x509_t *cert = load_end_certificate(msg->add_conn.me.cert, &my_id, this->logger); + x509_t *cert = load_end_certificate(msg->add_conn.me.cert, &my_id); if (my_ca == NULL && !my_ca_same && cert) { @@ -329,7 +311,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) } if (msg->add_conn.other.cert) { - x509_t *cert = load_end_certificate(msg->add_conn.other.cert, &other_id, this->logger); + x509_t *cert = load_end_certificate(msg->add_conn.other.cert, &other_id); if (other_ca == NULL && !other_ca_same && cert) { @@ -354,9 +336,9 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) { other_ca = identification_create_from_string("%any"); } - this->logger->log(this->logger, CONTROL|LEVEL1, " my ca: '%D'", my_ca); - this->logger->log(this->logger, CONTROL|LEVEL1, " other ca:'%D'", other_ca); - this->logger->log(this->logger, CONTROL|LEVEL1, " updown: '%s'", msg->add_conn.me.updown); + DBG2(SIG_DBG_CFG, " my ca: '%D'", my_ca); + DBG2(SIG_DBG_CFG, " other ca:'%D'", other_ca); + DBG2(SIG_DBG_CFG, " updown: '%s'", msg->add_conn.me.updown); connection = connection_create(msg->add_conn.name, msg->add_conn.ikev2, @@ -384,8 +366,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) proposal = proposal_create_from_string(PROTO_IKE, proposal_string); if (proposal == NULL) { - this->logger->log(this->logger, ERROR, - "invalid IKE proposal string: %s", proposal_string); + DBG1(SIG_DBG_CFG, "invalid IKE proposal string: %s", proposal_string); my_id->destroy(my_id); other_id->destroy(other_id); my_ts->destroy(my_ts); @@ -435,8 +416,7 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) proposal = proposal_create_from_string(PROTO_ESP, proposal_string); if (proposal == NULL) { - this->logger->log(this->logger, ERROR, - "invalid ESP proposal string: %s", proposal_string); + DBG1(SIG_DBG_CFG, "invalid ESP proposal string: %s", proposal_string); policy->destroy(policy); connection->destroy(connection); return; @@ -457,11 +437,11 @@ static void stroke_add_conn(private_stroke_t *this, stroke_msg_t *msg) /* add to global connection list */ charon->connections->add_connection(charon->connections, connection); - this->logger->log(this->logger, CONTROL, - "added connection \"%s\": %H[%D]...%H[%D]", msg->add_conn.name, - my_host, my_id, other_host, other_id); + DBG1(SIG_DBG_CFG, "added connection '%s': %H[%D]...%H[%D]", + msg->add_conn.name, my_host, my_id, other_host, other_id); /* add to global policy list */ charon->policies->add_policy(charon->policies, policy); + return; /* mopping up after parsing errors */ @@ -483,20 +463,18 @@ static void stroke_del_conn(private_stroke_t *this, stroke_msg_t *msg) status_t status; pop_string(msg, &(msg->del_conn.name)); - this->logger->log(this->logger, CONTROL, "received stroke: delete \"%s\"", msg->del_conn.name); + DBG1(SIG_DBG_CFG, "received stroke: delete '%s'", msg->del_conn.name); status = charon->connections->delete_connection(charon->connections, msg->del_conn.name); charon->policies->delete_policy(charon->policies, msg->del_conn.name); if (status == SUCCESS) { - this->stroke_logger->log(this->stroke_logger, CONTROL, - "Deleted connection '%s'", msg->del_conn.name); + fprintf(this->out, "deleted connection '%s'\n", msg->del_conn.name); } else { - this->stroke_logger->log(this->stroke_logger, ERROR, - "No connection named '%s'", msg->del_conn.name); + fprintf(this->out, "no connection named '%s'\n", msg->del_conn.name); } } @@ -508,19 +486,17 @@ static void stroke_initiate(private_stroke_t *this, stroke_msg_t *msg) initiate_job_t *job; connection_t *connection; policy_t *policy; + ike_sa_t *init_ike_sa = NULL; + signal_t signal; pop_string(msg, &(msg->initiate.name)); - this->logger->log(this->logger, CONTROL, - "received stroke: initiate \"%s\"", - msg->initiate.name); + DBG1(SIG_DBG_CFG, "received stroke: initiate '%s'", msg->initiate.name); connection = charon->connections->get_connection_by_name(charon->connections, msg->initiate.name); if (connection == NULL) { - this->stroke_logger->log(this->stroke_logger, ERROR, - "no connection named \"%s\"", - msg->initiate.name); + fprintf(this->out, "no connection named '%s'\n", msg->initiate.name); return; } if (!connection->is_ikev2(connection)) @@ -533,17 +509,54 @@ static void stroke_initiate(private_stroke_t *this, stroke_msg_t *msg) msg->initiate.name); if (policy == NULL) { - this->stroke_logger->log(this->stroke_logger, ERROR, - "no policy named \"%s\"", - msg->initiate.name); + fprintf(this->out, "no policy named '%s'\n", msg->initiate.name); connection->destroy(connection); return; } - this->stroke_logger->log(this->stroke_logger, CONTROL, - "initiating connection \"%s\" (see log)...", - msg->initiate.name); + fprintf(this->out, "initiating connection '%s'\n", msg->initiate.name); + job = initiate_job_create(connection, policy); + + charon->bus->set_listen_state(charon->bus, TRUE); charon->job_queue->add(charon->job_queue, (job_t*)job); + while (TRUE) + { + level_t level; + int thread; + ike_sa_t *ike_sa; + char* format; + va_list args; + + signal = charon->bus->listen(charon->bus, &level, &thread, &ike_sa, &format, &args); + + if (ike_sa == init_ike_sa && level <= LEVEL_CTRL) + { + if (vfprintf(this->out, format, args) < 0 || + fprintf(this->out, "\n") < 0 || + fflush(this->out)) + { + break; + } + } + + /* TODO: Handle INVALID_KE_PAYLOAD signal (ike_sa switch) */ + switch (signal) + { + case SIG_IKE_UP: + case SIG_IKE_FAILED: + case SIG_IKE_DOWN: + if (ike_sa == init_ike_sa) + { + charon->bus->set_listen_state(charon->bus, FALSE); + } + return; + case SIG_INITIATE: + init_ike_sa = ike_sa; + /* fall through */ + default: + continue; + } + } } /** @@ -556,10 +569,8 @@ static void stroke_route(private_stroke_t *this, stroke_msg_t *msg, bool route) policy_t *policy; pop_string(msg, &(msg->route.name)); - this->logger->log(this->logger, CONTROL, - "received stroke: %s \"%s\"", - route ? "route" : "unroute", - msg->route.name); + DBG1(SIG_DBG_CFG, "received stroke: %s '%s'", + route ? "route" : "unroute", msg->route.name); /* we wouldn't need a connection, but we only want to route policies * whose connections are keyexchange=ikev2. */ @@ -567,9 +578,7 @@ static void stroke_route(private_stroke_t *this, stroke_msg_t *msg, bool route) msg->route.name); if (connection == NULL) { - this->stroke_logger->log(this->stroke_logger, ERROR, - "no connection named \"%s\"", - msg->route.name); + fprintf(this->out, "no connection named '%s'\n", msg->route.name); return; } if (!connection->is_ikev2(connection)) @@ -582,16 +591,12 @@ static void stroke_route(private_stroke_t *this, stroke_msg_t *msg, bool route) msg->route.name); if (policy == NULL) { - this->stroke_logger->log(this->stroke_logger, ERROR, - "no policy named \"%s\"", - msg->route.name); + fprintf(this->out, "no policy named '%s'\n", msg->route.name); connection->destroy(connection); return; } - this->stroke_logger->log(this->stroke_logger, CONTROL, - "%s policy \"%s\"", - route ? "routing" : "unrouting", - msg->route.name); + fprintf(this->out, "%s policy '%s'\n", + route ? "routing" : "unrouting", msg->route.name); job = route_job_create(connection, policy, route); charon->job_queue->add(charon->job_queue, (job_t*)job); } @@ -602,37 +607,38 @@ static void stroke_route(private_stroke_t *this, stroke_msg_t *msg, bool route) static void stroke_terminate(private_stroke_t *this, stroke_msg_t *msg) { pop_string(msg, &(msg->terminate.name)); - this->logger->log(this->logger, CONTROL, "received stroke: terminate \"%s\"", msg->terminate.name); + DBG1(SIG_DBG_CFG, "received stroke: terminate '%s'", msg->terminate.name); charon->ike_sa_manager->delete_by_name(charon->ike_sa_manager, msg->terminate.name); } /** - * show status of (established) connections + * show status of daemon */ -static void stroke_status(private_stroke_t *this, stroke_msg_t *msg) +static void stroke_statusall(private_stroke_t *this, stroke_msg_t *msg) { + iterator_t *iterator; linked_list_t *list; host_t *host; + connection_t *connection; + policy_t *policy; + ike_sa_t *ike_sa; - leak_detective_status(this->stroke_logger); + leak_detective_status(this->out); - this->stroke_logger->log(this->stroke_logger, CONTROL|LEVEL1, - "job queue load: %d", - charon->job_queue->get_count(charon->job_queue)); - this->stroke_logger->log(this->stroke_logger, CONTROL|LEVEL1, - "scheduled events: %d", - charon->event_queue->get_count(charon->event_queue)); + fprintf(this->out, "worker threads idle: %d of %d\n", + charon->thread_pool->get_idle_threads(charon->thread_pool), + charon->thread_pool->get_pool_size(charon->thread_pool)); + fprintf(this->out, "job queue load: %d\n", + charon->job_queue->get_count(charon->job_queue)); + fprintf(this->out, "scheduled events: %d\n", + charon->event_queue->get_count(charon->event_queue)); list = charon->socket->create_local_address_list(charon->socket); - this->stroke_logger->log(this->stroke_logger, CONTROL|LEVEL1, - "listening on %d addresses:", - list->get_count(list)); + fprintf(this->out, "listening on %d addresses:\n", list->get_count(list)); while (list->remove_first(list, (void**)&host) == SUCCESS) { - this->stroke_logger->log(this->stroke_logger, CONTROL|LEVEL1, - " %H", host); + fprintf(this->out, " %H\n", host); host->destroy(host); - } list->destroy(list); @@ -640,10 +646,101 @@ static void stroke_status(private_stroke_t *this, stroke_msg_t *msg) { pop_string(msg, &(msg->status.name)); } - charon->connections->log_connections(charon->connections, - this->stroke_logger, msg->status.name); - charon->ike_sa_manager->log_status(charon->ike_sa_manager, - this->stroke_logger, msg->status.name); + + fprintf(this->out, "connections:\n"); + iterator = charon->connections->create_iterator(charon->connections); + while (iterator->iterate(iterator, (void**)&connection)) + { + if (connection->is_ikev2(connection) && (msg->status.name == NULL || + streq(msg->status.name, connection->get_name(connection)))) + { + fprintf(this->out, "%10s: %H...%H\n", + connection->get_name(connection), + connection->get_my_host(connection), + connection->get_other_host(connection)); + } + } + iterator->destroy(iterator); + + fprintf(this->out, "policies:\n"); + iterator = charon->policies->create_iterator(charon->policies); + while (iterator->iterate(iterator, (void**)&policy)) + { + if (msg->status.name == NULL || + streq(msg->status.name, policy->get_name(policy))) + { + fprintf(this->out, "%10s: %D...%D\n", + policy->get_name(policy), + policy->get_my_id(policy), + policy->get_other_id(policy)); + } + } + iterator->destroy(iterator); + + fprintf(this->out, "IKE_SAs:\n"); + iterator = charon->ike_sa_manager->create_iterator(charon->ike_sa_manager); + while (iterator->iterate(iterator, (void**)&ike_sa)) + { + bool ike_sa_printed = FALSE; + child_sa_t *child_sa; + iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa); + while (children->iterate(children, (void**)&child_sa)) + { + if (!ike_sa_printed && + (msg->status.name == NULL || + streq(msg->status.name, child_sa->get_name(child_sa)) || + streq(msg->status.name, ike_sa->get_name(ike_sa)))) + { + fprintf(this->out, "%#K\n", ike_sa); + ike_sa_printed = TRUE; + } + if (ike_sa_printed) + { + fprintf(this->out, "%#P\n", child_sa); + } + } + children->destroy(children); + } + iterator->destroy(iterator); +} + +/** + * show status of daemon + */ +static void stroke_status(private_stroke_t *this, stroke_msg_t *msg) +{ + iterator_t *iterator; + ike_sa_t *ike_sa; + + if (msg->status.name) + { + pop_string(msg, &(msg->status.name)); + } + + iterator = charon->ike_sa_manager->create_iterator(charon->ike_sa_manager); + while (iterator->iterate(iterator, (void**)&ike_sa)) + { + bool ike_sa_printed = FALSE; + child_sa_t *child_sa; + iterator_t *children = ike_sa->create_child_sa_iterator(ike_sa); + while (children->iterate(children, (void**)&child_sa)) + { + if (!ike_sa_printed && + (msg->status.name == NULL || + streq(msg->status.name, child_sa->get_name(child_sa)) || + streq(msg->status.name, ike_sa->get_name(ike_sa)))) + { + fprintf(this->out, "%K\n", ike_sa); + ike_sa_printed = TRUE; + } + if (ike_sa_printed) + { + fprintf(this->out, "%P\n", child_sa); + } + } + children->destroy(children); + } + iterator->destroy(iterator); } /** @@ -651,17 +748,62 @@ static void stroke_status(private_stroke_t *this, stroke_msg_t *msg) */ static void stroke_list(private_stroke_t *this, stroke_msg_t *msg) { + iterator_t *iterator; + if (msg->list.flags & LIST_CERTS) { - charon->credentials->log_certificates(charon->credentials, this->stroke_logger, msg->list.utc); + x509_t *cert; + + iterator = charon->credentials->create_cert_iterator(charon->credentials); + if (iterator->get_count(iterator)) + { + fprintf(this->out, "List of X.509 End Entity Certificates:\n"); + fprintf(this->out, "--------------------------------------\n"); + } + while (iterator->iterate(iterator, (void**)&cert)) + { + fprintf(this->out, "%#Q", cert, msg->list.utc); + if (charon->credentials->has_rsa_private_key( + charon->credentials, cert->get_public_key(cert))) + { + fprintf(this->out, ", has private key"); + } + fprintf(this->out, "\n\n"); + + } + iterator->destroy(iterator); } if (msg->list.flags & LIST_CACERTS) { - charon->credentials->log_ca_certificates(charon->credentials, this->stroke_logger, msg->list.utc); + x509_t *cert; + + iterator = charon->credentials->create_cacert_iterator(charon->credentials); + if (iterator->get_count(iterator)) + { + fprintf(this->out, "List of X.509 CA Certificates:\n"); + fprintf(this->out, "------------------------------\n"); + } + while (iterator->iterate(iterator, (void**)&cert)) + { + fprintf(this->out, "%#Q\n\n", cert, msg->list.utc); + } + iterator->destroy(iterator); } if (msg->list.flags & LIST_CRLS) { - charon->credentials->log_crls(charon->credentials, this->stroke_logger, msg->list.utc); + crl_t *crl; + + iterator = charon->credentials->create_crl_iterator(charon->credentials); + if (iterator->get_count(iterator)) + { + fprintf(this->out, "List of X.509 CRLs:\n"); + fprintf(this->out, "-------------------\n"); + } + while (iterator->iterate(iterator, (void**)&crl)) + { + fprintf(this->out, "%#U\n\n", crl, msg->list.utc); + } + iterator->destroy(iterator); } } @@ -680,107 +822,41 @@ static void stroke_reread(private_stroke_t *this, stroke_msg_t *msg) } } -logger_context_t get_context(char *context) -{ - if (strcasecmp(context, "ALL") == 0) return ALL_LOGGERS; - else if (strcasecmp(context, "PARSR") == 0) return PARSER; - else if (strcasecmp(context, "GNRAT") == 0) return GENERATOR; - else if (strcasecmp(context, "IKESA") == 0) return IKE_SA; - else if (strcasecmp(context, "SAMGR") == 0) return IKE_SA_MANAGER; - else if (strcasecmp(context, "CHDSA") == 0) return CHILD_SA; - else if (strcasecmp(context, "MESSG") == 0) return MESSAGE; - else if (strcasecmp(context, "TPOOL") == 0) return THREAD_POOL; - else if (strcasecmp(context, "WORKR") == 0) return WORKER; - else if (strcasecmp(context, "SCHED") == 0) return SCHEDULER; - else if (strcasecmp(context, "SENDR") == 0) return SENDER; - else if (strcasecmp(context, "RECVR") == 0) return RECEIVER; - else if (strcasecmp(context, "SOCKT") == 0) return SOCKET; - else if (strcasecmp(context, "TESTR") == 0) return TESTER; - else if (strcasecmp(context, "DAEMN") == 0) return DAEMON; - else if (strcasecmp(context, "CONFG") == 0) return CONFIG; - else if (strcasecmp(context, "ENCPL") == 0) return ENCRYPTION_PAYLOAD; - else if (strcasecmp(context, "PAYLD") == 0) return PAYLOAD; - else if (strcasecmp(context, "XFRM") == 0) return XFRM; - else return -2; -} - -/** - * set the type of logged messages in a context - */ -static void stroke_logtype(private_stroke_t *this, stroke_msg_t *msg) +signal_t get_signal_from_logtype(char *type) { - pop_string(msg, &(msg->logtype.context)); - pop_string(msg, &(msg->logtype.type)); - - this->logger->log(this->logger, CONTROL, "received stroke: logtype for %s", msg->logtype.context); - - log_level_t level; - logger_context_t context = get_context(msg->logtype.context); - if (context == -2) - { - this->stroke_logger->log(this->stroke_logger, ERROR, "invalid context (%s)!", msg->logtype.context); - return; - } - - if (strcasecmp(msg->logtype.type, "CONTROL") == 0) - level = CONTROL; - else if (strcasecmp(msg->logtype.type, "ERROR") == 0) - level = ERROR; - else if (strcasecmp(msg->logtype.type, "AUDIT") == 0) - level = AUDIT; - else if (strcasecmp(msg->logtype.type, "RAW") == 0) - level = RAW; - else if (strcasecmp(msg->logtype.type, "PRIVATE") == 0) - level = PRIVATE; - else - { - this->stroke_logger->log(this->stroke_logger, ERROR, "invalid type (%s)!", msg->logtype.type); - return; - } - - if (msg->logtype.enable) - { - logger_manager->enable_log_level(logger_manager, context, level); - } - else - { - logger_manager->disable_log_level(logger_manager, context, level); - } + if (strcasecmp(type, "any") == 0) return SIG_ANY; + else if (strcasecmp(type, "mgr") == 0) return SIG_DBG_MGR; + else if (strcasecmp(type, "ike") == 0) return SIG_DBG_IKE; + else if (strcasecmp(type, "chd") == 0) return SIG_DBG_CHD; + else if (strcasecmp(type, "job") == 0) return SIG_DBG_JOB; + else if (strcasecmp(type, "cfg") == 0) return SIG_DBG_CFG; + else if (strcasecmp(type, "knl") == 0) return SIG_DBG_KNL; + else if (strcasecmp(type, "net") == 0) return SIG_DBG_NET; + else if (strcasecmp(type, "enc") == 0) return SIG_DBG_ENC; + else if (strcasecmp(type, "lib") == 0) return SIG_DBG_LIB; + else return -1; } /** - * set the verbosity of a logger + * set the verbosity debug output */ static void stroke_loglevel(private_stroke_t *this, stroke_msg_t *msg) { - log_level_t level; - logger_context_t context; - - pop_string(msg, &(msg->loglevel.context)); - this->logger->log(this->logger, CONTROL, "received stroke: loglevel for %s", msg->loglevel.context); + signal_t signal; - context = get_context(msg->loglevel.context); - if (context == -2) - { - this->stroke_logger->log(this->stroke_logger, ERROR, "invalid context (%s)!", msg->loglevel.context); - return; - } + pop_string(msg, &(msg->loglevel.type)); + DBG1(SIG_DBG_CFG, "received stroke: loglevel %d for %s", + msg->loglevel.level, msg->loglevel.type); - if (msg->loglevel.level == 0) - level = LEVEL0; - else if (msg->loglevel.level == 1) - level = LEVEL1; - else if (msg->loglevel.level == 2) - level = LEVEL2; - else if (msg->loglevel.level == 3) - level = LEVEL3; - else + signal = get_signal_from_logtype(msg->loglevel.type); + if (signal < 0) { - this->stroke_logger->log(this->stroke_logger, ERROR, "invalid level (%d)!", msg->loglevel.level); + fprintf(this->out, "invalid type (%s)!\n", msg->loglevel.type); return; } - logger_manager->enable_log_level(logger_manager, context, level); + charon->outlog->set_level(charon->outlog, signal, msg->loglevel.level); + charon->syslog->set_level(charon->syslog, signal, msg->loglevel.level); } /** @@ -794,7 +870,6 @@ static void stroke_receive(private_stroke_t *this) int strokeaddrlen = sizeof(strokeaddr); ssize_t bytes_read; int strokefd; - FILE *strokefile; int oldstate; /* disable cancellation by default */ @@ -809,7 +884,7 @@ static void stroke_receive(private_stroke_t *this) if (strokefd < 0) { - this->logger->log(this->logger, ERROR, "accepting stroke connection failed: %s", strerror(errno)); + DBG1(SIG_DBG_CFG, "accepting stroke connection failed: %m"); continue; } @@ -817,7 +892,7 @@ static void stroke_receive(private_stroke_t *this) bytes_read = recv(strokefd, &msg_length, sizeof(msg_length), MSG_PEEK); if (bytes_read != sizeof(msg_length)) { - this->logger->log(this->logger, ERROR, "reading lenght of stroke message failed"); + DBG1(SIG_DBG_CFG, "reading lenght of stroke message failed"); close(strokefd); continue; } @@ -827,24 +902,21 @@ static void stroke_receive(private_stroke_t *this) bytes_read = recv(strokefd, msg, msg_length, 0); if (bytes_read != msg_length) { - this->logger->log(this->logger, ERROR, "reading stroke message failed: %s"); + DBG1(SIG_DBG_CFG, "reading stroke message failed: %m"); close(strokefd); continue; } - strokefile = fdopen(dup(strokefd), "w"); - if (strokefile == NULL) + this->out = fdopen(dup(strokefd), "w"); + if (this->out == NULL) { - this->logger->log(this->logger, ERROR, "opening stroke output channel failed:", strerror(errno)); + DBG1(SIG_DBG_CFG, "opening stroke output channel failed: %m"); close(strokefd); free(msg); continue; } - /* setup a logger which writes status to the unix socket */ - this->stroke_logger = logger_create("", CONTROL|ERROR, FALSE, strokefile); - - this->logger->log_bytes(this->logger, RAW, "stroke message", (void*)msg, msg_length); + DBG3(SIG_DBG_CFG, "stroke message %b", (void*)msg, msg_length); switch (msg->type) { @@ -864,8 +936,7 @@ static void stroke_receive(private_stroke_t *this) stroke_status(this, msg); break; case STR_STATUS_ALL: - this->stroke_logger->enable_level(this->stroke_logger, LEVEL1); - stroke_status(this, msg); + stroke_statusall(this, msg); break; case STR_ADD_CONN: stroke_add_conn(this, msg); @@ -873,9 +944,6 @@ static void stroke_receive(private_stroke_t *this) case STR_DEL_CONN: stroke_del_conn(this, msg); break; - case STR_LOGTYPE: - stroke_logtype(this, msg); - break; case STR_LOGLEVEL: stroke_loglevel(this, msg); break; @@ -886,10 +954,9 @@ static void stroke_receive(private_stroke_t *this) stroke_reread(this, msg); break; default: - this->logger->log(this->logger, ERROR, "received invalid stroke"); + DBG1(SIG_DBG_CFG, "received unknown stroke"); } - this->stroke_logger->destroy(this->stroke_logger); - fclose(strokefile); + fclose(this->out); close(strokefd); free(msg); } @@ -900,7 +967,6 @@ static void stroke_receive(private_stroke_t *this) */ static void destroy(private_stroke_t *this) { - pthread_cancel(this->assigned_thread); pthread_join(this->assigned_thread, NULL); @@ -909,7 +975,6 @@ static void destroy(private_stroke_t *this) free(this); } - /* * Described in header-file */ @@ -921,16 +986,11 @@ stroke_t *stroke_create() /* public functions */ this->public.destroy = (void (*)(stroke_t*))destroy; - /* private functions */ - this->stroke_receive = stroke_receive; - - this->logger = logger_manager->get_logger(logger_manager, CONFIG); - /* set up unix socket */ this->socket = socket(AF_UNIX, SOCK_STREAM, 0); if (this->socket == -1) { - this->logger->log(this->logger, ERROR, "could not create whack socket"); + DBG1(SIG_DBG_CFG, "could not create whack socket"); free(this); return NULL; } @@ -938,7 +998,7 @@ stroke_t *stroke_create() old = umask(~S_IRWXU); if (bind(this->socket, (struct sockaddr *)&socket_addr, sizeof(socket_addr)) < 0) { - this->logger->log(this->logger, ERROR, "could not bind stroke socket: %s", strerror(errno)); + DBG1(SIG_DBG_CFG, "could not bind stroke socket: %m"); close(this->socket); free(this); return NULL; @@ -947,7 +1007,7 @@ stroke_t *stroke_create() if (listen(this->socket, 0) < 0) { - this->logger->log(this->logger, ERROR, "could not listen on stroke socket: %s", strerror(errno)); + DBG1(SIG_DBG_CFG, "could not listen on stroke socket: %m"); close(this->socket); unlink(socket_addr.sun_path); free(this); @@ -955,9 +1015,9 @@ stroke_t *stroke_create() } /* start a thread reading from the socket */ - if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->stroke_receive, this) != 0) + if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))stroke_receive, this) != 0) { - this->logger->log(this->logger, ERROR, "Could not spawn stroke thread"); + DBG1(SIG_DBG_CFG, "Could not spawn stroke thread"); close(this->socket); unlink(socket_addr.sun_path); free(this); diff --git a/src/charon/threads/stroke_interface.h b/src/charon/threads/stroke_interface.h index 16eb66533..941841d14 100644 --- a/src/charon/threads/stroke_interface.h +++ b/src/charon/threads/stroke_interface.h @@ -23,9 +23,6 @@ #ifndef STROKE_INTERFACE_H_ #define STROKE_INTERFACE_H_ -#include <config/policies/policy_store.h> -#include <config/connections/connection_store.h> -#include <config/credentials/credential_store.h> typedef struct stroke_t stroke_t; @@ -36,17 +33,7 @@ typedef struct stroke_t stroke_t; * * stroke_t allows config manipulation (as whack in pluto). * Messages of type stroke_msg_t's are sent over a unix socket - * (/var/run/charon.ctl). stroke_t implements the connections_t - * and the policies_t interface, which means it acts as a - * configuration backend for those too. stroke_t uses an own - * thread to read from the socket. - * - * @warning DO NOT cast stroke_t to any of the implemented interfaces! - * stroke_t implements multiple interfaces, so you must use - * stroke_t.interface_xy to access the specific interface! You have - * been warned... - * - * @todo Add clean thread cancellation + * (/var/run/charon.ctl). * * @b Constructors: * - stroke_create() diff --git a/src/charon/threads/thread_pool.c b/src/charon/threads/thread_pool.c index 3b2938865..413f87f35 100644 --- a/src/charon/threads/thread_pool.c +++ b/src/charon/threads/thread_pool.c @@ -27,10 +27,9 @@ #include <errno.h> #include "thread_pool.h" - + #include <daemon.h> #include <queues/job_queue.h> -#include <utils/logger.h> typedef struct private_thread_pool_t private_thread_pool_t; @@ -47,17 +46,17 @@ struct private_thread_pool_t { /** * Number of running threads. */ - size_t pool_size; + u_int pool_size; /** - * Array of thread ids. + * Number of threads waiting for work */ - pthread_t *threads; + u_int idle_threads; /** - * Logger of the thread pool. + * Array of thread ids. */ - logger_t *logger; + pthread_t *threads; } ; /** @@ -71,13 +70,14 @@ static void process_jobs(private_thread_pool_t *this) /* cancellation disabled by default */ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); - this->logger->log(this->logger, CONTROL, - "worker thread running, thread_ID: %06u", - (int)pthread_self()); + DBG1(SIG_DBG_JOB, "worker thread running, thread_ID: %06u", + (int)pthread_self()); while (TRUE) { + this->idle_threads++; job = charon->job_queue->get(charon->job_queue); + this->idle_threads--; status = job->execute(job); @@ -91,21 +91,29 @@ static void process_jobs(private_thread_pool_t *this) /** * Implementation of thread_pool_t.get_pool_size. */ -static size_t get_pool_size(private_thread_pool_t *this) +static u_int get_pool_size(private_thread_pool_t *this) { return this->pool_size; } /** + * Implementation of thread_pool_t.get_idle_threads. + */ +static u_int get_idle_threads(private_thread_pool_t *this) +{ + return this->idle_threads; +} + +/** * Implementation of thread_pool_t.destroy. */ static void destroy(private_thread_pool_t *this) { int current; /* flag thread for termination */ - for (current = 0; current < this->pool_size; current++) { - this->logger->log(this->logger, CONTROL, - "cancelling worker thread #%d", current+1); + for (current = 0; current < this->pool_size; current++) + { + DBG1(SIG_DBG_JOB, "cancelling worker thread #%d", current+1); pthread_cancel(this->threads[current]); } @@ -113,13 +121,11 @@ static void destroy(private_thread_pool_t *this) for (current = 0; current < this->pool_size; current++) { if (pthread_join(this->threads[current], NULL) == 0) { - this->logger->log(this->logger, CONTROL, - "worker thread #%d terminated", current+1); + DBG1(SIG_DBG_JOB, "worker thread #%d terminated", current+1); } else { - this->logger->log(this->logger, ERROR, - "could not terminate worker thread #%d", current+1); + DBG1(SIG_DBG_JOB, "could not terminate worker thread #%d", current+1); } } @@ -138,39 +144,36 @@ thread_pool_t *thread_pool_create(size_t pool_size) /* fill in public fields */ this->public.destroy = (void(*)(thread_pool_t*))destroy; - this->public.get_pool_size = (size_t(*)(thread_pool_t*))get_pool_size; + this->public.get_pool_size = (u_int(*)(thread_pool_t*))get_pool_size; + this->public.get_idle_threads = (u_int(*)(thread_pool_t*))get_idle_threads; /* initialize member */ this->pool_size = pool_size; + this->idle_threads = 0; this->threads = malloc(sizeof(pthread_t) * pool_size); - this->logger = logger_manager->get_logger(logger_manager, THREAD_POOL); /* try to create as many threads as possible, up to pool_size */ - for (current = 0; current < pool_size; current++) + for (current = 0; current < pool_size; current++) { - if (pthread_create(&(this->threads[current]), NULL, + if (pthread_create(&(this->threads[current]), NULL, (void*(*)(void*))process_jobs, this) == 0) { - this->logger->log(this->logger, CONTROL, - "created worker thread #%d", current+1); + DBG1(SIG_DBG_JOB, "created worker thread #%d", current+1); } else { /* creation failed, is it the first one? */ - if (current == 0) + if (current == 0) { - this->logger->log(this->logger, ERROR, "Could not create any thread"); free(this->threads); free(this); - return NULL; + charon->kill(charon, "could not create any worker threads"); } /* not all threads could be created, but at least one :-/ */ - this->logger->log(this->logger, ERROR, - "Could only create %d from requested %d threads!", - current, pool_size); - + DBG1(SIG_DBG_JOB, "could only create %d from requested %d threads!", + current, pool_size); this->pool_size = current; - return (thread_pool_t*)this; + break; } } return (thread_pool_t*)this; diff --git a/src/charon/threads/thread_pool.h b/src/charon/threads/thread_pool.h index c27d0e0b3..e4a04e8cb 100644 --- a/src/charon/threads/thread_pool.h +++ b/src/charon/threads/thread_pool.h @@ -45,13 +45,22 @@ typedef struct thread_pool_t thread_pool_t; * @ingroup threads */ struct thread_pool_t { + /** * @brief Return currently instanciated thread count. - * + * * @param thread_pool calling object * @return size of thread pool */ - size_t (*get_pool_size) (thread_pool_t *thread_pool); + u_int (*get_pool_size) (thread_pool_t *thread_pool); + + /** + * @brief Get the number of threads currently waiting for work. + * + * @param thread_pool calling object + * @return number of idle threads + */ + u_int (*get_idle_threads) (thread_pool_t *thread_pool); /** * @brief Destroy a thread_pool_t object. |