summaryrefslogtreecommitdiff
path: root/contrib/idn/mdnkit/mdnsproxy/message.c
blob: deba583c25302ed4b27ebea49b64ceffdfdaaa8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
 * message.c - mDNS Proxy, message handling
 *
 *      message will passed with callback 'notify_message'.
 *      this module parse received message and forward request,
 *      or reply to originator
 */

/*
 * Copyright (c) 2000 Japan Network Information Center.  All rights reserved.
 *  
 * By using this file, you agree to the terms and conditions set forth bellow.
 * 
 * 			LICENSE TERMS AND CONDITIONS 
 * 
 * The following License Terms and Conditions apply, unless a different
 * license is obtained from Japan Network Information Center ("JPNIC"),
 * a Japanese association, Fuundo Bldg., 1-2 Kanda Ogawamachi, Chiyoda-ku,
 * Tokyo, Japan.
 * 
 * 1. Use, Modification and Redistribution (including distribution of any
 *    modified or derived work) in source and/or binary forms is permitted
 *    under this License Terms and Conditions.
 * 
 * 2. Redistribution of source code must retain the copyright notices as they
 *    appear in each source code file, this License Terms and Conditions.
 * 
 * 3. Redistribution in binary form must reproduce the Copyright Notice,
 *    this License Terms and Conditions, in the documentation and/or other
 *    materials provided with the distribution.  For the purposes of binary
 *    distribution the "Copyright Notice" refers to the following language:
 *    "Copyright (c) Japan Network Information Center.  All rights reserved."
 * 
 * 4. Neither the name of JPNIC may be used to endorse or promote products
 *    derived from this Software without specific prior written approval of
 *    JPNIC.
 * 
 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
 *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
 *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * 6. Indemnification by Licensee
 *    Any person or entities using and/or redistributing this Software under
 *    this License Terms and Conditions shall defend indemnify and hold
 *    harmless JPNIC from and against any and all judgements damages,
 *    expenses, settlement liabilities, cost and other liabilities of any
 *    kind as a result of use and redistribution of this Software or any
 *    claim, suite, action, litigation or proceeding by any third party
 *    arising out of or relates to this License Terms and Conditions.
 * 
 * 7. Governing Law, Jurisdiction and Venue
 *    This License Terms and Conditions shall be governed by and and
 *    construed in accordance with the law of Japan. Any person or entities
 *    using and/or redistributing this Software under this License Terms and
 *    Conditions hereby agrees and consent to the personal and exclusive
 *    jurisdiction and venue of Tokyo District Court of Japan.
 */

#ifndef lint
static char *rcsid = "$Id: message.c,v 1.1 2001/06/09 00:30:35 tale Exp $";
#endif

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#ifdef  WIN32
#include <windows.h>
#include <winsock.h>
#else   /* for normal systems */
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif

#include "mdnsproxy.h"

/*
 * address handling utilities
 *
 *      addrEq      check same addresses
 *      addrFmt     format address, port & protocol
 *
 *  these functions are same with those in 'server.c'.
 *  may be put in another 'utility' module.
 */

static  BOOL    addrEq(struct sockaddr *a1, struct sockaddr *a2)
{
    struct sockaddr_in  *ip1 = (struct sockaddr_in *) a1 ;
    struct sockaddr_in  *ip2 = (struct sockaddr_in *) a2 ;
    
    if (ip1->sin_addr.s_addr != ip2->sin_addr.s_addr) {
        return FALSE ;
    }
    if (ip1->sin_port != ip2->sin_port) {
        return FALSE ;
    }
    return TRUE ;
}

static  u_char  fmtbuff[64] ;

