@@ -491,9 +491,9 @@ func hostNameHandler(w http.ResponseWriter, r *http.Request) {
491
491
// udp server supports the hostName, echo and clientIP commands.
492
492
func startUDPServer (udpPort int ) {
493
493
serverAddress , err := net .ResolveUDPAddr ("udp" , fmt .Sprintf (":%d" , udpPort ))
494
- assertNoError (err )
494
+ assertNoError (err , fmt . Sprintf ( "failed to resolve UDP address for port %d" , sctpPort ) )
495
495
serverConn , err := net .ListenUDP ("udp" , serverAddress )
496
- assertNoError (err )
496
+ assertNoError (err , fmt . Sprintf ( "failed to create listener for UDP address %v" , serverAddress ) )
497
497
defer serverConn .Close ()
498
498
buf := make ([]byte , 2048 )
499
499
@@ -506,37 +506,37 @@ func startUDPServer(udpPort int) {
506
506
}()
507
507
for {
508
508
n , clientAddress , err := serverConn .ReadFromUDP (buf )
509
- assertNoError (err )
509
+ assertNoError (err , fmt . Sprintf ( "failed accepting UDP connections" ) )
510
510
receivedText := strings .ToLower (strings .TrimSpace (string (buf [0 :n ])))
511
511
if receivedText == "hostname" {
512
512
log .Println ("Sending udp hostName response" )
513
513
_ , err = serverConn .WriteToUDP ([]byte (getHostName ()), clientAddress )
514
- assertNoError (err )
514
+ assertNoError (err , fmt . Sprintf ( "failed to write hostname to UDP client %s" , clientAddress ) )
515
515
} else if strings .HasPrefix (receivedText , "echo " ) {
516
516
parts := strings .SplitN (receivedText , " " , 2 )
517
517
resp := ""
518
518
if len (parts ) == 2 {
519
519
resp = parts [1 ]
520
520
}
521
- log .Printf ("Echoing %v\n " , resp )
521
+ log .Printf ("Echoing %v to UDP client %s \n " , resp , clientAddress )
522
522
_ , err = serverConn .WriteToUDP ([]byte (resp ), clientAddress )
523
- assertNoError (err )
523
+ assertNoError (err , fmt . Sprintf ( "failed to echo to UDP client %s" , clientAddress ) )
524
524
} else if receivedText == "clientip" {
525
- log .Printf ("Sending back clientip to %s " , clientAddress . String () )
525
+ log .Printf ("Sending clientip back to UDP client %s \n " , clientAddress )
526
526
_ , err = serverConn .WriteToUDP ([]byte (clientAddress .String ()), clientAddress )
527
- assertNoError (err )
527
+ assertNoError (err , fmt . Sprintf ( "failed to write clientip to UDP client %s" , clientAddress ) )
528
528
} else if len (receivedText ) > 0 {
529
- log .Printf ("Unknown udp command received: %v\n " , receivedText )
529
+ log .Printf ("Unknown UDP command received from %s : %v\n " , clientAddress , receivedText )
530
530
}
531
531
}
532
532
}
533
533
534
534
// sctp server supports the hostName, echo and clientIP commands.
535
535
func startSCTPServer (sctpPort int ) {
536
536
serverAddress , err := sctp .ResolveSCTPAddr ("sctp" , fmt .Sprintf (":%d" , sctpPort ))
537
- assertNoError (err )
537
+ assertNoError (err , fmt . Sprintf ( "failed to resolve SCTP address for port %d" , sctpPort ) )
538
538
listener , err := sctp .ListenSCTP ("sctp" , serverAddress )
539
- assertNoError (err )
539
+ assertNoError (err , fmt . Sprintf ( "failed to create listener for SCTP address %v" , serverAddress ) )
540
540
defer listener .Close ()
541
541
buf := make ([]byte , 1024 )
542
542
@@ -549,43 +549,43 @@ func startSCTPServer(sctpPort int) {
549
549
}()
550
550
for {
551
551
conn , err := listener .AcceptSCTP ()
552
- assertNoError (err )
552
+ assertNoError (err , fmt .Sprintf ("failed accepting SCTP connections" ))
553
+ clientAddress := conn .RemoteAddr ().String ()
553
554
n , err := conn .Read (buf )
554
- assertNoError (err )
555
+ assertNoError (err , fmt . Sprintf ( "failed to read from SCTP client %s" , clientAddress ) )
555
556
receivedText := strings .ToLower (strings .TrimSpace (string (buf [0 :n ])))
556
557
if receivedText == "hostname" {
557
- log .Println ("Sending sctp hostName response" )
558
+ log .Println ("Sending SCTP hostName response" )
558
559
_ , err = conn .Write ([]byte (getHostName ()))
559
- assertNoError (err )
560
+ assertNoError (err , fmt . Sprintf ( "failed to write hostname to SCTP client %s" , clientAddress ) )
560
561
} else if strings .HasPrefix (receivedText , "echo " ) {
561
562
parts := strings .SplitN (receivedText , " " , 2 )
562
563
resp := ""
563
564
if len (parts ) == 2 {
564
565
resp = parts [1 ]
565
566
}
566
- log .Printf ("Echoing %v\n " , resp )
567
+ log .Printf ("Echoing %v to SCTP client %s \n " , resp , clientAddress )
567
568
_ , err = conn .Write ([]byte (resp ))
568
- assertNoError (err )
569
+ assertNoError (err , fmt . Sprintf ( "failed to echo to SCTP client %s" , clientAddress ) )
569
570
} else if receivedText == "clientip" {
570
- clientAddress := conn .RemoteAddr ()
571
- log .Printf ("Sending back clientip to %s" , clientAddress .String ())
572
- _ , err = conn .Write ([]byte (clientAddress .String ()))
573
- assertNoError (err )
571
+ log .Printf ("Sending clientip back to SCTP client %s\n " , clientAddress )
572
+ _ , err = conn .Write ([]byte (clientAddress ))
573
+ assertNoError (err , fmt .Sprintf ("failed to write clientip to SCTP client %s" , clientAddress ))
574
574
} else if len (receivedText ) > 0 {
575
- log .Printf ("Unknown sctp command received: %v\n " , receivedText )
575
+ log .Printf ("Unknown SCTP command received from %s : %v\n " , clientAddress , receivedText )
576
576
}
577
577
conn .Close ()
578
578
}
579
579
}
580
580
581
581
func getHostName () string {
582
582
hostName , err := os .Hostname ()
583
- assertNoError (err )
583
+ assertNoError (err , "failed to get hostname" )
584
584
return hostName
585
585
}
586
586
587
- func assertNoError (err error ) {
587
+ func assertNoError (err error , detail string ) {
588
588
if err != nil {
589
- log .Fatal ("Error occurred. error:" , err )
589
+ log .Fatalf ("Error occurred: %s:%v" , detail , err )
590
590
}
591
591
}
0 commit comments