netbios_query.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*****************************************************************************
  2. * __________________ _________ _____ _____ .__ ._.
  3. * \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
  4. * | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
  5. * | | \| ` \/ / Y \ / | | \ ___/ \|
  6. * |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
  7. * \/ \/ \/ \/ )/ \/ \/ \/
  8. *
  9. * This file is part of liBDSM. Copyright © 2014-2015 VideoLabs SAS
  10. *
  11. * Author: Julien 'Lta' BALLET <contact@lta.io>
  12. *
  13. * liBDSM is released under LGPLv2.1 (or later) and is also available
  14. * under a commercial license.
  15. *****************************************************************************
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU Lesser General Public License as published by
  18. * the Free Software Foundation; either version 2.1 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public License
  27. * along with this program; if not, write to the Free Software Foundation,
  28. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  29. *****************************************************************************/
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <assert.h>
  34. #include <arpa/inet.h>
  35. #include "netbios_query.h"
  36. netbios_query *netbios_query_new(size_t payload_size,
  37. int is_query, char opcode)
  38. {
  39. netbios_query *q;
  40. q = calloc(1, sizeof(netbios_query));
  41. if (!q)
  42. return NULL;
  43. q->packet = calloc(1, sizeof(netbios_query_packet) + payload_size);
  44. if (!q->packet) {
  45. free(q);
  46. return NULL;
  47. }
  48. q->payload_size = payload_size;
  49. q->packet->flags = htons(opcode << 11);
  50. netbios_query_set_flag(q, NETBIOS_FLAG_QUERY, !is_query);
  51. return q;
  52. }
  53. void netbios_query_destroy(netbios_query *q)
  54. {
  55. assert(q);
  56. free(q->packet);
  57. free(q);
  58. }
  59. void netbios_query_set_flag(netbios_query *q,
  60. uint16_t flag, int value)
  61. {
  62. assert(q && q->packet);
  63. if (value)
  64. q->packet->flags = htons(ntohs(q->packet->flags) | flag);
  65. else
  66. q->packet->flags = htons(ntohs(q->packet->flags) & ~flag);
  67. }
  68. void netbios_query_print(netbios_query *q)
  69. {
  70. assert(q && q->packet);
  71. printf("--- netbios_query dump :\n");
  72. printf("payload = %zu, cursor = %zu.\n", q->payload_size, q->cursor);
  73. printf("Transaction id = %u.\n", q->packet->trn_id);
  74. printf("-------------------------\n");
  75. for (unsigned i = 0; i < sizeof(netbios_query_packet) + q->cursor; i++)
  76. {
  77. char c;
  78. if ((i % 8) == 0 && i != 0)
  79. printf("\n");
  80. if ((i % 8) == 0)
  81. printf("0x");
  82. c = ((char *)q->packet)[i];
  83. printf("%.2hhX ", c);
  84. }
  85. printf("\n");
  86. printf("-------------------------\n");
  87. }
  88. int netbios_query_append(netbios_query *q, const char *data,
  89. size_t data_size)
  90. {
  91. assert(q && q->packet);
  92. if (q->payload_size - q->cursor < data_size)
  93. return -1;
  94. memcpy(((char *)&q->packet->payload) + q->cursor, data, data_size);
  95. q->cursor += data_size;
  96. return 0;
  97. }