static  u_char  *addrFmt(struct sockaddr *addr, int proto)
{
    struct sockaddr_in  *iaddr = (struct sockaddr_in *) addr ;
    u_char  *ap ;
    u_char  *pp ;

    ap = (u_char *) &iaddr->sin_addr ;
    pp = (u_char *) &iaddr->sin_port ;
    
    sprintf(fmtbuff, "%s:%d.%d.%d.%d:%d",
        (proto == SOCK_STREAM ? "TCP" : "UDP"),
        (ap[0] & 0xff), (ap[1] & 0xff), (ap[2] & 0xff), (ap[3] & 0xff),
	((pp[0] & 0xff) * 256 + (pp[1] & 0xff)) ) ;

    return fmtbuff ;
}

/*
 * Managing Message ID
 */
 
static  u_short msgidLast = 0xffff ;
static  u_short msgidMap[4096] = { 0 } ;

#define ID_INDEX(x) (((x) & 0xfff0) >> 4)
#define ID_MASK(x)  (1 << ((x) & 0x000f))

#define ID_CHECK(x)  (msgidMap[ID_INDEX((x))] & ID_MASK((x)))
#define ID_USEIT(x)  (msgidMap[ID_INDEX((x))] |= ID_MASK((x)))
#define ID_CLEAR(x)  (msgidMap[ID_INDEX((x))] &= ~ID_MASK((x)))

static  BOOL    idAlloc(u_short *id)
{
    u_short newid ;
    
    for (newid = (msgidLast + 1) & 0xffff ;
	 newid != msgidLast ;
	 newid = (newid + 1) & 0xffff) {
        if (ID_CHECK(newid) == 0) {
	    ID_USEIT(newid) ;
	    msgidLast = newid ;
	    *id = newid ;
	    return TRUE ;
	}
    }
    WARN("idAlloc - no more ID\n") ;
    return FALSE ;
}

static  void    idFree(u_short id)
{
    if (ID_CHECK(id) == 0) {
        WARN("idAlloc - %04x is not in use\n", id) ;
	return ;
    }
    ID_CLEAR(id) ;
}

/*
 * Message Managements
 *
 *      Request from client is identified with its ID word.  It is unique
 *      on one client, but proxy accepts requests from multiple clients,
 *      proxy cannot distinguish request with ID only, and cannot forward
 *      request with such ID word.
 *
 *      So, proxy will identify requests with combination of address, port,
 *      (which identified client) and ID word.  Then forwarding request,
 *      proxy allocate unique ID, and change request's ID with new one.
 *
 *      Response from DNS server will identified with newly allocated ID.
 *      For responding such response to originator, replace response's
 *      ID with original one, and send response to marked address/port.
 */
 
typedef struct  _MSG    *MSGPTR ;

typedef struct  _MSG {
    MSGPTR                  prev  ;
    MSGPTR                  next  ;
    time_t                  last  ;
    struct sockaddr         from  ;
    int                     proto ;
    u_short                 orgId ;  
    u_short                 newId ;
    translation_context_t   trctx ;
} MSGREC ;

static  MSGREC  listMsg = { 0 } ;

/*
 * searchReq - search request in message list, search on original ID
 */

static  MSGPTR  searchReq(u_short id, int proto, struct sockaddr *addr)
{
    MSGPTR  p ;
    
    if (listMsg.prev == NULL || listMsg.next == NULL) {
        listMsg.prev = &listMsg ;
	listMsg.next = &listMsg ;
    }
    for (p = listMsg.next ; p != &listMsg ; p = p->next) {
        if (p->orgId != id || p->proto != proto) {
	    continue ;
	}
	if (addrEq(&p->from, addr) != TRUE) {
	    continue ;
	}
	p->last = time(NULL) ;
	return p ;
    }
    return NULL ;
}

/*
 * searchOrg - search original request matching to new ID
 */

static  MSGPTR  searchOrg(u_short id, int proto)
{
    MSGPTR  p ;
    
    if (listMsg.prev == NULL || listMsg.next == NULL) {
        listMsg.prev = &listMsg ;
	listMsg.next = &listMsg ;
    }
    for (p = listMsg.next ; p != &listMsg ; p = p->next) {
        if (p->newId != id || p->proto != proto) {
	    continue ;
	}
	p->last = time(NULL) ;
	return p ;
    }
    return NULL ;
}

