v2gtp.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (C) 2007-2018 Siemens AG
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published
  6. * by the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*******************************************************************
  18. *
  19. * @author Sebastian.Kaebisch@siemens.com
  20. * @author Daniel.Peintner.EXT@siemens.com
  21. * @version 0.9.4
  22. * @contact Richard.Kuntschke@siemens.com
  23. *
  24. ********************************************************************/
  25. /*
  26. * This file implements the v2gtp header writer and reader.
  27. *
  28. */
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #include <stdio.h>
  33. #include <math.h>
  34. #include <string.h>
  35. #include "v2gtp.h"
  36. int write_v2gtpHeader(uint8_t* outStream, uint32_t outStreamLength, uint16_t payloadType)
  37. {
  38. /* write v2gtp version number 1=byte */
  39. outStream[0]=V2GTP_VERSION;
  40. /* write inverse v2gtp version */
  41. outStream[1]=V2GTP_VERSION_INV;
  42. /* write payload type */
  43. outStream[3] = (uint8_t)(payloadType & 0xFF);
  44. outStream[2] = (uint8_t)(payloadType >> 8 & 0xFF);
  45. /* write payload length */
  46. outStream[7] = (uint8_t)(outStreamLength & 0xFF);
  47. outStream[6] = (uint8_t)(outStreamLength>>8 & 0xFF);
  48. outStream[5] = (uint8_t)(outStreamLength>>16 & 0xFF);
  49. outStream[4] = (uint8_t)(outStreamLength>>24 & 0xFF);
  50. return 0;
  51. }
  52. int read_v2gtpHeader(uint8_t* inStream, uint32_t* payloadLength)
  53. {
  54. uint16_t payloadType=0;
  55. /* check, if we support this v2gtp version */
  56. if(inStream[0]!=V2GTP_VERSION || inStream[1]!=V2GTP_VERSION_INV)
  57. return -1;
  58. /* check, if we support this payload type*/
  59. payloadType = inStream[2];
  60. payloadType = (payloadType << 8 | inStream[3]);
  61. if(payloadType != V2GTP_EXI_TYPE)
  62. return -1;
  63. /* determine payload length*/
  64. *payloadLength = inStream[4];
  65. *payloadLength = (*payloadLength << 8 | inStream[5]);
  66. *payloadLength = (*payloadLength << 8 | inStream[6]);
  67. *payloadLength = (*payloadLength << 8 | inStream[7]);
  68. return 0;
  69. }