Skip to content
Snippets Groups Projects
udptl.c 33.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • 		ast_mutex_unlock(&c0->lock);
    		usleep(1);
    		ast_mutex_lock(&c0->lock);
    	}
    	pr0 = get_proto(c0);
    	pr1 = get_proto(c1);
    	if (!pr0) {
    		ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
    		ast_mutex_unlock(&c0->lock);
    		ast_mutex_unlock(&c1->lock);
    		return -1;
    	}
    	if (!pr1) {
    		ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
    		ast_mutex_unlock(&c0->lock);
    		ast_mutex_unlock(&c1->lock);
    		return -1;
    	}
    	pvt0 = c0->tech_pvt;
    	pvt1 = c1->tech_pvt;
    	p0 = pr0->get_udptl_info(c0);
    	p1 = pr1->get_udptl_info(c1);
    	if (!p0 || !p1) {
    		/* Somebody doesn't want to play... */
    		ast_mutex_unlock(&c0->lock);
    		ast_mutex_unlock(&c1->lock);
    		return -2;
    	}
    	if (pr0->set_udptl_peer(c0, p1)) {
    		ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
    	} else {
    		/* Store UDPTL peer */
    		ast_udptl_get_peer(p1, &ac1);
    	}
    	if (pr1->set_udptl_peer(c1, p0))
    		ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
    	else {
    		/* Store UDPTL peer */
    		ast_udptl_get_peer(p0, &ac0);
    	}
    	ast_mutex_unlock(&c0->lock);
    	ast_mutex_unlock(&c1->lock);
    	cs[0] = c0;
    	cs[1] = c1;
    	cs[2] = NULL;
    	for (;;) {
    		if ((c0->tech_pvt != pvt0)  ||
    			(c1->tech_pvt != pvt1) ||
    			(c0->masq || c0->masqr || c1->masq || c1->masqr)) {
    				ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
    				/* Tell it to try again later */
    				return -3;
    		}
    		to = -1;
    		ast_udptl_get_peer(p1, &t1);
    		ast_udptl_get_peer(p0, &t0);
    		if (inaddrcmp(&t1, &ac1)) {
    			ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d\n", 
    				c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t1.sin_addr), ntohs(t1.sin_port));
    			ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d\n", 
    				c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac1.sin_addr), ntohs(ac1.sin_port));
    			memcpy(&ac1, &t1, sizeof(ac1));
    		}
    		if (inaddrcmp(&t0, &ac0)) {
    			ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d\n", 
    				c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t0.sin_addr), ntohs(t0.sin_port));
    			ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d\n", 
    				c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac0.sin_addr), ntohs(ac0.sin_port));
    			memcpy(&ac0, &t0, sizeof(ac0));
    		}
    		who = ast_waitfor_n(cs, 2, &to);
    		if (!who) {
    			ast_log(LOG_DEBUG, "Ooh, empty read...\n");
    			/* check for hangup / whentohangup */
    			if (ast_check_hangup(c0) || ast_check_hangup(c1))
    				break;
    			continue;
    		}
    		f = ast_read(who);
    		if (!f) {
    			*fo = f;
    			*rc = who;
    			ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
    			/* That's all we needed */
    			return 0;
    		} else {
    			if (f->frametype == AST_FRAME_MODEM) {
    				/* Forward T.38 frames if they happen upon us */
    				if (who == c0) {
    					ast_write(c1, f);
    				} else if (who == c1) {
    					ast_write(c0, f);
    				}
    			}
    			ast_frfree(f);
    		}
    		/* Swap priority. Not that it's a big deal at this point */
    		cs[2] = cs[0];
    		cs[0] = cs[1];
    		cs[1] = cs[2];
    	}
    	return -1;
    }
    
    static int udptl_do_debug_ip(int fd, int argc, char *argv[])
    {
    	struct hostent *hp;
    	struct ast_hostent ahp;
    	char iabuf[INET_ADDRSTRLEN];
    	int port;
    	char *p;
    	char *arg;
    
    	port = 0;
    	if (argc != 4)
    		return RESULT_SHOWUSAGE;
    	arg = argv[3];
    	p = strstr(arg, ":");
    	if (p) {
    		*p = '\0';
    		p++;
    		port = atoi(p);
    	}
    	hp = ast_gethostbyname(arg, &ahp);
    	if (hp == NULL)
    		return RESULT_SHOWUSAGE;
    	udptldebugaddr.sin_family = AF_INET;
    	memcpy(&udptldebugaddr.sin_addr, hp->h_addr, sizeof(udptldebugaddr.sin_addr));
    	udptldebugaddr.sin_port = htons(port);
    	if (port == 0)
    		ast_cli(fd, "UDPTL Debugging Enabled for IP: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), udptldebugaddr.sin_addr));
    	else
    		ast_cli(fd, "UDPTL Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), udptldebugaddr.sin_addr), port);
    	udptldebug = 1;
    	return RESULT_SUCCESS;
    }
    
    static int udptl_do_debug(int fd, int argc, char *argv[])
    {
    	if (argc != 2) {
    		if (argc != 4)
    			return RESULT_SHOWUSAGE;
    		return udptl_do_debug_ip(fd, argc, argv);
    	}
    	udptldebug = 1;
    	memset(&udptldebugaddr,0,sizeof(udptldebugaddr));
    	ast_cli(fd, "UDPTL Debugging Enabled\n");
    	return RESULT_SUCCESS;
    }
       
    static int udptl_no_debug(int fd, int argc, char *argv[])
    {
    	if (argc !=3)
    		return RESULT_SHOWUSAGE;
    	udptldebug = 0;
    	ast_cli(fd,"UDPTL Debugging Disabled\n");
    	return RESULT_SUCCESS;
    }
    
    static char debug_usage[] =
      "Usage: udptl debug [ip host[:port]]\n"
      "       Enable dumping of all UDPTL packets to and from host.\n";
    
    static char no_debug_usage[] =
      "Usage: udptl no debug\n"
      "       Disable all UDPTL debugging\n";
    
    static struct ast_cli_entry  cli_debug_ip =
    {{ "udptl", "debug", "ip", NULL } , udptl_do_debug, "Enable UDPTL debugging on IP", debug_usage };
    
    static struct ast_cli_entry  cli_debug =
    {{ "udptl", "debug", NULL } , udptl_do_debug, "Enable UDPTL debugging", debug_usage };
    
    static struct ast_cli_entry  cli_no_debug =
    {{ "udptl", "no", "debug", NULL } , udptl_no_debug, "Disable UDPTL debugging", no_debug_usage };
    
    void ast_udptl_reload(void)
    {
    	struct ast_config *cfg;
    	char *s;
    
    	udptlstart = 4500;
    	udptlend = 4999;
    	udptlfectype = 0;
    	udptlfecentries = 0;
    	udptlfecspan = 0;
    	udptlmaxdatagram = 0;
    
    	if ((cfg = ast_config_load("udptl.conf"))) {
    		if ((s = ast_variable_retrieve(cfg, "general", "udptlstart"))) {
    			udptlstart = atoi(s);
    			if (udptlstart < 1024)
    				udptlstart = 1024;
    			if (udptlstart > 65535)
    				udptlstart = 65535;
    		}
    		if ((s = ast_variable_retrieve(cfg, "general", "udptlend"))) {
    			udptlend = atoi(s);
    			if (udptlend < 1024)
    				udptlend = 1024;
    			if (udptlend > 65535)
    				udptlend = 65535;
    		}
    		if ((s = ast_variable_retrieve(cfg, "general", "udptlchecksums"))) {
    #ifdef SO_NO_CHECK
    			if (ast_false(s))
    				nochecksums = 1;
    			else
    				nochecksums = 0;
    #else
    			if (ast_false(s))
    				ast_log(LOG_WARNING, "Disabling UDPTL checksums is not supported on this operating system!\n");
    #endif
    		}
    		if ((s = ast_variable_retrieve(cfg, "general", "T38FaxUdpEC"))) {
    			if (strcmp(s, "t38UDPFEC") == 0)
    				udptlfectype = 2;
    			else if (strcmp(s, "t38UDPRedundancy") == 0)
    				udptlfectype = 1;
    		}
    		if ((s = ast_variable_retrieve(cfg, "general", "T38FaxMaxDatagram"))) {
    			udptlmaxdatagram = atoi(s);
    			if (udptlmaxdatagram < 0)
    				udptlmaxdatagram = 0;
    			if (udptlmaxdatagram > LOCAL_FAX_MAX_DATAGRAM)
    				udptlmaxdatagram = LOCAL_FAX_MAX_DATAGRAM;
    		}
    		if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECentries"))) {
    			udptlfecentries = atoi(s);
    			if (udptlfecentries < 0)
    				udptlfecentries = 0;
    			if (udptlfecentries > MAX_FEC_ENTRIES)
    				udptlfecentries = MAX_FEC_ENTRIES;
    		}
    		if ((s = ast_variable_retrieve(cfg, "general", "UDPTLFECspan"))) {
    			udptlfecspan = atoi(s);
    			if (udptlfecspan < 0)
    				udptlfecspan = 0;
    			if (udptlfecspan > MAX_FEC_SPAN)
    				udptlfecspan = MAX_FEC_SPAN;
    		}
    		ast_config_destroy(cfg);
    	}
    	if (udptlstart >= udptlend) {
    		ast_log(LOG_WARNING, "Unreasonable values for UDPTL start/end\n");
    		udptlstart = 4500;
    		udptlend = 4999;
    	}
    	if (option_verbose > 1)
    		ast_verbose(VERBOSE_PREFIX_2 "UDPTL allocating from port range %d -> %d\n", udptlstart, udptlend);
    }
    
    void ast_udptl_init(void)
    {
    	ast_cli_register(&cli_debug);
    	ast_cli_register(&cli_debug_ip);
    	ast_cli_register(&cli_no_debug);
    	ast_udptl_reload();
    }