~ubuntu-branches/ubuntu/trusty/kopete/trusty

« back to all changes in this revision

Viewing changes to protocols/jabber/libiris/src/jdns/jdns_util.c

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2013-11-23 17:46:40 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20131123174640-gz1zjv1xqh81bi2h
Tags: 4:4.11.80-0ubuntu1
* New upstream beta release
* Bump build dependency to libotr5-dev
* Update symbols
* Update install files
* Drop new_linphone.diff, not required anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
#include "jdns_packet.h"
27
27
 
28
 
/* ---------------------------------------------------------------------------- */
29
 
/*  misc */
30
 
/* ---------------------------------------------------------------------------- */
 
28
//----------------------------------------------------------------------------
 
29
// misc
 
30
//----------------------------------------------------------------------------
31
31
void *jdns_alloc(int size)
32
32
{
33
33
        return malloc(size);
48
48
        char *p;
49
49
        int len;
50
50
 
51
 
        len = strlen(s) + 1; /*  the zero */
 
51
        len = strlen(s) + 1; // the zero
52
52
        p = (char *)jdns_alloc(len);
53
53
        memcpy(p, s, len);
54
54
        return p;
69
69
        int n;
70
70
        int len_a;
71
71
 
72
 
        /*  case-insensitive compare */
 
72
        // case-insensitive compare
73
73
        len_a = _ustrlen(a);
74
74
        if(len_a != (int)_ustrlen(b))
75
75
                return 0;
135
135
        }
136
136
        out = jdns_string_new();
137
137
        out->size = sizei - 1;
138
 
        out->data = dest; /*  must be zero-terminated, which it is */
 
138
        out->data = dest; // must be zero-terminated, which it is
139
139
        return out;
140
140
#else
141
141
        jdns_string_t *out;
153
153
{
154
154
#if defined(_MSC_VER) && _MSC_VER >= 1400
155
155
        int len;
156
 
        /*  deliberately unsafe */
 
156
        // deliberately unsafe
157
157
        len = strlen(src);
158
158
        if(strcpy_s(dst, len + 1, src) != 0)
159
159
                return 0;
163
163
#endif
164
164
}
165
165
 
166
 
/* ---------------------------------------------------------------------------- */
167
 
/*  jdns_object */
168
 
/* ---------------------------------------------------------------------------- */
 
166
//----------------------------------------------------------------------------
 
167
// jdns_object
 
168
//----------------------------------------------------------------------------
169
169
void *jdns_object_new(int size, void (*dtor)(void *), void *(*cctor)(const void *))
170
170
{
171
171
        jdns_object_t *a = (jdns_object_t *)jdns_alloc(size);
190
190
        jdns_free(a);
191
191
}
192
192
 
193
 
/* ---------------------------------------------------------------------------- */
194
 
/*  jdns_list */
195
 
/* ---------------------------------------------------------------------------- */
 
193
//----------------------------------------------------------------------------
 
194
// jdns_list
 
