en50221_app_auth.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. en50221 encoder An implementation for libdvb
  3. an implementation for the en50221 transport layer
  4. Copyright (C) 2004, 2005 Manu Abraham <abraham.manu@gmail.com>
  5. Copyright (C) 2005 Julian Scheel (julian at jusst dot de)
  6. Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
  7. This library is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Lesser General Public License as
  9. published by the Free Software Foundation; either version 2.1 of
  10. the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <string.h>
  20. #include <libdvbmisc/dvbmisc.h>
  21. #include <pthread.h>
  22. #include "en50221_app_auth.h"
  23. #include "en50221_app_tags.h"
  24. #include "asn_1.h"
  25. struct en50221_app_auth {
  26. struct en50221_app_send_functions *funcs;
  27. en50221_app_auth_request_callback callback;
  28. void *callback_arg;
  29. pthread_mutex_t lock;
  30. };
  31. static int en50221_app_auth_parse_request(struct en50221_app_auth *private,
  32. uint8_t slot_id,
  33. uint16_t session_number,
  34. uint8_t * data,
  35. uint32_t data_length);
  36. struct en50221_app_auth *en50221_app_auth_create(struct en50221_app_send_functions *funcs)
  37. {
  38. struct en50221_app_auth *auth = NULL;
  39. // create structure and set it up
  40. auth = malloc(sizeof(struct en50221_app_auth));
  41. if (auth == NULL) {
  42. return NULL;
  43. }
  44. auth->funcs = funcs;
  45. auth->callback = NULL;
  46. pthread_mutex_init(&auth->lock, NULL);
  47. // done
  48. return auth;
  49. }
  50. void en50221_app_auth_destroy(struct en50221_app_auth *auth)
  51. {
  52. pthread_mutex_destroy(&auth->lock);
  53. free(auth);
  54. }
  55. void en50221_app_auth_register_request_callback(struct en50221_app_auth *auth,
  56. en50221_app_auth_request_callback callback, void *arg)
  57. {
  58. pthread_mutex_lock(&auth->lock);
  59. auth->callback = callback;
  60. auth->callback_arg = arg;
  61. pthread_mutex_unlock(&auth->lock);
  62. }
  63. int en50221_app_auth_send(struct en50221_app_auth *auth,
  64. uint16_t session_number,
  65. uint16_t auth_protocol_id, uint8_t * auth_data,
  66. uint32_t auth_data_length)
  67. {
  68. uint8_t buf[10];
  69. // the header
  70. buf[0] = (TAG_AUTH_RESP >> 16) & 0xFF;
  71. buf[1] = (TAG_AUTH_RESP >> 8) & 0xFF;
  72. buf[2] = TAG_AUTH_RESP & 0xFF;
  73. // encode the length field
  74. int length_field_len;
  75. if ((length_field_len = asn_1_encode(auth_data_length + 2, buf + 3, 3)) < 0) {
  76. return -1;
  77. }
  78. // the phase_id
  79. buf[3 + length_field_len] = auth_protocol_id >> 8;
  80. buf[3 + length_field_len + 1] = auth_protocol_id;
  81. // build the iovecs
  82. struct iovec iov[2];
  83. iov[0].iov_base = buf;
  84. iov[0].iov_len = 3 + length_field_len + 2;
  85. iov[1].iov_base = auth_data;
  86. iov[1].iov_len = auth_data_length;
  87. // sendit
  88. return auth->funcs->send_datav(auth->funcs->arg, session_number,
  89. iov, 2);
  90. }
  91. int en50221_app_auth_message(struct en50221_app_auth *auth,
  92. uint8_t slot_id,
  93. uint16_t session_number,
  94. uint32_t resource_id,
  95. uint8_t * data, uint32_t data_length)
  96. {
  97. (void) resource_id;
  98. // get the tag
  99. if (data_length < 3) {
  100. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  101. return -1;
  102. }
  103. uint32_t tag = (data[0] << 16) | (data[1] << 8) | data[2];
  104. switch (tag) {
  105. case TAG_AUTH_REQ:
  106. return en50221_app_auth_parse_request(auth, slot_id,
  107. session_number,
  108. data + 3,
  109. data_length - 3);
  110. }
  111. print(LOG_LEVEL, ERROR, 1, "Received unexpected tag %x\n", tag);
  112. return -1;
  113. }
  114. static int en50221_app_auth_parse_request(struct en50221_app_auth *auth,
  115. uint8_t slot_id,
  116. uint16_t session_number,
  117. uint8_t * data,
  118. uint32_t data_length)
  119. {
  120. // first of all, decode the length field
  121. uint16_t asn_data_length;
  122. int length_field_len;
  123. if ((length_field_len = asn_1_decode(&asn_data_length, data, data_length)) < 0) {
  124. print(LOG_LEVEL, ERROR, 1, "ASN.1 decode error\n");
  125. return -1;
  126. }
  127. // check it
  128. if (asn_data_length < 2) {
  129. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  130. return -1;
  131. }
  132. if (asn_data_length > (data_length - length_field_len)) {
  133. print(LOG_LEVEL, ERROR, 1, "Received short data\n");
  134. return -1;
  135. }
  136. uint8_t *auth_data = data + length_field_len;
  137. // process it
  138. uint16_t auth_protocol_id = (auth_data[0] << 8) | auth_data[1];
  139. // tell the app
  140. pthread_mutex_lock(&auth->lock);
  141. en50221_app_auth_request_callback cb = auth->callback;
  142. void *cb_arg = auth->callback_arg;
  143. pthread_mutex_unlock(&auth->lock);
  144. if (cb) {
  145. return cb(cb_arg, slot_id, session_number,
  146. auth_protocol_id, auth_data + 2,
  147. asn_data_length - 2);
  148. }
  149. return 0;
  150. }