1 /*
2 	belle-sip - SIP (RFC3261) library.
3     Copyright (C) 2010  Belledonne Communications SARL
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 #ifndef belle_sip_resolver_h
21 #define belle_sip_resolver_h
22 
23 
24 typedef struct belle_sip_dns_srv belle_sip_dns_srv_t;
25 #define BELLE_SIP_DNS_SRV(obj) BELLE_SIP_CAST(obj,belle_sip_dns_srv_t)
26 
27 typedef struct belle_sip_resolver_context belle_sip_resolver_context_t;
28 #define BELLE_SIP_RESOLVER_CONTEXT(obj) BELLE_SIP_CAST(obj,belle_sip_resolver_context_t)
29 
30 /**
31  * Callback prototype for asynchronous DNS SRV resolution.
32  * The srv_list contains struct dns_srv elements that must be taken and (possibly later) freed by the callee, using belle_sip_free().
33  */
34 typedef void (*belle_sip_resolver_srv_callback_t)(void *data, const char *name, belle_sip_list_t *srv_list, uint32_t ttl);
35 
36 /**
37  * Callback prototype for asynchronous DNS A and AAAA resolution.
38  * The ai_list contains addrinfo elements that must be taken and (possibly later) freed by the callee, using freeaddrinfo().
39  * These elements are linked by their ai_next field.
40 **/
41 typedef void (*belle_sip_resolver_callback_t)(void *data, const char *name, struct addrinfo *ai_list, uint32_t ttl);
42 
43 
44 BELLE_SIP_BEGIN_DECLS
45 
46 BELLESIP_EXPORT const char *belle_sip_dns_srv_get_target(const belle_sip_dns_srv_t *obj);
47 
48 BELLESIP_EXPORT unsigned short belle_sip_dns_srv_get_priority(const belle_sip_dns_srv_t *obj);
49 
50 BELLESIP_EXPORT unsigned short belle_sip_dns_srv_get_weight(const belle_sip_dns_srv_t *obj);
51 
52 BELLESIP_EXPORT unsigned short belle_sip_dns_srv_get_port(const belle_sip_dns_srv_t *obj);
53 
54 
55 /**
56  * Asynchronously performs DNS SRV followed A/AAAA query. Automatically fallbacks to A/AAAA if SRV isn't found.
57  * @param stack the belle_sip_stack_t, used to schedule asynchronous execution.
58  * @param service the queried service ("sip", "stun", "turn"...)
59  * @param transport the queried transport ("udp", "tcp", "tls")
60  * @param name the SIP domain name
61  * @param port a port that will be filled in the addrinfo list returned by the callback, for the case where no SRV records are found.
62  * @param family address family expected in the addrinfo result list. AF_INET or AF_INET6. If AF_INET6 is used but no IPv6 records are found, the IPv4 addresses
63  * will be returned as IPv6 with v4 mapping (AI_V4MAPPED).
64  * @param cb a callback function that will be called to notify the results.
65  * @param data a user pointer passed through the callback as first argument.
66  * @return a #belle_sip_resolver_context_t that can be used to cancel the resolution if needed. The context must have been ref'd with belle_sip_object_ref().
67 **/
68 BELLESIP_EXPORT belle_sip_resolver_context_t * belle_sip_stack_resolve(belle_sip_stack_t *stack, const char *service, const char *transport, const char *name, int port, int family,
69 belle_sip_resolver_callback_t cb, void *data);
70 
71 /**
72  * Asynchronously performs DNS A or AAAA query.
73  * @param stack the belle_sip_stack_t, used to schedule asynchronous execution.
74  * @param name the SIP domain name
75  * @param port a port that will be filled in the addrinfo list returned by the callback, for the case where no SRV records are found.
76  * @param family address family expected in the addrinfo result list. AF_INET or AF_INET6. If AF_INET6 is used but no IPv4 records are found, the IPv4 addresses
77  * will be returned as IPv6 with v4 mapping (AI_V4MAPPED).
78  * @param cb a callback function that will be called to notify the results.
79  * @param data a user pointer passed through the callback as first argument.
80  * @return a #belle_sip_resolver_context_t that can be used to cancel the resolution if needed. The context must have been ref'd with belle_sip_object_ref().
81 **/
82 BELLESIP_EXPORT belle_sip_resolver_context_t * belle_sip_stack_resolve_a(belle_sip_stack_t *stack, const char *name, int port, int family, belle_sip_resolver_callback_t cb, void *data);
83 
84 /**
85  * Asynchronously performs DNS SRV query.
86  * @return a #belle_sip_resolver_context_t that can be used to cancel the resolution if needed. The context must have been ref'd with belle_sip_object_ref().
87 **/
88 BELLESIP_EXPORT belle_sip_resolver_context_t * belle_sip_stack_resolve_srv(belle_sip_stack_t *stack, const char *service, const char *transport, const char *name, belle_sip_resolver_srv_callback_t cb, void *data);
89 
90 /**
91  * Cancel a pending asynchronous DNS query. The context is unref'd automatically, as a result the context shall no longer be used after this call.
92  * If the query is already performed, cancellation has no effect, but context is unref'd anyway.
93  **/
94 BELLESIP_EXPORT void belle_sip_resolver_context_cancel(belle_sip_resolver_context_t *ctx);
95 
96 /**
97  * Lookups the source address from local interface that can be used to connect to a destination address.
98  * local_port is only used to be assigned into the result source address.
99  * This function always puts something in src and srclen (the loopback address) even if anything fails.
100  * The return code is 0 if successful, or -errno if an error was encoutered. Typical error is -ENETUNREACH when IPv6 network is not reachable.
101 **/
102 BELLESIP_EXPORT int belle_sip_get_src_addr_for(const struct sockaddr *dest, socklen_t destlen, struct sockaddr *src, socklen_t *srclen, int local_port);
103 
104 
105 BELLE_SIP_END_DECLS
106 
107 
108 #endif
109