Revision 11fce711 util/stun.c

View differences:

util/stun.c
54 54
//#include <resolv.h>
55 55
#include <net/if.h>
56 56

  
57
#include "ml_log.h"
57 58
#include "stun.h"
58 59
#include "udpSocket.h"
59 60

  
......
179 180
  if ( verbose )
180 181
  {
181 182

  
182
      printf( "\t mappedAddr= ipv4 %s port %d \n \t changedAddr= ipv4 %s port %d \n",
183
      info( "\t mappedAddr= ipv4 %s port %d \n \t changedAddr= ipv4 %s port %d \n",
183 184
              mapped_addr ,
184 185
              response->mappedAddress.ipv4.port,
185 186
              changed_addr,
......
200 201
{
201 202
   if ( hdrLen != 8 )
202 203
   {
203
      printf("hdrlen is not 8 \n ");
204
      error("hdrlen is not 8 \n ");
204 205
      return false;
205 206
   }
206 207
   result->pad = *body++;
......
212 213
      UInt16 nport;
213 214
      memcpy(&nport, body, 2); body+=2;
214 215
      
215
      if (verbose) printf("port %i \n",ntohs(nport));
216
      if (verbose) info("port %i \n",ntohs(nport));
216 217

  
217 218
      result->ipv4.port = ntohs(nport);
218 219

  
......
228 229

  
229 230
      sprintf(str_p,"%d.%d.%d.%d",(ip_addr >> 24) & 0xff,(ip_addr >> 16) & 0xff,(ip_addr >> 8) & 0xff,ip_addr & 0xff);          
230 231

  
231
      printf(" ipv4 as str %s \n  ",str_p);
232
      info(" ipv4 as str %s \n  ",str_p);
232 233
	}
233 234

  
234 235
      result->ipv4.addr = ntohl(naddr);
......
239 240
   else if (result->family == IPv6Family)
240 241

  
241 242
   {
242
      printf("ipv6 not supported  \n");
243
      warn("ipv6 not supported  \n");
243 244
   }
244 245
   else
245 246
   {
246
      printf("bad address family: %i \n ",result->family );
247
      error("bad address family: %i \n ",result->family );
247 248
   }
248 249
	
249 250
   return false;
......
254 255
bool
255 256
stunParseMessage( char* buf, unsigned int bufLen, StunMessage *msg, bool verbose)
256 257
{
257
  if (verbose) printf("Received stun message: %d bytes \n",bufLen);
258
  if (verbose) info("Received stun message: %d bytes \n",bufLen);
258 259
   memset(msg, 0, sizeof(msg));
259 260
	
260 261
   if (sizeof(StunMsgHdr) > bufLen)
261 262
   {
262
     printf("Bad message \n");
263
     error("Bad message \n");
263 264
      return false;
264 265
   }
265 266
	
......
269 270
	
270 271
   if (msg->msgHdr.msgLength + sizeof(StunMsgHdr) != bufLen)
271 272
   {
272
     printf("Message header length doesn't match message size: %d - %d \n"
273
     error("Message header length doesn't match message size: %d - %d \n"
273 274
	    ,msg->msgHdr.msgLength,bufLen);
274 275
      return false;
275 276
   }
......
291 292
      //if (verbose) clog << "Found attribute type=" << AttrNames[atrType] << " length=" << attrLen << endl;
292 293
      if ( attrLen+4 > size ) 
293 294
      {
294
	printf( "claims attribute is larger than size of message " \
295
	debug( "claims attribute is larger than size of message " \
295 296
		"(attribute type= %d \n" ,atrType);
296 297
         return false;
297 298
      }
......
305 306
	//printf("has mapped address \n");
306 307
        if ( stunParseAtrAddress(body, attrLen, &msg->mappedAddress, verbose) == false)
307 308
            {
308
	      printf("problem parsing MappedAddress \n");
309
        	debug("problem parsing MappedAddress \n");
309 310
               return false;
310 311
            }
311 312
            else
312 313
            {
313
	      if (verbose) {printf("MappedAddress -------------------\n");}
314
	      if (verbose) {info("MappedAddress -------------------\n");}
314 315
            }
315 316
					
316 317
      }else 
......
319 320
            msg->hasResponseAddress = true;
320 321
            if ( stunParseAtrAddress(  body,  attrLen,  &msg->responseAddress, verbose )== false )
321 322
            {
322
	      printf("problem parsing ResponseAddress \n");
323
            	debug("problem parsing ResponseAddress \n");
323 324
              return false;
324 325
            }
325 326
            else
326 327
            {
327
	      if (verbose) {printf("ResponseAddress ---------------------\n");}
328
	      if (verbose) {info("ResponseAddress ---------------------\n");}
328 329
            }
329 330
             
330 331
	}else if(atrType == ChangeRequest)
......
332 333
            msg->hasChangeRequest = true;
333 334
            if (stunParseAtrChangeRequest( body, attrLen, &msg->changeRequest) == false)
334 335
            {
335
	      printf("problem parsing ChangeRequest \n");
336
            	debug("problem parsing ChangeRequest \n");
336 337
               return false;
337 338
            }
338 339
            else
339 340
            {
340
	      if (verbose) printf("ChangeRequest = %i \n",(int)&msg->changeRequest.value);
341
	      if (verbose) info("ChangeRequest = %i \n",(int)&msg->changeRequest.value);
341 342
            }
342 343
            
343 344
	} else if (atrType == SourceAddress)
......
345 346
            msg->hasSourceAddress = true;
346 347
            if ( stunParseAtrAddress(  body,  attrLen,  &msg->sourceAddress, verbose )== false )
347 348
            {
348
	      printf("problem parsing SourceAddress \n");
349
            	debug("problem parsing SourceAddress \n");
349 350
               return false;
350 351
            }
351 352
            else
352 353
            {
353
	      if (verbose) printf("SourceAddress ------------------\n");
354
	      if (verbose) info("SourceAddress ------------------\n");
354 355
            }
355 356
	} else if (atrType ==  ChangedAddress)
356 357
	{
357 358
            msg->hasChangedAddress = true;
358 359
            if ( stunParseAtrAddress(  body,  attrLen, &msg->changedAddress, verbose )== false )
359 360
            {
360
	      printf("problem parsing ChangedAddress \n");
361
            	debug("problem parsing ChangedAddress \n");
361 362
               return false;
362 363
            }
363 364
            else
364 365
            {
365
	      if (verbose) printf("ChangedAddress = ---------------------\n");
366
	      if (verbose) info("ChangedAddress = ---------------------\n");
366 367
            }
367 368
	} else if (atrType ==  Username) 
368 369
	{
369 370
            msg->hasUsername = true;
370 371
            if (stunParseAtrString( body, attrLen, &msg->username) == false)
371 372
            {
372
	      printf("problem parsing Username \n");
373
            	debug("problem parsing Username \n");
373 374
               return false;
374 375
            }
375 376
            else
376 377
            {
377
	      if (verbose) printf("Username = %s \n",msg->username.value);
378
	      if (verbose) info("Username = %s \n",msg->username.value);
378 379
            }
379 380
	}else if (atrType ==  Password) 
380 381
	 {
381 382
            msg->hasPassword = true;
382 383
            if (stunParseAtrString( body, attrLen, &msg->password) == false)
383 384
            {
384
	      printf("problem parsing Password \n");
385
	      debug("problem parsing Password \n");
385 386
               return false;
386 387
            }
387 388
            else
388 389
            {
389
	      if (verbose) printf("Password = %s \n",(char *)&msg->password.value);
390
	      if (verbose) info("Password = %s \n",(char *)&msg->password.value);
390 391
            }
391 392
	 }else if (atrType == MessageIntegrity)
392 393
	{
393 394
            msg->hasMessageIntegrity = true;
394 395
            if (stunParseAtrIntegrity( body, attrLen,&msg->messageIntegrity) == false)
395 396
            {
396
	      printf("problem parsing MessageIntegrity \n");
397
	      debug("problem parsing MessageIntegrity \n");
397 398
               return false;
398 399
            }
399 400
            else
......
411 412
            msg->hasErrorCode = true;
412 413
            if (stunParseAtrError(body, attrLen, &msg->errorCode) == false)
413 414
            {
414
	      printf("problem parsing ErrorCode \n");
415
	      debug("problem parsing ErrorCode \n");
415 416
               return false;
416 417
            }
417 418
            else
418 419
            {
419
	      if (verbose) printf("ErrorCode = %d %d %s \n",(int)(msg->errorCode.errorClass)                                 ,(int)(msg->errorCode.number) 
420
	      if (verbose) info("ErrorCode = %d %d %s \n",(int)(msg->errorCode.errorClass)                                 ,(int)(msg->errorCode.number)
420 421
				 ,msg->errorCode.reason);
421 422
            }
422 423
					
......
425 426
            msg->hasUnknownAttributes = true;
426 427
            if (stunParseAtrUnknown(body, attrLen,&msg->unknownAttributes) == false)
427 428
            {
428
	      printf("problem parsing UnknownAttribute \n");
429
	      debug("problem parsing UnknownAttribute \n");
429 430
               return false;
430 431
            }
431 432
	} else if (atrType == ReflectedFrom)
......
433 434
            msg->hasReflectedFrom = true;
434 435
            if ( stunParseAtrAddress(  body,  attrLen,  &msg->reflectedFrom, verbose ) == false )
435 436
            {
436
	      printf("problem parsing ReflectedFrom \n");
437
	      debug("problem parsing ReflectedFrom \n");
437 438
               return false;
438 439
            }
439 440
	} else if (atrType == XorMappedAddress)
......
441 442
            msg->hasXorMappedAddress = true;
442 443
            if ( stunParseAtrAddress(  body,  attrLen,  &msg->xorMappedAddress, verbose ) == false )
443 444
            {
444
	      printf("problem parsing XorMappedAddress \n");
445
	      debug("problem parsing XorMappedAddress \n");
445 446
               return false;
446 447
            }
447 448
            else
448 449
            {
449
	      if (verbose) printf("XorMappedAddress ------------------- \n");
450
	      if (verbose) info("XorMappedAddress ------------------- \n");
450 451
            }
451 452
	} else if (atrType == XorOnly)
452 453
	{
453 454
            msg->xorOnly = true;
454 455
            if (verbose) 
455 456
            {
456
	      printf("xorOnly = true \n");
457
	      debug("xorOnly = true \n");
457 458
            }
458 459
	} else if (atrType == ServerName) 
459 460
	{			
460 461
            msg->hasServerName = true;
461 462
            if (stunParseAtrString( body, attrLen, &msg->serverName) == false)
462 463
            {
463
	      printf("problem parsing ServerName \n");
464
	      debug("problem parsing ServerName \n");
464 465
               return false;
465 466
            }
466 467
            else
467 468
            {
468
	      if (verbose) printf("ServerName = %s \n",(char *)&msg->serverName.value);
469
	      if (verbose) info("ServerName = %s \n",(char *)&msg->serverName.value);
469 470
            }
470 471
	} else if (atrType == SecondaryAddress)
471 472
	{
472 473
            msg->hasSecondaryAddress = true;
473 474
            if ( stunParseAtrAddress(  body,  attrLen, &msg->secondaryAddress, verbose ) == false )
474 475
            {
475
	      printf("problem parsing secondaryAddress \n");
476
	      debug("problem parsing secondaryAddress \n");
476 477
               return false;
477 478
            }
478 479
            else
479 480
            {
480
	      if (verbose) printf("SecondaryAddress ---------------- \n");
481
	      if (verbose) info("SecondaryAddress ---------------- \n");
481 482
            }
482 483
	}  else 
483 484
	{				
484 485
         
485
	   if (verbose) printf("Unknown attribute: %i \n",atrType);
486
	   if (verbose) info("Unknown attribute: %i \n",atrType);
486 487
            if ( atrType <= 0x7FFF ) 
487 488
            {
488 489
               return false;
......
555 556

  
556 557
   if (msg.hasMappedAddress)
557 558
   {
558
     //if (verbose) printf("Encoding MappedAddress: %s \n",msg.mappedAddress.ipv4);
559
     //if (verbose) info("Encoding MappedAddress: %s \n",msg.mappedAddress.ipv4);
559 560
      ptr = encodeAtrAddress4 (ptr, MappedAddress, msg.mappedAddress);
560 561
   }
561 562
   if (msg.hasResponseAddress)
562 563
   {
563
     //if (verbose) printf("Encoding ResponseAddress: %s \n",msg.responseAddress.ipv4);
564
     //if (verbose) info("Encoding ResponseAddress: %s \n",msg.responseAddress.ipv4);
564 565
      ptr = encodeAtrAddress4(ptr, ResponseAddress, msg.responseAddress);
565 566
   }
566 567
   if (msg.hasChangeRequest)
567 568
   {
568
     //if (verbose) printf("Encoding ChangeRequest: %s \n",msg.changeRequest.value);
569
     //if (verbose) info("Encoding ChangeRequest: %s \n",msg.changeRequest.value);
569 570
      ptr = encodeAtrChangeRequest(ptr, msg.changeRequest);
570 571
   }
571 572
   if (msg.hasSourceAddress)
572 573
   {
573
     //if (verbose) printf("Encoding SourceAddress: %s \n ",msg.sourceAddress.ipv4);
574
     //if (verbose) info("Encoding SourceAddress: %s \n ",msg.sourceAddress.ipv4);
574 575
      ptr = encodeAtrAddress4(ptr, SourceAddress, msg.sourceAddress);
575 576
   }
576 577
   if (msg.hasChangedAddress)
577 578
   {
578
     //if (verbose) printf("Encoding ChangedAddress: %s \n ",msg.changedAddress.ipv4);
579
     //if (verbose) info("Encoding ChangedAddress: %s \n ",msg.changedAddress.ipv4);
579 580
      ptr = encodeAtrAddress4(ptr, ChangedAddress, msg.changedAddress);
580 581
   }
581 582
   if (msg.hasUsername)
582 583
   {
583
     if (verbose) printf("Encoding Username: %s \n", msg.username.value);
584
     if (verbose) info("Encoding Username: %s \n", msg.username.value);
584 585
      ptr = encodeAtrString(ptr, Username, msg.username);
585 586
   }
586 587
   if (msg.hasPassword)
587 588
   {
588
     if (verbose) printf("Encoding Password: %s \n ",msg.password.value);
589
     if (verbose) info("Encoding Password: %s \n ",msg.password.value);
589 590
      ptr = encodeAtrString(ptr, Password, msg.password);
590 591
   }
591 592
   if (msg.hasErrorCode)
592 593
   {
593
     if (verbose) printf("Encoding ErrorCode: class= %d number= %d reason= %s \n" 
594
     if (verbose) info("Encoding ErrorCode: class= %d number= %d reason= %s \n"
594 595
			 ,(int)msg.errorCode.errorClass  
595 596
			 ,(int)msg.errorCode.number 
596 597
		         ,msg.errorCode.reason 
......
600 601
   }
601 602
   if (msg.hasUnknownAttributes)
602 603
   {
603
     if (verbose) printf("Encoding UnknownAttribute: ??? \n");
604
     if (verbose) info("Encoding UnknownAttribute: ??? \n");
604 605
      ptr = encodeAtrUnknown(ptr, msg.unknownAttributes);
605 606
   }
606 607
   if (msg.hasReflectedFrom)
607 608
   {
608
     //if (verbose) printf("Encoding ReflectedFrom: %s \n ",msg.reflectedFrom.ipv4);
609
     //if (verbose) info("Encoding ReflectedFrom: %s \n ",msg.reflectedFrom.ipv4);
609 610
      ptr = encodeAtrAddress4(ptr, ReflectedFrom, msg.reflectedFrom);
610 611
   }
611 612
   if (msg.hasXorMappedAddress)
612 613
   {
613
     //if (verbose) printf("Encoding XorMappedAddress: %s \n ",msg.xorMappedAddress.ipv4 );
614
     //if (verbose) info("Encoding XorMappedAddress: %s \n ",msg.xorMappedAddress.ipv4 );
614 615
      ptr = encodeAtrAddress4 (ptr, XorMappedAddress, msg.xorMappedAddress);
615 616
   }
616 617
   if (msg.xorOnly)
617 618
   {
618
     if (verbose) printf("Encoding xorOnly: \n");
619
     if (verbose) info("Encoding xorOnly: \n");
619 620
      ptr = encodeXorOnly( ptr );
620 621
   }
621 622
   if (msg.hasServerName)
622 623
   {
623
     if (verbose) printf("Encoding ServerName: %s \n ",msg.serverName.value);
624
     if (verbose) info("Encoding ServerName: %s \n ",msg.serverName.value);
624 625
      ptr = encodeAtrString(ptr, ServerName, msg.serverName);
625 626
   }
626 627
   if (msg.hasSecondaryAddress)
627 628
   {
628
     //if (verbose) printf("Encoding SecondaryAddress: %s \n", msg.secondaryAddress.ipv4);
629
     //if (verbose) info("Encoding SecondaryAddress: %s \n", msg.secondaryAddress.ipv4);
629 630
      ptr = encodeAtrAddress4 (ptr, SecondaryAddress, msg.secondaryAddress);
630 631
   }
631 632

  
632 633
   if (password.sizeValue > 0)
633 634
   {
634
     if (verbose) printf("HMAC with password: %s \n ",password.value);
635
     if (verbose) info("HMAC with password: %s \n ",password.value);
635 636
		
636 637
      StunAtrIntegrity integrity;
637 638
      //computeHmac(integrity.hash, buf, (int)(ptr-buf) , password.value, password.sizeValue);
......
767 768
   if ( h == NULL )
768 769
   {
769 770
     //int err = getErrno();
770
      printf("error was %d \n",errno);
771
      error("error was %d \n",errno);
771 772
      ip = ntohl( 0x7F000001L );
772 773
      return false;
773 774
   }
......
790 791
{
791 792
   if ( hdrLen != 4 )
792 793
   {
793
      printf("hdr length = %d excepcting %d \n ",hdrLen,sizeof(result));
794
      error("hdr length = %d excepcting %d \n ",hdrLen,sizeof(result));
794 795

  
795
      printf("Incorrect size for ChangeRequest \n ");
796
      error("Incorrect size for ChangeRequest \n ");
796 797

  
797 798
      return false;
798 799
   }
......
835 836
{
836 837
   if ( hdrLen >= STUN_MAX_STRING )
837 838
   {
838
     printf( "String is too large \n");
839
     debug( "String is too large \n");
839 840
      return false;
840 841
   }
841 842
   else
842 843
   {
843 844
      if (hdrLen % 4 != 0)
844 845
      {
845
	printf("Bad length string %d \n ",hdrLen);
846
	debug("Bad length string %d \n ",hdrLen);
846 847
         return false;
847 848
      }
848 849
		
......
859 860
{
860 861
   if ( hdrLen != 20)
861 862
   {
862
     printf("MessageIntegrity must be 20 bytes \n");
863
     debug("MessageIntegrity must be 20 bytes \n");
863 864
      return false;
864 865
   }
865 866
   else
......
885 886
{
886 887
   if ( hdrLen >= sizeof(StunAtrError) )
887 888
   {
888
      printf("head on Error too large \n  ");
889
      debug("head on Error too large \n  ");
889 890
      return false;
890 891
   }
891 892
   else

Also available in: Unified diff