+static GSSEAP_THREAD_ONCE gssEapStatusInfoKeyOnce = GSSEAP_ONCE_INITIALIZER;
+static GSSEAP_THREAD_KEY gssEapStatusInfoKey;
+
+struct gss_eap_status_info {
+ OM_uint32 code;
+ char *message;
+ struct gss_eap_status_info *next;
+};
+
+static void
+destroyStatusInfo(void *arg)
+{
+ struct gss_eap_status_info *p = arg, *next;
+
+ for (p = arg; p != NULL; p = next) {
+ next = p->next;
+ GSSEAP_FREE(p->message);
+ GSSEAP_FREE(p);
+ }
+}
+
+static void
+createStatusInfoKey(void)
+{
+ GSSEAP_KEY_CREATE(&gssEapStatusInfoKey, destroyStatusInfo);
+}
+
+/*
+ * Associate a message with a mechanism (minor) status code. This function
+ * takes ownership of the message regardless of success. The message must
+ * be explicitly cleared, if required, so it is suggested that a specific
+ * minor code is either always or never associated with a message, to avoid
+ * dangling (and potentially confusing) error messages.
+ */
+static void
+saveStatusInfoNoCopy(OM_uint32 minor, char *message)
+{
+ struct gss_eap_status_info **next = NULL, *p;
+
+ GSSEAP_ONCE(&gssEapStatusInfoKeyOnce, createStatusInfoKey);
+
+ p = GSSEAP_GETSPECIFIC(gssEapStatusInfoKey);
+ for (; p != NULL; p = p->next) {
+ if (p->code == minor) {
+ /* Set message in-place */
+ if (p->message != NULL)
+ GSSEAP_FREE(p->message);
+ p->message = message;
+ return;
+ }
+ next = &p->next;
+ }
+
+ p = GSSEAP_CALLOC(1, sizeof(*p));
+ if (p == NULL) {
+ if (message != NULL)
+ GSSEAP_FREE(message);
+ return;
+ }
+
+ p->code = minor;
+ p->message = message;
+
+ if (next != NULL)
+ *next = p;
+ else
+ GSSEAP_SETSPECIFIC(gssEapStatusInfoKey, p);
+}
+
+static const char *
+getStatusInfo(OM_uint32 minor)
+{
+ struct gss_eap_status_info *p;
+
+ GSSEAP_ONCE(&gssEapStatusInfoKeyOnce, createStatusInfoKey);
+
+ for (p = GSSEAP_GETSPECIFIC(gssEapStatusInfoKey);
+ p != NULL;
+ p = p->next) {
+ if (p->code == minor)
+ return p->message;
+ }
+
+ return NULL;
+}
+
+void
+gssEapSaveStatusInfo(OM_uint32 minor, const char *format, ...)
+{
+ char *s;
+ int n;
+ va_list ap;
+
+ va_start(ap, format);
+ n = vasprintf(&s, format, ap);
+ va_end(ap);
+
+ if (n >= 0)
+ saveStatusInfoNoCopy(minor, s);
+}
+