/*
 * createReq - create new message record for new request
 *
 *      it also allocate new ID for this request, used for
 *      forwarding this request
 */

static  MSGPTR  createReq(u_short id, int proto, struct sockaddr *addr)
{
    u_short newid ;
    MSGPTR  pMsg, prev, next ;
    
    if (listMsg.prev == NULL || listMsg.next == NULL) {
        listMsg.prev = &listMsg ;
	listMsg.next = &listMsg ;
    }

    if (idAlloc(&newid) != TRUE) {
        WARN("createReq - no more ID\n") ;
	return NULL ;
    }
    if ((pMsg = (MSGPTR) malloc(sizeof(MSGREC))) == NULL) {
        WARN("createReq - cannot allocate message record\n") ;
	idFree(newid) ;
	return NULL ;
    }

    memset(pMsg, 0, sizeof(MSGREC)) ;
    
    memcpy(&pMsg->from, addr, sizeof(struct sockaddr)) ;
    pMsg->proto = proto ;
    pMsg->orgId = id    ;
    pMsg->newId = newid ;
    pMsg->last = time(NULL) ;

    pMsg->trctx.client   = &pMsg->from ;
    pMsg->trctx.protocol = pMsg->proto ;
    pMsg->trctx.old_id   = pMsg->orgId ;
    pMsg->trctx.new_id   = pMsg->newId ;
    
    prev = listMsg.prev ;
    next = prev->next   ;    
    
    prev->next = pMsg ;
    next->prev = pMsg ;
    pMsg->prev = prev ;
    pMsg->next = next ;
    
    return pMsg ;
}

/*
 * disposeReq - dispose message record
 */

static  void    disposeReq(MSGPTR pMsg)
{
    MSGPTR  p ;
    
    if (listMsg.prev == NULL || listMsg.next == NULL) {
        listMsg.prev = &listMsg ;
	listMsg.next = &listMsg ;
    }

    for (p = listMsg.next ; p != &listMsg ; p = p->next) {
        if (p == pMsg) {
	    break ;
	}
    }
    if (p == pMsg) {        /* safe to unlink it */
        pMsg->prev->next = pMsg->next ;
	pMsg->next->prev = pMsg->prev ;
    }
    idFree(pMsg->newId) ;
    free(pMsg) ;
}

/*
 * messageForward - forward the request
 */

static  void    errorOnRequest(MSGPTR pMsg, u_char *msg, int len, size_t err)
{
    u_short     errmsg[6] ;
    u_short     flags     ;
    u_short     *ps       ;
    
    TRACE("errorOnRequest %d\n", err) ;
    
    ps = (u_short *) msg ;
    flags = ntohs(ps[1]) ;
    flags = ((flags & 0x7fff) | 0x8000) ;           /* QR to response   */
    flags = ((flags & 0xfff8) | (err & 0x0007)) ;   /* set RCODE        */
    
    memset(errmsg, 0, sizeof(errmsg)) ;
    errmsg[0] = htons(pMsg->orgId) ;
    errmsg[1] = htons(flags)       ;
    
    server_response(&pMsg->from, pMsg->proto, (u_char *) errmsg, sizeof(errmsg)) ;
}

