~ubuntu-branches/ubuntu/vivid/whoopsie/vivid-proposed

« back to all changes in this revision

Viewing changes to src/identifier.c

  • Committer: Package Import Robot
  • Author(s): Evan Dandrea
  • Date: 2012-06-18 09:32:46 UTC
  • Revision ID: package-import@ubuntu.com-20120618093246-djrgv8yn3lfrfocx
Tags: 0.2.1
Provide a libwhoopsie library. This allows the control center panel
via activity-log-manager to provide a 'Show Previous Reports'
button.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#include "identifier.h"
13
13
 
14
14
void
15
 
hex_to_char (char* buf, const char *str, int len)
 
15
whoopsie_hex_to_char (char* buf, const char *str, int len)
16
16
{
17
17
    char* p = NULL;
18
18
    int i = 0;
19
19
 
 
20
    g_return_if_fail (buf);
 
21
    g_return_if_fail (str);
 
22
 
20
23
    p = buf;
21
24
    for (i = 0; i < len; i++) {
22
25
        snprintf(p, 3, "%02x", (unsigned char) str[i]);
26
29
}
27
30
 
28
31
void
29
 
identifier_get_mac_address (char** res, GError** error)
 
32
whoopsie_identifier_get_mac_address (char** res, GError** error)
30
33
{
31
34
    struct ifreq ifr;
32
35
    struct ifconf ifc;
36
39
    struct ifreq* it;
37
40
    int i = 0;
38
41
 
 
42
    g_return_if_fail (res);
 
43
 
39
44
    sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
40
45
    if (sock == -1) {
41
46
        g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), 0,
79
84
}
80
85
 
81
86
void
82
 
identifier_get_system_uuid (char** res, GError** error)
 
87
whoopsie_identifier_get_system_uuid (char** res, GError** error)
83
88
{
84
89
    int fp;
85
90
 
 
91
    g_return_if_fail (res);
 
92
 
86
93
    fp = open ("/sys/class/dmi/id/product_uuid", O_RDONLY);
87
94
    if (fp < 0) {
88
95
        g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), 0,
99
106
}
100
107
 
101
108
void
102
 
identifier_sha512 (char* source, char* res, GError** error)
 
109
whoopsie_identifier_sha512 (char* source, char* res, GError** error)
103
110
{
104
111
    int md_len;
105
112
    gcry_md_hd_t sha512 = NULL;
106
113
    unsigned char* id = NULL;
107
114
 
 
115
    g_return_if_fail (source);
 
116
    g_return_if_fail (res);
 
117
 
108
118
    if (!gcry_check_version (GCRYPT_VERSION)) {
109
119
        g_set_error (error, g_quark_from_static_string ("whoopsie-quark"), 0,
110
120
                     "libcrypt version mismatch.");
131
141
        gcry_md_close (sha512);
132
142
        return;
133
143
    }
134
 
    hex_to_char (res, (const char*)id, md_len);
 
144
    whoopsie_hex_to_char (res, (const char*)id, md_len);
135
145
    gcry_md_close (sha512);
136
146
}
 
147
 
 
148
void
 
149
whoopsie_identifier_generate (char** res, GError** error)
 
150
{
 
151
    char* identifier = NULL;
 
152
 
 
153
    g_return_if_fail (res);
 
154
 
 
155
    whoopsie_identifier_get_system_uuid (&identifier, error);
 
156
    if ((!error || !(*error)) && identifier)
 
157
        goto out;
 
158
 
 
159
    if (error && *error) {
 
160
        g_error_free (*error);
 
161
        *error = NULL;
 
162
    }
 
163
 
 
164
    whoopsie_identifier_get_mac_address (&identifier, error);
 
165
    if ((!error || !(*error)) && identifier)
 
166
        goto out;
 
167
 
 
168
    return;
 
169
 
 
170
out:
 
171
    *res = malloc (HASHLEN + 1);
 
172
    whoopsie_identifier_sha512 (identifier, *res, error);
 
173
    free (identifier);
 
174
}
 
175