diff options
Diffstat (limited to 'libcacard')
-rw-r--r-- | libcacard/Makefile | 11 | ||||
-rw-r--r-- | libcacard/cac.c | 52 | ||||
-rw-r--r-- | libcacard/card_7816.c | 28 | ||||
-rw-r--r-- | libcacard/event.c | 25 | ||||
-rw-r--r-- | libcacard/vcard.c | 38 | ||||
-rw-r--r-- | libcacard/vcard_emul_nss.c | 46 | ||||
-rw-r--r-- | libcacard/vreader.c | 59 | ||||
-rw-r--r-- | libcacard/vscclient.c | 83 |
8 files changed, 135 insertions, 207 deletions
diff --git a/libcacard/Makefile b/libcacard/Makefile index 6b064485f..0e7903fcf 100644 --- a/libcacard/Makefile +++ b/libcacard/Makefile @@ -3,13 +3,7 @@ libcacard_includedir=$(includedir)/cacard TOOLS += vscclient$(EXESUF) # objects linked into a shared library, built with libtool with -fPIC if required -libcacard-obj-y = $(stub-obj-y) $(libcacard-y) -libcacard-obj-y += util/osdep.o util/cutils.o util/qemu-timer-common.o -libcacard-obj-y += util/error.o util/qemu-error.o -libcacard-obj-$(CONFIG_WIN32) += util/oslib-win32.o util/qemu-thread-win32.o -libcacard-obj-$(CONFIG_POSIX) += util/oslib-posix.o util/qemu-thread-posix.o -libcacard-obj-y += $(filter trace/%, $(util-obj-y)) - +libcacard-obj-y = $(libcacard-y) libcacard-lobj-y=$(patsubst %.o,%.lo,$(libcacard-obj-y)) # libtool will build the .o files, too @@ -24,8 +18,7 @@ vscclient$(EXESUF): libcacard/vscclient.o libcacard.la # Rules for building libcacard standalone library libcacard.la: LDFLAGS += -rpath $(libdir) -no-undefined \ - -export-syms $(SRC_PATH)/libcacard/libcacard.syms -libcacard.la: LIBS = $(libcacard_libs) + -export-symbols $(SRC_PATH)/libcacard/libcacard.syms libcacard.la: $(libcacard-lobj-y) $(call LINK,$^) diff --git a/libcacard/cac.c b/libcacard/cac.c index 74ef3e3ce..ae8c3784b 100644 --- a/libcacard/cac.c +++ b/libcacard/cac.c @@ -93,17 +93,15 @@ cac_common_process_apdu(VCard *card, VCardAPDU *apdu, VCardResponse **response) static VCardStatus cac_applet_pki_reset(VCard *card, int channel) { - VCardAppletPrivate *applet_private = NULL; - CACPKIAppletData *pki_applet = NULL; + VCardAppletPrivate *applet_private; + CACPKIAppletData *pki_applet; applet_private = vcard_get_current_applet_private(card, channel); assert(applet_private); pki_applet = &(applet_private->u.pki_data); pki_applet->cert_buffer = NULL; - if (pki_applet->sign_buffer) { - g_free(pki_applet->sign_buffer); - pki_applet->sign_buffer = NULL; - } + g_free(pki_applet->sign_buffer); + pki_applet->sign_buffer = NULL; pki_applet->cert_buffer_len = 0; pki_applet->sign_buffer_len = 0; return VCARD_DONE; @@ -113,8 +111,8 @@ static VCardStatus cac_applet_pki_process_apdu(VCard *card, VCardAPDU *apdu, VCardResponse **response) { - CACPKIAppletData *pki_applet = NULL; - VCardAppletPrivate *applet_private = NULL; + CACPKIAppletData *pki_applet; + VCardAppletPrivate *applet_private; int size, next; unsigned char *sign_buffer; vcard_7816_status_t status; @@ -169,17 +167,8 @@ cac_applet_pki_process_apdu(VCard *card, VCardAPDU *apdu, } size = apdu->a_Lc; - sign_buffer = realloc(pki_applet->sign_buffer, - pki_applet->sign_buffer_len+size); - if (sign_buffer == NULL) { - g_free(pki_applet->sign_buffer); - pki_applet->sign_buffer = NULL; - pki_applet->sign_buffer_len = 0; - *response = vcard_make_response( - VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE); - ret = VCARD_DONE; - break; - } + sign_buffer = g_realloc(pki_applet->sign_buffer, + pki_applet->sign_buffer_len + size); memcpy(sign_buffer+pki_applet->sign_buffer_len, apdu->a_body, size); size += pki_applet->sign_buffer_len; switch (apdu->a_p1) { @@ -288,18 +277,14 @@ cac_applet_container_process_apdu(VCard *card, VCardAPDU *apdu, static void cac_delete_pki_applet_private(VCardAppletPrivate *applet_private) { - CACPKIAppletData *pki_applet_data = NULL; + CACPKIAppletData *pki_applet_data; if (applet_private == NULL) { return; } pki_applet_data = &(applet_private->u.pki_data); - if (pki_applet_data->cert != NULL) { - g_free(pki_applet_data->cert); - } - if (pki_applet_data->sign_buffer != NULL) { - g_free(pki_applet_data->sign_buffer); - } + g_free(pki_applet_data->cert); + g_free(pki_applet_data->sign_buffer); if (pki_applet_data->key != NULL) { vcard_emul_delete_key(pki_applet_data->key); } @@ -310,16 +295,11 @@ static VCardAppletPrivate * cac_new_pki_applet_private(const unsigned char *cert, int cert_len, VCardKey *key) { - CACPKIAppletData *pki_applet_data = NULL; - VCardAppletPrivate *applet_private = NULL; - applet_private = (VCardAppletPrivate *)g_malloc(sizeof(VCardAppletPrivate)); + CACPKIAppletData *pki_applet_data; + VCardAppletPrivate *applet_private; + applet_private = g_new0(VCardAppletPrivate, 1); pki_applet_data = &(applet_private->u.pki_data); - pki_applet_data->cert_buffer = NULL; - pki_applet_data->cert_buffer_len = 0; - pki_applet_data->sign_buffer = NULL; - pki_applet_data->sign_buffer_len = 0; - pki_applet_data->key = NULL; pki_applet_data->cert = (unsigned char *)g_malloc(cert_len+1); /* * if we want to support compression, then we simply change the 0 to a 1 @@ -341,8 +321,8 @@ static VCardApplet * cac_new_pki_applet(int i, const unsigned char *cert, int cert_len, VCardKey *key) { - VCardAppletPrivate *applet_private = NULL; - VCardApplet *applet = NULL; + VCardAppletPrivate *applet_private; + VCardApplet *applet; unsigned char pki_aid[] = { 0xa0, 0x00, 0x00, 0x00, 0x79, 0x01, 0x00 }; int pki_aid_len = sizeof(pki_aid); diff --git a/libcacard/card_7816.c b/libcacard/card_7816.c index c28bb60fe..814fa1662 100644 --- a/libcacard/card_7816.c +++ b/libcacard/card_7816.c @@ -51,7 +51,7 @@ vcard_response_new_data(unsigned char *buf, int len) { VCardResponse *new_response; - new_response = (VCardResponse *)g_malloc(sizeof(VCardResponse)); + new_response = g_new(VCardResponse, 1); new_response->b_data = g_malloc(len + 2); memcpy(new_response->b_data, buf, len); new_response->b_total_len = len+2; @@ -132,7 +132,7 @@ vcard_response_new_status(vcard_7816_status_t status) { VCardResponse *new_response; - new_response = (VCardResponse *)g_malloc(sizeof(VCardResponse)); + new_response = g_new(VCardResponse, 1); new_response->b_data = &new_response->b_sw1; new_response->b_len = 0; new_response->b_total_len = 2; @@ -149,7 +149,7 @@ vcard_response_new_status_bytes(unsigned char sw1, unsigned char sw2) { VCardResponse *new_response; - new_response = (VCardResponse *)g_malloc(sizeof(VCardResponse)); + new_response = g_new(VCardResponse, 1); new_response->b_data = &new_response->b_sw1; new_response->b_len = 0; new_response->b_total_len = 2; @@ -172,16 +172,12 @@ vcard_response_delete(VCardResponse *response) switch (response->b_type) { case VCARD_MALLOC: /* everything was malloc'ed */ - if (response->b_data) { - g_free(response->b_data); - } + g_free(response->b_data); g_free(response); break; case VCARD_MALLOC_DATA: /* only the data buffer was malloc'ed */ - if (response->b_data) { - g_free(response->b_data); - } + g_free(response->b_data); break; case VCARD_MALLOC_STRUCT: /* only the structure was malloc'ed */ @@ -336,9 +332,8 @@ vcard_apdu_new(unsigned char *raw_apdu, int len, vcard_7816_status_t *status) return NULL; } - new_apdu = (VCardAPDU *)g_malloc(sizeof(VCardAPDU)); - new_apdu->a_data = g_malloc(len); - memcpy(new_apdu->a_data, raw_apdu, len); + new_apdu = g_new(VCardAPDU, 1); + new_apdu->a_data = g_memdup(raw_apdu, len); new_apdu->a_len = len; *status = vcard_apdu_set_class(new_apdu); if (*status != VCARD7816_STATUS_SUCCESS) { @@ -359,9 +354,7 @@ vcard_apdu_delete(VCardAPDU *apdu) if (apdu == NULL) { return; } - if (apdu->a_data) { - g_free(apdu->a_data); - } + g_free(apdu->a_data); g_free(apdu); } @@ -417,7 +410,7 @@ VCARD_RESPONSE_NEW_STATIC_STATUS(VCARD7816_STATUS_ERROR_GENERAL) VCardResponse * vcard_make_response(vcard_7816_status_t status) { - VCardResponse *response = NULL; + VCardResponse *response; switch (status) { /* known 7816 response codes */ @@ -544,9 +537,8 @@ vcard_make_response(vcard_7816_status_t status) return VCARD_RESPONSE_GET_STATIC( VCARD7816_STATUS_EXC_ERROR_MEMORY_FAILURE); } + return response; } - assert(response); - return response; } /* diff --git a/libcacard/event.c b/libcacard/event.c index 2d7500fac..4c551e4e3 100644 --- a/libcacard/event.c +++ b/libcacard/event.c @@ -6,7 +6,6 @@ */ #include "qemu-common.h" -#include "qemu/thread.h" #include "vcard.h" #include "vreader.h" @@ -17,7 +16,7 @@ vevent_new(VEventType type, VReader *reader, VCard *card) { VEvent *new_vevent; - new_vevent = (VEvent *)g_malloc(sizeof(VEvent)); + new_vevent = g_new(VEvent, 1); new_vevent->next = NULL; new_vevent->type = type; new_vevent->reader = vreader_reference(reader); @@ -43,13 +42,11 @@ vevent_delete(VEvent *vevent) static VEvent *vevent_queue_head; static VEvent *vevent_queue_tail; -static QemuMutex vevent_queue_lock; -static QemuCond vevent_queue_condition; +static CompatGMutex vevent_queue_lock; +static CompatGCond vevent_queue_condition; void vevent_queue_init(void) { - qemu_mutex_init(&vevent_queue_lock); - qemu_cond_init(&vevent_queue_condition); vevent_queue_head = vevent_queue_tail = NULL; } @@ -57,7 +54,7 @@ void vevent_queue_vevent(VEvent *vevent) { vevent->next = NULL; - qemu_mutex_lock(&vevent_queue_lock); + g_mutex_lock(&vevent_queue_lock); if (vevent_queue_head) { assert(vevent_queue_tail); vevent_queue_tail->next = vevent; @@ -65,8 +62,8 @@ vevent_queue_vevent(VEvent *vevent) vevent_queue_head = vevent; } vevent_queue_tail = vevent; - qemu_cond_signal(&vevent_queue_condition); - qemu_mutex_unlock(&vevent_queue_lock); + g_cond_signal(&vevent_queue_condition); + g_mutex_unlock(&vevent_queue_lock); } /* must have lock */ @@ -86,11 +83,11 @@ VEvent *vevent_wait_next_vevent(void) { VEvent *vevent; - qemu_mutex_lock(&vevent_queue_lock); + g_mutex_lock(&vevent_queue_lock); while ((vevent = vevent_dequeue_vevent()) == NULL) { - qemu_cond_wait(&vevent_queue_condition, &vevent_queue_lock); + g_cond_wait(&vevent_queue_condition, &vevent_queue_lock); } - qemu_mutex_unlock(&vevent_queue_lock); + g_mutex_unlock(&vevent_queue_lock); return vevent; } @@ -98,9 +95,9 @@ VEvent *vevent_get_next_vevent(void) { VEvent *vevent; - qemu_mutex_lock(&vevent_queue_lock); + g_mutex_lock(&vevent_queue_lock); vevent = vevent_dequeue_vevent(); - qemu_mutex_unlock(&vevent_queue_lock); + g_mutex_unlock(&vevent_queue_lock); return vevent; } diff --git a/libcacard/vcard.c b/libcacard/vcard.c index 539177bb4..87ad5166a 100644 --- a/libcacard/vcard.c +++ b/libcacard/vcard.c @@ -37,9 +37,8 @@ vcard_buffer_response_new(unsigned char *buffer, int size) { VCardBufferResponse *new_buffer; - new_buffer = (VCardBufferResponse *)g_malloc(sizeof(VCardBufferResponse)); - new_buffer->buffer = (unsigned char *)g_malloc(size); - memcpy(new_buffer->buffer, buffer, size); + new_buffer = g_new(VCardBufferResponse, 1); + new_buffer->buffer = (unsigned char *)g_memdup(buffer, size); new_buffer->buffer_len = size; new_buffer->current = new_buffer->buffer; new_buffer->len = size; @@ -52,9 +51,7 @@ vcard_buffer_response_delete(VCardBufferResponse *buffer_response) if (buffer_response == NULL) { return; } - if (buffer_response->buffer) { - g_free(buffer_response->buffer); - } + g_free(buffer_response->buffer); g_free(buffer_response); } @@ -102,15 +99,11 @@ vcard_new_applet(VCardProcessAPDU applet_process_function, { VCardApplet *applet; - applet = (VCardApplet *)g_malloc(sizeof(VCardApplet)); - applet->next = NULL; - applet->applet_private = NULL; - applet->applet_private_free = NULL; + applet = g_new0(VCardApplet, 1); applet->process_apdu = applet_process_function; applet->reset_applet = applet_reset_function; - applet->aid = g_malloc(aid_len); - memcpy(applet->aid, aid, aid_len); + applet->aid = g_memdup(aid, aid_len); applet->aid_len = aid_len; return applet; } @@ -124,12 +117,8 @@ vcard_delete_applet(VCardApplet *applet) } if (applet->applet_private_free) { applet->applet_private_free(applet->applet_private); - applet->applet_private = NULL; - } - if (applet->aid) { - g_free(applet->aid); - applet->aid = NULL; } + g_free(applet->aid); g_free(applet); } @@ -149,18 +138,11 @@ VCard * vcard_new(VCardEmul *private, VCardEmulFree private_free) { VCard *new_card; - int i; - new_card = (VCard *)g_malloc(sizeof(VCard)); - new_card->applet_list = NULL; - for (i = 0; i < MAX_CHANNEL; i++) { - new_card->current_applet[i] = NULL; - } - new_card->vcard_buffer_response = NULL; + new_card = g_new0(VCard, 1); new_card->type = VCARD_VM; new_card->vcard_private = private; new_card->vcard_private_free = private_free; - new_card->vcard_get_atr = NULL; new_card->reference_count = 1; return new_card; } @@ -178,8 +160,8 @@ vcard_reference(VCard *vcard) void vcard_free(VCard *vcard) { - VCardApplet *current_applet = NULL; - VCardApplet *next_applet = NULL; + VCardApplet *current_applet; + VCardApplet *next_applet; if (vcard == NULL) { return; @@ -190,8 +172,6 @@ vcard_free(VCard *vcard) } if (vcard->vcard_private_free) { (*vcard->vcard_private_free)(vcard->vcard_private); - vcard->vcard_private_free = 0; - vcard->vcard_private = 0; } for (current_applet = vcard->applet_list; current_applet; current_applet = next_applet) { diff --git a/libcacard/vcard_emul_nss.c b/libcacard/vcard_emul_nss.c index ee2dfaee8..f1bba57c2 100644 --- a/libcacard/vcard_emul_nss.c +++ b/libcacard/vcard_emul_nss.c @@ -94,9 +94,9 @@ static void vcard_emul_alloc_arrays(unsigned char ***certsp, int **cert_lenp, VCardKey ***keysp, int cert_count) { - *certsp = (unsigned char **)g_malloc(sizeof(unsigned char *)*cert_count); - *cert_lenp = (int *)g_malloc(sizeof(int)*cert_count); - *keysp = (VCardKey **)g_malloc(sizeof(VCardKey *)*cert_count); + *certsp = g_new(unsigned char *, cert_count); + *cert_lenp = g_new(int, cert_count); + *keysp = g_new(VCardKey *, cert_count); } /* @@ -139,7 +139,7 @@ vcard_emul_make_key(PK11SlotInfo *slot, CERTCertificate *cert) { VCardKey *key; - key = (VCardKey *)g_malloc(sizeof(VCardKey)); + key = g_new(VCardKey, 1); key->slot = PK11_ReferenceSlot(slot); key->cert = CERT_DupCertificate(cert); /* NOTE: if we aren't logged into the token, this could return NULL */ @@ -367,7 +367,7 @@ vcard_7816_status_t vcard_emul_login(VCard *card, unsigned char *pin, int pin_len) { PK11SlotInfo *slot; - unsigned char *pin_string = NULL; + unsigned char *pin_string; int i; SECStatus rv; @@ -423,7 +423,7 @@ static VReader * vcard_emul_find_vreader_from_slot(PK11SlotInfo *slot) { VReaderList *reader_list = vreader_get_reader_list(); - VReaderListEntry *current_entry = NULL; + VReaderListEntry *current_entry; if (reader_list == NULL) { return NULL; @@ -433,11 +433,13 @@ vcard_emul_find_vreader_from_slot(PK11SlotInfo *slot) VReader *reader = vreader_list_get_reader(current_entry); VReaderEmul *reader_emul = vreader_get_private(reader); if (reader_emul->slot == slot) { + vreader_list_delete(reader_list); return reader; } vreader_free(reader); } + vreader_list_delete(reader_list); return NULL; } @@ -449,7 +451,7 @@ vreader_emul_new(PK11SlotInfo *slot, VCardEmulType type, const char *params) { VReaderEmul *new_reader_emul; - new_reader_emul = (VReaderEmul *)g_malloc(sizeof(VReaderEmul)); + new_reader_emul = g_new(VReaderEmul, 1); new_reader_emul->slot = PK11_ReferenceSlot(slot); new_reader_emul->default_type = type; @@ -469,9 +471,7 @@ vreader_emul_delete(VReaderEmul *vreader_emul) if (vreader_emul->slot) { PK11_FreeSlot(vreader_emul->slot); } - if (vreader_emul->type_params) { - g_free(vreader_emul->type_params); - } + g_free(vreader_emul->type_params); g_free(vreader_emul); } @@ -616,11 +616,6 @@ vcard_emul_mirror_card(VReader *vreader) cert_count++; } - if (cert_count == 0) { - PK11_DestroyGenericObjects(firstObj); - return NULL; - } - /* allocate the arrays */ vcard_emul_alloc_arrays(&certs, &cert_len, &keys, cert_count); @@ -1050,7 +1045,7 @@ void vcard_emul_replay_insertion_events(void) { VReaderListEntry *current_entry; - VReaderListEntry *next_entry = NULL; + VReaderListEntry *next_entry; VReaderList *list = vreader_get_reader_list(); for (current_entry = vreader_list_get_first(list); current_entry; @@ -1059,6 +1054,8 @@ vcard_emul_replay_insertion_events(void) next_entry = vreader_list_get_next(current_entry); vreader_queue_card_event(vreader); } + + vreader_list_delete(list); } /* @@ -1150,7 +1147,7 @@ vcard_emul_options(const char *args) char type_str[100]; VCardEmulType type; int count, i; - VirtualReaderOptions *vreaderOpt = NULL; + VirtualReaderOptions *vreaderOpt; args = strip(args + 5); if (*args != '(') { @@ -1162,7 +1159,8 @@ vcard_emul_options(const char *args) NEXT_TOKEN(vname) NEXT_TOKEN(type_params) type_params_length = MIN(type_params_length, sizeof(type_str)-1); - pstrcpy(type_str, type_params_length, type_params); + memcpy(type_str, type_params, type_params_length); + type_str[type_params_length] = '\0'; type = vcard_emul_type_from_string(type_str); NEXT_TOKEN(type_params) @@ -1173,14 +1171,10 @@ vcard_emul_options(const char *args) if (opts->vreader_count >= reader_count) { reader_count += READER_STEP; - vreaderOpt = realloc(opts->vreader, - reader_count * sizeof(*vreaderOpt)); - if (vreaderOpt == NULL) { - return opts; /* we're done */ - } + opts->vreader = g_renew(VirtualReaderOptions, opts->vreader, + reader_count); } - opts->vreader = vreaderOpt; - vreaderOpt = &vreaderOpt[opts->vreader_count]; + vreaderOpt = &opts->vreader[opts->vreader_count]; vreaderOpt->name = g_strndup(name, name_length); vreaderOpt->vname = g_strndup(vname, vname_length); vreaderOpt->card_type = type; @@ -1188,7 +1182,7 @@ vcard_emul_options(const char *args) g_strndup(type_params, type_params_length); count = count_tokens(args, ',', ')') + 1; vreaderOpt->cert_count = count; - vreaderOpt->cert_name = (char **)g_malloc(count*sizeof(char *)); + vreaderOpt->cert_name = g_new(char *, count); for (i = 0; i < count; i++) { const char *cert = args; args = strpbrk(args, ",)"); diff --git a/libcacard/vreader.c b/libcacard/vreader.c index 5793d73ff..0315dd892 100644 --- a/libcacard/vreader.c +++ b/libcacard/vreader.c @@ -9,10 +9,8 @@ #undef G_LOG_DOMAIN #endif #define G_LOG_DOMAIN "libcacard" -#include <glib.h> #include "qemu-common.h" -#include "qemu/thread.h" #include "vcard.h" #include "vcard_emul.h" @@ -28,7 +26,7 @@ struct VReaderStruct { VCard *card; char *name; vreader_id_t id; - QemuMutex lock; + CompatGMutex lock; VReaderEmul *reader_private; VReaderEmulFree reader_private_free; }; @@ -97,13 +95,13 @@ apdu_ins_to_string(int ins) static inline void vreader_lock(VReader *reader) { - qemu_mutex_lock(&reader->lock); + g_mutex_lock(&reader->lock); } static inline void vreader_unlock(VReader *reader) { - qemu_mutex_unlock(&reader->lock); + g_mutex_unlock(&reader->lock); } /* @@ -115,8 +113,8 @@ vreader_new(const char *name, VReaderEmul *private, { VReader *reader; - reader = (VReader *)g_malloc(sizeof(VReader)); - qemu_mutex_init(&reader->lock); + reader = g_new(VReader, 1); + g_mutex_init(&reader->lock); reader->reference_count = 1; reader->name = g_strdup(name); reader->card = NULL; @@ -152,12 +150,11 @@ vreader_free(VReader *reader) return; } vreader_unlock(reader); + g_mutex_clear(&reader->lock); if (reader->card) { vcard_free(reader->card); } - if (reader->name) { - g_free(reader->name); - } + g_free(reader->name); if (reader->reader_private_free) { reader->reader_private_free(reader->reader_private); } @@ -273,22 +270,20 @@ vreader_xfr_bytes(VReader *reader, response = vcard_make_response(status); card_status = VCARD_DONE; } else { - g_debug("%s: CLS=0x%x,INS=0x%x,P1=0x%x,P2=0x%x,Lc=%d,Le=%d %s\n", + g_debug("%s: CLS=0x%x,INS=0x%x,P1=0x%x,P2=0x%x,Lc=%d,Le=%d %s", __func__, apdu->a_cla, apdu->a_ins, apdu->a_p1, apdu->a_p2, apdu->a_Lc, apdu->a_Le, apdu_ins_to_string(apdu->a_ins)); card_status = vcard_process_apdu(card, apdu, &response); if (response) { - g_debug("%s: status=%d sw1=0x%x sw2=0x%x len=%d (total=%d)\n", + g_debug("%s: status=%d sw1=0x%x sw2=0x%x len=%d (total=%d)", __func__, response->b_status, response->b_sw1, response->b_sw2, response->b_len, response->b_total_len); } } - assert(card_status == VCARD_DONE); - if (card_status == VCARD_DONE) { - int size = MIN(*receive_buf_len, response->b_total_len); - memcpy(receive_buf, response->b_data, size); - *receive_buf_len = size; - } + assert(card_status == VCARD_DONE && response); + int size = MIN(*receive_buf_len, response->b_total_len); + memcpy(receive_buf, response->b_data, size); + *receive_buf_len = size; vcard_response_delete(response); vcard_apdu_delete(apdu); vcard_free(card); /* free our reference */ @@ -312,10 +307,7 @@ vreader_list_entry_new(VReader *reader) { VReaderListEntry *new_reader_list_entry; - new_reader_list_entry = (VReaderListEntry *) - g_malloc(sizeof(VReaderListEntry)); - new_reader_list_entry->next = NULL; - new_reader_list_entry->prev = NULL; + new_reader_list_entry = g_new0(VReaderListEntry, 1); new_reader_list_entry->reader = vreader_reference(reader); return new_reader_list_entry; } @@ -336,9 +328,7 @@ vreader_list_new(void) { VReaderList *new_reader_list; - new_reader_list = (VReaderList *)g_malloc(sizeof(VReaderList)); - new_reader_list->head = NULL; - new_reader_list->tail = NULL; + new_reader_list = g_new0(VReaderList, 1); return new_reader_list; } @@ -346,14 +336,12 @@ void vreader_list_delete(VReaderList *list) { VReaderListEntry *current_entry; - VReaderListEntry *next_entry = NULL; + VReaderListEntry *next_entry; for (current_entry = vreader_list_get_first(list); current_entry; current_entry = next_entry) { next_entry = vreader_list_get_next(current_entry); vreader_list_entry_delete(current_entry); } - list->head = NULL; - list->tail = NULL; g_free(list); } @@ -413,32 +401,31 @@ vreader_dequeue(VReaderList *list, VReaderListEntry *entry) } static VReaderList *vreader_list; -static QemuMutex vreader_list_mutex; +static CompatGMutex vreader_list_mutex; static void vreader_list_init(void) { vreader_list = vreader_list_new(); - qemu_mutex_init(&vreader_list_mutex); } static void vreader_list_lock(void) { - qemu_mutex_lock(&vreader_list_mutex); + g_mutex_lock(&vreader_list_mutex); } static void vreader_list_unlock(void) { - qemu_mutex_unlock(&vreader_list_mutex); + g_mutex_unlock(&vreader_list_mutex); } static VReaderList * vreader_copy_list(VReaderList *list) { - VReaderList *new_list = NULL; - VReaderListEntry *current_entry = NULL; + VReaderList *new_list; + VReaderListEntry *current_entry; new_list = vreader_list_new(); if (new_list == NULL) { @@ -470,7 +457,7 @@ VReader * vreader_get_reader_by_id(vreader_id_t id) { VReader *reader = NULL; - VReaderListEntry *current_entry = NULL; + VReaderListEntry *current_entry; if (id == (vreader_id_t) -1) { return NULL; @@ -494,7 +481,7 @@ VReader * vreader_get_reader_by_name(const char *name) { VReader *reader = NULL; - VReaderListEntry *current_entry = NULL; + VReaderListEntry *current_entry; vreader_list_lock(); for (current_entry = vreader_list_get_first(vreader_list); current_entry; diff --git a/libcacard/vscclient.c b/libcacard/vscclient.c index 3477ab3e1..80111df00 100644 --- a/libcacard/vscclient.c +++ b/libcacard/vscclient.c @@ -11,13 +11,13 @@ */ #ifndef _WIN32 +#include <sys/socket.h> +#include <netinet/in.h> #include <netdb.h> +#define closesocket(x) close(x) #endif -#include <glib.h> #include "qemu-common.h" -#include "qemu/thread.h" -#include "qemu/sockets.h" #include "vscard_common.h" @@ -54,7 +54,7 @@ print_usage(void) { static GIOChannel *channel_socket; static GByteArray *socket_to_send; -static QemuMutex socket_to_send_lock; +static CompatGMutex socket_to_send_lock; static guint socket_tag; static void @@ -103,7 +103,7 @@ send_msg( ) { VSCMsgHeader mhHeader; - qemu_mutex_lock(&socket_to_send_lock); + g_mutex_lock(&socket_to_send_lock); if (verbose > 10) { printf("sending type=%d id=%u, len =%u (0x%x)\n", @@ -117,22 +117,22 @@ send_msg( g_byte_array_append(socket_to_send, (guint8 *)msg, length); g_idle_add(socket_prepare_sending, NULL); - qemu_mutex_unlock(&socket_to_send_lock); + g_mutex_unlock(&socket_to_send_lock); return 0; } static VReader *pending_reader; -static QemuMutex pending_reader_lock; -static QemuCond pending_reader_condition; +static CompatGMutex pending_reader_lock; +static CompatGCond pending_reader_condition; #define MAX_ATR_LEN 40 -static void * -event_thread(void *arg) +static gpointer +event_thread(gpointer arg) { unsigned char atr[MAX_ATR_LEN]; - int atr_len = MAX_ATR_LEN; - VEvent *event = NULL; + int atr_len; + VEvent *event; unsigned int reader_id; @@ -149,20 +149,20 @@ event_thread(void *arg) /* ignore events from readers qemu has rejected */ /* if qemu is still deciding on this reader, wait to see if need to * forward this event */ - qemu_mutex_lock(&pending_reader_lock); + g_mutex_lock(&pending_reader_lock); if (!pending_reader || (pending_reader != event->reader)) { /* wasn't for a pending reader, this reader has already been * rejected by qemu */ - qemu_mutex_unlock(&pending_reader_lock); + g_mutex_unlock(&pending_reader_lock); vevent_delete(event); continue; } /* this reader hasn't been told its status from qemu yet, wait for * that status */ while (pending_reader != NULL) { - qemu_cond_wait(&pending_reader_condition, &pending_reader_lock); + g_cond_wait(&pending_reader_condition, &pending_reader_lock); } - qemu_mutex_unlock(&pending_reader_lock); + g_mutex_unlock(&pending_reader_lock); /* now recheck the id */ reader_id = vreader_get_id(event->reader); if (reader_id == VSCARD_UNDEFINED_READER_ID) { @@ -178,12 +178,12 @@ event_thread(void *arg) /* wait until qemu has responded to our first reader insert * before we send a second. That way we won't confuse the responses * */ - qemu_mutex_lock(&pending_reader_lock); + g_mutex_lock(&pending_reader_lock); while (pending_reader != NULL) { - qemu_cond_wait(&pending_reader_condition, &pending_reader_lock); + g_cond_wait(&pending_reader_condition, &pending_reader_lock); } pending_reader = vreader_reference(event->reader); - qemu_mutex_unlock(&pending_reader_lock); + g_mutex_unlock(&pending_reader_lock); reader_name = vreader_get_name(event->reader); if (verbose > 10) { printf(" READER INSERT: %s\n", reader_name); @@ -246,7 +246,6 @@ on_host_init(VSCMsgHeader *mhHeader, VSCMsgInit *incoming) int num_capabilities = 1 + ((mhHeader->length - sizeof(VSCMsgInit)) / sizeof(uint32_t)); int i; - QemuThread thread_id; incoming->version = ntohl(incoming->version); if (incoming->version != VSCARD_VERSION) { @@ -269,7 +268,7 @@ on_host_init(VSCMsgHeader *mhHeader, VSCMsgInit *incoming) send_msg(VSC_ReaderRemove, VSCARD_MINIMAL_READER_ID, NULL, 0); /* launch the event_thread. This will trigger reader adds for all the * existing readers */ - qemu_thread_create(&thread_id, "vsc/event", event_thread, NULL, 0); + g_thread_new("vsc/event", event_thread, NULL); return 0; } @@ -379,26 +378,26 @@ do_socket_read(GIOChannel *source, case VSC_Error: error_msg = (VSCMsgError *) pbSendBuffer; if (error_msg->code == VSC_SUCCESS) { - qemu_mutex_lock(&pending_reader_lock); + g_mutex_lock(&pending_reader_lock); if (pending_reader) { vreader_set_id(pending_reader, mhHeader.reader_id); vreader_free(pending_reader); pending_reader = NULL; - qemu_cond_signal(&pending_reader_condition); + g_cond_signal(&pending_reader_condition); } - qemu_mutex_unlock(&pending_reader_lock); + g_mutex_unlock(&pending_reader_lock); break; } printf("warning: qemu refused to add reader\n"); if (error_msg->code == VSC_CANNOT_ADD_MORE_READERS) { /* clear pending reader, qemu can't handle any more */ - qemu_mutex_lock(&pending_reader_lock); + g_mutex_lock(&pending_reader_lock); if (pending_reader) { pending_reader = NULL; /* make sure the event loop doesn't hang */ - qemu_cond_signal(&pending_reader_condition); + g_cond_signal(&pending_reader_condition); } - qemu_mutex_unlock(&pending_reader_lock); + g_mutex_unlock(&pending_reader_lock); } break; case VSC_Init: @@ -502,8 +501,7 @@ do_command(GIOChannel *source, if (reader != NULL) { error = vcard_emul_force_card_insert(reader); printf("insert %s, returned %d\n", - reader ? vreader_get_name(reader) - : "invalid reader", error); + vreader_get_name(reader), error); } else { printf("no reader by id %u found\n", reader_id); } @@ -515,8 +513,7 @@ do_command(GIOChannel *source, if (reader != NULL) { error = vcard_emul_force_card_remove(reader); printf("remove %s, returned %d\n", - reader ? vreader_get_name(reader) - : "invalid reader", error); + vreader_get_name(reader), error); } else { printf("no reader by id %u found\n", reader_id); } @@ -572,6 +569,7 @@ do_command(GIOChannel *source, "CARD_PRESENT" : " ", vreader_get_name(reader)); } + vreader_list_delete(list); } else if (*string != 0) { printf("valid commands:\n"); printf("insert [reader_id]\n"); @@ -602,7 +600,7 @@ connect_to_qemu( struct addrinfo *server; int ret, sock; - sock = qemu_socket(AF_INET, SOCK_STREAM, 0); + sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { /* Error */ fprintf(stderr, "Error opening socket!\n"); @@ -655,8 +653,20 @@ main( int cert_count = 0; int c, sock; - if (socket_init() != 0) +#ifdef _WIN32 + WSADATA Data; + + if (WSAStartup(MAKEWORD(2, 2), &Data) != 0) { + c = WSAGetLastError(); + fprintf(stderr, "WSAStartup: %d\n", c); return 1; + } +#endif +#if !GLIB_CHECK_VERSION(2, 31, 0) + if (!g_thread_supported()) { + g_thread_init(NULL); + } +#endif while ((c = getopt(argc, argv, "c:e:pd:")) != -1) { switch (c) { @@ -723,13 +733,8 @@ main( } socket_to_send = g_byte_array_new(); - qemu_mutex_init(&socket_to_send_lock); - qemu_mutex_init(&pending_reader_lock); - qemu_cond_init(&pending_reader_condition); - vcard_emul_init(command_line_options); - - loop = g_main_loop_new(NULL, true); + loop = g_main_loop_new(NULL, TRUE); printf("> "); fflush(stdout); |