📄 iscsi-slp-discovery.c
字号:
} free(start_url); } else { if (discovery) { send_slp_discovery_data(discovery); free_slp_discovery_data(discovery); } } return SLP_TRUE;}SLPBoolean AttrQueryCallback(SLPHandle hslp, const char* attrlist, int errcode, void* cookie){ struct slp_discovery_process *process; struct slp_target *target; struct slp_portal *portal; struct iscsi_discovery_process *discovery = NULL; char *tmp_url, *start_url = NULL; char *tag, *tmp; char *ip, *port, *target_name = NULL; if (errcode == SLP_OK) { debugmsg(7, "SLP attribute list :%s\n",attrlist); } if (attrlist) { start_url = strdup(attrlist); } tmp_url = start_url; if (cookie) { discovery = (struct iscsi_discovery_process *) ((struct slp_discovery_pg_cookie *)cookie)-> discovery; ip = (char *)((struct slp_discovery_pg_cookie *)cookie)->ip; port = (char *)((struct slp_discovery_pg_cookie *)cookie)->port; target_name = (char *)((struct slp_discovery_pg_cookie *) cookie)->target_name; } if (tmp_url) { tmp = strstr(tmp_url, "portal-group="); tmp = strchr(tmp, '='); tmp++; tmp = strtok(tmp,")"); tag = strdup(tmp); if (list) { for (process = list->head; process; process = process->next) { if (process->discovery == discovery) { for (target = process->targets; target; target = target->next) { if (strcmp(target->target_name, target_name) == 0) { for (portal = target-> portals; portal; portal = portal-> next) { portal->tag = tag; } } } } } } free(start_url); } else { return SLP_TRUE; } return SLP_TRUE;}voidslp_multicast_srv_query(struct iscsi_discovery_process *discovery, char *pl, unsigned short flag){ SLPError result; SLPHandle hslp; struct iscsi_slp_config *config = discovery->entry->config.slp; if (SLPOpen(NULL, SLP_FALSE, &hslp) == SLP_OK) { if ((result = SLPAssociateIFList(hslp, config->interfaces)) != SLP_OK) { logmsg(AS_ERROR, "SLP query error on discovery pid = %d " "with SLP errorcode: %i\n", discovery->pid, result); SLPClose(hslp); return; } } switch (flag) { case GENERIC_QUERY: result = SLPFindSrvs(hslp, ISCSI_SERVICE, NULL, pl, SrvUrlCallback, discovery); break; default: result = SLP_FALSE; } if (result != SLP_OK) { logmsg(AS_ERROR, "SLP query error on discovery pid = %d " "with SLP errorcode: %i\n", discovery->pid, result); } SLPClose(hslp);}void slp_multicast_attr_query(struct iscsi_discovery_process *discovery, char* url, char *pl, char *target_name, char *ip, char *port){ SLPError result; SLPHandle hslp; struct iscsi_slp_config *config = discovery->entry->config.slp; struct slp_discovery_pg_cookie cookie; if (SLPOpen(NULL,SLP_FALSE,&hslp) == SLP_OK) { if ((result = SLPAssociateIFList(hslp, config->interfaces)) \ != SLP_OK) { logmsg(AS_ERROR, "SLP query error on discovery %p with " "SLP errorcode: %i\n",discovery, result); SLPClose(hslp); return; } } cookie.discovery = (struct iscsi_discovery_process *)discovery; cookie.target_name = (char *) malloc(strlen(target_name) + 1); memset(cookie.target_name, 0, strlen(target_name) + 1); strcpy(cookie.target_name, target_name); cookie.ip = (char *) malloc(strlen(ip) + 1); memset(cookie.ip, 0, strlen(ip) + 1); strcpy(cookie.ip, ip); cookie.port = (char *) malloc(strlen(port) + 1); memset(cookie.port, 0, strlen(port) + 1); strcpy(cookie.port, port); result = SLPFindAttrs(hslp, ISCSI_SERVICE, NULL, pl, AttrQueryCallback, &cookie); if (result != SLP_OK) { logmsg(AS_ERROR, "SLP query error on discovery %p " "with SLP errorcode: %i\n",discovery, result); } SLPClose(hslp);}voidslp_unicast_srv_query(struct iscsi_discovery_process *discovery, char *pl, unsigned short flag){ SLPError result; SLPHandle hslp; struct iscsi_slp_config *config = discovery->entry->config.slp; if (SLPOpen(NULL, SLP_FALSE, &hslp) == SLP_OK) { if ((result = SLPAssociateIP(hslp, config->address)) != SLP_OK) { logmsg(AS_ERROR, "SLP query error on discovery pid = %d " "with SLP errorcode: %i\n", discovery->pid, result); SLPClose(hslp); return; } } switch (flag) { case GENERIC_QUERY: result = SLPFindSrvs(hslp, ISCSI_SERVICE, NULL, pl, SrvUrlCallback, discovery); break; default: result = SLP_FALSE; } if (result != SLP_OK) { logmsg(AS_ERROR, "SLP query error on discovery pid = %d " "with SLP errorcode: %i\n", discovery->pid, result); } SLPClose(hslp);}void slp_unicast_attr_query(struct iscsi_discovery_process *discovery, char* url, char *pl, char *target_name, char *ip, char *port){ SLPError result; SLPHandle hslp; struct iscsi_slp_config *config = discovery->entry->config.slp; struct slp_discovery_pg_cookie cookie; if (SLPOpen(NULL,SLP_FALSE,&hslp) == SLP_OK) { if ((result = SLPAssociateIP(hslp, config->address)) \ != SLP_OK) { logmsg(AS_ERROR, "SLP query error on discovery %p with " "SLP errorcode: %i\n",discovery, result); SLPClose(hslp); return; } } cookie.discovery = (struct iscsi_discovery_process *)discovery; cookie.target_name = (char *) malloc(strlen(target_name) + 1); memset(cookie.target_name, 0, strlen(target_name) + 1); strcpy(cookie.target_name, target_name); cookie.ip = (char *) malloc(strlen(ip) + 1); memset(cookie.ip, 0, strlen(ip) + 1); strcpy(cookie.ip, ip); cookie.port = (char *) malloc(strlen(port) + 1); memset(cookie.port, 0, strlen(port) + 1); strcpy(cookie.port, port); result = SLPFindAttrs(hslp, url, NULL, pl, AttrQueryCallback, &cookie); if (result != SLP_OK) { logmsg(AS_ERROR, "SLP query error on discovery %p " "with SLP errorcode: %i\n",discovery, result); } SLPClose(hslp);}char *generate_pl(char *interfaces){ char slp_predicate_list[PREDICATE_LEN]; char *pl = slp_predicate_list; int SLPpl; char *temp, *startp = strdup(interfaces); char *endp; char *c; temp = startp; if (strlen(temp)) { sprintf(pl, "(auth-addr=any)"); endp = strrchr(temp, ','); /* more than one IP address */ if (endp) { while (endp > temp) { c = strchr(temp, ','); *c = '\0'; c += 1; sprintf(pl + strlen(pl), "(auth-addr=%s)", temp); temp = c; } } sprintf(pl + strlen(pl), "(auth-addr=%s)", temp); free(startp); SLPpl = 1; } else SLPpl = 0; if (SLPpl) { pl = strdup(slp_predicate_list); return pl; } return NULL;}char *multicast_predicate_list(struct iscsi_discovery_process *discovery){ struct iscsi_slp_config *config = discovery->entry->config.slp; char *mpl = NULL; if (config->interfaces && strlen(config->interfaces)) { if (strcmp(config->interfaces, "none") == 0) { debugmsg(1, "SLP Multicast is disabled for " "discovery process pid = %d\n", discovery->pid); } else if (strcmp(config->interfaces, "all") == 0) { free(config->interfaces); config->interfaces = get_all_ip(discovery); if (config->interfaces) mpl = generate_pl(config->interfaces); } else { /* comma separated list of IP's */ mpl = generate_pl(config->interfaces); } } return mpl;}char *unicast_predicate_list(struct iscsi_discovery_process *discovery){ struct iscsi_slp_config *config = discovery->entry->config.slp; char *interfaces; char *upl = NULL; if (config->address && strlen(config->address)) { interfaces = get_all_ip(discovery); if (interfaces) upl = generate_pl(interfaces); } return upl;}char *cred_predicate_list(struct iscsi_discovery_process *discovery){ struct iscsi_slp_config *config = discovery->entry->config.slp; char cred_predicate_list[CRED_PREDICATE_LEN]; char *slpc = cred_predicate_list; sprintf(slpc, "(auth-cred=any)"); if (strlen(config->auth_options.username)) { /* We ll have one more case when we add SRP support */ switch(config->auth_options.authmethod) { case CHAP_AUTHENTICATION: default: sprintf(slpc + strlen(slpc), "(auth-cred=chap/%s)", config->auth_options.username); } } slpc = strdup(cred_predicate_list); return slpc;}char *name_predicate_list(void){ char name_predicate[NAME_PREDICATE_LEN]; char *slpn = name_predicate; char *initiator_name; sprintf(slpn, "(auth-name=any)"); initiator_name = get_iscsi_initiatorname(INITIATOR_NAME); if (initiator_name && strlen(initiator_name)) { sprintf(slpn + strlen(slpn), "(auth-name=%s)", initiator_name); } slpn = strdup(name_predicate); return slpn;}char *generate_predicate_list(struct iscsi_discovery_process *discovery, unsigned short *flag){ char *mpl; char *upl; char *cpl; char *npl; char *pl; int plen = 0; mpl = multicast_predicate_list(discovery); upl = unicast_predicate_list(discovery); cpl = cred_predicate_list(discovery); npl = name_predicate_list(); if (mpl) { debugmsg(7, "multicast predicate list for " "discovery process pid = %d is %s\n", discovery->pid, mpl); plen += strlen(mpl); *flag = SLP_MULTICAST_ENABLED; } if (upl) { debugmsg(7, "unicast predicate list for discovery " "process pid = %d is %s\n", discovery->pid, upl); plen += strlen(upl); *flag = SLP_UNICAST_ENABLED; } if (plen) { if (cpl) { debugmsg(7, "credential predicate list for " "discovery process pid = %d is %s\n", discovery->pid, cpl); plen += strlen(cpl); } if (npl) { debugmsg(7, "name predicate list for discovery " "process pid = %d is %s\n", discovery->pid, npl); logmsg(AS_ERROR, "name predicate list for " "discovery process pid = %d is %s\n", discovery->pid, npl); plen += strlen(npl); } } if (plen) { char *c; plen += 16; /* This extra space is required for * "(&(|%s)(|%s)(|%s)(|%s))" * whearas, 1st %s is for mpl, * 2nd %s is for upl, * 3rd %s is for cpl, * 4th %s is for npl. */ pl = (char *) malloc(sizeof (char) * plen); memset(pl, 0, plen); c = pl; sprintf(c, "(&"); c += strlen(c); if (mpl) { sprintf(c, "(|%s)", mpl); c += strlen(c); free(mpl); } if (upl) { sprintf(c, "(|%s)", upl); c += strlen(c); free(upl); } sprintf(c, "(|%s)", cpl); c += strlen(c); free(cpl); sprintf(c, "(|%s)", npl); c += strlen(c); free(npl); sprintf(c, ")"); /* close brace correspond to '(&' */ debugmsg(3, "SLP predicate list for discover process %d is %s\n", discovery->pid, pl); return pl; } return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -