1515
1616#include "list.h"
1717#include "translate.h"
18+ #include "logging.h"
1819
1920#define MAX (x , y ) ((x) > (y) ? (x) : (y))
2021
@@ -73,7 +74,7 @@ static ssize_t tftp_send_data(struct tftp_client *client,
7374
7475 len = pread (client -> fd , p , client -> blksize , offset );
7576 if (len < 0 ) {
76- printf ( "[TQFTP] failed to read data\n " );
77+ TQFTP_LOG_ERR ( " failed to read data" );
7778 free (buf );
7879 return len ;
7980 }
@@ -85,7 +86,7 @@ static ssize_t tftp_send_data(struct tftp_client *client,
8586 /* Header (4 bytes) + data size */
8687 send_len = 4 + response_size ;
8788 if (send_len > (size_t )(p - buf )) {
88- printf ( "[TQFTP] requested data of %ld bytes but only read %ld bytes from file, rejecting\n " , response_size , len );
89+ TQFTP_LOG_ERR ( " requested data of %ld bytes but only read %ld bytes from file, rejecting" , response_size , len );
8990 free (buf );
9091 return - EINVAL ;
9192 }
@@ -234,7 +235,7 @@ static void parse_options(const char *buf, size_t len, size_t *blksize,
234235 } else if (!strcmp (opt , "seek" )) {
235236 * seek = atoi (value );
236237 } else {
237- printf ( "[TQFTP] Ignoring unknown option '%s' with value '%s'\n " , opt , value );
238+ TQFTP_LOG_WARN ( " Ignoring unknown option '%s' with value '%s'" , opt , value );
238239 }
239240 }
240241}
@@ -267,7 +268,7 @@ static void handle_rrq(const char *buf, size_t len, struct sockaddr_qrtr *sq)
267268
268269 if (strcasecmp (mode , "octet" )) {
269270 /* XXX: error */
270- printf ( "[TQFTP] not octet, reject\n " );
271+ TQFTP_LOG_ERR ( "Mode is not octet, reject" );
271272 return ;
272273 }
273274
@@ -277,25 +278,25 @@ static void handle_rrq(const char *buf, size_t len, struct sockaddr_qrtr *sq)
277278 & timeoutms , & rsize , & seek );
278279 }
279280
280- printf ( "[TQFTP] RRQ: %s (mode=%s rsize=%ld seek=%ld)\n " , filename , mode , rsize , seek );
281+ TQFTP_LOG_DEBUG ( " RRQ: %s (mode=%s rsize=%ld seek=%ld)" , filename , mode , rsize , seek );
281282
282283 sock = qrtr_open (0 );
283284 if (sock < 0 ) {
284285 /* XXX: error */
285- printf ( "[TQFTP] unable to create new qrtr socket, reject\n " );
286+ TQFTP_LOG_ERR ( " unable to create new qrtr socket, reject" );
286287 return ;
287288 }
288289
289290 ret = connect (sock , (struct sockaddr * )sq , sizeof (* sq ));
290291 if (ret < 0 ) {
291292 /* XXX: error */
292- printf ( "[TQFTP] unable to connect new qrtr socket to remote\n " );
293+ TQFTP_LOG_ERR ( " unable to connect new qrtr socket to remote" );
293294 return ;
294295 }
295296
296297 fd = translate_open (filename , O_RDONLY );
297298 if (fd < 0 ) {
298- printf ( "[TQFTP] unable to open %s (%d), reject\n " , filename , errno );
299+ TQFTP_LOG_ERR ( " unable to open %s (%d), reject" , filename , errno );
299300 tftp_send_error (sock , 1 , "file not found" );
300301 return ;
301302 }
@@ -318,17 +319,17 @@ static void handle_rrq(const char *buf, size_t len, struct sockaddr_qrtr *sq)
318319
319320 client -> blk_buf = calloc (1 , blksize + 4 );
320321 if (!client -> blk_buf ) {
321- printf ( "[TQFTP] Memory allocation failure\n " );
322+ TQFTP_LOG_ERR ( " Memory allocation failure" );
322323 return ;
323324 }
324325
325326 client -> rw_buf = calloc (1 , client -> rw_buf_size );
326327 if (!client -> rw_buf ) {
327- printf ( "[TQFTP] Memory allocation failure\n " );
328+ TQFTP_LOG_ERR ( " Memory allocation failure" );
328329 return ;
329330 }
330331
331- // printf("[TQFTP] new reader added\n ");
332+ TQFTP_LOG_DEBUG ( " new reader added" );
332333
333334 list_add (& readers , & client -> node );
334335
@@ -367,11 +368,11 @@ static void handle_wrq(const char *buf, size_t len, struct sockaddr_qrtr *sq)
367368
368369 if (strcasecmp (mode , "octet" )) {
369370 /* XXX: error */
370- printf ( "[TQFTP] not octet, reject\n " );
371+ TQFTP_LOG_ERR ( " not octet, reject" );
371372 return ;
372373 }
373374
374- printf ( "[TQFTP] WRQ: %s (%s)\n " , filename , mode );
375+ TQFTP_LOG_DEBUG ( " WRQ: %s (%s)" , filename , mode );
375376
376377 if (p < buf + len ) {
377378 do_oack = true;
@@ -382,21 +383,21 @@ static void handle_wrq(const char *buf, size_t len, struct sockaddr_qrtr *sq)
382383 fd = translate_open (filename , O_WRONLY | O_CREAT );
383384 if (fd < 0 ) {
384385 /* XXX: error */
385- printf ( "[TQFTP] unable to open %s (%d), reject\n " , filename , errno );
386+ TQFTP_LOG_ERR ( " unable to open %s (%d), reject" , filename , errno );
386387 return ;
387388 }
388389
389390 sock = qrtr_open (0 );
390391 if (sock < 0 ) {
391392 /* XXX: error */
392- printf ( "[TQFTP] unable to create new qrtr socket, reject\n " );
393+ TQFTP_LOG_ERR ( " unable to create new qrtr socket, reject" );
393394 return ;
394395 }
395396
396397 ret = connect (sock , (struct sockaddr * )sq , sizeof (* sq ));
397398 if (ret < 0 ) {
398399 /* XXX: error */
399- printf ( "[TQFTP] unable to connect new qrtr socket to remote\n " );
400+ TQFTP_LOG_ERR ( " unable to connect new qrtr socket to remote" );
400401 return ;
401402 }
402403
@@ -414,17 +415,17 @@ static void handle_wrq(const char *buf, size_t len, struct sockaddr_qrtr *sq)
414415
415416 client -> blk_buf = calloc (1 , blksize + 4 );
416417 if (!client -> blk_buf ) {
417- printf ( "[TQFTP] Memory allocation failure\n " );
418+ TQFTP_LOG_ERR ( " Memory allocation failure" );
418419 return ;
419420 }
420421
421422 client -> rw_buf = calloc (1 , client -> rw_buf_size );
422423 if (!client -> rw_buf ) {
423- printf ( "[TQFTP] Memory allocation failure\n " );
424+ TQFTP_LOG_ERR ( " Memory allocation failure" );
424425 return ;
425426 }
426427
427- // printf("[TQFTP] new writer added\n ");
428+ TQFTP_LOG_DEBUG ( " new writer added" );
428429
429430 list_add (& writers , & client -> node );
430431
@@ -457,14 +458,14 @@ static int handle_reader(struct tftp_client *client)
457458 if (len < 0 ) {
458459 ret = - errno ;
459460 if (ret != - ENETRESET )
460- fprintf ( stderr , "[TQFTP] recvfrom failed: %d\n " , ret );
461+ TQFTP_LOG_ERROR ( " recvfrom failed: %d" , ret );
461462 return -1 ;
462463 }
463464
464465 /* Drop unsolicited messages */
465466 if (sq .sq_node != client -> sq .sq_node ||
466467 sq .sq_port != client -> sq .sq_port ) {
467- printf ( "[TQFTP] Discarding spoofed message\n " );
468+ TQFTP_LOG_ERR ( " Discarding spoofed message" );
468469 return -1 ;
469470 }
470471
@@ -474,12 +475,12 @@ static int handle_reader(struct tftp_client *client)
474475 int err = buf [2 ] << 8 | buf [3 ];
475476 /* "End of Transfer" is not an error, used with stat(2)-like calls */
476477 if (err == ERROR_END_OF_TRANSFER )
477- printf ( "[TQFTP] Remote returned END OF TRANSFER: %d - %s\n " , err , buf + 4 );
478+ TQFTP_LOG_DEBUG ( " Remote returned END OF TRANSFER: %d - %s" , err , buf + 4 );
478479 else
479- printf ( "[TQFTP] Remote returned an error: %d - %s\n " , err , buf + 4 );
480+ TQFTP_LOG_ERR ( " Remote returned an error: %d - %s" , err , buf + 4 );
480481 return -1 ;
481482 } else if (opcode != OP_ACK ) {
482- printf ( "[TQFTP] Expected ACK, got %d\n " , opcode );
483+ TQFTP_LOG_ERR ( " Expected ACK, got %d" , opcode );
483484 return -1 ;
484485 }
485486
@@ -500,7 +501,7 @@ static int handle_reader(struct tftp_client *client)
500501 n = tftp_send_data (client , block + 1 ,
501502 offset , response_size );
502503 if (n < 0 ) {
503- printf ( "[TQFTP] Sent block %d failed: %zd\n " , block + 1 , n );
504+ TQFTP_LOG_ERR ( " Sent block %d failed: %zd" , block + 1 , n );
504505 break ;
505506 }
506507 // printf("[TQFTP] Sent block %d of %zd\n", block + 1, n);
@@ -530,7 +531,7 @@ static int handle_writer(struct tftp_client *client)
530531 if (len < 0 ) {
531532 ret = - errno ;
532533 if (ret != - ENETRESET )
533- fprintf ( stderr , "[TQFTP] recvfrom failed: %d\n " , ret );
534+ TQFTP_LOG_ERROR ( " recvfrom failed: %d" , ret );
534535 return -1 ;
535536 }
536537
@@ -542,7 +543,7 @@ static int handle_writer(struct tftp_client *client)
542543 opcode = buf [0 ] << 8 | buf [1 ];
543544 block = buf [2 ] << 8 | buf [3 ];
544545 if (opcode != OP_DATA ) {
545- printf ( "[TQFTP] Expected DATA opcode, got %d\n " , opcode );
546+ TQFTP_LOG_ERR ( " Expected DATA opcode, got %d" , opcode );
546547 tftp_send_error (client -> sock , 4 , "Expected DATA opcode" );
547548 return -1 ;
548549 }
@@ -554,7 +555,7 @@ static int handle_writer(struct tftp_client *client)
554555 if (block != client -> blk_expected ) {
555556 uint16_t blk_expected = client -> blk_expected ;
556557
557- printf ( "[TQFTP] Block number out of sequence: %d (expected %d)\n " ,
558+ TQFTP_LOG_ERR ( " Block number out of sequence: %d (expected %d)" ,
558559 block , blk_expected );
559560 tftp_send_error (client -> sock , 4 , "Block number out of sequence" );
560561
@@ -581,7 +582,7 @@ static int handle_writer(struct tftp_client *client)
581582 ret = write (client -> fd , client -> rw_buf , client -> blk_offset );
582583 if (ret < 0 ) {
583584 /* XXX: report error */
584- printf ( "[TQFTP] failed to write data\n " );
585+ TQFTP_LOG_ERR ( " failed to write data" );
585586 return -1 ;
586587 }
587588
@@ -619,16 +620,18 @@ int main()
619620
620621 fd = qrtr_open (0 );
621622 if (fd < 0 ) {
622- fprintf ( stderr , "failed to open qrtr socket\n " );
623+ TQFTP_LOG_ERR ( "failed to open qrtr socket" );
623624 exit (1 );
624625 }
625626
626627 ret = qrtr_publish (fd , 4096 , 1 , 0 );
627628 if (ret < 0 ) {
628- fprintf ( stderr , "failed to publish service registry service\n " );
629+ TQFTP_LOG_ERR ( "failed to publish service registry service" );
629630 exit (1 );
630631 }
631632
633+ TQFTP_LOG_INFO ("TQFTP server ready, listening for connections" );
634+
632635 for (;;) {
633636 FD_ZERO (& rfds );
634637 FD_SET (fd , & rfds );
@@ -649,7 +652,7 @@ int main()
649652 if (errno == EINTR ) {
650653 continue ;
651654 } else {
652- fprintf ( stderr , "select failed\n " );
655+ TQFTP_LOG_ERROR ( "select failed" );
653656 break ;
654657 }
655658 }
@@ -676,28 +679,28 @@ int main()
676679 if (len < 0 ) {
677680 ret = - errno ;
678681 if (ret != - ENETRESET )
679- fprintf ( stderr , "[TQFTP] recvfrom failed: %d\n " , ret );
682+ TQFTP_LOG_ERROR ( " recvfrom failed: %d" , ret );
680683 return ret ;
681684 }
682685
683686 /* Ignore control messages */
684687 if (sq .sq_port == QRTR_PORT_CTRL ) {
685688 ret = qrtr_decode (& pkt , buf , len , & sq );
686689 if (ret < 0 ) {
687- fprintf ( stderr , "[TQFTP] unable to decode qrtr packet\n " );
690+ TQFTP_LOG_ERROR ( " unable to decode qrtr packet" );
688691 return ret ;
689692 }
690693
691694 switch (pkt .type ) {
692695 case QRTR_TYPE_BYE :
693- // fprintf(stderr, "[TQFTP] got bye\n ");
696+ TQFTP_LOG_DEBUG ( "Got bye on QRTE_PORT_CTRL port " );
694697 list_for_each_entry_safe (client , next , & writers , node ) {
695698 if (client -> sq .sq_node == sq .sq_node )
696699 client_close_and_free (client );
697700 }
698701 break ;
699702 case QRTR_TYPE_DEL_CLIENT :
700- // fprintf(stderr, "[TQFTP] got del_client\n ");
703+ TQFTP_LOG_DEBUG ( "Got DEL_CLIENT on QRTE_PORT_CTRL port " );
701704 list_for_each_entry_safe (client , next , & writers , node ) {
702705 if (!memcmp (& client -> sq , & sq , sizeof (sq )))
703706 client_close_and_free (client );
@@ -711,18 +714,19 @@ int main()
711714 opcode = buf [0 ] << 8 | buf [1 ];
712715 switch (opcode ) {
713716 case OP_RRQ :
717+ TQFTP_LOG_DEBUG ("Got OP_WRQ" );
714718 handle_rrq (buf , len , & sq );
715719 break ;
716720 case OP_WRQ :
717- // printf("[TQFTP] write\n ");
721+ TQFTP_LOG_DEBUG ( "Got OP_WRQ " );
718722 handle_wrq (buf , len , & sq );
719723 break ;
720724 case OP_ERROR :
721725 buf [len ] = '\0' ;
722- printf ( "[TQFTP] received error: %d - %s\n " , buf [2 ] << 8 | buf [3 ], buf + 4 );
726+ TQFTP_LOG_ERR ( " received error: %d - %s" , buf [2 ] << 8 | buf [3 ], buf + 4 );
723727 break ;
724728 default :
725- printf ( "[TQFTP] unhandled op %d\n " , opcode );
729+ TQFTP_LOG_ERR ( " unhandled op %d" , opcode );
726730 break ;
727731 }
728732 }
0 commit comments