mark PurpleImageClass as private
[pidgin-git.git] / libpurple / protocols / novell / nmconn.c
blobecd261f5a8688cfaba0bc7f5de274a368401e41b
1 /*
2 * nmconn.c
4 * Copyright (c) 2004 Novell, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 #include <glib.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <time.h>
27 #include "nmconn.h"
29 #ifdef _WIN32
30 #include <windows.h>
31 #endif
33 #include "util.h"
35 #define NO_ESCAPE(ch) ((ch == 0x20) || (ch >= 0x30 && ch <= 0x39) || \
36 (ch >= 0x41 && ch <= 0x5a) || (ch >= 0x61 && ch <= 0x7a))
38 /* Read data from conn until the end of a line */
39 static NMERR_T
40 read_line(NMConn * conn, char *buff, int len)
42 NMERR_T rc = NM_OK;
43 int total_bytes = 0;
45 while ((rc == NM_OK) && (total_bytes < (len - 1))) {
46 rc = nm_read_all(conn, &buff[total_bytes], 1);
47 if (rc == NM_OK) {
48 total_bytes += 1;
49 if (buff[total_bytes - 1] == '\n') {
50 break;
54 buff[total_bytes] = '\0';
56 return rc;
59 static char *
60 url_escape_string(char *src)
62 guint32 escape = 0;
63 char *p;
64 char *q;
65 char *encoded = NULL;
66 int ch;
68 static const char hex_table[16] = "0123456789abcdef";
70 if (src == NULL) {
71 return NULL;
74 /* Find number of chars to escape */
75 for (p = src; *p != '\0'; p++) {
76 ch = (guchar) *p;
77 if (!NO_ESCAPE(ch)) {
78 escape++;
82 encoded = g_malloc((p - src) + (escape * 2) + 1);
84 /* Escape the string */
85 for (p = src, q = encoded; *p != '\0'; p++) {
86 ch = (guchar) * p;
87 if (NO_ESCAPE(ch)) {
88 if (ch != 0x20) {
89 *q = ch;
90 q++;
91 } else {
92 *q = '+';
93 q++;
95 } else {
96 *q = '%';
97 q++;
99 *q = hex_table[ch >> 4];
100 q++;
102 *q = hex_table[ch & 15];
103 q++;
106 *q = '\0';
108 return encoded;
111 static char *
112 encode_method(guint8 method)
114 char *str;
116 switch (method) {
117 case NMFIELD_METHOD_EQUAL:
118 str = "G";
119 break;
120 case NMFIELD_METHOD_UPDATE:
121 str = "F";
122 break;
123 case NMFIELD_METHOD_GTE:
124 str = "E";
125 break;
126 case NMFIELD_METHOD_LTE:
127 str = "D";
128 break;
129 case NMFIELD_METHOD_NE:
130 str = "C";
131 break;
132 case NMFIELD_METHOD_EXIST:
133 str = "B";
134 break;
135 case NMFIELD_METHOD_NOTEXIST:
136 str = "A";
137 break;
138 case NMFIELD_METHOD_SEARCH:
139 str = "9";
140 break;
141 case NMFIELD_METHOD_MATCHBEGIN:
142 str = "8";
143 break;
144 case NMFIELD_METHOD_MATCHEND:
145 str = "7";
146 break;
147 case NMFIELD_METHOD_NOT_ARRAY:
148 str = "6";
149 break;
150 case NMFIELD_METHOD_OR_ARRAY:
151 str = "5";
152 break;
153 case NMFIELD_METHOD_AND_ARRAY:
154 str = "4";
155 break;
156 case NMFIELD_METHOD_DELETE_ALL:
157 str = "3";
158 break;
159 case NMFIELD_METHOD_DELETE:
160 str = "2";
161 break;
162 case NMFIELD_METHOD_ADD:
163 str = "1";
164 break;
165 default: /* NMFIELD_METHOD_VALID */
166 str = "0";
167 break;
170 return str;
173 NMConn *
174 nm_create_conn(const char *addr, int port)
176 NMConn *conn = g_new0(NMConn, 1);
177 conn->addr = g_strdup(addr);
178 conn->port = port;
179 return conn;
182 void nm_release_conn(NMConn *conn)
184 if (conn) {
185 GSList *node;
186 for (node = conn->requests; node; node = node->next) {
187 if (node->data)
188 nm_release_request(node->data);
190 g_slist_free(conn->requests);
191 conn->requests = NULL;
192 g_free(conn->ssl_conn);
193 conn->ssl_conn = NULL;
194 g_free(conn->addr);
195 conn->addr = NULL;
196 g_free(conn);
201 nm_tcp_write(NMConn * conn, const void *buff, int len)
203 if (conn == NULL || buff == NULL)
204 return -1;
206 if (!conn->use_ssl)
207 return (write(conn->fd, buff, len));
208 else if (conn->ssl_conn && conn->ssl_conn->write)
209 return (conn->ssl_conn->write(conn->ssl_conn->data, buff, len));
210 else
211 return -1;
215 nm_tcp_read(NMConn * conn, void *buff, int len)
217 if (conn == NULL || buff == NULL)
218 return -1;
220 if (!conn->use_ssl)
221 return (read(conn->fd, buff, len));
222 else if (conn->ssl_conn && conn->ssl_conn->read)
223 return ((conn->ssl_conn->read)(conn->ssl_conn->data, buff, len));
224 else
225 return -1;
228 NMERR_T
229 nm_read_all(NMConn * conn, char *buff, int len)
231 NMERR_T rc = NM_OK;
232 int bytes_left = len;
233 int bytes_read;
234 int total_bytes = 0;
235 int retry = 1000;
237 if (conn == NULL || buff == NULL)
238 return NMERR_BAD_PARM;
240 /* Keep reading until buffer is full */
241 while (bytes_left) {
242 bytes_read = nm_tcp_read(conn, &buff[total_bytes], bytes_left);
243 if (bytes_read > 0) {
244 bytes_left -= bytes_read;
245 total_bytes += bytes_read;
246 } else {
247 if (errno == EAGAIN) {
248 if (--retry == 0) {
249 rc = NMERR_TCP_READ;
250 break;
252 #ifdef _WIN32
253 Sleep(1);
254 #else
255 usleep(1000);
256 #endif
257 } else {
258 rc = NMERR_TCP_READ;
259 break;
263 return rc;
266 NMERR_T
267 nm_read_uint32(NMConn *conn, guint32 *val)
269 NMERR_T rc = NM_OK;
271 rc = nm_read_all(conn, (char *)val, sizeof(*val));
272 if (rc == NM_OK) {
273 *val = GUINT32_FROM_LE(*val);
276 return rc;
279 NMERR_T
280 nm_read_uint16(NMConn *conn, guint16 *val)
282 NMERR_T rc = NM_OK;
284 rc = nm_read_all(conn, (char *)val, sizeof(*val));
285 if (rc == NM_OK) {
286 *val = GUINT16_FROM_LE(*val);
289 return rc;
292 NMERR_T
293 nm_write_fields(NMConn * conn, NMField * fields)
295 NMERR_T rc = NM_OK;
296 NMField *field;
297 char *value = NULL;
298 char *method = NULL;
299 char buffer[4096];
300 int ret;
301 int bytes_to_send;
302 int val = 0;
304 if (conn == NULL || fields == NULL) {
305 return NMERR_BAD_PARM;
308 /* Format each field as valid "post" data and write it out */
309 for (field = fields; (rc == NM_OK) && (field->tag); field++) {
311 /* We don't currently handle binary types */
312 if (field->method == NMFIELD_METHOD_IGNORE ||
313 field->type == NMFIELD_TYPE_BINARY) {
314 continue;
317 /* Write the field tag */
318 bytes_to_send = g_snprintf(buffer, sizeof(buffer), "&tag=%s", field->tag);
319 ret = nm_tcp_write(conn, buffer, bytes_to_send);
320 if (ret < 0) {
321 rc = NMERR_TCP_WRITE;
324 /* Write the field method */
325 if (rc == NM_OK) {
326 method = encode_method(field->method);
327 bytes_to_send = g_snprintf(buffer, sizeof(buffer), "&cmd=%s", method);
328 ret = nm_tcp_write(conn, buffer, bytes_to_send);
329 if (ret < 0) {
330 rc = NMERR_TCP_WRITE;
334 /* Write the field value */
335 if (rc == NM_OK) {
336 switch (field->type) {
337 case NMFIELD_TYPE_UTF8:
338 case NMFIELD_TYPE_DN:
340 value = url_escape_string((char *) field->ptr_value);
341 bytes_to_send = g_snprintf(buffer, sizeof(buffer),
342 "&val=%s", value);
343 if (bytes_to_send > (int)sizeof(buffer)) {
344 ret = nm_tcp_write(conn, buffer, sizeof(buffer));
345 } else {
346 ret = nm_tcp_write(conn, buffer, bytes_to_send);
349 if (ret < 0) {
350 rc = NMERR_TCP_WRITE;
353 g_free(value);
355 break;
357 case NMFIELD_TYPE_ARRAY:
358 case NMFIELD_TYPE_MV:
360 val = nm_count_fields((NMField *) field->ptr_value);
361 bytes_to_send = g_snprintf(buffer, sizeof(buffer),
362 "&val=%u", val);
363 ret = nm_tcp_write(conn, buffer, bytes_to_send);
364 if (ret < 0) {
365 rc = NMERR_TCP_WRITE;
368 break;
370 default:
372 bytes_to_send = g_snprintf(buffer, sizeof(buffer),
373 "&val=%u", field->value);
374 ret = nm_tcp_write(conn, buffer, bytes_to_send);
375 if (ret < 0) {
376 rc = NMERR_TCP_WRITE;
379 break;
383 /* Write the field type */
384 if (rc == NM_OK) {
385 bytes_to_send = g_snprintf(buffer, sizeof(buffer),
386 "&type=%u", field->type);
387 ret = nm_tcp_write(conn, buffer, bytes_to_send);
388 if (ret < 0) {
389 rc = NMERR_TCP_WRITE;
393 /* If the field is a sub array then post its fields */
394 if (rc == NM_OK && val > 0) {
395 if (field->type == NMFIELD_TYPE_ARRAY ||
396 field->type == NMFIELD_TYPE_MV) {
398 rc = nm_write_fields(conn, (NMField *) field->ptr_value);
404 return rc;
407 NMERR_T
408 nm_send_request(NMConn *conn, char *cmd, NMField *fields,
409 nm_response_cb cb, gpointer data, NMRequest **request)
411 NMERR_T rc = NM_OK;
412 char buffer[512];
413 int bytes_to_send;
414 int ret;
415 NMField *request_fields = NULL;
416 char *str = NULL;
418 if (conn == NULL || cmd == NULL)
419 return NMERR_BAD_PARM;
421 /* Write the post */
422 bytes_to_send = g_snprintf(buffer, sizeof(buffer),
423 "POST /%s HTTP/1.0\r\n", cmd);
424 ret = nm_tcp_write(conn, buffer, bytes_to_send);
425 if (ret < 0) {
426 rc = NMERR_TCP_WRITE;
429 /* Write headers */
430 if (rc == NM_OK) {
431 if (purple_strequal("login", cmd)) {
432 bytes_to_send = g_snprintf(buffer, sizeof(buffer),
433 "Host: %s:%d\r\n\r\n", conn->addr, conn->port);
434 ret = nm_tcp_write(conn, buffer, bytes_to_send);
435 if (ret < 0) {
436 rc = NMERR_TCP_WRITE;
438 } else {
439 bytes_to_send = g_snprintf(buffer, sizeof(buffer), "\r\n");
440 ret = nm_tcp_write(conn, buffer, bytes_to_send);
441 if (ret < 0) {
442 rc = NMERR_TCP_WRITE;
447 /* Add the transaction id to the request fields */
448 if (rc == NM_OK) {
449 if (fields)
450 request_fields = nm_copy_field_array(fields);
452 str = g_strdup_printf("%d", ++(conn->trans_id));
453 request_fields = nm_field_add_pointer(request_fields, NM_A_SZ_TRANSACTION_ID, 0,
454 NMFIELD_METHOD_VALID, 0,
455 str, NMFIELD_TYPE_UTF8);
458 /* Send the request to the server */
459 if (rc == NM_OK) {
460 rc = nm_write_fields(conn, request_fields);
463 /* Write the CRLF to terminate the data */
464 if (rc == NM_OK) {
465 ret = nm_tcp_write(conn, "\r\n", strlen("\r\n"));
466 if (ret < 0) {
467 rc = NMERR_TCP_WRITE;
471 /* Create a request struct, add it to our queue, and return it */
472 if (rc == NM_OK) {
473 NMRequest *new_request = nm_create_request(cmd, conn->trans_id,
474 time(0), cb, NULL, data);
475 nm_conn_add_request_item(conn, new_request);
477 /* Set the out param if it was sent in, otherwise release the request */
478 if (request)
479 *request = new_request;
480 else
481 nm_release_request(new_request);
484 if (request_fields != NULL)
485 nm_free_fields(&request_fields);
487 return rc;
490 NMERR_T
491 nm_read_header(NMConn * conn)
493 NMERR_T rc = NM_OK;
494 char buffer[512];
495 char *ptr = NULL;
496 int i;
497 char rtn_buf[8];
498 int rtn_code = 0;
500 if (conn == NULL)
501 return NMERR_BAD_PARM;
503 *buffer = '\0';
504 rc = read_line(conn, buffer, sizeof(buffer));
505 if (rc == NM_OK) {
507 /* Find the return code */
508 ptr = strchr(buffer, ' ');
509 if (ptr != NULL) {
510 ptr++;
512 i = 0;
513 while (isdigit(*ptr) && (i < 3)) {
514 rtn_buf[i] = *ptr;
515 i++;
516 ptr++;
518 rtn_buf[i] = '\0';
520 if (i > 0)
521 rtn_code = atoi(rtn_buf);
525 /* Finish reading header, in the future we might want to do more processing here */
526 /* TODO: handle more general redirects in the future */
527 while ((rc == NM_OK) && (!purple_strequal(buffer, "\r\n"))) {
528 rc = read_line(conn, buffer, sizeof(buffer));
531 if (rc == NM_OK && rtn_code == 301)
532 rc = NMERR_SERVER_REDIRECT;
534 return rc;
537 NMERR_T
538 nm_read_fields(NMConn * conn, int count, NMField ** fields)
540 NMERR_T rc = NM_OK;
541 guint8 type;
542 guint8 method;
543 guint32 val;
544 char tag[64];
545 NMField *sub_fields = NULL;
546 char *str = NULL;
548 if (conn == NULL || fields == NULL)
549 return NMERR_BAD_PARM;
551 do {
552 if (count > 0) {
553 count--;
556 /* Read the field type, method, and tag */
557 rc = nm_read_all(conn, (char *)&type, sizeof(type));
558 if (rc != NM_OK || type == 0)
559 break;
561 rc = nm_read_all(conn, (char *)&method, sizeof(method));
562 if (rc != NM_OK)
563 break;
565 rc = nm_read_uint32(conn, &val);
566 if (rc != NM_OK)
567 break;
569 if (val > sizeof(tag)) {
570 rc = NMERR_PROTOCOL;
571 break;
574 rc = nm_read_all(conn, tag, val);
575 if (rc != NM_OK)
576 break;
578 if (type == NMFIELD_TYPE_MV || type == NMFIELD_TYPE_ARRAY) {
580 /* Read the subarray (first read the number of items in the array) */
581 rc = nm_read_uint32(conn, &val);
582 if (rc != NM_OK)
583 break;
585 if (val > 0) {
586 rc = nm_read_fields(conn, val, &sub_fields);
587 if (rc != NM_OK)
588 break;
591 *fields = nm_field_add_pointer(*fields, tag, 0, method,
592 0, sub_fields, type);
594 sub_fields = NULL;
596 } else if (type == NMFIELD_TYPE_UTF8 || type == NMFIELD_TYPE_DN) {
598 /* Read the string (first read the length) */
599 rc = nm_read_uint32(conn, &val);
600 if (rc != NM_OK)
601 break;
603 if (val >= NMFIELD_MAX_STR_LENGTH) {
604 rc = NMERR_PROTOCOL;
605 break;
608 if (val > 0) {
609 str = g_new0(char, val + 1);
611 rc = nm_read_all(conn, str, val);
612 if (rc != NM_OK)
613 break;
615 *fields = nm_field_add_pointer(*fields, tag, 0, method,
616 0, str, type);
617 str = NULL;
620 } else {
622 /* Read the numerical value */
623 rc = nm_read_uint32(conn, &val);
624 if (rc != NM_OK)
625 break;
627 *fields = nm_field_add_number(*fields, tag, 0, method,
628 0, val, type);
631 } while ((type != 0) && (count != 0));
634 g_free(str);
636 if (sub_fields != NULL) {
637 nm_free_fields(&sub_fields);
640 return rc;
643 void
644 nm_conn_add_request_item(NMConn * conn, NMRequest * request)
646 if (conn == NULL || request == NULL)
647 return;
649 nm_request_add_ref(request);
650 conn->requests = g_slist_append(conn->requests, request);
653 void
654 nm_conn_remove_request_item(NMConn * conn, NMRequest * request)
656 if (conn == NULL || request == NULL)
657 return;
659 conn->requests = g_slist_remove(conn->requests, request);
660 nm_release_request(request);
663 NMRequest *
664 nm_conn_find_request(NMConn * conn, int trans_id)
666 NMRequest *req = NULL;
667 GSList *itr = NULL;
669 if (conn == NULL)
670 return NULL;
672 itr = conn->requests;
673 while (itr) {
674 req = (NMRequest *) itr->data;
675 if (req != NULL && nm_request_get_trans_id(req) == trans_id) {
676 return req;
678 itr = g_slist_next(itr);
680 return NULL;
683 const char *
684 nm_conn_get_addr(NMConn * conn)
686 if (conn == NULL)
687 return NULL;
688 else
689 return conn->addr;
693 nm_conn_get_port(NMConn * conn)
695 if (conn == NULL)
696 return -1;
697 else
698 return conn->port;