+static int add_listener(char *where, int listen_id);
+
+#if YAZ_HAVE_XML2
+static xmlDocPtr xml_config_doc = 0;
+#endif
+
+#if YAZ_HAVE_XML2
+static xmlNodePtr xml_config_get_root(void)
+{
+ xmlNodePtr ptr = 0;
+ if (xml_config_doc)
+ {
+ ptr = xmlDocGetRootElement(xml_config_doc);
+ if (!ptr || ptr->type != XML_ELEMENT_NODE ||
+ strcmp((const char *) ptr->name, "yazgfs"))
+ {
+ yaz_log(YLOG_WARN, "Bad/missing root element for config %s",
+ control_block.xml_config);
+ return 0;
+
+ }
+ }
+ return ptr;
+}
+#endif
+
+#if YAZ_HAVE_XML2
+static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr)
+{
+ unsigned char *cp;
+ xmlNodePtr p;
+ int len = 1; /* start with 1, because of trailing 0 */
+ unsigned char *str;
+ int first = 1; /* whitespace lead flag .. */
+ /* determine length */
+ for (p = ptr; p; p = p->next)
+ {
+ if (p->type == XML_TEXT_NODE)
+ len += xmlStrlen(p->content);
+ }
+ /* now allocate for the string */
+ str = nmem_malloc(n, len);
+ *str = '\0'; /* so we can use strcat */
+ for (p = ptr; p; p = p->next)
+ {
+ if (p->type == XML_TEXT_NODE)
+ {
+ cp = p->content;
+ if (first)
+ {
+ while(*cp && isspace(*cp))
+ cp++;
+ if (*cp)
+ first = 0; /* reset if we got non-whitespace out */
+ }
+ strcat((char *)str, (const char *)cp); /* append */
+ }
+ }
+ /* remove trailing whitespace */
+ cp = strlen((const char *)str) + str;
+ while (cp != str && isspace(cp[-1]))
+ cp--;
+ *cp = '\0';
+ /* return resulting string */
+ return (char *) str;
+}
+#endif
+
+static struct gfs_server * gfs_server_new(void)
+{
+ struct gfs_server *n = nmem_malloc(gfs_nmem, sizeof(*n));
+ memcpy(&n->cb, &control_block, sizeof(control_block));
+ n->next = 0;
+ n->host = 0;
+ n->listen_ref = 0;
+ n->cql_transform = 0;
+ n->server_node_ptr = 0;
+ n->directory = 0;
+ n->docpath = 0;
+ n->stylesheet = 0;
+#if YAZ_HAVE_XML2
+ n->retrieval = yaz_retrieval_create();
+#endif
+ return n;
+}
+
+static struct gfs_listen * gfs_listen_new(const char *id,
+ const char *address)
+{
+ struct gfs_listen *n = nmem_malloc(gfs_nmem, sizeof(*n));
+ if (id)
+ n->id = nmem_strdup(gfs_nmem, id);
+ else
+ n->id = 0;
+ n->next = 0;
+ n->address = nmem_strdup(gfs_nmem, address);
+ return n;
+}
+
+static void gfs_server_chdir(struct gfs_server *gfs)
+{
+ if (gfs_root_dir[0])
+ {
+ if (chdir(gfs_root_dir))
+ yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s", gfs_root_dir);
+ }
+ if (gfs->directory)
+ {
+ if (chdir(gfs->directory))
+ yaz_log(YLOG_WARN|YLOG_ERRNO, "chdir %s",
+ gfs->directory);
+ }
+}
+
+int control_association(association *assoc, const char *host, int force_open)
+{
+ char vhost[128], *cp;
+ if (host)
+ {
+ strncpy(vhost, host, 127);
+ vhost[127] = '\0';
+ cp = strchr(vhost, ':');
+ if (cp)
+ *cp = '\0';
+ host = vhost;
+ }
+ assoc->server = 0;
+ if (control_block.xml_config[0])
+ {
+ struct gfs_server *gfs;
+ for (gfs = gfs_server_list; gfs; gfs = gfs->next)
+ {
+ int listen_match = 0;
+ int host_match = 0;
+ if ( !gfs->host || (host && gfs->host && !strcmp(host, gfs->host)))
+ host_match = 1;
+ if (!gfs->listen_ref ||
+ gfs->listen_ref == assoc->client_chan->chan_id)
+ listen_match = 1;
+ if (listen_match && host_match)
+ {
+ if (force_open ||
+ (assoc->last_control != &gfs->cb && assoc->backend))
+ {
+ statserv_setcontrol(assoc->last_control);
+ if (assoc->backend && assoc->init)
+ {
+ gfs_server_chdir(gfs);
+ (assoc->last_control->bend_close)(assoc->backend);
+ }
+ assoc->backend = 0;
+ xfree(assoc->init);
+ assoc->init = 0;
+ }
+ assoc->server = gfs;
+ assoc->last_control = &gfs->cb;
+ statserv_setcontrol(&gfs->cb);
+
+ gfs_server_chdir(gfs);
+ break;
+ }
+ }
+ if (!gfs)
+ {
+ statserv_setcontrol(0);
+ assoc->last_control = 0;
+ return 0;
+ }
+ }
+ else
+ {
+ statserv_setcontrol(&control_block);
+ assoc->last_control = &control_block;
+ }
+ yaz_log(YLOG_DEBUG, "server select: config=%s",
+ assoc->last_control->configname);
+
+ assoc->maximumRecordSize = assoc->last_control->maxrecordsize;
+ assoc->preferredMessageSize = assoc->last_control->maxrecordsize;
+ cs_set_max_recv_bytes(assoc->client_link, assoc->maximumRecordSize);
+ return 1;
+}
+
+static void xml_config_read(void)
+{
+ struct gfs_server **gfsp = &gfs_server_list;
+ struct gfs_listen **gfslp = &gfs_listen_list;
+#if YAZ_HAVE_XML2
+ xmlNodePtr ptr = xml_config_get_root();
+
+ if (!ptr)
+ return;
+ for (ptr = ptr->children; ptr; ptr = ptr->next)
+ {
+ struct _xmlAttr *attr;
+ if (ptr->type != XML_ELEMENT_NODE)
+ continue;
+ attr = ptr->properties;
+ if (!strcmp((const char *) ptr->name, "listen"))
+ {
+ /*
+ <listen id="listenerid">tcp:@:9999</listen>
+ */
+ const char *id = 0;
+ const char *address =
+ nmem_dup_xml_content(gfs_nmem, ptr->children);
+ for ( ; attr; attr = attr->next)
+ if (!xmlStrcmp(attr->name, BAD_CAST "id")
+ && attr->children && attr->children->type == XML_TEXT_NODE)
+ id = nmem_dup_xml_content(gfs_nmem, attr->children);
+ if (address)
+ {
+ *gfslp = gfs_listen_new(id, address);
+ gfslp = &(*gfslp)->next;
+ *gfslp = 0; /* make listener list consistent for search */
+ }
+ }
+ else if (!strcmp((const char *) ptr->name, "server"))
+ {
+ xmlNodePtr ptr_server = ptr;
+ xmlNodePtr ptr;
+ const char *listenref = 0;
+ const char *id = 0;
+ struct gfs_server *gfs;
+
+ for ( ; attr; attr = attr->next)
+ if (!xmlStrcmp(attr->name, BAD_CAST "listenref")
+ && attr->children && attr->children->type == XML_TEXT_NODE)
+ listenref = nmem_dup_xml_content(gfs_nmem, attr->children);
+ else if (!xmlStrcmp(attr->name, BAD_CAST "id")
+ && attr->children
+ && attr->children->type == XML_TEXT_NODE)
+ id = nmem_dup_xml_content(gfs_nmem, attr->children);
+ else
+ yaz_log(YLOG_WARN, "Unknown attribute '%s' for server",
+ attr->name);
+ gfs = *gfsp = gfs_server_new();
+ gfs->server_node_ptr = ptr_server;
+ if (listenref)
+ {
+ int id_no;
+ struct gfs_listen *gl = gfs_listen_list;
+ for (id_no = 1; gl; gl = gl->next, id_no++)
+ if (gl->id && !strcmp(gl->id, listenref))
+ {
+ gfs->listen_ref = id_no;
+ break;
+ }
+ if (!gl)
+ yaz_log(YLOG_WARN, "Non-existent listenref '%s' in server "
+ "config element", listenref);
+ }
+ for (ptr = ptr_server->children; ptr; ptr = ptr->next)
+ {
+ if (ptr->type != XML_ELEMENT_NODE)
+ continue;
+ if (!strcmp((const char *) ptr->name, "host"))
+ {
+ gfs->host = nmem_dup_xml_content(gfs_nmem,
+ ptr->children);
+ }
+ else if (!strcmp((const char *) ptr->name, "config"))
+ {
+ strcpy(gfs->cb.configname,
+ nmem_dup_xml_content(gfs_nmem, ptr->children));
+ }
+ else if (!strcmp((const char *) ptr->name, "cql2rpn"))
+ {
+ gfs->cql_transform = cql_transform_open_fname(
+ nmem_dup_xml_content(gfs_nmem, ptr->children)
+ );
+ }
+ else if (!strcmp((const char *) ptr->name, "directory"))
+ {
+ gfs->directory =
+ nmem_dup_xml_content(gfs_nmem, ptr->children);
+ }
+ else if (!strcmp((const char *) ptr->name, "docpath"))
+ {
+ gfs->docpath =
+ nmem_dup_xml_content(gfs_nmem, ptr->children);
+ }
+ else if (!strcmp((const char *) ptr->name, "maximumrecordsize"))
+ {
+ gfs->cb.maxrecordsize = atoi(
+ nmem_dup_xml_content(gfs_nmem, ptr->children));
+ }
+ else if (!strcmp((const char *) ptr->name, "stylesheet"))
+ {
+ char *s = nmem_dup_xml_content(gfs_nmem, ptr->children);
+ gfs->stylesheet =
+ nmem_malloc(gfs_nmem, strlen(s) + 2);
+ sprintf(gfs->stylesheet, "/%s", s);
+ }
+ else if (!strcmp((const char *) ptr->name, "explain"))
+ {
+ ; /* being processed separately */
+ }
+ else if (!strcmp((const char *) ptr->name, "retrievalinfo"))
+ {
+ if (yaz_retrieval_configure(gfs->retrieval, ptr))
+ {
+ yaz_log(YLOG_FATAL, "%s in config %s",
+ yaz_retrieval_get_error(gfs->retrieval),
+ control_block.xml_config);
+ exit(1);
+ }
+ }
+ else
+ {
+ yaz_log(YLOG_FATAL, "Unknown element '%s' in config %s",
+ ptr->name, control_block.xml_config);
+ exit(1);
+ }
+ }
+ gfsp = &(*gfsp)->next;
+ }
+ }
+#endif
+ *gfsp = 0;
+}
+
+static void xml_config_open(void)
+{
+ if (!getcwd(gfs_root_dir, FILENAME_MAX))
+ {
+ yaz_log(YLOG_WARN|YLOG_ERRNO, "getcwd failed");
+ gfs_root_dir[0] = '\0';
+ }
+#ifdef WIN32
+ init_control_tls = 1;
+ current_control_tls = TlsAlloc();
+#elif YAZ_POSIX_THREADS
+ init_control_tls = 1;
+ pthread_key_create(¤t_control_tls, 0);
+#endif
+
+ gfs_nmem = nmem_create();
+#if YAZ_HAVE_XML2
+ if (control_block.xml_config[0] == '\0')
+ return;
+
+ if (!xml_config_doc)
+ {
+ xml_config_doc = xmlParseFile(control_block.xml_config);
+ if (!xml_config_doc)
+ {
+ yaz_log(YLOG_FATAL, "Could not parse %s", control_block.xml_config);
+ exit(1);
+ }
+ else
+ {
+ int noSubstitutions = xmlXIncludeProcess(xml_config_doc);
+ if (noSubstitutions == -1)
+ {
+ yaz_log(YLOG_WARN, "XInclude processing failed for config %s",
+ control_block.xml_config);
+ exit(1);
+ }
+ }
+ }
+ xml_config_read();
+#endif
+}
+
+static void xml_config_close(void)
+{
+#if YAZ_HAVE_XML2
+ if (xml_config_doc)
+ {
+ xmlFreeDoc(xml_config_doc);
+ xml_config_doc = 0;
+ }
+#endif
+ gfs_server_list = 0;
+ nmem_destroy(gfs_nmem);
+#ifdef WIN32
+ if (init_control_tls)
+ TlsFree(current_control_tls);
+#elif YAZ_POSIX_THREADS
+ if (init_control_tls)
+ pthread_key_delete(current_control_tls);
+#endif
+}
+
+static void xml_config_add_listeners(void)
+{
+ struct gfs_listen *gfs = gfs_listen_list;
+ int id_no;
+
+ for (id_no = 1; gfs; gfs = gfs->next, id_no++)
+ {
+ if (gfs->address)
+ add_listener(gfs->address, id_no);
+ }
+}
+
+static void xml_config_bend_start(void)
+{
+ if (control_block.xml_config[0])
+ {
+ struct gfs_server *gfs = gfs_server_list;
+ for (; gfs; gfs = gfs->next)
+ {
+ yaz_log(YLOG_DEBUG, "xml_config_bend_start config=%s",
+ gfs->cb.configname);
+ statserv_setcontrol(&gfs->cb);
+ if (control_block.bend_start)
+ {
+ gfs_server_chdir(gfs);
+ (control_block.bend_start)(&gfs->cb);
+ }
+ }
+ }
+ else
+ {
+ yaz_log(YLOG_DEBUG, "xml_config_bend_start default config");
+ statserv_setcontrol(&control_block);
+ if (control_block.bend_start)
+ (*control_block.bend_start)(&control_block);
+ }
+}
+
+static void xml_config_bend_stop(void)
+{
+ if (control_block.xml_config[0])
+ {
+ struct gfs_server *gfs = gfs_server_list;
+ for (; gfs; gfs = gfs->next)
+ {
+ yaz_log(YLOG_DEBUG, "xml_config_bend_stop config=%s",
+ gfs->cb.configname);
+ statserv_setcontrol(&gfs->cb);
+ if (control_block.bend_stop)
+ (control_block.bend_stop)(&gfs->cb);
+ }
+ }
+ else
+ {
+ yaz_log(YLOG_DEBUG, "xml_config_bend_stop default config");
+ statserv_setcontrol(&control_block);
+ if (control_block.bend_stop)
+ (*control_block.bend_stop)(&control_block);
+ }
+}
+