| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "asterisk.h" |
| 18 |
#include "asterisk/lock.h" |
| 19 |
#include "asterisk/netsock2.h" |
| 20 |
#include "asterisk/config.h" |
| 21 |
|
| 22 |
#include "ooports.h" |
| 23 |
#include "ooh323ep.h" |
| 24 |
#include "ootrace.h" |
| 25 |
|
| 26 |
|
| 27 |
extern OOH323EndPoint gH323ep; |
| 28 |
extern ast_mutex_t bindPortLock; |
| 29 |
|
| 30 |
|
| 31 |
int ooGetNextPort (OOH323PortType type) |
| 32 |
{ |
| 33 |
if(type==OOTCP) |
| 34 |
{ |
| 35 |
if(gH323ep.tcpPorts.current <= gH323ep.tcpPorts.max) |
| 36 |
return gH323ep.tcpPorts.current++; |
| 37 |
else |
| 38 |
{ |
| 39 |
gH323ep.tcpPorts.current = gH323ep.tcpPorts.start; |
| 40 |
return gH323ep.tcpPorts.current++; |
| 41 |
} |
| 42 |
} |
| 43 |
if(type==OOUDP) |
| 44 |
{ |
| 45 |
if(gH323ep.udpPorts.current <= gH323ep.udpPorts.max) |
| 46 |
return gH323ep.udpPorts.current++; |
| 47 |
else |
| 48 |
{ |
| 49 |
gH323ep.udpPorts.current = gH323ep.udpPorts.start; |
| 50 |
return gH323ep.udpPorts.current++; |
| 51 |
} |
| 52 |
} |
| 53 |
if(type==OORTP) |
| 54 |
{ |
| 55 |
if(gH323ep.rtpPorts.current <= gH323ep.rtpPorts.max) |
| 56 |
return gH323ep.rtpPorts.current++; |
| 57 |
else |
| 58 |
{ |
| 59 |
gH323ep.rtpPorts.current = gH323ep.rtpPorts.start; |
| 60 |
return gH323ep.rtpPorts.current++; |
| 61 |
} |
| 62 |
} |
| 63 |
return OO_FAILED; |
| 64 |
} |
| 65 |
|
| 66 |
int ooBindPort (OOH323PortType type, OOSOCKET socket, char *ip) |
| 67 |
{ |
| 68 |
int initialPort, bindPort, ret; |
| 69 |
OOIPADDR ipAddrs; |
| 70 |
|
| 71 |
ast_mutex_lock(&bindPortLock); |
| 72 |
|
| 73 |
initialPort = ooGetNextPort (type); |
| 74 |
bindPort = initialPort; |
| 75 |
|
| 76 |
ret=ast_parse_arg(ip, PARSE_ADDR, &ipAddrs); |
| 77 |
|
| 78 |
while(1) |
| 79 |
{ |
| 80 |
if((ret=ooSocketBind(socket, ipAddrs, bindPort))==0) |
| 81 |
{ |
| 82 |
ast_mutex_unlock(&bindPortLock); |
| 83 |
return bindPort; |
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
bindPort = ooGetNextPort (type); |
| 88 |
if (bindPort == initialPort) { ast_mutex_unlock(&bindPortLock); return OO_FAILED; } |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
#ifdef _WIN32 |
| 94 |
int ooBindOSAllocatedPort(OOSOCKET socket, char *ip) |
| 95 |
{ |
| 96 |
OOIPADDR ipAddrs; |
| 97 |
int size, ret; |
| 98 |
struct sockaddr_in name; |
| 99 |
size = sizeof(struct sockaddr_in); |
| 100 |
ret= ooSocketStrToAddr (ip, &ipAddrs); |
| 101 |
if((ret=ooSocketBind(socket, ipAddrs, |
| 102 |
0))==ASN_OK) |
| 103 |
{ |
| 104 |
ret = ooSocketGetSockName(socket, &name, &size); |
| 105 |
if(ret == ASN_OK) |
| 106 |
{ |
| 107 |
return name.sin_port; |
| 108 |
|
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
return OO_FAILED; |
| 113 |
} |
| 114 |
#endif |