+/* Formats arguments to a string and returns it after allocating memory
+ for it. It must be remembered to free it later. */
+
+char *silc_format(char *fmt, ...)
+{
+ va_list args;
+ static char buf[8192];
+
+ memset(buf, 0, sizeof(buf));
+ va_start(args, fmt);
+ vsnprintf(buf, sizeof(buf) - 1, fmt, args);
+ va_end(args);
+
+ return strdup(buf);
+}
+
+/* Renders ID to suitable to print for example to log file. */
+
+static char rid[256];
+#define _PUT_STRING(__d__, __s__) \
+do { \
+ int __sp = sizeof(__d__) - 1 - strlen(__d__); \
+ if (__sp < strlen(__s__)) { \
+ if (__sp) \
+ strncat(__d__, __s__, (sizeof(__d__) - 1) - strlen(__d__)); \
+ } else { \
+ strncat(__d__, __s__, strlen(__s__)); \
+ } \
+} while(0)
+
+char *silc_id_render(void *id, SilcUInt16 type)
+{
+ char tmp[100];
+ unsigned char tmps[2];
+ char *cp;
+
+ memset(rid, 0, sizeof(rid));
+ switch(type) {
+ case SILC_ID_SERVER:
+ {
+ SilcServerID *server_id = (SilcServerID *)id;
+ if (server_id->ip.data_len > 4) {
+#ifdef HAVE_IPV6
+ struct sockaddr_in6 ipv6;
+ memset(&ipv6, 0, sizeof(ipv6));
+ ipv6.sin6_family = AF_INET6;
+ memmove(&ipv6.sin6_addr, server_id->ip.data, sizeof(ipv6.sin6_addr));
+ if (!getnameinfo((struct sockaddr *)&ipv6, sizeof(ipv6),
+ tmp, sizeof(tmp) - 1, NULL, 0, NI_NUMERICHOST))
+ _PUT_STRING(rid, tmp);
+#endif
+ } else {
+ struct in_addr ipv4;
+ memmove(&ipv4.s_addr, server_id->ip.data, 4);
+ cp = inet_ntoa(ipv4);
+ if (cp)
+ _PUT_STRING(rid, cp);
+ }
+
+ memset(tmp, 0, sizeof(tmp));
+ snprintf(tmp, sizeof(tmp) - 1, ",%d,", ntohs(server_id->port));
+ _PUT_STRING(rid, tmp);
+ SILC_PUT16_MSB(server_id->rnd, tmps);
+ memset(tmp, 0, sizeof(tmp));
+ snprintf(tmp, sizeof(tmp) - 1, "[%02x %02x]", tmps[0], tmps[1]);
+ _PUT_STRING(rid, tmp);
+ }
+ break;
+ case SILC_ID_CLIENT:
+ {
+ SilcClientID *client_id = (SilcClientID *)id;
+ if (client_id->ip.data_len > 4) {
+#ifdef HAVE_IPV6
+ struct sockaddr_in6 ipv6;
+ memset(&ipv6, 0, sizeof(ipv6));
+ ipv6.sin6_family = AF_INET6;
+ memmove(&ipv6.sin6_addr, client_id->ip.data, sizeof(ipv6.sin6_addr));
+ if (!getnameinfo((struct sockaddr *)&ipv6, sizeof(ipv6),
+ tmp, sizeof(tmp) - 1, NULL, 0, NI_NUMERICHOST))
+ _PUT_STRING(rid, tmp);
+#endif
+ } else {
+ struct in_addr ipv4;
+ memmove(&ipv4.s_addr, client_id->ip.data, 4);
+ cp = inet_ntoa(ipv4);
+ if (cp)
+ _PUT_STRING(rid, cp);
+ }
+
+ memset(tmp, 0, sizeof(tmp));
+ snprintf(tmp, sizeof(tmp) - 1, ",%02x,", client_id->rnd);
+ _PUT_STRING(rid, tmp);
+ memset(tmp, 0, sizeof(tmp));
+ snprintf(tmp, sizeof(tmp) - 1, "[%02x %02x %02x %02x...]",
+ client_id->hash[0], client_id->hash[1],
+ client_id->hash[2], client_id->hash[3]);
+ _PUT_STRING(rid, tmp);
+ }
+ break;
+ case SILC_ID_CHANNEL:
+ {
+ SilcChannelID *channel_id = (SilcChannelID *)id;
+ if (channel_id->ip.data_len > 4) {
+#ifdef HAVE_IPV6
+ struct sockaddr_in6 ipv6;
+ memset(&ipv6, 0, sizeof(ipv6));
+ ipv6.sin6_family = AF_INET6;
+ memmove(&ipv6.sin6_addr, channel_id->ip.data, sizeof(ipv6.sin6_addr));
+ if (!getnameinfo((struct sockaddr *)&ipv6, sizeof(ipv6),
+ tmp, sizeof(tmp) - 1, NULL, 0, NI_NUMERICHOST))
+ _PUT_STRING(rid, tmp);
+#endif
+ } else {
+ struct in_addr ipv4;
+ memmove(&ipv4.s_addr, channel_id->ip.data, 4);
+ cp = inet_ntoa(ipv4);
+ if (cp)
+ _PUT_STRING(rid, cp);
+ }
+
+ memset(tmp, 0, sizeof(tmp));
+ snprintf(tmp, sizeof(tmp) - 1, ",%d,", ntohs(channel_id->port));
+ _PUT_STRING(rid, tmp);
+ SILC_PUT16_MSB(channel_id->rnd, tmps);
+ memset(tmp, 0, sizeof(tmp));
+ snprintf(tmp, sizeof(tmp) - 1, "[%02x %02x]", tmps[0], tmps[1]);
+ _PUT_STRING(rid, tmp);
+ }
+ break;
+ }
+
+ return rid;
+}
+#undef _PUT_STRING
+
+/* Compares two strings. Strings may include wildcards '*' and '?'.