4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <sys/ioctl.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
36 #define _LIBC_INTERNAL_
43 #include <glib/gprintf.h>
46 static const char *epid_ns_uuid
= "fcacfb03-8a73-46ef-91b1-e5ebeeaba4fe";
48 #define UUID_OFFSET_TO_LAST_SEGMENT 24
50 void readUUID(const char *string
, sipe_uuid_t
*uuid
)
53 sscanf(string
, "%08x-%04hx-%04hx-%02hhx%02hhx-", &uuid
->time_low
54 , &uuid
->time_mid
, &uuid
->time_hi_and_version
55 , &uuid
->clock_seq_hi_and_reserved
56 , &uuid
->clock_seq_low
);
60 sscanf(&string
[UUID_OFFSET_TO_LAST_SEGMENT
+i
*2], "%02hhx", &uuid
->node
[i
]);
64 void printUUID(sipe_uuid_t
*uuid
, char *string
)
68 sprintf(string
, "%08x-%04x-%04x-%02x%02x-", uuid
->time_low
69 , uuid
->time_mid
, uuid
->time_hi_and_version
70 , uuid
->clock_seq_hi_and_reserved
76 pos
+= sprintf(&string
[pos
], "%02x", uuid
->node
[i
]);
80 void createUUIDfromHash(sipe_uuid_t
*uuid
, const unsigned char *hash
)
82 memcpy(uuid
, hash
, sizeof(sipe_uuid_t
));
83 uuid
->time_hi_and_version
&= 0x0FFF;
84 uuid
->time_hi_and_version
|= 0x5000;
85 uuid
->clock_seq_hi_and_reserved
&= 0x3F;
86 uuid
->clock_seq_hi_and_reserved
|= 0x80;
89 char *generateUUIDfromEPID(const gchar
*epid
)
92 PurpleCipherContext
*ctx
;
93 unsigned char hash
[20];
96 readUUID(epid_ns_uuid
, &result
);
97 memcpy(buf
, &result
, sizeof(sipe_uuid_t
));
98 strcpy(&buf
[sizeof(sipe_uuid_t
)], epid
);
100 ctx
= purple_cipher_context_new_by_name("sha1", NULL
);
101 purple_cipher_context_append(ctx
, (guchar
*) buf
, strlen(buf
));
102 purple_cipher_context_digest(ctx
, sizeof(hash
), hash
, NULL
);
103 purple_cipher_context_destroy(ctx
);
105 createUUIDfromHash(&result
, hash
);
106 printUUID(&result
, buf
);
107 return g_strdup(buf
);
111 * Generates epid from user SIP URI, hostname and IP address.
112 * Thus epid will be the same each start and
113 * not needed to be persistent.
115 * Using MAC address proved to be poorly portable solution.
117 char *sipe_get_epid(const char *self_sip_uri
,
118 const char *hostname
,
119 const char *ip_address
)
121 #define SIPE_EPID_HASH_START 15
122 #define SIPE_EPID_HASH_END 20
123 #define SIPE_EPID_LENGTH (2 * (SIPE_EPID_HASH_END - SIPE_EPID_HASH_START + 1))
126 PurpleCipherContext
*ctx
;
127 unsigned char hash
[SIPE_EPID_HASH_END
];
128 char out
[SIPE_EPID_LENGTH
+ 1];
129 char *buf
= g_strdup_printf("%s:%s:%s", self_sip_uri
, hostname
, ip_address
);
131 ctx
= purple_cipher_context_new_by_name("sha1", NULL
);
132 purple_cipher_context_append(ctx
, (guchar
*)buf
, strlen(buf
));
133 purple_cipher_context_digest(ctx
, sizeof(hash
), hash
, NULL
);
134 purple_cipher_context_destroy(ctx
);
136 for (i
= SIPE_EPID_HASH_START
, j
= 0;
137 i
<= SIPE_EPID_HASH_END
;
139 g_sprintf(&out
[j
], "%02x", hash
[i
]);
141 out
[SIPE_EPID_LENGTH
] = 0;
144 return g_strdup(out
);