summaryrefslogtreecommitdiffstats
path: root/client/smartcard_channel.h
blob: 60c6db5c04b16d7c4be5d92cd002b6b6151f88b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef __SMART_CARD_H__
#define __SMART_CARD_H__

#include <map>

#include <vreadert.h>
#include <vscard_common.h>
#include <eventt.h>

#include "red_channel.h"
#include "red_peer.h"

class Application;

struct SmartcardOptions {
    std::vector<std::string> certs;
    std::string dbname;
    bool enable;
    SmartcardOptions();
};

void smartcard_init(const SmartcardOptions* options);

struct ReaderData {
    ReaderData() :
        vreader(NULL),
        reader_id(VSCARD_UNDEFINED_READER_ID),
        name(NULL),
        card_insert_pending(false)
    {}
    VReader *vreader;
    reader_id_t reader_id;
    char* name;
    bool card_insert_pending;
};

void virtual_card_remove();
void virtual_card_insert();

class SmartCardChannel;

class VEventEvent : public Event {
public:
    VEventEvent(SmartCardChannel* smartcard_channel, VEvent* vevent);
    ~VEventEvent();
    SmartCardChannel* _smartcard_channel;
    VReader* _vreader;
    VEvent* _vevent;
};

class ReaderAddEvent: public VEventEvent {
public:
    ReaderAddEvent(SmartCardChannel* smartcard_channel, VEvent* vevent)
        : VEventEvent(smartcard_channel, vevent) {}
    virtual void response(AbstractProcessLoop& events_loop);
};

class ReaderRemoveEvent: public VEventEvent {
public:
    ReaderRemoveEvent(SmartCardChannel* smartcard_channel, VEvent* vevent)
        : VEventEvent(smartcard_channel, vevent) {}
    virtual void response(AbstractProcessLoop& events_loop);
};

class CardInsertEvent: public VEventEvent {
public:
    CardInsertEvent(SmartCardChannel* smartcard_channel, VEvent* vevent)
        : VEventEvent(smartcard_channel, vevent) {}
    virtual void response(AbstractProcessLoop& events_loop);
};

class CardRemoveEvent: public VEventEvent {
public:
    CardRemoveEvent(SmartCardChannel* smartcard_channel, VEvent* vevent)
        : VEventEvent(smartcard_channel, vevent) {}
    virtual void response(AbstractProcessLoop& events_loop);
};

class VSCMessageEvent: public Event {
public:
    VSCMessageEvent(SmartCardChannel* smartcard_channel,
        VSCMsgHeader* vheader);
    ~VSCMessageEvent();
    SmartCardChannel* _smartcard_channel;
    VSCMsgHeader* _vheader;
    virtual void response(AbstractProcessLoop& events_loop);
};

class SmartCardChannel : public RedChannel {

public:
    SmartCardChannel(RedClient& client, uint32_t id);
    void handle_smartcard_data(RedPeer::InMessage* message);

    void virtual_card_remove();
    void virtual_card_insert();
    static ChannelFactory& Factory();
protected:
    virtual void on_connect();
    virtual void on_disconnect();

private:
    static void* cac_card_events_thread_entry(void* data);
    void cac_card_events_thread_main();
    void send_message(reader_id_t reader_id, VSCMsgType type, uint8_t* data, uint32_t len);

    Thread* _event_thread;

    Application* _app;

    VReaderList *_reader_list;
    typedef std::map<reader_id_t, ReaderData*> readers_by_id_t;
    readers_by_id_t _readers_by_id;
    typedef std::map<VReader*, ReaderData*> readers_by_vreader_t;
    readers_by_vreader_t _readers_by_vreader;
    readers_by_vreader_t _unallocated_readers_by_vreader;

    ReaderData* reader_data_from_vreader(VReader* vreader);
    ReaderData* reader_data_from_reader_id(reader_id_t reader_id);
    void add_unallocated_reader(VReader* vreader, const char* name);
    ReaderData* add_reader(reader_id_t reader_id);
    void remove_reader(ReaderData* data);
    void send_reader_added(const char* reader_name);
    void send_reader_removed(reader_id_t reader_id);
    void send_atr(VReader* vreader);

    friend class ReaderAddEvent;
    friend class ReaderRemoveEvent;
    friend class CardInsertEvent;
    friend class CardRemoveEvent;
    friend class VSCMessageEvent;
};

#endif // __SMART_CARD_H__