char *locator_query(char *hostname, enum locator_servicetype_t svctype, char **extras) { static char *buf = NULL; static int bufsz = 0; int res, bufneeded; bufneeded = strlen(hostname) + 100; if (extras) bufneeded += 1024; if (!buf) { bufsz = bufneeded; buf = (char *)malloc(bufsz); } else if (bufneeded > bufsz) { bufsz = bufneeded; buf = (char *)realloc(buf, bufsz); } if (havecache[svctype] && !extras) { char *cachedata = locator_querycache(svctype, hostname); if (cachedata) return cachedata; } sprintf(buf, "Q|%s|%s", servicetype_names[svctype], hostname); if (extras) buf[0] = 'X'; res = call_locator(buf, bufsz); if (res == -1) return NULL; switch (*buf) { case '!': /* This host is fixed on an available server */ case '*': /* Roaming host */ if (extras) { *extras = strchr(buf+2, '|'); if (**extras == '|') { **extras = '\0'; (*extras)++; } } if (havecache[svctype] && !extras) locator_updatecache(svctype, hostname, buf+2); return ((strlen(buf) > 2) ? buf+2 : NULL); case '?': /* No available server to handle the request */ locator_flushcache(svctype, hostname); return NULL; } return NULL; }
int locator_register_server(char *servername, enum locator_servicetype_t svctype, int weight, enum locator_sticky_t sticky, char *extras) { char *buf; int bufsz; int res; bufsz = strlen(servername) + 100; if (extras) bufsz += (strlen(extras) + 1); buf = (char *)malloc(bufsz); if (sticky == LOC_SINGLESERVER) weight = -1; sprintf(buf, "S|%s|%s|%d|%d|%s", servername, servicetype_names[svctype], weight, ((sticky == LOC_STICKY) ? 1 : 0), (extras ? extras : "")); res = call_locator(buf, bufsz); xfree(buf); return res; }