#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <arpa/inet.h>
#include <slp-internal.h>
#include <slp_net_utils.h>
typedef enum {
SLP_REMOTE_PROX = 0,
SLP_SUBNET_PROX = 1,
SLP_LOCAL_PROX = 2
} slp_net_prox;
struct da_node {
struct sockaddr_in sin;
char *scopes;
SLPBoolean used, failed;
int coverage;
slp_net_prox proximity;
struct da_node *next, *prev;
};
struct scope_targets {
struct da_node *da;
struct scope_targets *next;
};
struct target_list {
struct scope_targets **scopes;
struct scope_targets **state;
char *uc_scopes;
char *mc_scopes;
char *all_scopes;
struct da_node *DAs;
};
static void add2scopes_list(struct da_node *, struct target_list *);
static void add_da_entry(struct da_node **, struct sockaddr_in *,
char *, slp_net_prox, int);
static SLPSrvURLCallback collect_DAs;
static void format_query(char *, const char *);
SLPError slp_new_target_list(slp_handle_impl_t *hp, const char *scopes,
slp_target_list_t **handle) {
struct target_list *tl;
int scope_cnt;
char *p;
struct da_node *te;
char *query, *reply;
SLPError err;
void *collator = NULL;
scope_cnt = 0;
for (p = (char *)scopes; p; p++) {
p = slp_utf_strchr(p, ',');
scope_cnt++;
if (!p)
break;
}
if (!(tl = calloc(1, sizeof (*tl)))) {
slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
return (SLP_MEMORY_ALLOC_FAILED);
}
tl->DAs = NULL;
if (!(tl->scopes = calloc(scope_cnt + 1, sizeof (*(tl->scopes))))) {
slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
free(tl);
return (SLP_MEMORY_ALLOC_FAILED);
}
tl->uc_scopes = NULL;
tl->state = tl->scopes;
if (!(tl->all_scopes = strdup(scopes))) {
slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
free(tl->scopes); free(tl);
return (SLP_MEMORY_ALLOC_FAILED);
}
if (!(tl->mc_scopes = strdup(scopes))) {
slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
free(tl->scopes); free(tl->all_scopes); free(tl);
return (SLP_MEMORY_ALLOC_FAILED);
}
if (hp->force_multicast) {
*handle = tl;
return (SLP_OK);
}
if (!(query = malloc(strlen(scopes) -
(scope_cnt - 1) +
strlen(SLP_SUN_VERSION_TAG) +
strlen("(&(=2)(|))") + 1 +
(scope_cnt *
(strlen(SLP_SUN_SCOPES_TAG) +
strlen("(=)")))))) {
slp_err(LOG_CRIT, 0, "slp_new_target_list", "out of memory");
free(tl->scopes);
free(tl->all_scopes);
free(tl->mc_scopes);
free(tl);
return (SLP_MEMORY_ALLOC_FAILED);
}
format_query(query, scopes);
if ((err = slp_find_das(query, &reply)) != SLP_OK &&
err != SLP_NETWORK_ERROR) {
free(tl->scopes);
free(tl->all_scopes);
free(tl->mc_scopes);
free(tl);
free(query);
return (err);
}
free(query);
if (reply) {
int numResults = 0;
hp->internal_call = SLP_TRUE;
(void) slp_unpackSrvReply(hp, reply, collect_DAs,
tl, &collator, &numResults);
free(reply);
(void) slp_unpackSrvReply(hp, NULL, collect_DAs,
tl, &collator, &numResults);
hp->internal_call = SLP_FALSE;
}
for (te = tl->DAs; te; te = te->next)
add2scopes_list(te, tl);
*handle = tl;
return (SLP_OK);
}
const char *slp_get_uc_scopes(slp_target_list_t *h) {
struct target_list *tl = (struct target_list *)h;
return (tl->uc_scopes);
}
const char *slp_get_mc_scopes(slp_target_list_t *h) {
struct target_list *tl = (struct target_list *)h;
return (tl->mc_scopes);
}
slp_target_t *slp_next_uc_target(slp_target_list_t *h) {
struct scope_targets *p;
struct target_list *tl = (struct target_list *)h;
if (!(*tl->state))
return (NULL);
for (; *tl->state; tl->state++) {
if (!(*tl->state)->da->used && !(*tl->state)->da->failed)
return (*tl->state++);
if ((*tl->state)->da->failed) {
if (p = slp_next_failover(*tl->state)) {
tl->state++;
return (p);
}
}
}
return (NULL);
}
slp_target_t *slp_next_failover(slp_target_t *h) {
struct scope_targets *p = (struct scope_targets *)h;
for (p = p->next; p; p = p->next) {
if (p->da->used)
return (NULL);
if (!p->da->used && !p->da->failed)
return (p);
}
return (NULL);
}
void *slp_get_target_sin(slp_target_t *h) {
struct scope_targets *p = (struct scope_targets *)h;
return (void *)(p ? &(p->da->sin) : NULL);
}
void slp_mark_target_used(slp_target_t *h) {
struct scope_targets *p = (struct scope_targets *)h;
p->da->used = SLP_TRUE;
}
void slp_mark_target_failed(slp_target_t *h) {
struct scope_targets *p = (struct scope_targets *)h;
p->da->failed = SLP_TRUE;
}
slp_target_t *slp_fabricate_target(void *s) {
struct da_node *dn;
struct scope_targets *st;
struct sockaddr_in *sin = (struct sockaddr_in *)s;
if (!(st = malloc(sizeof (*st)))) {
slp_err(LOG_CRIT, 0, "slp_fabricate_target", "out of memory");
return (NULL);
}
if (!(dn = malloc(sizeof (*dn)))) {
free(st);
slp_err(LOG_CRIT, 0, "slp_fabricate_target", "out of memory");
return (NULL);
}
(void) memcpy(&(dn->sin), sin, sizeof (dn->sin));
dn->used = dn->failed = SLP_FALSE;
dn->coverage = 0;
dn->proximity = SLP_REMOTE_PROX;
dn->next = dn->prev = NULL;
st->da = dn;
st->next = NULL;
return (st);
}
void slp_free_target(slp_target_t *target) {
struct scope_targets *t = (struct scope_targets *)target;
if (!t)
return;
free(t->da);
free(t);
}
void slp_destroy_target_list(slp_target_list_t *h) {
struct da_node *das, *dap;
int i;
struct target_list *tl = (struct target_list *)h;
for (das = tl->DAs; das; das = dap) {
dap = das->next;
free(das->scopes);
free(das);
}
for (i = 0; tl->scopes[i]; i++) {
struct scope_targets *sts, *stp;
for (sts = tl->scopes[i]; sts; sts = stp) {
stp = sts->next;
free(sts);
}
}
free(tl->scopes);
if (tl->uc_scopes)
free(tl->uc_scopes);
if (tl->mc_scopes)
free(tl->mc_scopes);
free(tl->all_scopes);
free(tl);
}
static void add2scopes_list(struct da_node *te, struct target_list *tl) {
struct scope_targets **scopes = tl->scopes;
char *p, *s;
int i;
i = 0;
for (s = tl->uc_scopes; s; s = p) {
p = slp_utf_strchr(s, ',');
if (p)
*p = 0;
if (slp_onlist(s, te->scopes)) {
struct scope_targets *st, *stp;
if (!(st = malloc(sizeof (*st)))) {
slp_err(LOG_CRIT, 0, "add2scopes_list",
"out of memory");
return;
}
st->da = te;
st->next = NULL;
for (stp = scopes[i]; stp && stp->next; ) {
stp = stp->next;
}
if (stp)
stp->next = st;
else
scopes[i] = st;
}
if (p)
*p++ = ',';
i++;
}
}
static void add_da_entry(struct da_node **tel, struct sockaddr_in *sin,
char *scopes, slp_net_prox proximity, int c) {
struct da_node *te, *p;
if (!(te = malloc(sizeof (*te)))) {
slp_err(LOG_CRIT, 0, "add_da_entry", "out of memory");
return;
}
te->scopes = scopes;
te->coverage = c;
te->proximity = proximity;
(void) memcpy(&(te->sin), sin, sizeof (te->sin));
te->used = SLP_FALSE;
te->failed = SLP_FALSE;
te->prev = NULL;
te->next = NULL;
if (!(*tel)) {
*tel = te;
return;
}
for (p = *tel; p; p = p->next)
if (c >= p->coverage) {
for (; p && proximity < p->proximity; )
p = p->next;
if (!p) {
break;
}
te->next = p;
te->prev = p->prev;
if (p->prev)
p->prev->next = te;
else
(*tel) = te;
p->prev = te;
return;
}
for (p = *tel; p->next; )
p = p->next;
p->next = te;
te->prev = p;
}
static SLPBoolean collect_DAs(SLPHandle h, const char *u,
unsigned short lifetime,
SLPError errCode, void *cookie) {
SLPSrvURL *surl = NULL;
char *s, *p, *sscopes, *sscopes_end, *url;
int coverage, proximity;
struct sockaddr_in sin[1];
struct target_list *tl = (struct target_list *)cookie;
if (errCode != SLP_OK)
return (SLP_TRUE);
if (!(url = strdup(u))) {
slp_err(LOG_CRIT, 0, "collect_DAs", "out of memory");
return (SLP_FALSE);
}
if (SLPParseSrvURL(url, &surl) != SLP_OK) {
return (SLP_TRUE);
}
if (slp_surl2sin(surl, sin) != SLP_OK) {
goto cleanup;
}
if (slp_on_localhost(h, sin->sin_addr)) {
proximity = SLP_LOCAL_PROX;
} else if (slp_on_subnet(h, sin->sin_addr)) {
proximity = SLP_SUBNET_PROX;
} else {
proximity = SLP_REMOTE_PROX;
}
coverage = 0;
if (!(sscopes = slp_utf_strchr(surl->s_pcSrvPart, '='))) {
goto cleanup;
}
sscopes++;
if (sscopes_end = slp_utf_strchr(sscopes, '=')) {
*sscopes_end = 0;
}
if (!(sscopes = strdup(sscopes))) {
slp_err(LOG_CRIT, 0, "collect_DAs", "out of memory");
free(surl);
return (SLP_FALSE);
}
for (s = tl->all_scopes; s; s = p) {
p = slp_utf_strchr(s, ',');
if (p)
*p = 0;
if (slp_onlist(s, sscopes)) {
slp_add2list(s, &(tl->uc_scopes), SLP_TRUE);
slp_list_subtract(s, &(tl->mc_scopes));
coverage++;
}
if (p)
*p++ = ',';
}
if (coverage)
add_da_entry(&(tl->DAs), sin, sscopes, proximity, coverage);
cleanup:
free(url);
if (surl) free(surl);
return (SLP_TRUE);
}
static void format_query(char *q, const char *scopes) {
char *p, *s;
int more_than_one = slp_utf_strchr(scopes, ',') ? 1 : 0;
*q++ = '('; *q++ = '&';
if (more_than_one) {
*q++ = '('; *q++ = '|';
}
for (p = s = (char *)scopes; p; s = p) {
*q++ = '(';
(void) strcpy(q, SLP_SUN_SCOPES_TAG);
q += strlen(SLP_SUN_SCOPES_TAG);
*q++ = '=';
p = slp_utf_strchr(s, ',');
if (p) {
(void) memcpy(q, s, p - s);
q += (p - s);
p++;
} else {
(void) strcpy(q, s);
q += strlen(s);
}
*q++ = ')';
}
if (more_than_one) {
*q++ = ')';
}
*q++ = '(';
(void) strcpy(q, SLP_SUN_VERSION_TAG);
q += strlen(SLP_SUN_VERSION_TAG);
*q++ = '=';
*q++ = '2';
*q++ = ')';
*q++ = ')';
*q = 0;
}