Mercurial > pidgin
annotate src/protocols/novell/nmconn.c @ 9125:668ffb8fec00
[gaim-migrate @ 9902]
(12:53:05) nosnilmot: LSchiere: not majorly important, but the pref changes
listed in the ChangeLog are out of sync
committer: Tailor Script <tailor@pidgin.im>
| author | Luke Schierer <lschiere@pidgin.im> |
|---|---|
| date | Sun, 30 May 2004 16:54:40 +0000 |
| parents | 6663ad2386d9 |
| children | 54fb1f466953 |
| rev | line source |
|---|---|
| 8675 | 1 /* |
| 2 * nmconn.c | |
| 3 * | |
| 8933 | 4 * Copyright (c) 2004 Novell, Inc. All Rights Reserved. |
| 5 * | |
| 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. | |
| 8675 | 9 * |
| 8933 | 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. | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
14 * |
| 8933 | 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 8675 | 18 * |
| 19 */ | |
| 20 | |
| 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" | |
| 28 | |
| 29 #ifdef _WIN32 | |
| 30 #include <windows.h> | |
| 31 #endif | |
| 32 | |
| 33 #define NO_ESCAPE(ch) ((ch == 0x20) || (ch >= 0x30 && ch <= 0x39) || \ | |
| 34 (ch >= 0x41 && ch <= 0x5a) || (ch >= 0x61 && ch <= 0x7a)) | |
| 35 | |
| 36 /* Read data from conn until the end of a line */ | |
| 37 static NMERR_T | |
| 38 read_line(NMConn * conn, char *buff, int len) | |
| 39 { | |
| 40 NMERR_T rc = NM_OK; | |
| 41 int total_bytes = 0; | |
| 42 | |
| 43 while ((rc == NM_OK) && (total_bytes < (len - 1))) { | |
| 44 rc = nm_read_all(conn, &buff[total_bytes], 1); | |
| 45 if (rc == NM_OK) { | |
| 46 total_bytes += 1; | |
| 47 if (buff[total_bytes - 1] == '\n') { | |
| 48 break; | |
| 49 } | |
| 50 } | |
| 51 } | |
| 52 buff[total_bytes] = '\0'; | |
| 53 | |
| 54 return rc; | |
| 55 } | |
| 56 | |
| 57 static char * | |
| 58 url_escape_string(char *src) | |
| 59 { | |
| 60 guint32 escape = 0; | |
| 61 char *p; | |
| 62 char *q; | |
| 63 char *encoded = NULL; | |
| 64 int ch; | |
| 65 | |
| 66 static const char hex_table[16] = "0123456789abcdef"; | |
| 67 | |
| 68 if (src == NULL) { | |
| 69 return NULL; | |
| 70 } | |
| 71 | |
| 72 /* Find number of chars to escape */ | |
| 73 for (p = src; *p != '\0'; p++) { | |
| 74 ch = (guchar) *p; | |
| 75 if (!NO_ESCAPE(ch)) { | |
| 76 escape++; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 encoded = g_malloc((p - src) + (escape * 2) + 1); | |
| 81 | |
| 82 /* Escape the string */ | |
| 83 for (p = src, q = encoded; *p != '\0'; p++) { | |
| 84 ch = (guchar) * p; | |
| 85 if (NO_ESCAPE(ch)) { | |
| 86 if (ch != 0x20) { | |
| 87 *q = ch; | |
| 88 q++; | |
| 89 } else { | |
| 90 *q = '+'; | |
| 91 q++; | |
| 92 } | |
| 93 } else { | |
| 94 *q = '%'; | |
| 95 q++; | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
96 |
| 8675 | 97 *q = hex_table[ch >> 4]; |
| 98 q++; | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
99 |
| 8675 | 100 *q = hex_table[ch & 15]; |
| 101 q++; | |
| 102 } | |
| 103 } | |
| 104 *q = '\0'; | |
| 105 | |
| 106 return encoded; | |
| 107 } | |
| 108 | |
| 109 static char * | |
| 110 encode_method(guint8 method) | |
| 111 { | |
| 112 char *str; | |
| 113 | |
| 114 switch (method) { | |
| 115 case NMFIELD_METHOD_EQUAL: | |
| 116 str = "G"; | |
| 117 break; | |
| 118 case NMFIELD_METHOD_UPDATE: | |
| 119 str = "F"; | |
| 120 break; | |
| 121 case NMFIELD_METHOD_GTE: | |
| 122 str = "E"; | |
| 123 break; | |
| 124 case NMFIELD_METHOD_LTE: | |
| 125 str = "D"; | |
| 126 break; | |
| 127 case NMFIELD_METHOD_NE: | |
| 128 str = "C"; | |
| 129 break; | |
| 130 case NMFIELD_METHOD_EXIST: | |
| 131 str = "B"; | |
| 132 break; | |
| 133 case NMFIELD_METHOD_NOTEXIST: | |
| 134 str = "A"; | |
| 135 break; | |
| 136 case NMFIELD_METHOD_SEARCH: | |
| 137 str = "9"; | |
| 138 break; | |
| 139 case NMFIELD_METHOD_MATCHBEGIN: | |
| 140 str = "8"; | |
| 141 break; | |
| 142 case NMFIELD_METHOD_MATCHEND: | |
| 143 str = "7"; | |
| 144 break; | |
| 145 case NMFIELD_METHOD_NOT_ARRAY: | |
| 146 str = "6"; | |
| 147 break; | |
| 148 case NMFIELD_METHOD_OR_ARRAY: | |
| 149 str = "5"; | |
| 150 break; | |
| 151 case NMFIELD_METHOD_AND_ARRAY: | |
| 152 str = "4"; | |
| 153 break; | |
| 154 case NMFIELD_METHOD_DELETE_ALL: | |
| 155 str = "3"; | |
| 156 break; | |
| 157 case NMFIELD_METHOD_DELETE: | |
| 158 str = "2"; | |
| 159 break; | |
| 160 case NMFIELD_METHOD_ADD: | |
| 161 str = "1"; | |
| 162 break; | |
| 163 default: /* NMFIELD_METHOD_VALID */ | |
| 164 str = "0"; | |
| 165 break; | |
| 166 } | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
167 |
| 8675 | 168 return str; |
| 169 } | |
| 170 | |
| 171 int | |
| 172 nm_tcp_write(NMConn * conn, const void *buff, int len) | |
| 173 { | |
| 174 if (conn == NULL || buff == NULL) | |
| 175 return -1; | |
| 176 | |
| 177 if (!conn->use_ssl) | |
| 178 return (write(conn->fd, buff, len)); | |
| 179 else if (conn->ssl_conn && conn->ssl_conn->write) | |
| 180 return (conn->ssl_conn->write(conn->ssl_conn->data, buff, len)); | |
| 181 else | |
| 182 return -1; | |
| 183 } | |
| 184 | |
| 185 int | |
| 186 nm_tcp_read(NMConn * conn, void *buff, int len) | |
| 187 { | |
| 188 if (conn == NULL || buff == NULL) | |
| 189 return -1; | |
| 190 | |
| 191 if (!conn->use_ssl) | |
| 192 return (read(conn->fd, buff, len)); | |
| 193 else if (conn->ssl_conn && conn->ssl_conn->read) | |
| 194 return (conn->ssl_conn->read(conn->ssl_conn->data, buff, len)); | |
| 195 else | |
| 196 return -1; | |
| 197 } | |
| 198 | |
| 199 NMERR_T | |
| 200 nm_read_all(NMConn * conn, char *buff, int len) | |
| 201 { | |
| 202 NMERR_T rc = NM_OK; | |
| 203 int bytes_left = len; | |
| 204 int bytes_read; | |
| 205 int total_bytes = 0; | |
| 206 int retry = 10; | |
| 207 | |
| 208 if (conn == NULL || buff == NULL) | |
| 209 return NMERR_BAD_PARM; | |
| 210 | |
| 211 /* Keep reading until buffer is full */ | |
| 212 while (bytes_left) { | |
| 213 bytes_read = nm_tcp_read(conn, &buff[total_bytes], bytes_left); | |
| 214 if (bytes_read > 0) { | |
| 215 bytes_left -= bytes_read; | |
| 216 total_bytes += bytes_read; | |
| 217 } else { | |
| 218 if (errno == EAGAIN) { | |
| 219 if (--retry == 0) { | |
| 220 rc = NMERR_TCP_READ; | |
| 221 break; | |
| 222 } | |
| 223 #ifdef _WIN32 | |
| 224 Sleep(1000); | |
| 225 #else | |
| 226 usleep(1000); | |
| 227 #endif | |
| 228 } else { | |
| 229 rc = NMERR_TCP_READ; | |
| 230 break; | |
| 231 } | |
| 232 } | |
| 233 } | |
| 234 return rc; | |
| 235 } | |
| 236 | |
| 237 NMERR_T | |
| 8874 | 238 nm_read_uint32(NMConn *conn, guint32 *val) |
| 239 { | |
| 240 NMERR_T rc = NM_OK; | |
| 241 | |
| 242 rc = nm_read_all(conn, (char *)val, sizeof(*val)); | |
| 243 if (rc == NM_OK) { | |
| 244 *val = GUINT32_FROM_LE(*val); | |
| 245 } | |
| 246 | |
| 247 return rc; | |
| 248 } | |
| 249 | |
| 250 NMERR_T | |
| 251 nm_read_uint16(NMConn *conn, guint16 *val) | |
| 252 { | |
| 253 NMERR_T rc = NM_OK; | |
| 254 | |
| 255 rc = nm_read_all(conn, (char *)val, sizeof(*val)); | |
| 256 if (rc == NM_OK) { | |
| 257 *val = GUINT16_FROM_LE(*val); | |
| 258 } | |
| 259 | |
| 260 return rc; | |
| 261 } | |
| 262 | |
| 263 NMERR_T | |
| 8675 | 264 nm_write_fields(NMConn * conn, NMField * fields) |
| 265 { | |
| 266 NMERR_T rc = NM_OK; | |
| 267 NMField *field; | |
| 268 char *value = NULL; | |
| 269 char *method = NULL; | |
| 8874 | 270 char buffer[4096]; |
| 8675 | 271 int ret; |
| 272 int bytes_to_send; | |
| 273 int val = 0; | |
| 274 | |
| 275 if (conn == NULL || fields == NULL) { | |
| 276 return NMERR_BAD_PARM; | |
| 277 } | |
| 278 | |
| 279 /* Format each field as valid "post" data and write it out */ | |
| 280 for (field = fields; (rc == NM_OK) && (field->tag); field++) { | |
| 281 | |
| 282 /* We don't currently handle binary types */ | |
| 283 if (field->method == NMFIELD_METHOD_IGNORE || | |
| 284 field->type == NMFIELD_TYPE_BINARY) { | |
| 285 continue; | |
| 286 } | |
| 287 | |
| 288 /* Write the field tag */ | |
| 289 bytes_to_send = g_snprintf(buffer, sizeof(buffer), "&tag=%s", field->tag); | |
| 290 ret = nm_tcp_write(conn, buffer, bytes_to_send); | |
| 291 if (ret < 0) { | |
| 292 rc = NMERR_TCP_WRITE; | |
| 293 } | |
| 294 | |
| 295 /* Write the field method */ | |
| 296 if (rc == NM_OK) { | |
| 297 method = encode_method(field->method); | |
| 298 bytes_to_send = g_snprintf(buffer, sizeof(buffer), "&cmd=%s", method); | |
| 299 ret = nm_tcp_write(conn, buffer, bytes_to_send); | |
| 300 if (ret < 0) { | |
| 301 rc = NMERR_TCP_WRITE; | |
| 302 } | |
| 303 } | |
| 304 | |
| 305 /* Write the field value */ | |
| 306 if (rc == NM_OK) { | |
| 307 switch (field->type) { | |
| 308 case NMFIELD_TYPE_UTF8: | |
| 309 case NMFIELD_TYPE_DN: | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
310 |
| 8933 | 311 value = url_escape_string((char *) field->ptr_value); |
| 8675 | 312 bytes_to_send = g_snprintf(buffer, sizeof(buffer), |
| 313 "&val=%s", value); | |
| 8874 | 314 if (bytes_to_send > (int)sizeof(buffer)) { |
| 315 ret = nm_tcp_write(conn, buffer, sizeof(buffer)); | |
| 316 } else { | |
| 317 ret = nm_tcp_write(conn, buffer, bytes_to_send); | |
| 318 } | |
| 319 | |
| 8675 | 320 if (ret < 0) { |
| 321 rc = NMERR_TCP_WRITE; | |
| 322 } | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
323 |
| 8675 | 324 g_free(value); |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
325 |
| 8675 | 326 break; |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
327 |
| 8675 | 328 case NMFIELD_TYPE_ARRAY: |
| 329 case NMFIELD_TYPE_MV: | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
330 |
| 8933 | 331 val = nm_count_fields((NMField *) field->ptr_value); |
| 8675 | 332 bytes_to_send = g_snprintf(buffer, sizeof(buffer), |
| 333 "&val=%u", val); | |
| 334 ret = nm_tcp_write(conn, buffer, bytes_to_send); | |
| 335 if (ret < 0) { | |
| 336 rc = NMERR_TCP_WRITE; | |
| 337 } | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
338 |
| 8675 | 339 break; |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
340 |
| 8675 | 341 default: |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
342 |
| 8675 | 343 bytes_to_send = g_snprintf(buffer, sizeof(buffer), |
| 344 "&val=%u", field->value); | |
| 345 ret = nm_tcp_write(conn, buffer, bytes_to_send); | |
| 346 if (ret < 0) { | |
| 347 rc = NMERR_TCP_WRITE; | |
| 348 } | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
349 |
| 8675 | 350 break; |
| 351 } | |
| 352 } | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
353 |
| 8675 | 354 /* Write the field type */ |
| 355 if (rc == NM_OK) { | |
| 356 bytes_to_send = g_snprintf(buffer, sizeof(buffer), | |
| 357 "&type=%u", field->type); | |
| 358 ret = nm_tcp_write(conn, buffer, bytes_to_send); | |
| 359 if (ret < 0) { | |
| 360 rc = NMERR_TCP_WRITE; | |
| 361 } | |
| 362 } | |
| 363 | |
| 364 /* If the field is a sub array then post its fields */ | |
| 365 if (rc == NM_OK && val > 0) { | |
| 366 if (field->type == NMFIELD_TYPE_ARRAY || | |
| 367 field->type == NMFIELD_TYPE_MV) { | |
| 368 | |
| 8933 | 369 rc = nm_write_fields(conn, (NMField *) field->ptr_value); |
| 8675 | 370 |
| 371 } | |
| 372 } | |
| 373 } | |
| 374 | |
| 375 return rc; | |
| 376 } | |
| 377 | |
| 378 NMERR_T | |
| 379 nm_send_request(NMConn * conn, char *cmd, NMField * fields, NMRequest ** req) | |
| 380 { | |
| 381 NMERR_T rc = NM_OK; | |
| 382 char buffer[512]; | |
| 383 int bytes_to_send; | |
| 384 int ret; | |
| 385 NMField *request = NULL; | |
| 386 | |
| 387 if (conn == NULL || cmd == NULL) | |
| 388 return NMERR_BAD_PARM; | |
| 389 | |
| 390 /* Write the post */ | |
| 391 bytes_to_send = g_snprintf(buffer, sizeof(buffer), | |
| 392 "POST /%s HTTP/1.0\r\n", cmd); | |
| 393 ret = nm_tcp_write(conn, buffer, bytes_to_send); | |
| 394 if (ret < 0) { | |
| 395 rc = NMERR_TCP_WRITE; | |
| 396 } | |
| 397 | |
| 398 /* Write headers */ | |
| 399 if (rc == NM_OK) { | |
| 400 if (strcmp("login", cmd) == 0) { | |
| 401 bytes_to_send = g_snprintf(buffer, sizeof(buffer), | |
| 402 "Host: %s:%d\r\n\r\n", conn->addr, conn->port); | |
| 403 ret = nm_tcp_write(conn, buffer, bytes_to_send); | |
| 404 if (ret < 0) { | |
| 405 rc = NMERR_TCP_WRITE; | |
| 406 } | |
| 407 } else { | |
| 408 bytes_to_send = g_snprintf(buffer, sizeof(buffer), "\r\n"); | |
| 409 ret = nm_tcp_write(conn, buffer, bytes_to_send); | |
| 410 if (ret < 0) { | |
| 411 rc = NMERR_TCP_WRITE; | |
| 412 } | |
| 413 } | |
| 414 } | |
| 415 | |
| 416 /* Add the transaction id to the request fields */ | |
| 417 if (rc == NM_OK) { | |
| 418 request = nm_copy_field_array(fields); | |
| 419 if (request) { | |
| 420 char *str = g_strdup_printf("%d", ++(conn->trans_id)); | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
421 |
| 8933 | 422 request = nm_field_add_pointer(request, NM_A_SZ_TRANSACTION_ID, 0, |
| 423 NMFIELD_METHOD_VALID, 0, | |
| 424 str, NMFIELD_TYPE_UTF8); | |
| 8675 | 425 } |
| 426 } | |
| 427 | |
| 428 /* Send the request to the server */ | |
| 429 if (rc == NM_OK) { | |
| 430 rc = nm_write_fields(conn, request); | |
| 431 } | |
| 432 | |
| 433 /* Write the CRLF to terminate the data */ | |
| 434 if (rc == NM_OK) { | |
| 435 ret = nm_tcp_write(conn, "\r\n", strlen("\r\n")); | |
| 436 if (ret < 0) { | |
| 437 rc = NMERR_TCP_WRITE; | |
| 438 } | |
| 439 } | |
| 440 | |
| 441 /* Create a request struct and return it */ | |
| 442 if (rc == NM_OK) { | |
| 443 *req = nm_create_request(cmd, conn->trans_id, time(0)); | |
| 444 } | |
| 445 | |
| 446 if (request != NULL) { | |
| 447 nm_free_fields(&request); | |
| 448 } | |
| 449 | |
| 450 return rc; | |
| 451 } | |
| 452 | |
| 453 NMERR_T | |
| 454 nm_read_header(NMConn * conn) | |
| 455 { | |
| 456 NMERR_T rc = NM_OK; | |
| 457 char buffer[512]; | |
| 458 char *ptr = NULL; | |
| 459 int i; | |
| 460 char rtn_buf[8]; | |
| 461 int rtn_code = 0; | |
| 462 | |
| 463 if (conn == NULL) | |
| 464 return NMERR_BAD_PARM; | |
| 465 | |
| 466 *buffer = '\0'; | |
| 467 rc = read_line(conn, buffer, sizeof(buffer)); | |
| 468 if (rc == NM_OK) { | |
| 469 | |
| 470 /* Find the return code */ | |
| 471 ptr = strchr(buffer, ' '); | |
| 472 if (ptr != NULL) { | |
| 473 ptr++; | |
| 474 | |
| 475 i = 0; | |
| 476 while (isdigit(*ptr) && (i < 3)) { | |
| 477 rtn_buf[i] = *ptr; | |
| 478 i++; | |
| 479 ptr++; | |
| 480 } | |
| 481 rtn_buf[i] = '\0'; | |
|
8684
046dd8ef2920
[gaim-migrate @ 9437]
Christian Hammond <chipx86@chipx86.com>
parents:
8675
diff
changeset
|
482 |
| 8675 | 483 if (i > 0) |
| 484 rtn_code = atoi(rtn_buf); | |
| 485 } | |
| 486 } | |
| 487 | |
| 488 /* Finish reading header, in the future we might want to do more processing here */ | |
| 489 /* TODO: handle more general redirects in the future */ | |
| 490 while ((rc == NM_OK) && (strcmp(buffer, "\r\n") != 0)) { | |
| 491 rc = read_line(conn, buffer, sizeof(buffer)); | |
| 492 } | |
| 493 | |
| 8933 | 494 if (rc == NM_OK && rtn_code == 301) |
| 495 rc = NMERR_SERVER_REDIRECT; | |
| 8675 | 496 |
| 497 return rc; | |
| 498 } | |
| 499 | |
| 500 NMERR_T | |
| 501 nm_read_fields(NMConn * conn, int count, NMField ** fields) | |
| 502 { | |
| 503 NMERR_T rc = NM_OK; | |
| 504 guint8 type; | |
| 505 guint8 method; | |
| 506 guint32 val; | |
| 507 char tag[64]; | |
| 508 NMField *sub_fields = NULL; | |
| 509 char *str = NULL; | |
| 510 | |
| 511 if (conn == NULL || fields == NULL) | |
| 512 return NMERR_BAD_PARM; | |
| 513 | |
| 514 do { | |
| 8753 | 515 if (count > 0) { |
| 8675 | 516 count--; |
| 517 } | |
| 518 | |
| 519 /* Read the field type, method, and tag */ | |
| 8933 | 520 rc = nm_read_all(conn, (char *)&type, sizeof(type)); |
| 8675 | 521 if (rc != NM_OK || type == 0) |
| 522 break; | |
| 523 | |
| 8933 | 524 rc = nm_read_all(conn, (char *)&method, sizeof(method)); |
| 8675 | 525 if (rc != NM_OK) |
| 526 break; | |
| 527 | |
| 8874 | 528 rc = nm_read_uint32(conn, &val); |
| 8675 | 529 if (rc != NM_OK) |
| 530 break; | |
| 531 | |
| 532 if (val > sizeof(tag)) { | |
| 533 rc = NMERR_PROTOCOL; | |
| 534 break; | |
| 535 } | |
| 536 | |
| 537 rc = nm_read_all(conn, tag, val); | |
| 538 if (rc != NM_OK) | |
| 539 break; | |
| 540 | |
| 541 if (type == NMFIELD_TYPE_MV || type == NMFIELD_TYPE_ARRAY) { | |
| 542 | |
| 543 /* Read the subarray (first read the number of items in the array) */ | |
| 8874 | 544 rc = nm_read_uint32(conn, &val); |
| 8675 | 545 if (rc != NM_OK) |
| 546 break; | |
| 547 | |
| 548 if (val > 0) { | |
| 549 rc = nm_read_fields(conn, val, &sub_fields); | |
| 550 if (rc != NM_OK) | |
| 551 break; | |
| 552 } | |
| 553 | |
| 8933 | 554 *fields = nm_field_add_pointer(*fields, tag, 0, method, |
| 555 0, sub_fields, type); | |
| 8675 | 556 |
| 557 sub_fields = NULL; | |
| 558 | |
| 559 } else if (type == NMFIELD_TYPE_UTF8 || type == NMFIELD_TYPE_DN) { | |
| 560 | |
| 561 /* Read the string (first read the length) */ | |
| 8874 | 562 rc = nm_read_uint32(conn, &val); |
| 8675 | 563 if (rc != NM_OK) |
| 564 break; | |
| 565 | |
| 8753 | 566 if (val >= NMFIELD_MAX_STR_LENGTH) { |
| 567 rc = NMERR_PROTOCOL; | |
| 568 break; | |
| 569 } | |
| 570 | |
| 8675 | 571 if (val > 0) { |
| 572 str = g_new0(char, val + 1); | |
| 573 | |
| 574 rc = nm_read_all(conn, str, val); | |
| 575 if (rc != NM_OK) | |
| 576 break; | |
| 8753 | 577 |
| 8933 | 578 *fields = nm_field_add_pointer(*fields, tag, 0, method, |
| 579 0, str, type); | |
| 8753 | 580 str = NULL; |
| 8675 | 581 } |
| 582 | |
| 583 } else { | |
| 584 | |
| 585 /* Read the numerical value */ | |
| 8874 | 586 rc = nm_read_uint32(conn, &val); |
| 8675 | 587 if (rc != NM_OK) |
| 588 break; | |
| 589 | |
| 8933 | 590 *fields = nm_field_add_number(*fields, tag, 0, method, |
| 591 0, val, type); | |
| 8675 | 592 } |
| 593 | |
| 594 } while ((type != 0) && (count != 0)); | |
| 595 | |
| 596 | |
| 597 if (str != NULL) { | |
| 598 g_free(str); | |
| 599 } | |
| 600 | |
| 601 if (sub_fields != NULL) { | |
| 602 nm_free_fields(&sub_fields); | |
| 603 } | |
| 604 | |
| 605 return rc; | |
| 606 } | |
| 607 | |
| 608 void | |
| 609 nm_conn_add_request_item(NMConn * conn, NMRequest * request) | |
| 610 { | |
| 611 if (conn == NULL || request == NULL) | |
| 612 return; | |
| 613 | |
| 614 nm_request_add_ref(request); | |
| 615 conn->requests = g_slist_append(conn->requests, request); | |
| 616 } | |
| 617 | |
| 618 void | |
| 619 nm_conn_remove_request_item(NMConn * conn, NMRequest * request) | |
| 620 { | |
| 621 if (conn == NULL || request == NULL) | |
| 622 return; | |
| 623 | |
| 624 conn->requests = g_slist_remove(conn->requests, request); | |
| 625 nm_release_request(request); | |
| 626 } | |
| 627 | |
| 628 NMRequest * | |
| 629 nm_conn_find_request(NMConn * conn, int trans_id) | |
| 630 { | |
| 631 NMRequest *req = NULL; | |
| 632 GSList *itr = NULL; | |
| 633 | |
| 634 if (conn == NULL) | |
| 635 return NULL; | |
| 636 | |
| 637 itr = conn->requests; | |
| 638 while (itr) { | |
| 639 req = (NMRequest *) itr->data; | |
| 640 if (req != NULL && nm_request_get_trans_id(req) == trans_id) { | |
| 641 return req; | |
| 642 } | |
| 643 itr = g_slist_next(itr); | |
| 644 } | |
| 645 return NULL; | |
| 646 } | |
| 647 | |
| 648 const char * | |
| 649 nm_conn_get_addr(NMConn * conn) | |
| 650 { | |
| 651 if (conn == NULL) | |
| 652 return NULL; | |
| 653 else | |
| 654 return conn->addr; | |
| 655 } | |
| 656 | |
| 657 int | |
| 658 nm_conn_get_port(NMConn * conn) | |
| 659 { | |
| 660 if (conn == NULL) | |
| 661 return -1; | |
| 662 else | |
| 663 return conn->port; | |
| 664 } |