static  void    messageForward(MSGPTR pMsg, u_char *msg, int len)
{
    u_short *p ;
    u_char  buff[1024] ;
    u_char  *bbase ;
    size_t  bleng  ;
    size_t  cleng = 0 ;     /* avoid un-expected length on xlate error  */
    size_t  cstat = 0 ;     /* avoid un-expected status on xlate error  */
    
    TRACE("messageForward - %04x -> %04x\n", pMsg->orgId, pMsg->newId) ;
    
    /*
     * prepare conversion buffer
     */

    if (len < sizeof(buff) / 2) {
        bbase = buff ;
	bleng = sizeof(buff) ;
    } else {
        bbase = malloc(len * 2) ;
	bleng = len * 2 ;
    }
    if (bbase == NULL) {
        WARN("messageForward - cannot prepare conversion buffer\n") ;
	return ;
    }
    
    /*
     * translate message (domain names)
     */

    TRACE("messageForward - translate request\n") ;
    
    cstat = translate_request(&pMsg->trctx, msg, len, bbase, bleng, &cleng) ;
    
    TRACE("messageForward - translated status %d length %d\n", cstat, cleng) ;

    if (cstat != 0) {       /* error on conversion */
        WARN("messageForward - translation error %d\n", cstat) ;
        errorOnRequest(pMsg, msg, len, cstat) ;
	return ;
    }
    if (pMsg->proto == SOCK_DGRAM && cleng > 512) {
        WARN("messageForward - translation overflow %d\n", cleng) ;
	errorOnRequest(pMsg, msg, len, 2) ;
	return ;
    }
    
    /*
     * forward the request
     */
     
    p = (u_short *) bbase ;
    p[0] = htons(pMsg->newId) ;
    
    server_forward(NULL, pMsg->proto, bbase, cleng) ;
    
    /*
     * cleanup buffer
     */

    if (bbase != buff) {
        free(bbase) ;
    }
}

/*
 * messageResponse - response to originating client
 */

static  void    errorOnResponse(MSGPTR pMsg, u_char *msg, int len, size_t err)
{
    u_short     errmsg[6] ;
    u_short     flags     ;
    u_short     *ps       ;
    
    TRACE("errorOnResponse %d\n", err) ;
    
    ps = (u_short *) msg ;
    flags = ntohs(ps[1]) ;
    flags = ((flags & 0x7fff) | 0x8000) ;           /* QR to response   */
    flags = ((flags & 0xfff8) | (err & 0x0007)) ;   /* set RCODE        */
    
    memset(errmsg, 0, sizeof(errmsg)) ;
    errmsg[0] = htons(pMsg->orgId) ;
    errmsg[1] = htons(flags)       ;
    
    server_response(&pMsg->from, pMsg->proto, (u_char *) errmsg, sizeof(errmsg)) ;
}

static  void    messageResponse(MSGPTR pMsg, u_char *msg, int len)
{
    u_short *p ;
    u_char  buff[1024] ;
    u_char  *bbase ;
    size_t  bleng  ;
    size_t  cleng  ;
    size_t  cstat  ;
    
    TRACE("messageResponse - %04x <- %04x\n", pMsg->orgId, pMsg->newId) ;

    /*
     * prepare conversion buffer
     */

    if (len < sizeof(buff) / 2) {
        bbase = buff ;
	bleng = sizeof(buff) ;
    } else {
        bbase = malloc(len * 2) ;
	bleng = len * 2 ;
    }
    if (bbase == NULL) {
        WARN("messageResponse - cannot prepare conversion buffer\n") ;
	return ;
    }
    
    /*
     * translate message (domain names)
     */

    TRACE("messageResponse - translate response\n") ;

    cstat = translate_reply(&pMsg->trctx, msg, len, bbase, bleng, &cleng) ;
    
    TRACE("messageResponse - translated status %d length %d\n", cstat, cleng) ;

    if (cstat != 0) {       /* error on conversion */
        WARN("messageResponse - translation error %d\n", cstat) ;
        errorOnResponse(pMsg, msg, len, cstat) ;
	return ;
    }
    if (pMsg->proto == SOCK_DGRAM && cleng > 512) {
        WARN("messageResponse - translation overflow %d\n", cleng) ;
        errorOnResponse(pMsg, msg, len, 2) ;
	return ;
    }
    
    /*
     * reply back to requester
     */
     
    p = (u_short *) bbase ;
    p[0] = htons(pMsg->orgId) ;
    
    server_response(&pMsg->from, pMsg->proto, bbase, cleng) ;

    /*
     * cleanup buffer
     */

    if (bbase != buff) {
        free(bbase) ;
    }
}

