build-system: Allow building with static pjproject
[asterisk/asterisk.git] / third-party / pjproject / patches / 0001-2.4.5-fix-for-tls-async-ops.patch
1 diff --git a/pjlib/include/pj/ssl_sock.h b/pjlib/include/pj/ssl_sock.h
2 index 1682bda..a69af32 100644
3 --- a/pjlib/include/pj/ssl_sock.h
4 +++ b/pjlib/include/pj/ssl_sock.h
5 @@ -864,6 +864,18 @@ PJ_DECL(void) pj_ssl_sock_param_default(pj_ssl_sock_param *param);
6  
7  
8  /**
9 + * Duplicate pj_ssl_sock_param.
10 + *
11 + * @param pool Pool to allocate memory.
12 + * @param dst  Destination parameter.
13 + * @param src  Source parameter.
14 + */
15 +PJ_DECL(void) pj_ssl_sock_param_copy(pj_pool_t *pool, 
16 +                                    pj_ssl_sock_param *dst,
17 +                                    const pj_ssl_sock_param *src);
18 +
19 +
20 +/**
21   * Create secure socket instance.
22   *
23   * @param pool         The pool for allocating secure socket instance.
24 @@ -1115,6 +1127,30 @@ PJ_DECL(pj_status_t) pj_ssl_sock_start_accept(pj_ssl_sock_t *ssock,
25  
26  
27  /**
28 + * Same as #pj_ssl_sock_start_accept(), but application can provide
29 + * a secure socket parameter, which will be used to create a new secure
30 + * socket reported in \a on_accept_complete() callback when there is
31 + * an incoming connection.
32 + *
33 + * @param ssock                The secure socket.
34 + * @param pool         Pool used to allocate some internal data for the
35 + *                     operation.
36 + * @param localaddr    Local address to bind on.
37 + * @param addr_len     Length of buffer containing local address.
38 + * @param newsock_param        Secure socket parameter for new accepted sockets.
39 + *
40 + * @return             PJ_SUCCESS if the operation has been successful,
41 + *                     or the appropriate error code on failure.
42 + */
43 +PJ_DECL(pj_status_t)
44 +pj_ssl_sock_start_accept2(pj_ssl_sock_t *ssock,
45 +                         pj_pool_t *pool,
46 +                         const pj_sockaddr_t *local_addr,
47 +                         int addr_len,
48 +                         const pj_ssl_sock_param *newsock_param);
49 +
50 +
51 +/**
52   * Starts asynchronous socket connect() operation and SSL/TLS handshaking 
53   * for this socket. Once the connection is done (either successfully or not),
54   * the \a on_connect_complete() callback will be called.
55 diff --git a/pjlib/src/pj/ssl_sock_common.c b/pjlib/src/pj/ssl_sock_common.c
56 index 913efee..717ab1d 100644
57 --- a/pjlib/src/pj/ssl_sock_common.c
58 +++ b/pjlib/src/pj/ssl_sock_common.c
59 @@ -19,6 +19,7 @@
60  #include <pj/ssl_sock.h>
61  #include <pj/assert.h>
62  #include <pj/errno.h>
63 +#include <pj/pool.h>
64  #include <pj/string.h>
65  
66  /*
67 @@ -48,6 +49,31 @@ PJ_DEF(void) pj_ssl_sock_param_default(pj_ssl_sock_param *param)
68  }
69  
70  
71 +/*
72 + * Duplicate SSL socket parameter.
73 + */
74 +PJ_DEF(void) pj_ssl_sock_param_copy( pj_pool_t *pool, 
75 +                                    pj_ssl_sock_param *dst,
76 +                                    const pj_ssl_sock_param *src)
77 +{
78 +    /* Init secure socket param */
79 +    pj_memcpy(dst, src, sizeof(*dst));
80 +    if (src->ciphers_num > 0) {
81 +       unsigned i;
82 +       dst->ciphers = (pj_ssl_cipher*)
83 +                       pj_pool_calloc(pool, src->ciphers_num, 
84 +                                      sizeof(pj_ssl_cipher));
85 +       for (i = 0; i < src->ciphers_num; ++i)
86 +           dst->ciphers[i] = src->ciphers[i];
87 +    }
88 +
89 +    if (src->server_name.slen) {
90 +        /* Server name must be null-terminated */
91 +        pj_strdup_with_null(pool, &dst->server_name, &src->server_name);
92 +    }
93 +}
94 +
95 +
96  PJ_DEF(pj_status_t) pj_ssl_cert_get_verify_status_strings(
97                                                 pj_uint32_t verify_status, 
98                                                 const char *error_strings[],
99 diff --git a/pjlib/src/pj/ssl_sock_ossl.c b/pjlib/src/pj/ssl_sock_ossl.c
100 index 40a5a1e..6a701b7 100644
101 --- a/pjlib/src/pj/ssl_sock_ossl.c
102 +++ b/pjlib/src/pj/ssl_sock_ossl.c
103 @@ -141,6 +141,7 @@ struct pj_ssl_sock_t
104      pj_pool_t           *pool;
105      pj_ssl_sock_t       *parent;
106      pj_ssl_sock_param    param;
107 +    pj_ssl_sock_param    newsock_param;
108      pj_ssl_cert_t       *cert;
109      
110      pj_ssl_cert_info     local_cert_info;
111 @@ -1757,11 +1758,9 @@ static pj_bool_t asock_on_accept_complete (pj_activesock_t *asock,
112      unsigned i;
113      pj_status_t status;
114  
115 -    PJ_UNUSED_ARG(src_addr_len);
116 -
117      /* Create new SSL socket instance */
118 -    status = pj_ssl_sock_create(ssock_parent->pool, &ssock_parent->param,
119 -                               &ssock);
120 +    status = pj_ssl_sock_create(ssock_parent->pool,
121 +                               &ssock_parent->newsock_param, &ssock);
122      if (status != PJ_SUCCESS)
123         goto on_return;
124  
125 @@ -2183,20 +2182,8 @@ PJ_DEF(pj_status_t) pj_ssl_sock_create (pj_pool_t *pool,
126         return status;
127  
128      /* Init secure socket param */
129 -    ssock->param = *param;
130 +    pj_ssl_sock_param_copy(pool, &ssock->param, param);
131      ssock->param.read_buffer_size = ((ssock->param.read_buffer_size+7)>>3)<<3;
132 -    if (param->ciphers_num > 0) {
133 -       unsigned i;
134 -       ssock->param.ciphers = (pj_ssl_cipher*)
135 -                              pj_pool_calloc(pool, param->ciphers_num, 
136 -                                             sizeof(pj_ssl_cipher));
137 -       for (i = 0; i < param->ciphers_num; ++i)
138 -           ssock->param.ciphers[i] = param->ciphers[i];
139 -    }
140 -
141 -    /* Server name must be null-terminated */
142 -    pj_strdup_with_null(pool, &ssock->param.server_name, 
143 -                       &param->server_name);
144  
145      /* Finally */
146      *p_ssock = ssock;
147 @@ -2617,12 +2604,36 @@ PJ_DEF(pj_status_t) pj_ssl_sock_start_accept (pj_ssl_sock_t *ssock,
148                                               const pj_sockaddr_t *localaddr,
149                                               int addr_len)
150  {
151 +    return pj_ssl_sock_start_accept2(ssock, pool, localaddr, addr_len,
152 +                                    &ssock->param);
153 +}
154 +
155 +
156 +/**
157 + * Same as #pj_ssl_sock_start_accept(), but application provides parameter
158 + * for new accepted secure sockets.
159 + */
160 +PJ_DEF(pj_status_t)
161 +pj_ssl_sock_start_accept2(pj_ssl_sock_t *ssock,
162 +                         pj_pool_t *pool,
163 +                         const pj_sockaddr_t *localaddr,
164 +                         int addr_len,
165 +                         const pj_ssl_sock_param *newsock_param)
166 +{
167      pj_activesock_cb asock_cb;
168      pj_activesock_cfg asock_cfg;
169      pj_status_t status;
170  
171      PJ_ASSERT_RETURN(ssock && pool && localaddr && addr_len, PJ_EINVAL);
172  
173 +    /* Verify new socket parameters */
174 +    if (newsock_param->grp_lock != ssock->param.grp_lock ||
175 +        newsock_param->sock_af != ssock->param.sock_af ||
176 +        newsock_param->sock_type != ssock->param.sock_type)
177 +    {
178 +        return PJ_EINVAL;
179 +    }
180 +
181      /* Create socket */
182      status = pj_sock_socket(ssock->param.sock_af, ssock->param.sock_type, 0, 
183                             &ssock->sock);
184 @@ -2691,6 +2702,7 @@ PJ_DEF(pj_status_t) pj_ssl_sock_start_accept (pj_ssl_sock_t *ssock,
185         goto on_error;
186  
187      /* Start accepting */
188 +    pj_ssl_sock_param_copy(pool, &ssock->newsock_param, newsock_param);
189      status = pj_activesock_start_accept(ssock->asock, pool);
190      if (status != PJ_SUCCESS)
191         goto on_error;
192 diff --git a/pjsip/src/pjsip/sip_transport_tls.c b/pjsip/src/pjsip/sip_transport_tls.c
193 index a9e95fb..91d99a7 100644
194 --- a/pjsip/src/pjsip/sip_transport_tls.c
195 +++ b/pjsip/src/pjsip/sip_transport_tls.c
196 @@ -314,7 +314,7 @@ PJ_DEF(pj_status_t) pjsip_tls_transport_start2( pjsip_endpoint *endpt,
197      int af, sip_ssl_method;
198      pj_uint32_t sip_ssl_proto;
199      struct tls_listener *listener;
200 -    pj_ssl_sock_param ssock_param;
201 +    pj_ssl_sock_param ssock_param, newsock_param;
202      pj_sockaddr *listener_addr;
203      pj_bool_t has_listener;
204      pj_status_t status;
205 @@ -473,9 +473,14 @@ PJ_DEF(pj_status_t) pjsip_tls_transport_start2( pjsip_endpoint *endpt,
206       */
207      has_listener = PJ_FALSE;
208  
209 -    status = pj_ssl_sock_start_accept(listener->ssock, pool, 
210 +    pj_memcpy(&newsock_param, &ssock_param, sizeof(newsock_param));
211 +    newsock_param.async_cnt = 1;
212 +    newsock_param.cb.on_data_read = &on_data_read;
213 +    newsock_param.cb.on_data_sent = &on_data_sent;
214 +    status = pj_ssl_sock_start_accept2(listener->ssock, pool, 
215                           (pj_sockaddr_t*)listener_addr, 
216 -                         pj_sockaddr_get_len((pj_sockaddr_t*)listener_addr));
217 +                         pj_sockaddr_get_len((pj_sockaddr_t*)listener_addr),
218 +                         &newsock_param);
219      if (status == PJ_SUCCESS || status == PJ_EPENDING) {
220         pj_ssl_sock_info info;
221         has_listener = PJ_TRUE;
222 -- 
223 cgit v0.11.2
224