-/*
- * hunts an mISDNport that is available for an outgoing call
- * if no ifname was given, any interface that is not an extension
- * will be searched.
- */
-struct mISDNport *EndpointAppPBX::hunt_port(char *ifname, int *channel)
-{
- struct interface *interface;
- struct interface_port *ifport, *ifport_start;
- struct select_channel *selchannel;
- struct mISDNport *mISDNport;
- int index, i;
-
- interface = interface_first;
-
- /* first find the given interface or, if not given, one with no extension */
- checknext:
- if (!interface)
- return(NULL);
-
- /* check for given interface */
- if (ifname)
- {
- if (!strcasecmp(interface->name, ifname))
- {
- /* found explicit interface */
- trace_header("CHANNEL SELECTION (found interface)", DIRECTION_NONE);
- add_trace("interface", NULL, "%s", ifname);
- end_trace();
- goto foundif;
- }
-
- } else
- {
- if (!interface->extension)
- {
- /* found non extension */
- trace_header("CHANNEL SELECTION (found non extension interface)", DIRECTION_NONE);
- add_trace("interface", NULL, "%s", interface->name);
- end_trace();
- goto foundif;
- }
- }
-
- interface = interface->next;
- goto checknext;
-foundif:
-
- /* see if interface has ports */
- if (!interface->ifport)
- {
- /* no ports */
- trace_header("CHANNEL SELECTION (interface has no active ports, skipping)", DIRECTION_NONE);
- add_trace("interface", NULL, "%s", interface->name);
- end_trace();
- interface = interface->next;
- goto checknext;
- }
-
- /* select port by algorithm */
- ifport_start = interface->ifport;
- index = 0;
- if (interface->hunt == HUNT_ROUNDROBIN)
- {
- while(ifport_start->next && index<interface->hunt_next)
- {
- ifport_start = ifport_start->next;
- index++;
- }
- trace_header("CHANNEL SELECTION (starting round-robin)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport_start->portnum);
- add_trace("position", NULL, "%d", index);
- end_trace();
- }
-
- /* loop ports */
- ifport = ifport_start;
- nextport:
-
- /* see if port is available */
- if (!ifport->mISDNport)
- {
- trace_header("CHANNEL SELECTION (port not available, skipping)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- end_trace();
- goto portbusy;
- }
- mISDNport = ifport->mISDNport;
-
- /* see if port is administratively blocked */
- if (ifport->block)
- {
- trace_header("CHANNEL SELECTION (port blocked by admin, skipping)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- end_trace();
- goto portbusy;
- }
-
- /* see if link is up on PTP*/
- if (mISDNport->ptp && !mISDNport->l2link)
- {
- trace_header("CHANNEL SELECTION (port is ptp with layer 2 down, skipping)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- end_trace();
- goto portbusy;
- }
-
- /* check for channel form selection list */
- *channel = 0;
- selchannel = ifport->out_channel;
- while(selchannel)
- {
- switch(selchannel->channel)
- {
- case CHANNEL_FREE: /* free channel */
- if (mISDNport->b_reserved >= mISDNport->b_num)
- break; /* all channel in use or reserverd */
- /* find channel */
- i = 0;
- while(i < mISDNport->b_num)
- {
- if (mISDNport->b_port[i] == NULL)
- {
- *channel = i+1+(i>=15);
- trace_header("CHANNEL SELECTION (selecting free channel)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- add_trace("channel", NULL, "%d", *channel);
- end_trace();
- break;
- }
- i++;
- }
- if (*channel)
- break;
- trace_header("CHANNEL SELECTION (no channel is 'free')", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- end_trace();
- break;
-
- case CHANNEL_ANY: /* don't ask for channel */
- if (mISDNport->b_reserved >= mISDNport->b_num)
- {
- trace_header("CHANNEL SELECTION (cannot ask for 'any' channel, all reserved)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- add_trace("total", NULL, "%d", mISDNport->b_num);
- add_trace("reserved", NULL, "%d", mISDNport->b_reserved);
- end_trace();
- break; /* all channel in use or reserverd */
- }
- trace_header("CHANNEL SELECTION (using 'any' channel)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- end_trace();
- *channel = CHANNEL_ANY;
- break;
-
- case CHANNEL_NO: /* call waiting */
- trace_header("CHANNEL SELECTION (using 'no' channel, call-waiting)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- end_trace();
- *channel = CHANNEL_NO;
- break;
-
- default:
- if (selchannel->channel<1 || selchannel->channel==16)
- {
- trace_header("CHANNEL SELECTION (channel out of range)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- add_trace("channel", NULL, "%d", selchannel->channel);
- end_trace();
- break; /* invalid channels */
- }
- i = selchannel->channel-1-(selchannel->channel>=17);
- if (i >= mISDNport->b_num)
- {
- trace_header("CHANNEL SELECTION (channel out of range)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- add_trace("channel", NULL, "%d", selchannel->channel);
- add_trace("channels", NULL, "%d", mISDNport->b_num);
- end_trace();
- break; /* channel not in port */
- }
- if (mISDNport->b_port[i] == NULL)
- {
- *channel = selchannel->channel;
- trace_header("CHANNEL SELECTION (selecting given channel)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- add_trace("channel", NULL, "%d", *channel);
- end_trace();
- break;
- }
- break;
- }
- if (*channel)
- break; /* found channel */
- selchannel = selchannel->next;
- }
-
- /* if channel was found, return mISDNport and channel */
- if (*channel)
- {
- /* setting next port to start next time */
- if (interface->hunt == HUNT_ROUNDROBIN)
- {
- index++;
- if (!ifport->next)
- index = 0;
- interface->hunt_next = index;
- }
-
- return(mISDNport);
- }
-
- trace_header("CHANNEL SELECTION (skipping, no channel found)", DIRECTION_NONE);
- add_trace("port", NULL, "%d", ifport->portnum);
- add_trace("position", NULL, "%d", index);
- end_trace();
-
- portbusy:
- /* go next port, until all ports are checked */
- index++;
- ifport = ifport->next;
- if (!ifport)
- {
- index = 0;
- ifport = interface->ifport;
- }
- if (ifport != ifport_start)
- goto nextport;
-
- return(NULL); /* no port found */
-}
-