/*
 * notify_message - callback from server loop
 */

void    notify_message(struct sockaddr *from, int proto, u_char *msg, int len)
{
    u_short     *p = (u_short *) msg ;
    u_short     msgid, flags ;
    MSGPTR      pMsg ;
#ifdef  DEBUG
    char    logbuf[256] ;
#endif
    
    msgid = ntohs(p[0]) ;
    flags = ntohs(p[1]) ;
    
#ifdef DEBUG
    if ((flags & 0x8000) == 0) {
        sprintf(logbuf, "Request  %04x (%04x) from %s, %d bytes", 
                            msgid, flags, addrFmt(from, proto), len) ;
    } else {
        sprintf(logbuf, "Response %04x (%04x) from %s %d bytes", 
	                    msgid, flags, addrFmt(from, proto), len) ;
    }
    TRACE("%s\n", logbuf) ;

    strcpy(logbuf, "    ") ;

    switch ((flags & 0x7800) >> 11) {
        case 0  : strcat(logbuf, "QUERY  ") ; break ;
	case 1  : strcat(logbuf, "IQUERY ") ; break ;
	case 2  : strcat(logbuf, "STATUS ") ; break ;
	default : strcat(logbuf, "UNKNOWN") ; break ;
    }
    if ((flags & 0x0400) != 0) {
        strcat(logbuf, ",AA") ;
    }
    if ((flags & 0x0200) != 0) {
        strcat(logbuf, ",TC") ;
    }
    if ((flags & 0x0100) != 0) {
        strcat(logbuf, ",RD") ;
    }
    if ((flags & 0x0080) != 0) {
        strcat(logbuf, ",RA") ;
    }
    switch (flags & 0x00f) {
        case 0  : strcat(logbuf, ",No Error       ") ; break ;
	case 1  : strcat(logbuf, ",Format Error   ") ; break ;
	case 2  : strcat(logbuf, ",Server Failure ") ; break ;
	case 3  : strcat(logbuf, ",Name Error     ") ; break ;
	case 4  : strcat(logbuf, ",Not Implemented") ; break ;
	case 5  : strcat(logbuf, ",Refused        ") ; break ;
	default : strcat(logbuf, ",Unknown Error  ") ; break ;
    }
    TRACE("%s\n", logbuf) ;
#endif

    if ((flags & 0x8000) == 0) {        /* request from client  */

        if ((pMsg = searchReq(msgid, proto, from)) == NULL) {
	    pMsg = createReq(msgid, proto, from) ;
	}
	if (pMsg == NULL) {
	    WARN("notify_message - cannot create message record\n") ;
	    return ;
	}
	messageForward(pMsg, msg, len) ;
	
    } else {                            /* response from server */

        if ((pMsg = searchOrg(msgid, proto)) == NULL) {
	    WARN("notify_message - no corresponding request\n") ;
	    return ;
	}
	messageResponse(pMsg, msg, len) ;
	disposeReq(pMsg);
    }
}

/*
 * notify_timer - timer callback
 */

static  time_t      timeLastCheck = 0 ;
static  time_t      timeInterval = 60 ;
static  time_t      timeTimeout = (60 * 10) ;

void    notify_timer(void)
{
    time_t  t = time(NULL) ;
    MSGPTR  p, np ;
#ifdef DEBUG
    int ndiscarded = 0;
#endif
    
    if (listMsg.prev == NULL || listMsg.next == NULL) {
        listMsg.prev = &listMsg ;
	listMsg.next = &listMsg ;
    }

    if ((t - timeLastCheck) < timeInterval) {
        return ;
    }
    for (p = listMsg.next ; p != &listMsg ; p = np) {
        np = p->next ;
	if ((t - p->last) > timeTimeout) {
            disposeReq(p) ;
#ifdef DEBUG
	    ndiscarded++;
#endif
	}
    }
#ifdef DEBUG
    TRACE("notify_timer: %d discarded\n", ndiscarded);
#endif
    timeLastCheck = t;
}