195
//----------------------------------------------------------------------------
196
196
jdns_list_t *jdns_list_new()
197
197
{
198
198
        jdns_list_t *a = JDNS_OBJECT_NEW(jdns_list);
207
207
{
208
208
        jdns_list_t *c = jdns_list_new();
209
209
 
210
 
        /*  note: copying a list with autoDelete should not ever be done. */
211
 
        /*    heck, let's not even allow it.  return an empty list. */
 
210
        // note: copying a list with autoDelete should not ever be done.
 
211
        //   heck, let's not even allow it.  return an empty list.
212
212
        if(a->autoDelete)
213
213
                return c;
214
214
 
215
215
        c->valueList = a->valueList;
216
216
 
217
 
        /*  copy the items */
 
217
        // copy the items
218
218
        if(a->item)
219
219
        {
220
220
                int n;
222
222
                c->item = (void **)jdns_alloc(sizeof(void *) * c->count);
223
223
                if(a->valueList)
224
224
                {
225
 
                        /*  deep copy */
 
225
                        // deep copy
226
226
                        for(n = 0; n < c->count; ++n)
227
227
                                c->item[n] = jdns_object_copy(a->item[n]);
228
228
                }
229
229
                else
230
230
                {
231
 
                        /*  just the pointer */
 
231
                        // just the pointer
232
232
                        for(n = 0; n < c->count; ++n)
233
233
                                c->item[n] = a->item[n];
234
234
                }
248
248
{
249
249
        if(a->item)
250
250
        {
251
 
                /*  delete the items if necessary */
 
251
                // delete the items if necessary
252
252
                if(a->valueList || a->autoDelete)
253
253
                {
254
254
                        int n;
263
263
 
264
264
void jdns_list_insert(jdns_list_t *a, void *item, int pos)
265
265
{
266
 
        /*  make memory */
 
266
        // make memory
267
267
        if(!a->item)
268
268
                a->item = (void **)jdns_alloc(sizeof(void *));
269
269
        else
270
270
                a->item = (void **)jdns_realloc(a->item, sizeof(void *) * (a->count + 1));
271
271
 
272
 
        /*  prepare position */
 
272
        // prepare position
273
273
        if(pos != -1)
274
274
                memmove(a->item + pos + 1, a->item + pos, (a->count - pos) * sizeof(void *));
275
275
        else
276
276
                pos = a->count;
277
277
 
278
 
        /*  insert it */
 
278
        // insert it
279
279
        if(a->valueList)
280
280
                a->item[pos] = jdns_object_copy(item);
281
281
        else
311
311
        if(pos < 0 || pos >= a->count)
312
312
                return;
313
313
 
314
 
        /*  delete the item if necessary */
 
314
        // delete the item if necessary
315
315
        if(a->valueList || a->autoDelete)
316
316
                jdns_object_delete(a->item[pos]);
317
317
 
318
 
        /*  free the position */
 
318
        // free the position
319
319
        if(a->count > 1)
320
320
        {
321
321
                memmove(a->item + pos, a->item + pos + 1, (a->count - pos - 1) * sizeof(void *));
329
329
        }
330
330
}
331
331
 
332
 
/* ---------------------------------------------------------------------------- */
333
 
/*  jdns_string */
334
 
/* ---------------------------------------------------------------------------- */
 
332
//----------------------------------------------------------------------------
 
333
// jdns_string
 
334
//----------------------------------------------------------------------------
335
335
jdns_string_t *jdns_string_new()
336
336
{
337
337
        jdns_string_t *s = JDNS_OBJECT_NEW(jdns_string);
397
397
                if(n == -1)
398
398
                        n = s->size;
399
399
                len = n - at;
400
 
                /*  FIXME: should we allow empty items? */
401
 
                /* if(len == 0) */
402
 
                /*      break; */
 
400
                // FIXME: should we allow empty items?
 
401
                //if(len == 0)
 
402
                //      break;
403
403
                str = jdns_string_new();
404
404
                jdns_string_set(str, s->data + at, len);
405
405
                jdns_stringlist_append(out, str);
406
406
                jdns_string_delete(str);
407
 
                at = n + 1; /*  skip over separator */
 
407
                at = n + 1; // skip over separator
408
408
        }
409
409
        return out;
410
410
}
411
411
 
412
 
/* ---------------------------------------------------------------------------- */
413
 
/*  jdns_stringlist */
414
 
/* ---------------------------------------------------------------------------- */
 
412
//----------------------------------------------------------------------------
 
413
// jdns_stringlist
 
414
//----------------------------------------------------------------------------
415
415
jdns_stringlist_t *jdns_stringlist_new()
416
416
{
417
417
        jdns_list_t *a = jdns_list_new();
427
427
void jdns_stringlist_delete(jdns_stringlist_t *a)
428
428
{
429
429
        jdns_list_delete((jdns_list_t *)a);
430
 
        /*  note: no need to call jdns_object_free() here */
 
430
        // note: no need to call jdns_object_free() here
431
431
}
432
432
 
433
433
void jdns_stringlist_append(jdns_stringlist_t *a, const jdns_string_t *str)
435
435
        jdns_list_insert_value((jdns_list_t *)a, str, -1);
436
436
}
437
437
 
438
 
/* ---------------------------------------------------------------------------- */
439
 
/*  jdns_address */
440
 
/* ---------------------------------------------------------------------------- */
 
438
//----------------------------------------------------------------------------
 
439
// jdns_address
 
440
//----------------------------------------------------------------------------
441
441
jdns_address_t *jdns_address_new()
442
442
{
443
443
        jdns_address_t *a = alloc_type(jdns_address_t);
474
474
        jdns_free(a->c_str);
475
475
        a->isIpv6 = 0;
476
476
        a->addr.v4 = ipv4;
477
 
        a->c_str = (char *)jdns_alloc(16); /*  max size (3 * 4 + 3 + 1) */
 
477
        a->c_str = (char *)jdns_alloc(16); // max size (3 * 4 + 3 + 1)
478
478
        jdns_sprintf_s(a->c_str, 16, "%d.%d.%d.%d",
479
479
                (unsigned char)((ipv4 >> 24) & 0xff),
480
480
                (unsigned char)((ipv4 >> 16) & 0xff),
494
494
        a->addr.v6 = (unsigned char *)jdns_alloc(16);
495
495
        memcpy(a->addr.v6, ipv6, 16);
496
496
        p = (unsigned char *)a->addr.v6;
497
 
        a->c_str = (char *)jdns_alloc(40); /*  max size (8 * 4 + 7 + 1) */
498
 
        /*  each word in a 16-byte ipv6 address is network byte order */
 
497
        a->c_str = (char *)jdns_alloc(40); // max size (8 * 4 + 7 + 1)
 
498
        // each word in a 16-byte ipv6 address is network byte order
499
499
        for(n = 0; n < 8; ++n)
500
500
                word[n] = ((unsigned short)(p[n * 2]) << 8) + (unsigned short)(p[n * 2 + 1]);
501
501
        jdns_sprintf_s(a->c_str, 40, "%04X:%04X:%04X:%04X:%04X:%04X:%04X:%04X", word[0], word[1], word[2], word[3], word[4], word[5], word[6], word[7]);
505
505
{
506
506
        int slen = strlen(str);
507
507
 
508
 
        /*  ipv6 */
 
508
        // ipv6
509
509
        if(strchr(str, ':'))
510
510
        {
511
511
                jdns_string_t *in;
518
518
                list = jdns_string_split(in, ':');
519
519
                jdns_string_delete(in);
520
520
 
521
 
                /*  a confusing outputting-backwards parser adapted from qt */
 
521
                // a confusing outputting-backwards parser adapted from qt
522
522
 
523
523
                count = list->count;
524
524
 
622
622
                                p2 = str + slen;
623
623
                        len = p2 - p;
624
624
 
625
 
                        /*  convert the section into a byte */
 
625
                        // convert the section into a byte
626
626
                        part = (char *)jdns_alloc(len + 1);
627
627
                        memcpy(part, p, len);
628
628
                        part[len] = 0;
632
632
                                break;
633
633
                        b[at++] = (unsigned char)x;
634
634
 
635
 
                        /*  done? */
 
635
                        // done?
636
636
                        if(p2 >= str + slen)
637
637
                                break;
638
638
 
639
 
                        /*  skip over the separator */
 
639
                        // skip over the separator
640
640
                        p = p2 + 1;
641
641
                }
642
642
                if(at != 4)
660
660
 
661
661
int jdns_address_cmp(const jdns_address_t *a, const jdns_address_t *b)
662
662
{
663
 
        /*  same protocol? */
 
663
        // same protocol?
664
664
        if(a->isIpv6 != b->isIpv6)
665
665
                return 0;
666
666
        if(a->isIpv6)
682
682
        return 0;
683
683
}
684
684
 
685
 
/*  FF02::FB */
 
685
// FF02::FB
686
686
unsigned char jdns_multicast_addr6_value_v6[] =
687
687
{
688
688
        0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
703
703
        return a;
704
704
}
705
705
 
706
 
/* ---------------------------------------------------------------------------- */
707
 
/*  jdns_server */
708
 
/* ---------------------------------------------------------------------------- */
 
706
//----------------------------------------------------------------------------
 
707
// jdns_server
 
708
//----------------------------------------------------------------------------
709
709
jdns_server_t *jdns_server_new()
710
710
{
711
711
        jdns_server_t *s = alloc_type(jdns_server_t);
743
743
        s->name = _ustrdup(name);
744
744
}
745
745
 
746
 
/* ---------------------------------------------------------------------------- */
747
 
/*  jdns_nameserver */
748
 
/* ---------------------------------------------------------------------------- */
 
746
//----------------------------------------------------------------------------
 
747
// jdns_nameserver
 
748
//----------------------------------------------------------------------------
749
749
jdns_nameserver_t *jdns_nameserver_new()
750
750
{
751
751
        jdns_nameserver_t *a = alloc_type(jdns_nameserver_t);
779
779
        a->port = port;
780
780
}
781
781
 
782
 
/* ---------------------------------------------------------------------------- */
783
 
/*  jdns_nameserverlist */
784
 
/* ---------------------------------------------------------------------------- */
 
782
//----------------------------------------------------------------------------
 
783
// jdns_nameserverlist
 
784
//----------------------------------------------------------------------------
785
785
jdns_nameserverlist_t *jdns_nameserverlist_new()
786
786
{
787
787
        jdns_nameserverlist_t *a = alloc_type(jdns_nameserverlist_t);
829
829
        ++a->count;
830
830
}
831
831
 
832
 
/* ---------------------------------------------------------------------------- */
833
 
/*  jdns_dnshost */
834
 
/* ---------------------------------------------------------------------------- */
 
832
//----------------------------------------------------------------------------
 
833
// jdns_dnshost
 
834
//----------------------------------------------------------------------------
835
835
jdns_dnshost_t *jdns_dnshost_new()
836
836
{
837
837
        jdns_dnshost_t *a = alloc_type(jdns_dnshost_t);
859
859
        jdns_free(a);
860
860
}
861
861
 
862
 
/* ---------------------------------------------------------------------------- */
863
 
/*  jdns_dnshostlist */
864
 
/* ---------------------------------------------------------------------------- */
 
862
//----------------------------------------------------------------------------
 
863
// jdns_dnshostlist
 
864
//----------------------------------------------------------------------------
865
865
jdns_dnshostlist_t *jdns_dnshostlist_new()
866
866
{
867
867
        jdns_dnshostlist_t *a = alloc_type(jdns_dnshostlist_t);
908
908
        ++a->count;
909
909
}
910
910
 
911
 
/* ---------------------------------------------------------------------------- */
912
 
/*  jdns_dnsparams */
913
 
/* ---------------------------------------------------------------------------- */
 
911
//----------------------------------------------------------------------------
 
912
// jdns_dnsparams
 
913
//----------------------------------------------------------------------------
914
914
jdns_dnsparams_t *jdns_dnsparams_new()
915
915
{
916
916
        jdns_dnsparams_t *a = alloc_type(jdns_dnsparams_t);
958
958
        jdns_dnshost_delete(h);
959
959
}
960
960
 
961
 
/* ---------------------------------------------------------------------------- */
962
 
/*  jdns_rr */
963
 
/* ---------------------------------------------------------------------------- */
 
961
//----------------------------------------------------------------------------
 
962
// jdns_rr
 
963
//----------------------------------------------------------------------------
964
964
void _jdns_rr_data_reset(jdns_rr_t *r)
965
965
{
966
966
        if(r->rdata)
1184
1184
                case JDNS_RTYPE_MX:
1185
1185
                case JDNS_RTYPE_SRV:
1186
1186
                {
1187
 
                        /*  consider it valid if we don't have a known to check */
 
1187
                        // consider it valid if we don't have a known to check
1188
1188
                        if(!r->haveKnown)
1189
1189
                                return 1;
1190
1190
                        if(!jdns_packet_name_isvalid(r->data.server->name, _ustrlen(r->data.server->name)))
1260
1260
        return out;
1261
1261
}
1262
1262
 
1263
 
/*  if the type is known, then it must be parsed properly */
1264
 
/*  if the type is unknown, then that's ok */
1265
 
/*  rdata is always copied, known or not */
 
1263
// if the type is known, then it must be parsed properly
 
1264
// if the type is unknown, then that's ok
 
1265
// rdata is always copied, known or not
1266
1266
jdns_rr_t *jdns_rr_from_resource(const jdns_packet_resource_t *pr, const jdns_packet_t *ref)
1267
1267
{
1268
1268
        jdns_rr_t *rr = 0;
1435
1435
        {
1436
1436
                rr->qclass = pr->qclass;
1437
1437
                rr->owner = _ustrdup(pr->qname->data);
1438
 
                rr->ttl = (int)pr->ttl; /*  pr->ttl is 31-bits, cast is safe */
 
1438
                rr->ttl = (int)pr->ttl; // pr->ttl is 31-bits, cast is safe
1439
1439
                rr->rdlength = pr->rdlength;
1440
1440
                rr->rdata = jdns_copy_array(pr->rdata, pr->rdlength);
1441
1441
        }
1443
1443
        return rr;
1444
1444
}
1445
1445
 
1446
 
/* ---------------------------------------------------------------------------- */
1447
 
/*  jdns_response */
1448
 
/* ---------------------------------------------------------------------------- */
 
1446
//----------------------------------------------------------------------------
 
1447
// jdns_response
 
1448
//----------------------------------------------------------------------------
1449
1449
#define ARRAY_DELETE(array, count, dtor) \
1450
1450
        { \
1451
1451
                if(count > 0) \
1538
1538
        jdns_rr_t *rr = r->answerRecords[pos];
1539
1539
        jdns_rr_delete(rr);
1540
1540
 
1541
 
        /*  free the position */
 
1541
        // free the position
1542
1542
        if(r->answerCount > 1)
1543
1543
        {
1544
1544
                memmove(r->answerRecords + pos, r->answerRecords + pos + 1, (r->answerCount - pos - 1) * sizeof(void *));