- if (!(addr = cs_straddr(link, global_parameters.zproxy_override)))
- {
- yaz_log(YLOG_WARN|YLOG_ERRNO,
- "Lookup of IP address %s failed",
- global_parameters.zproxy_override);
- return 0;
- }
- }
-
- res = cs_connect(link, addr);
- if (res < 0)
- {
- yaz_log(YLOG_WARN|YLOG_ERRNO, "cs_connect %s", cl->database->url);
- return 0;
- }
-
- if ((new = connection_freelist))
- connection_freelist = new->next;
- else
- {
- new = xmalloc(sizeof (struct connection));
- new->ibuf = 0;
- new->ibufsize = 0;
- }
- new->state = Conn_Connecting;
- new->host = cl->database->host;
- new->next = new->host->connections;
- new->host->connections = new;
- new->client = cl;
- cl->connection = new;
- new->link = link;
-
- new->iochan = iochan_create(cs_fileno(link), 0, handler, 0);
- iochan_setdata(new->iochan, new);
- new->iochan->next = channel_list;
- channel_list = new->iochan;
- return new;
-}
-
-// Close connection and set state to error
-static void client_fatal(struct client *cl)
-{
- yaz_log(YLOG_WARN, "Fatal error from %s", cl->database->url);
- connection_destroy(cl->connection);
- cl->state = Client_Error;
-}
-
-// Ensure that client has a connection associated
-static int client_prep_connection(struct client *cl)
-{
- struct connection *co;
- struct session *se = cl->session;
- struct host *host = cl->database->host;
-
- co = cl->connection;
-
- yaz_log(YLOG_DEBUG, "Client prep %s", cl->database->url);
-
- if (!co)
- {
- // See if someone else has an idle connection
- // We should look at timestamps here to select the longest-idle connection
- for (co = host->connections; co; co = co->next)
- if (co->state == Conn_Open && (!co->client || co->client->session != se))
- break;
- if (co)
- {
- connection_release(co);
- cl->connection = co;
- co->client = cl;
- }
- else
- co = connection_create(cl);
- }
- if (co)
- {
- if (co->state == Conn_Connecting)
- {
- cl->state = Client_Connecting;
- iochan_setflag(co->iochan, EVENT_OUTPUT);
- }
- else if (co->state == Conn_Open)
- {
- if (cl->state == Client_Error || cl->state == Client_Disconnected)
- cl->state = Client_Idle;
- iochan_setflag(co->iochan, EVENT_OUTPUT);
- }
- return 1;
- }
- else
- return 0;
-}
-
-#ifdef GAGA // Moved to database.c
-
-// This function will most likely vanish when a proper target profile mechanism is
-// introduced.
-void load_simpletargets(const char *fn)
-{
- FILE *f = fopen(fn, "r");
- char line[256];
-
- if (!f)
- {
- yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", fn);
- exit(1);
- }
-
- while (fgets(line, 255, f))
- {
- char *url, *db;
- char *name;
- struct host *host;
- struct database *database;
-
- if (strncmp(line, "target ", 7))
- continue;
- line[strlen(line) - 1] = '\0';
-
- if ((name = strchr(line, ';')))
- *(name++) = '\0';
-
- url = line + 7;
- if ((db = strchr(url, '/')))
- *(db++) = '\0';
- else
- db = "Default";
-
- yaz_log(YLOG_LOG, "Target: %s, '%s'", url, db);
- for (host = hosts; host; host = host->next)
- if (!strcmp((const char *) url, host->hostport))
- break;
- if (!host)
- {
- struct addrinfo *addrinfo, hints;
- char *port;
- char ipport[128];
- unsigned char addrbuf[4];
- int res;
-
- host = xmalloc(sizeof(struct host));
- host->hostport = xstrdup(url);
- host->connections = 0;
-
- if ((port = strchr(url, ':')))
- *(port++) = '\0';
- else
- port = "210";
-
- hints.ai_flags = 0;
- hints.ai_family = PF_INET;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- hints.ai_addrlen = 0;
- hints.ai_addr = 0;
- hints.ai_canonname = 0;
- hints.ai_next = 0;
- // This is not robust code. It assumes that getaddrinfo returns AF_INET
- // address.
- if ((res = getaddrinfo(url, port, &hints, &addrinfo)))
- {
- yaz_log(YLOG_WARN, "Failed to resolve %s: %s", url, gai_strerror(res));
- xfree(host->hostport);
- xfree(host);
- continue;
- }
- assert(addrinfo->ai_family == PF_INET);
- memcpy(addrbuf, &((struct sockaddr_in*)addrinfo->ai_addr)->sin_addr.s_addr, 4);
- sprintf(ipport, "%u.%u.%u.%u:%s",
- addrbuf[0], addrbuf[1], addrbuf[2], addrbuf[3], port);
- host->ipport = xstrdup(ipport);
- freeaddrinfo(addrinfo);
- host->next = hosts;
- hosts = host;
- }
- database = xmalloc(sizeof(struct database));
- database->host = host;
- database->url = xmalloc(strlen(url) + strlen(db) + 2);
- strcpy(database->url, url);
- strcat(database->url, "/");
- strcat(database->url, db);
- if (name)
- database->name = xstrdup(name);
- else
- database->name = 0;
-
- database->databases = xmalloc(2 * sizeof(char *));
- database->databases[0] = xstrdup(db);
- database->databases[1] = 0;
- database->errors = 0;
- database->qprofile = 0;
- database->rprofile = database_retrieval_profile(database);
- database->next = databases;
- databases = database;
-
- }
- fclose(f);
-}
-
-#endif
-
-static struct client *client_create(void)
-{
- struct client *r;
- if (client_freelist)
- {
- r = client_freelist;
- client_freelist = client_freelist->next;
- }
- else
- r = xmalloc(sizeof(struct client));
- r->database = 0;
- r->connection = 0;
- r->session = 0;
- r->hits = 0;
- r->records = 0;
- r->setno = 0;
- r->requestid = -1;
- r->diagnostic = 0;
- r->state = Client_Disconnected;
- r->next = 0;
- return r;
-}
-
-void client_destroy(struct client *c)
-{
- struct session *se = c->session;
- if (c == se->clients)
- se->clients = c->next;
- else
- {
- struct client *cc;
- for (cc = se->clients; cc && cc->next != c; cc = cc->next)
- ;
- if (cc)
- cc->next = c->next;
- }
- if (c->connection)
- connection_release(c->connection);
- c->next = client_freelist;
- client_freelist = c;
-}
-
-void session_set_watch(struct session *s, int what, session_watchfun fun, void *data)
-{
- s->watchlist[what].fun = fun;
- s->watchlist[what].data = data;
-}
-
-void session_alert_watch(struct session *s, int what)
-{
- if (!s->watchlist[what].fun)
- return;
- (*s->watchlist[what].fun)(s->watchlist[what].data);
- s->watchlist[what].fun = 0;
- s->watchlist[what].data = 0;
-}
-
-//callback for grep_databases
-static void select_targets_callback(void *context, struct database *db)
-{
- struct session *se = (struct session*) context;
- struct client *cl = client_create();
- cl->database = db;
- cl->session = se;
- cl->next = se->clients;
- se->clients = cl;
-}
-
-// This should be extended with parameters to control selection criteria
-// Associates a set of clients with a session;
-int select_targets(struct session *se, struct database_criterion *crit)
-{
- while (se->clients)
- client_destroy(se->clients);
-
- return grep_databases(se, crit, select_targets_callback);
-}
-
-int session_active_clients(struct session *s)
-{
- struct client *c;
- int res = 0;
-
- for (c = s->clients; c; c = c->next)
- if (c->connection && (c->state == Client_Connecting ||
- c->state == Client_Initializing ||
- c->state == Client_Searching ||
- c->state == Client_Presenting))
- res++;
-
- return res;
-}
-
-// parses crit1=val1,crit2=val2|val3,...
-static struct database_criterion *parse_filter(NMEM m, const char *buf)
-{
- struct database_criterion *res = 0;
- char **values;
- int num;
- int i;
-
- if (!buf || !*buf)
- return 0;
- nmem_strsplit(m, ",", buf, &values, &num);
- for (i = 0; i < num; i++)
- {
- char **subvalues;
- int subnum;
- int subi;
- struct database_criterion *new = nmem_malloc(m, sizeof(*new));
- char *eq = strchr(values[i], '=');
- if (!eq)
- {
- yaz_log(YLOG_WARN, "Missing equal-sign in filter");
- return 0;
- }
- *(eq++) = '\0';
- new->name = values[i];
- nmem_strsplit(m, "|", eq, &subvalues, &subnum);
- new->values = 0;
- for (subi = 0; subi < subnum; subi++)
- {
- struct database_criterion_value *newv = nmem_malloc(m, sizeof(*newv));
- newv->value = subvalues[subi];
- newv->next = new->values;
- new->values = newv;
- }
- new->next = res;
- res = new;
- }
- return res;
-}
-
-char *search(struct session *se, char *query, char *filter)
-{
- int live_channels = 0;
- struct client *cl;
- struct database_criterion *criteria;
-
- yaz_log(YLOG_DEBUG, "Search");
-
- nmem_reset(se->nmem);
- criteria = parse_filter(se->nmem, filter);
- strcpy(se->query, query);
- se->requestid++;
- // Release any existing clients
- select_targets(se, criteria);
- for (cl = se->clients; cl; cl = cl->next)
- {
- if (client_prep_connection(cl))
- live_channels++;
- }
- if (live_channels)
- {
- int maxrecs = live_channels * global_parameters.toget;
- se->num_termlists = 0;
- se->reclist = reclist_create(se->nmem, maxrecs);
- // This will be initialized in send_search()
- se->relevance = 0;
- se->total_records = se->total_hits = se->total_merged = 0;
- se->expected_maxrecs = maxrecs;
- }
- else
- return "NOTARGETS";
-
- return 0;
-}
-
-void destroy_session(struct session *s)
-{
- yaz_log(YLOG_LOG, "Destroying session");
- while (s->clients)
- client_destroy(s->clients);
- nmem_destroy(s->nmem);
- wrbuf_destroy(s->wrbuf);
-}
-
-struct session *new_session()
-{
- int i;
- struct session *session = xmalloc(sizeof(*session));
-
- yaz_log(YLOG_DEBUG, "New pazpar2 session");
-
- session->total_hits = 0;
- session->total_records = 0;
- session->num_termlists = 0;
- session->reclist = 0;
- session->requestid = -1;
- session->clients = 0;
- session->expected_maxrecs = 0;
- session->query[0] = '\0';
- session->nmem = nmem_create();
- session->wrbuf = wrbuf_alloc();
- for (i = 0; i <= SESSION_WATCH_MAX; i++)
- {
- session->watchlist[i].data = 0;
- session->watchlist[i].fun = 0;