tclink.c

filter: Statementstmt 0.0% 0 / 371 Branchbran 0.0% 0 / 216 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 587 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« TCLink.xs ↑ Summary
1 /* tclink.c - Library code for the TCLink client API.
2 *
3 * TCLink Copyright (c) 2003 TrustCommerce.
4 * http://www.trustcommerce.com
5 * developer@trustcommerce.com
6 * (626) 744-7700
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22  
23 #include "tclink.h"
24  
25 #include <stdio.h>
26 #include <memory.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <sys/time.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <netdb.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <fcntl.h>
39 #include <signal.h>
40  
41 #define OPENSSL_NO_KRB5 1
42  
43 #include <openssl/crypto.h>
44 #include <openssl/x509.h>
45 #include <openssl/pem.h>
46 #include <openssl/ssl.h>
47 #include <openssl/err.h>
48 #include <openssl/rand.h>
49  
50 #define DEFAULT_HOST "gateway2048.trustcommerce.com"
51 #define TIMEOUT 40 /* seconds */
52  
53 #define TC_BUFF_MAX 16000
54 #define TC_LINE_MAX ((PARAM_MAX_LEN * 2) + 2)
55  
56 char *tclink_version = TCLINK_VERSION; /* TCLINK_VERSION is defined in Makefile */
57 char *tclink_host = DEFAULT_HOST;
58 int tclink_port = 443;
59  
60 /*************************************************/
61 /* Data structures used only within this module. */
62 /*************************************************/
63  
64 /* Variables used for transaction data. */
65  
66 typedef struct param_data
67 {
68 char *name;
69 char *value;
70 struct param_data *next;
71 } param;
72  
73 typedef struct _TCLinkCon
74 {
75 /* Connection data */
76 int *ip;
77 int num_ips;
78 int sd;
79  
80 /* SSL encryption */
81 X509 *tc_cert;
82 SSL_METHOD *meth;
83 SSL_CTX *ctx;
84 SSL *ssl;
85  
86 /* Transaction parameters, sent and received */
87 param *send_param_list, *send_param_tail;
88 param *recv_param_list;
89  
90 /* Connection status */
91 int is_error;
92 int pass;
93 time_t start_time;
94 int dns;
95  
96 } TCLinkCon;
97  
98 /* The TrustCommerce certificate. */
99 enum { TC_CERT_SIZE = 952 };
100 unsigned char cert_data[TC_CERT_SIZE]={
101 0x30,0x82,0x03,0xb4,0x30,0x82,0x02,0x9c,0x02,0x01,0x00,0x30,0x0d,0x06,0x09,0x2a,
102 0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x04,0x05,0x00,0x30,0x81,0x9f,0x31,0x0b,0x30,
103 0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x53,0x31,0x13,0x30,0x11,0x06,0x03,
104 0x55,0x04,0x08,0x13,0x0a,0x43,0x61,0x6c,0x69,0x66,0x6f,0x72,0x6e,0x69,0x61,0x31,
105 0x14,0x30,0x12,0x06,0x03,0x55,0x04,0x07,0x13,0x0b,0x4c,0x6f,0x73,0x20,0x41,0x6e,
106 0x67,0x65,0x6c,0x65,0x73,0x31,0x16,0x30,0x14,0x06,0x03,0x55,0x04,0x0a,0x13,0x0d,
107 0x54,0x72,0x75,0x73,0x74,0x43,0x6f,0x6d,0x6d,0x65,0x72,0x63,0x65,0x31,0x26,0x30,
108 0x24,0x06,0x03,0x55,0x04,0x03,0x13,0x1d,0x67,0x61,0x74,0x65,0x77,0x61,0x79,0x32,
109 0x30,0x34,0x38,0x2e,0x74,0x72,0x75,0x73,0x74,0x63,0x6f,0x6d,0x6d,0x65,0x72,0x63,
110 0x65,0x2e,0x63,0x6f,0x6d,0x31,0x25,0x30,0x23,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,
111 0x0d,0x01,0x09,0x01,0x16,0x16,0x69,0x6e,0x66,0x6f,0x40,0x74,0x72,0x75,0x73,0x74,
112 0x63,0x6f,0x6d,0x6d,0x65,0x72,0x63,0x65,0x2e,0x63,0x6f,0x6d,0x30,0x1e,0x17,0x0d,
113 0x30,0x33,0x30,0x33,0x31,0x32,0x30,0x30,0x35,0x35,0x35,0x37,0x5a,0x17,0x0d,0x30,
114 0x33,0x30,0x34,0x31,0x31,0x30,0x30,0x35,0x35,0x35,0x37,0x5a,0x30,0x81,0x9f,0x31,
115 0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x53,0x31,0x13,0x30,0x11,
116 0x06,0x03,0x55,0x04,0x08,0x13,0x0a,0x43,0x61,0x6c,0x69,0x66,0x6f,0x72,0x6e,0x69,
117 0x61,0x31,0x14,0x30,0x12,0x06,0x03,0x55,0x04,0x07,0x13,0x0b,0x4c,0x6f,0x73,0x20,
118 0x41,0x6e,0x67,0x65,0x6c,0x65,0x73,0x31,0x16,0x30,0x14,0x06,0x03,0x55,0x04,0x0a,
119 0x13,0x0d,0x54,0x72,0x75,0x73,0x74,0x43,0x6f,0x6d,0x6d,0x65,0x72,0x63,0x65,0x31,
120 0x26,0x30,0x24,0x06,0x03,0x55,0x04,0x03,0x13,0x1d,0x67,0x61,0x74,0x65,0x77,0x61,
121 0x79,0x32,0x30,0x34,0x38,0x2e,0x74,0x72,0x75,0x73,0x74,0x63,0x6f,0x6d,0x6d,0x65,
122 0x72,0x63,0x65,0x2e,0x63,0x6f,0x6d,0x31,0x25,0x30,0x23,0x06,0x09,0x2a,0x86,0x48,
123 0x86,0xf7,0x0d,0x01,0x09,0x01,0x16,0x16,0x69,0x6e,0x66,0x6f,0x40,0x74,0x72,0x75,
124 0x73,0x74,0x63,0x6f,0x6d,0x6d,0x65,0x72,0x63,0x65,0x2e,0x63,0x6f,0x6d,0x30,0x82,
125 0x01,0x22,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01,0x05,
126 0x00,0x03,0x82,0x01,0x0f,0x00,0x30,0x82,0x01,0x0a,0x02,0x82,0x01,0x01,0x00,0xb7,
127 0x9e,0x49,0xa6,0xf2,0x34,0xc2,0x1c,0xbc,0x23,0xee,0x66,0x52,0x10,0x8a,0xba,0xf6,
128 0x6c,0x31,0x78,0x58,0x4e,0x44,0xce,0x40,0x0f,0x90,0x52,0x66,0x3e,0xa7,0x34,0xef,
129 0x31,0x24,0x13,0x73,0x20,0x33,0x4b,0xe2,0x3a,0x4d,0x4c,0xdd,0x48,0x8b,0xf4,0xb9,
130 0xdd,0x4e,0x54,0xc8,0x79,0xd3,0x13,0xac,0x73,0x93,0x3d,0x47,0x99,0x1a,0x5c,0x4a,
131 0xdb,0x0f,0x2d,0xf9,0xb5,0x5d,0x3e,0x91,0x15,0x4e,0x7d,0x7e,0x21,0x47,0xf2,0xb3,
132 0xcc,0xd3,0x7c,0xc4,0x2f,0x9e,0xfe,0xec,0xd5,0x0f,0xe7,0xc8,0x7e,0x7f,0xf6,0x32,
133 0xb8,0xac,0xff,0xac,0x8e,0xbf,0xb2,0xc1,0xaa,0xf3,0x66,0x4d,0x86,0x9a,0xd9,0xd4,
134 0x04,0x97,0x5a,0xc3,0xa3,0x33,0xc8,0x2e,0x4f,0x07,0xca,0x6f,0x1c,0x58,0xbd,0x00,
135 0x9e,0xe9,0xd8,0x3c,0xe2,0x87,0x80,0xb5,0x99,0x40,0x37,0x2f,0x24,0x05,0x4b,0xc2,
136 0xf9,0xe7,0x25,0x8e,0xfc,0x76,0xb1,0x76,0xde,0xf2,0xe6,0x5f,0x06,0x44,0x31,0x59,
137 0x1c,0xed,0x8b,0x1c,0x1c,0xb1,0x2f,0x48,0xf7,0xeb,0x78,0x73,0xa5,0x57,0xac,0xf0,
138 0x30,0x7f,0xdd,0x09,0xa8,0x7b,0xb5,0xc5,0x30,0x2c,0xcc,0x6a,0xaf,0x6e,0x98,0x6d,
139 0xa3,0x08,0xf8,0x94,0x0e,0x7a,0xad,0xe6,0xe5,0x27,0x01,0xc8,0x2f,0x01,0x0f,0x59,
140 0x87,0x3f,0x5f,0x6f,0x5f,0x2c,0x27,0x3d,0xe8,0x33,0x2e,0xc1,0x69,0xbb,0xe9,0xa0,
141 0xea,0xea,0x19,0x8b,0x9d,0x0f,0xa0,0x88,0x51,0x53,0xc1,0xf9,0x3d,0x41,0x9f,0xe2,
142 0xa8,0x66,0x0c,0xb3,0x21,0x64,0xd3,0x4b,0x81,0x91,0xd7,0x7d,0xce,0xc7,0xb5,0x02,
143 0x03,0x01,0x00,0x01,0x30,0x0d,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,
144 0x04,0x05,0x00,0x03,0x82,0x01,0x01,0x00,0x12,0x8d,0xa7,0x9b,0x9f,0x0b,0x8e,0x26,
145 0x8f,0xf2,0xf5,0x77,0xe2,0x71,0x85,0xd0,0x82,0x30,0x3f,0x44,0x68,0x6e,0xc6,0x4a,
146 0xe2,0x19,0x35,0x00,0x5e,0x6b,0xb1,0xd1,0xd4,0xd4,0x62,0x92,0xbe,0xc2,0x10,0x07,
147 0xbe,0x86,0x7b,0x8a,0xa5,0xe6,0xef,0x2b,0x6c,0x86,0x4d,0xc3,0x72,0x73,0xbf,0x68,
148 0x2a,0xaf,0xee,0x7d,0x97,0xad,0x20,0x3c,0xef,0x5c,0x83,0x78,0x01,0xf5,0xe5,0x08,
149 0x4f,0x33,0x52,0xaa,0xe6,0xb9,0x57,0xc9,0x26,0x48,0x59,0x20,0xe2,0x8b,0xdf,0xce,
150 0x49,0x6f,0x2f,0x7c,0x66,0x03,0x75,0x62,0x97,0x35,0xdf,0x23,0x8d,0x81,0xde,0x85,
151 0x9b,0x21,0x28,0x8b,0x74,0x35,0xa8,0x24,0x64,0x26,0x73,0xae,0xda,0xbd,0x81,0x2c,
152 0x87,0x97,0xd4,0xe6,0x0b,0x7e,0xcc,0x1b,0x1f,0xa9,0x6c,0x6a,0x70,0xbd,0x7f,0x1e,
153 0x9c,0x2d,0x7c,0xcc,0x62,0x7f,0x1a,0x93,0x12,0xaf,0xd9,0xd5,0xd8,0xe6,0x94,0xb7,
154 0x64,0x09,0x2c,0xc2,0xc5,0x5b,0x73,0x8f,0x4c,0x58,0x1b,0xbc,0x37,0x42,0x3c,0x4c,
155 0x37,0x7e,0x9e,0x79,0x1f,0xd1,0x0c,0xfe,0x54,0xc7,0x82,0x8f,0xde,0x87,0xf0,0xe2,
156 0x40,0x68,0x4f,0x47,0x03,0x0d,0x23,0x46,0xf2,0x67,0xb9,0x30,0x3b,0x42,0xe3,0x77,
157 0x40,0xa6,0xc2,0x9e,0x28,0xdf,0x28,0xda,0xfc,0xec,0xd0,0xc0,0x6a,0xd9,0x80,0x41,
158 0x60,0x5a,0xfb,0x24,0x23,0x2e,0x4d,0x50,0xb1,0xec,0xfb,0xb3,0x97,0xe2,0x4a,0x5a,
159 0xc7,0xda,0x12,0xbb,0x2a,0x6b,0x04,0xd6,0x90,0x91,0xe9,0xf7,0x5d,0x42,0x98,0xd7,
160 0x81,0x8a,0xd3,0x70,0x31,0xbd,0x11,0x4a};
161  
162  
163 /*************************************
164 * Internal functions, not exported. *
165 *************************************/
166  
167 /* Random number from min to max. */
168 0 static int number(int min, int max)
169 {
170 0 return (rand() % (max - min + 1)) + min;
171 }
172  
173 /* Safe string copy and append functions. */
174 #define SAFE_COPY(d, s) safe_copy((d), (s), sizeof(d));
175 #define SAFE_APPEND(d, s) safe_append((d), (s), sizeof(d));
176  
177 0 void safe_copy(char *dst, const char *src, int size)
178 {
179 0 int len = strlen(src);
180 0 ▶ if (len < size)
Branch: 1 branch
BranchTrueFalseTotal
if (len < size) 0 0 0
181 0 strcpy(dst, src);
182 else {
183 0 strncpy(dst, src, size - 1);
184 0 dst[size-1] = 0;
185 }
186 0 }
187  
188 0 void safe_append(char *dst, const char *src, int size)
189 {
190 0 int dlen = strlen(dst);
191 0 int slen = strlen(src);
192 0 int avail = size - dlen;
193 0 ▶ if (avail < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (avail < 1) 0 0 0
194 0 return;
195  
196 0 ▶ if (slen < avail)
Branch: 1 branch
BranchTrueFalseTotal
if (slen < avail) 0 0 0
197 0 strcpy(dst+dlen, src);
198 else {
199 0 strncpy(dst+dlen, src, avail - 1);
200 0 dst[size-1] = 0;
201 }
202 }
203  
204 /* Add a parameter-value pair to the recieved list. */
205 0 static void AddRecvParam(TCLinkCon *c, const char *name, const char *value)
206 {
207 param *p;
208  
209 0 ▶ if (name[0] == 0 || value[0] == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (name[0] == 0 || value[0] == 0) 0 0 0
if (name[0] == 0 || value[0] == 0) 0 0 0
210 0 return;
211  
212 0 p = (param *)malloc(sizeof(param));
213 0 p->name = strdup(name);
214 0 p->value = strdup(value);
215 0 p->next = c->recv_param_list;
216 0 c->recv_param_list = p;
217 }
218  
219 /* Add a string to the received list. */
220 0 static int AddRecvString(TCLinkCon *c, char *string)
221 {
222 0 char *ptr = strchr(string, '=');
223 0 ▶ if (ptr == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (ptr == NULL) 0 0 0
224 0 return 0;
225  
226 0 *ptr = 0;
227 0 AddRecvParam(c, string, ptr+1);
228  
229 0 return 1;
230 }
231  
232 /* Deallocate the send list. */
233 0 static void ClearSendList(TCLinkCon *c)
234 {
235 param *p, *next;
236 0 ▶ for (p = c->send_param_list; p; p = next)
Branch: 1 branch
BranchTrueFalseTotal
for (p = c->send_param_list; p; p = next) 0 0 0
237 {
238 0 next = p->next;
239 0 free(p->name);
240 0 free(p->value);
241 0 free(p);
242 }
243  
244 0 c->send_param_list = c->send_param_tail = NULL;
245 0 }
246  
247 /* Deallocate the recv list. */
248 0 static void ClearRecvList(TCLinkCon *c)
249 {
250 param *p, *next;
251 0 ▶ for (p = c->recv_param_list; p; p = next)
Branch: 1 branch
BranchTrueFalseTotal
for (p = c->recv_param_list; p; p = next) 0 0 0
252 {
253 0 next = p->next;
254 0 free(p->name);
255 0 free(p->value);
256 0 free(p);
257 }
258  
259 0 c->recv_param_list = NULL;
260 0 }
261  
262 /* Open a socket to the host_ip specified. Returns the socket's file
263 * descriptor on success (the open attempt is underway) or -1 for failure
264 * (should never happen in practice). Note that this function DOES NOT block
265 * and wait for the connection; you'll need to select() on the socket later to see
266 * if it opened successfully.
267 */
268 0 static int BeginConnection(TCLinkCon *c, int host_ip)
269 {
270 struct sockaddr_in sa;
271 int sd;
272  
273 0 sd = socket(AF_INET, SOCK_STREAM, 0);
274 0 ▶ if (sd < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sd < 0) 0 0 0
275 0 return -1;
276  
277 0 fcntl(sd, F_SETFL, O_NONBLOCK);
278  
279 0 memset(&sa, 0, sizeof(sa));
280 0 sa.sin_family = AF_INET;
281 0 sa.sin_addr.s_addr = host_ip;
282 0 sa.sin_port = htons(tclink_port);
283  
284 0 connect(sd, (struct sockaddr *) &sa, sizeof(sa));
285  
286 0 return sd;
287 }
288  
289 /* This function is called on a socket file descriptor once the connection has been
290 * established and we're ready to negotiate SSL. If the SSL handshake fails for some
291 * reason (such as the host on the other end not using SSL), it will return 0 for
292 * failure. Success returns 1.
293 */
294 0 static int FinishConnection(TCLinkCon *c, int sd)
295 {
296 int ssl_connected, is_error, errcode, res;
297 X509 *server_cert;
298 time_t start, remaining;
299 fd_set in, out, err;
300 struct timeval tv;
301  
302 /* check if socket has connected successfully */
303 int val;
304 0 int /*socklen_t*/ size = 4;
305 0 getsockopt(sd, SOL_SOCKET, SO_ERROR, &val, &size);
306 0 ▶ if (val != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (val != 0) 0 0 0
307 0 return 0;
308  
309 0 c->ssl = SSL_new(c->ctx);
310 0 ▶ if (!c->ssl)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->ssl) 0 0 0
311 0 return 0;
312  
313 0 ▶ FD_ZERO(&in); FD_SET((unsigned)sd, &in);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&in); FD_SET((unsigned)sd, &in); 0 0 0
314 0 ▶ FD_ZERO(&out); FD_SET((unsigned)sd, &out);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&out); FD_SET((unsigned)sd, &out); 0 0 0
315 0 ▶ FD_ZERO(&err); FD_SET((unsigned)sd, &err);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&err); FD_SET((unsigned)sd, &err); 0 0 0
316  
317 0 SSL_set_fd(c->ssl, sd);
318  
319 0 ssl_connected = 0;
320 0 is_error = 0;
321 0 start = time(0);
322  
323 0 ▶ while (!ssl_connected && !is_error)
Branch: 2 branches
BranchTrueFalseTotal
while (!ssl_connected && !is_error) 0 0 0
while (!ssl_connected && !is_error) 0 0 0
324 {
325 0 remaining = 5 - (time(0) - start);
326 0 ▶ if (remaining <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (remaining <= 0) { 0 0 0
327 0 is_error = 1;
328 0 break;
329 }
330  
331 0 res = SSL_connect(c->ssl);
332  
333 0 ▶ ssl_connected = ((res == 1) && SSL_is_init_finished(c->ssl));
Branch: 2 branches
BranchTrueFalseTotal
ssl_connected = ((res == 1) && SSL_is_init_finished(c->ssl)); 0 0 0
ssl_connected = ((res == 1) && SSL_is_init_finished(c->ssl)); 0 0 0
334  
335 0 ▶ if (!ssl_connected)
Branch: 1 branch
BranchTrueFalseTotal
if (!ssl_connected) 0 0 0
336 {
337 0 errcode = SSL_get_error(c->ssl, res);
338 0 switch (errcode)
339 {
340 0 case SSL_ERROR_NONE:
341 /* no error, we should have a connection, check again */
342 0 break;
343  
344 0 case SSL_ERROR_WANT_READ:
345 case SSL_ERROR_WANT_WRITE:
346 /* no error, just wait for more data */
347 0 tv.tv_sec = remaining; tv.tv_usec = 0;
348 0 ▶ if (select(sd+1, &in, &out, &err, &tv) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (select(sd+1, &in, &out, &err, &tv) < 0) 0 0 0
349 0 is_error = 1;
350 0 break;
351  
352 0 case SSL_ERROR_ZERO_RETURN: /* peer closed the connection */
353 case SSL_ERROR_SSL: /* error in SSL handshake */
354 default:
355 0 is_error = 1;
356 }
357 }
358 }
359  
360 0 ▶ if (is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_error) { 0 0 0
361 0 SSL_free(c->ssl);
362 0 return 0;
363 }
364
365 0 fcntl(sd, F_SETFL, 0); /* make the socket blocking again */
366  
367 /* verify that server certificate is authentic */
368 0 server_cert = SSL_get_peer_certificate(c->ssl);
369 0 ▶ if (!server_cert || (X509_cmp(server_cert, c->tc_cert) != 0)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!server_cert || (X509_cmp(server_cert, c->tc_cert) != 0)) { 0 0 0
if (!server_cert || (X509_cmp(server_cert, c->tc_cert) != 0)) { 0 0 0
370 0 SSL_free(c->ssl);
371 0 return 0;
372 }
373  
374 0 X509_free(server_cert);
375  
376 0 return 1;
377 }
378  
379 /* This function should be called on list of socket file descriptors (sd) to determine
380 * if any have opened successfully. If so, it will return which one (index into
381 * the array). Otherwise it returns -1 if none have successfully opened.
382 * This function will block for a maximum of 3 seconds.
383 * As this function calls FinishConnection(), you shouldn't need to do anything special
384 * after it returns success - the socket is set up and ready for use.
385 */
386 0 static int CheckConnection(TCLinkCon *c, int *sd, int num_sd)
387 {
388 fd_set wr_set, err_set;
389 struct timeval tv;
390 0 int max_sd = -1, i;
391  
392 0 tv.tv_sec = 3; /* wait 3 seconds for soc->mething to happen */
393 0 tv.tv_usec = 0;
394  
395 /* build the fd_sets used for select() */
396 0 ▶ FD_ZERO(&wr_set);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&wr_set); 0 0 0
397 0 ▶ FD_ZERO(&err_set);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&err_set); 0 0 0
398 0 ▶ for (i = 0; i < num_sd; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_sd; i++) 0 0 0
399 {
400 0 ▶ if (sd[i] < 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (sd[i] < 0) continue; 0 0 0
401 0 FD_SET(sd[i], &wr_set);
402 0 FD_SET(sd[i], &err_set);
403 0 ▶ if (sd[i] > max_sd)
Branch: 1 branch
BranchTrueFalseTotal
if (sd[i] > max_sd) 0 0 0
404 0 max_sd = sd[i];
405 }
406  
407 /* run the select and see what we have waiting for us */
408 0 ▶ if (select(max_sd + 1, NULL, &wr_set, &err_set, &tv) < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (select(max_sd + 1, NULL, &wr_set, &err_set, &tv) < 1) 0 0 0
409 0 return -1; /* I hope this never happens */
410  
411 0 ▶ for (i = 0; i < num_sd; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_sd; i++) 0 0 0
412 0 ▶ if (sd[i] >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sd[i] >= 0) 0 0 0
413 {
414 0 ▶ if (FD_ISSET(sd[i], &err_set))
Branch: 1 branch
BranchTrueFalseTotal
if (FD_ISSET(sd[i], &err_set)) 0 0 0
415 {
416 /* error - close the socket and mark it defunct */
417 0 close(sd[i]);
418 0 sd[i] = -1;
419 }
420 0 ▶ else if (FD_ISSET(sd[i], &wr_set))
Branch: 1 branch
BranchTrueFalseTotal
else if (FD_ISSET(sd[i], &wr_set)) 0 0 0
421 {
422 /* socket has opened! try to negotiate SSL */
423 0 ▶ if (FinishConnection(c, sd[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (FinishConnection(c, sd[i])) { 0 0 0
424 /* socket is ready to go, so return success */
425 0 c->sd = sd[i];
426 0 return i;
427 }
428 else {
429 /* SSL handshake had errors, close the socket and mark it defunct */
430 0 close(sd[i]);
431 0 sd[i] = -1;
432 }
433 }
434 }
435  
436 /* if we get here, nothing much interesting happened during those 3 seconds */
437 0 return -1;
438 }
439  
440 0 void do_SSL_randomize()
441 {
442 enum { RAND_VALS = 32 };
443 int randbuf[RAND_VALS];
444 char fname[512];
445 int use_rand_file;
446 time_t t;
447 int i, c;
448  
449 /* if they have a /dev/urandom we can skip this function */
450 0 ▶ if (RAND_status() != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (RAND_status() != 0) 0 0 0
451 0 return;
452  
453 0 t = time(0);
454 0 RAND_seed((char *)&t, sizeof(time_t));
455  
456 /* have they specified a random file with RANDFILE environment variable? */
457 0 use_rand_file = RAND_file_name(fname, sizeof(fname)) ? 1 : 0;
458 0 ▶ if (use_rand_file)
Branch: 1 branch
BranchTrueFalseTotal
if (use_rand_file) 0 0 0
459 0 RAND_load_file(fname, 4096);
460  
461 /* stuff it with packets of random numbers until it is satisfied */
462 0 ▶ for (i = 0; i < 256 && RAND_status() == 0; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < 256 && RAND_status() == 0; i++) 0 0 0
for (i = 0; i < 256 && RAND_status() == 0; i++) 0 0 0
463 {
464 0 ▶ for (c = 0; c < RAND_VALS; c++)
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < RAND_VALS; c++) 0 0 0
465 0 randbuf[c] = rand();
466 0 RAND_seed((char *)randbuf, sizeof(int) * RAND_VALS);
467 }
468 }
469  
470 /* Open a connection to one of the TrustCommerce gateway servers. */
471 0 static int Connect(TCLinkCon *c, int host_hash)
472 {
473 struct hostent default_he;
474 char *addr_list[4]; int addr[3];
475 struct hostent *he;
476 unsigned int **gw;
477  
478 enum { MAX_HOSTS = 32 };
479 time_t last_connect[MAX_HOSTS];
480 int sd[MAX_HOSTS];
481 0 int num_sd = 0;
482 int host;
483  
484 int i, j, sort, sort_val;
485  
486 0 unsigned char *cert_data_ptr = cert_data;
487  
488 0 c->sd = -1;
489 0 c->is_error = 0;
490  
491 0 srand(time(0));
492  
493 /* These are used as BACKUP ONLY if the DNS if offline. */
494 0 addr[0] = inet_addr("216.34.199.222");
495 0 addr[1] = inet_addr("216.120.83.124");
496 0 addr[2] = inet_addr("64.14.242.58");
497 0 addr_list[0] = (char *)&addr[0];
498 0 addr_list[1] = (char *)&addr[1];
499 0 addr_list[2] = (char *)&addr[2];
500 0 addr_list[3] = 0;
501 0 default_he.h_addr_list = addr_list;
502  
503 /* determine IP addresses of gateway */
504 0 ▶ if (!c->ip)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->ip) 0 0 0
505 {
506 0 he = gethostbyname(tclink_host);
507 0 ▶ if (he)
Branch: 1 branch
BranchTrueFalseTotal
if (he) 0 0 0
508 0 c->dns = 1;
509 else {
510 /* fall back to hardcoded IPs in an emergency */
511 0 c->dns = 0;
512 0 he = &default_he;
513 }
514  
515 0 ▶ for (c->num_ips = 0; he->h_addr_list[c->num_ips]; c->num_ips++)
Branch: 1 branch
BranchTrueFalseTotal
for (c->num_ips = 0; he->h_addr_list[c->num_ips]; c->num_ips++) 0 0 0
516 ;
517  
518 0 c->ip = (int *)malloc(c->num_ips * sizeof(int));
519 0 gw = (int unsigned **)he->h_addr_list;
520  
521 /* sort the IP address list before storing it */
522 0 ▶ for (i = 0; i < c->num_ips; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->num_ips; i++) 0 0 0
523 {
524 0 sort = 0; sort_val = *gw[0];
525 0 ▶ for (j = 1; j < c->num_ips; j++)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 1; j < c->num_ips; j++) 0 0 0
526 0 ▶ if (*gw[j] > sort_val)
Branch: 1 branch
BranchTrueFalseTotal
if (*gw[j] > sort_val) 0 0 0
527 {
528 0 sort = j;
529 0 sort_val = *gw[j];
530 }
531  
532 0 c->ip[i] = sort_val;
533 0 *gw[sort] = 0;
534 }
535 }
536  
537 /* do some SSL setup */
538 0 ▶ if (!c->meth)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->meth) 0 0 0
539 {
540 0 do_SSL_randomize(); /* handle systems without /dev/urandom */
541 0 SSLeay_add_ssl_algorithms();
542 0 c->meth = SSLv3_client_method();
543 }
544  
545 0 ▶ if (!c->ctx)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->ctx) 0 0 0
546 {
547 0 c->ctx = SSL_CTX_new(c->meth);
548 0 ▶ if (!c->ctx) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->ctx) return 0; 0 0 0
549 }
550  
551 /* create the valid certificate */
552 0 ▶ if (c->tc_cert == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->tc_cert == NULL) { 0 0 0
553 0 c->tc_cert = d2i_X509(NULL, &cert_data_ptr, TC_CERT_SIZE);
554 0 ▶ if (!c->tc_cert) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->tc_cert) return 0; 0 0 0
555 }
556  
557 /* This loop works as follows:
558 * Grab the first host. Try to open a connection to it. If there was an
559 * error (host down or unreachable) go to the next one. If nothing has happened
560 * after 3 seconds, open a second socket (the first one is still open!) and try
561 * with the next fail-over host. Continue to do this for a maximum of MAX_HOSTS
562 * sockets, or until our TIMEOUT value runs out. We also keep track of how recently
563 * we tried to connect to a given host, so that we avoid saturating the machines
564 * in a heavy-load situation (which could be caused by anything from heavy internet
565 * lag between the local host and the TrustCommerce servers, to heavy load on the
566 * servers themselves due to half a million people trying to run credit card
567 * transactions in the same half second - unlikely, but certainly possible.)
568 */
569 0 c->start_time = time(0);
570 0 c->pass = 1;
571 0 memset(last_connect, 0, MAX_HOSTS * sizeof(time_t));
572 0 host = host_hash % c->num_ips;
573  
574 0 ▶ for ( ; time(0) < (c->start_time + TIMEOUT); c->pass++)
Branch: 1 branch
BranchTrueFalseTotal
for ( ; time(0) < (c->start_time + TIMEOUT); c->pass++) 0 0 0
575 {
576 /* retry the first host at least once */
577 0 ▶ if (c->pass > 2) host += 1;
Branch: 1 branch
BranchTrueFalseTotal
if (c->pass > 2) host += 1; 0 0 0
578 0 ▶ if (host >= c->num_ips) host = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (host >= c->num_ips) host = 0; 0 0 0
579  
580 /* only connect if we haven't tried this host before, or it's been a little
581 * while (note random modifier to help stagger network traffic) */
582 0 ▶ if (last_connect[host] == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (last_connect[host] == 0 || 0 0 0
583 0 ▶ (time(0) - last_connect[host]) >= number(TIMEOUT / 4, TIMEOUT))
Branch: 1 branch
BranchTrueFalseTotal
(time(0) - last_connect[host]) >= number(TIMEOUT / 4, TIMEOUT)) 0 0 0
584 {
585 0 ▶ if (num_sd < MAX_HOSTS)
Branch: 1 branch
BranchTrueFalseTotal
if (num_sd < MAX_HOSTS) 0 0 0
586 {
587 /* fire up a new connection to this host */
588 0 ▶ if (c->pass != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (c->pass != 1) 0 0 0
589 0 last_connect[host] = time(0);
590  
591 0 sd[num_sd] = BeginConnection(c, c->ip[host]);
592 0 ▶ if (sd[num_sd] >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (sd[num_sd] >= 0) 0 0 0
593 0 num_sd++;
594 }
595  
596 /* scan all current sockets and see if we've made a successful connection
597 * somewhere. note that this also includes SSL and all that sort of fun,
598 * so once it returns success, we're all done. */
599 0 ▶ if (num_sd > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (num_sd > 0) 0 0 0
600 0 ▶ if (CheckConnection(c, sd, num_sd) >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (CheckConnection(c, sd, num_sd) >= 0) 0 0 0
601 {
602 /* Success: close all other file handles and return */
603 0 ▶ for (i = 0; i < num_sd; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < num_sd; i++) 0 0 0
604 0 ▶ if (sd[i] >= 0 && sd[i] != c->sd)
Branch: 2 branches
BranchTrueFalseTotal
if (sd[i] >= 0 && sd[i] != c->sd) 0 0 0
if (sd[i] >= 0 && sd[i] != c->sd) 0 0 0
605 0 close(sd[i]);
606  
607 0 return 1;
608 }
609 }
610 }
611  
612 0 return 0;
613 }
614  
615 /* Send a chunk of data through a connection previously opened with Connect(). */
616 0 static int Send(TCLinkCon *c, const char *string)
617 {
618 0 ▶ if (SSL_write(c->ssl, string, strlen(string)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (SSL_write(c->ssl, string, strlen(string)) < 0) 0 0 0
619 0 return 0;
620  
621 0 return 1;
622 }
623  
624 /* Peel a line off the current input. Note that this DOESN'T necessarily wait for all
625 * input to come in, only up to a "\n". -1 is returned for a network error, otherwise
626 * it returns the length of the line read. If there is not a complete line pending
627 * for read this will block until there is, or an error occurs.
628 */
629 0 static int ReadLine(TCLinkCon *c, char *buffer, char *destbuf)
630 {
631 struct timeval tv;
632 fd_set read;
633 fd_set error;
634  
635 while (1) /* we wait for a line to come in or an error to occur */
636 0 {
637 0 char *eol = strchr(buffer, '\n');
638 0 ▶ if (eol != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (eol != NULL) 0 0 0
639 {
640 /* peel off the line and return it */
641 0 *eol++ = 0;
642 0 safe_copy(destbuf, buffer, TC_LINE_MAX);
643 0 memmove(buffer, eol, strlen(eol)+1);
644 0 return strlen(destbuf);
645 }
646 else
647 {
648 0 ▶ if (c->is_error == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (c->is_error == 1) 0 0 0
649 0 return -1;
650  
651 /* do socket work to grab the most recent chunk of incoming data */
652 0 ▶ FD_ZERO(&read); FD_SET(c->sd, &read);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&read); FD_SET(c->sd, &read); 0 0 0
653 0 ▶ FD_ZERO(&error); FD_SET(c->sd, &error);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&error); FD_SET(c->sd, &error); 0 0 0
654 0 tv.tv_sec = TIMEOUT;
655 0 tv.tv_usec = 0;
656  
657 0 ▶ if (select(c->sd + 1, &read, NULL, &error, &tv) < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (select(c->sd + 1, &read, NULL, &error, &tv) < 1) 0 0 0
658 0 c->is_error = 1;
659 0 ▶ else if (FD_ISSET(c->sd, &error))
Branch: 1 branch
BranchTrueFalseTotal
else if (FD_ISSET(c->sd, &error)) 0 0 0
660 0 c->is_error = 1;
661 0 ▶ else if (FD_ISSET(c->sd, &read))
Branch: 1 branch
BranchTrueFalseTotal
else if (FD_ISSET(c->sd, &read)) 0 0 0
662 {
663 0 int buffer_end = strlen(buffer);
664 0 int size = SSL_read(c->ssl, buffer + buffer_end, TC_BUFF_MAX-1 - buffer_end);
665 0 ▶ if (size < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (size < 0) 0 0 0
666 0 c->is_error = 1;
667 else
668 0 buffer[buffer_end + size] = 0;
669 }
670 }
671 }
672 }
673  
674 /* Closes a connection opened with Connect() and frees memory associated with it.
675 * You ONLY need to Close() connections which opened successfully; those that don't
676 * clean up after themselves before Connect() returns.
677 */
678 0 static int Close(TCLinkCon *c)
679 {
680 0 ▶ if (c->ssl) SSL_shutdown(c->ssl);
Branch: 1 branch
BranchTrueFalseTotal
if (c->ssl) SSL_shutdown(c->ssl); 0 0 0
681  
682 0 ▶ if (c->sd >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->sd >= 0) { 0 0 0
683 0 close(c->sd);
684 0 c->sd = -1;
685 }
686  
687 0 ▶ if (c->ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->ssl) { 0 0 0
688 0 SSL_free(c->ssl);
689 0 c->ssl = NULL;
690 }
691  
692 0 ▶ if (c->ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->ctx) { 0 0 0
693 0 SSL_CTX_free(c->ctx);
694 0 c->ctx = NULL;
695 }
696  
697 /* We DON'T free c->meth or c->tc_cert here, because they can be *
698 * reused by other transactions run on this same TCLinkHandle. */
699  
700 0 return 1;
701 }
702  
703 /**********************************************
704 * API functions exported to the user client. *
705 **********************************************/
706  
707 0 TCLinkHandle TCLinkCreate()
708 {
709 0 TCLinkCon *c = (TCLinkCon *)malloc(sizeof(TCLinkCon));
710  
711 0 c->ip = NULL;
712 0 c->num_ips = 0;
713 0 c->sd = -1;
714  
715 0 c->tc_cert = NULL;
716 0 c->meth = NULL;
717 0 c->ctx = NULL;
718 0 c->ssl = NULL;
719  
720 0 c->send_param_list = NULL;
721 0 c->send_param_tail = NULL;
722 0 c->recv_param_list = NULL;
723  
724 0 c->is_error = 0;
725 0 c->pass = 0;
726 0 c->start_time = 0;
727 0 c->dns = -1;
728  
729 0 return (TCLinkHandle)c;
730 }
731  
732 0 void TCLinkPushParam(TCLinkHandle handle, const char *name, const char *value)
733 {
734 param *p;
735 char *ch;
736  
737 0 TCLinkCon *c = (TCLinkCon *)handle;
738  
739 0 ▶ if (name && value)
Branch: 2 branches
BranchTrueFalseTotal
if (name && value) 0 0 0
if (name && value) 0 0 0
740 {
741 0 p = (param *)malloc(sizeof(param));
742 0 p->name = strdup(name);
743 0 p->value = strdup(value);
744 0 p->next = NULL;
745 0 ▶ if (c->send_param_tail)
Branch: 1 branch
BranchTrueFalseTotal
if (c->send_param_tail) 0 0 0
746 0 c->send_param_tail->next = p;
747 else
748 0 c->send_param_list = p;
749 0 c->send_param_tail = p;
750  
751 /* remove newlines and equals signs from the parameter name */
752 0 ▶ for (ch = p->name; *ch; ch++)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = p->name; *ch; ch++) 0 0 0
753 0 ▶ if (*ch == '=' || *ch == '\n') *ch = ' ';
Branch: 2 branches
BranchTrueFalseTotal
if (*ch == '=' || *ch == '\n') *ch = ' '; 0 0 0
if (*ch == '=' || *ch == '\n') *ch = ' '; 0 0 0
754  
755 /* remove newlines from the value */
756 0 ▶ for (ch = p->value; *ch; ch++)
Branch: 1 branch
BranchTrueFalseTotal
for (ch = p->value; *ch; ch++) 0 0 0
757 0 ▶ if (*ch == '\n') *ch = ' ';
Branch: 1 branch
BranchTrueFalseTotal
if (*ch == '\n') *ch = ' '; 0 0 0
758 }
759 0 }
760  
761 0 void TCLinkSend(TCLinkHandle handle)
762 {
763 param *p, *next;
764 char buf[TC_BUFF_MAX], destbuf[TC_LINE_MAX];
765 char buf2[1024];
766 0 int host_hash = 1;
767 0 int retval = 0;
768  
769 0 TCLinkCon *c = (TCLinkCon *)handle;
770  
771 0 ClearRecvList(c);
772  
773 /* build most of the string we will send to the processor */
774 0 sprintf(buf, "BEGIN\nversion=%s\n", tclink_version);
775  
776 0 ▶ for (p = c->send_param_list; p; p = next)
Branch: 1 branch
BranchTrueFalseTotal
for (p = c->send_param_list; p; p = next) 0 0 0
777 {
778 0 next = p->next;
779 0 SAFE_COPY(buf2, p->name);
780 0 SAFE_APPEND(buf2, "=");
781 0 SAFE_APPEND(buf2, p->value);
782 0 SAFE_APPEND(buf2, "\n");
783 0 SAFE_APPEND(buf, buf2);
784 0 ▶ if (!strcasecmp(p->name, "custid")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!strcasecmp(p->name, "custid")) { 0 0 0
785 0 host_hash = atoi(p->value);
786 0 host_hash = (host_hash / 100) + (host_hash % 100);
787 }
788 0 free(p->name);
789 0 free(p->value);
790 0 free(p);
791 }
792  
793 0 c->send_param_list = c->send_param_tail = NULL;
794  
795 /* try to make the connection */
796 0 ▶ if (!Connect(c, host_hash))
Branch: 1 branch
BranchTrueFalseTotal
if (!Connect(c, host_hash)) 0 0 0
797 {
798 0 Close(c); /* clean up any memory Connect() may have left lying around */
799 0 AddRecvParam(c, "status", "error");
800 0 AddRecvParam(c, "errortype", "cantconnect");
801 0 return;
802 }
803  
804 /* append some data about the connection */
805 0 sprintf(buf+strlen(buf), "pass=%d\ntime=%ld\n", c->pass, time(0) - c->start_time);
806 0 ▶ if (c->dns != 1) SAFE_APPEND(buf, "dns=n\n");
Branch: 1 branch
BranchTrueFalseTotal
if (c->dns != 1) SAFE_APPEND(buf, "dns=n\n"); 0 0 0
807 0 SAFE_APPEND(buf, "END\n");
808  
809 /* send the data */
810 0 ▶ if (Send(c, buf))
Branch: 1 branch
BranchTrueFalseTotal
if (Send(c, buf)) 0 0 0
811 {
812 0 int state = 0;
813 0 buf[0] = destbuf[0] = 0; /* recycle buf */
814 0 c->is_error = 0;
815 while (1)
816 0 {
817 0 int len = ReadLine(c, buf, destbuf);
818 0 ▶ if (len == 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) continue; 0 0 0
819 0 ▶ if (len < 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 0) break; 0 0 0
820 0 ▶ if (strcasecmp(destbuf, "BEGIN") == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcasecmp(destbuf, "BEGIN") == 0) 0 0 0
821 {
822 0 ▶ if (state != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (state != 0) 0 0 0
823 0 { state = -1; break; }
824 0 state = 1;
825 }
826 0 ▶ else if (strcasecmp(destbuf, "END") == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (strcasecmp(destbuf, "END") == 0) 0 0 0
827 {
828 0 ▶ if (state != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (state != 1) 0 0 0
829 0 state = -1;
830 else
831 0 state = 2;
832 0 break;
833 }
834 else
835 {
836 0 ▶ if (state != 1 || !AddRecvString(c, destbuf))
Branch: 2 branches
BranchTrueFalseTotal
if (state != 1 || !AddRecvString(c, destbuf)) 0 0 0
if (state != 1 || !AddRecvString(c, destbuf)) 0 0 0
837 0 { state = -1; break; }
838 }
839 }
840 0 ▶ if (state == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (state == 2) 0 0 0
841 0 retval = 1;
842 }
843  
844 0 Close(c);
845  
846 0 ▶ if (!retval)
Branch: 1 branch
BranchTrueFalseTotal
if (!retval) 0 0 0
847 {
848 0 ClearRecvList(c);
849 0 AddRecvParam(c, "status", "error");
850 0 AddRecvParam(c, "errortype", "linkfailure");
851 }
852 }
853
854 0 char *TCLinkGetResponse(TCLinkHandle handle, const char *name, char *value)
855 {
856 param *p;
857 0 TCLinkCon *c = (TCLinkCon *)handle;
858  
859 0 ▶ for (p = c->recv_param_list; p; p = p->next)
Branch: 1 branch
BranchTrueFalseTotal
for (p = c->recv_param_list; p; p = p->next) 0 0 0
860 0 ▶ if (strcasecmp(name, p->name) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (strcasecmp(name, p->name) == 0) 0 0 0
861 {
862 0 safe_copy(value, p->value, PARAM_MAX_LEN);
863 0 return value;
864 }
865  
866 0 return NULL;
867 }
868  
869 0 static void stuff_string(char *buf, int *len, int size, const char *add)
870 {
871 0 int newlen = strlen(add);
872 0 ▶ if ((*len + newlen) >= size)
Branch: 1 branch
BranchTrueFalseTotal
if ((*len + newlen) >= size) 0 0 0
873 0 newlen = size - *len - 1;
874 0 ▶ if (newlen < 1) return;
Branch: 1 branch
BranchTrueFalseTotal
if (newlen < 1) return; 0 0 0
875 0 strncpy(buf + *len, add, newlen);
876 0 *len += newlen;
877 0 buf[*len] = 0;
878 }
879  
880 0 char *TCLinkGetEntireResponse(TCLinkHandle handle, char *buf, int size)
881 {
882 param *p;
883 0 int len = 0;
884 0 TCLinkCon *c = (TCLinkCon *)handle;
885  
886 0 ▶ for (p = c->recv_param_list; p; p = p->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = c->recv_param_list; p; p = p->next) { 0 0 0
887 0 stuff_string(buf, &len, size, p->name);
888 0 stuff_string(buf, &len, size, "=");
889 0 stuff_string(buf, &len, size, p->value);
890 0 stuff_string(buf, &len, size, "\n");
891 }
892  
893 0 return buf;
894 }
895  
896 0 void TCLinkDestroy(TCLinkHandle handle)
897 {
898 0 TCLinkCon *c = (TCLinkCon *)handle;
899 0 ▶ if (!c) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!c) return; 0 0 0
900  
901 0 ClearSendList(c);
902 0 ClearRecvList(c);
903 0 Close(c);
904  
905 0 ▶ if (c->ip)
Branch: 1 branch
BranchTrueFalseTotal
if (c->ip) 0 0 0
906 0 free(c->ip);
907  
908 0 ▶ if (c->tc_cert)
Branch: 1 branch
BranchTrueFalseTotal
if (c->tc_cert) 0 0 0
909 0 X509_free(c->tc_cert);
910  
911 0 free(c);
912 }
913  
914 0 char *TCLinkGetVersion(char *buf)
915 {
916 0 strcpy(buf, tclink_version);
917 0 return buf;
918 }
919  
« TCLink.xs ↑ Summary