udppacket.cpp
上传用户:zslianheng
上传日期:2013-04-03
资源大小:946k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

Visual C++

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *   This program is free software; you can redistribute it and/or modify  *
  4.  *   it under the terms of the GNU General Public License as published by  *
  5.  *   the Free Software Foundation; either version 2 of the License, or     *
  6.  *   (at your option) any later version.                                   *
  7.  *                                                                         *
  8.  *   copyright            : (C) 2002 by Zhang Yong                         *
  9.  *   email                : z-yong163@163.com                              *
  10.  ***************************************************************************/
  11. #include <string.h>
  12. #include "udppacket.h"
  13. #include "ndes.h"
  14. UdpOutPacket::UdpOutPacket(UdpSession *p)
  15. {
  16. attempts = 0;
  17. expire = 0;
  18. session = p;
  19. }
  20. void UdpOutPacket::write8(uint8 b)
  21. {
  22. if (cursor <= data + MAX_PACKET_SIZE - sizeof(b)) {
  23. *(uint8 *) cursor = b;
  24. cursor += sizeof(b);
  25. }
  26. }
  27. void UdpOutPacket::write16(uint16 w)
  28. {
  29. if (cursor <= data + MAX_PACKET_SIZE - sizeof(w)) {
  30. *(uint16 *) cursor = htons(w);
  31. cursor += sizeof(w);
  32. }
  33. }
  34. void UdpOutPacket::write32(uint32 dw)
  35. {
  36. if (cursor <= data + MAX_PACKET_SIZE - sizeof(dw)) {
  37. *(uint32 *) cursor = htonl(dw);
  38. cursor += sizeof(dw);
  39. }
  40. }
  41. void UdpOutPacket::writeString(const char *str)
  42. {
  43. uint16 len = strlen(str) + 1;
  44. if (cursor <= data + MAX_PACKET_SIZE - sizeof(len) - len) {
  45. write16(len);
  46. strcpy(cursor, str);
  47. cursor += len;
  48. }
  49. }
  50. int UdpOutPacket::send(int sock, uint32 ip, uint16 port)
  51. {
  52. sockaddr_in addr;
  53. memset(&addr, 0, sizeof(addr));
  54. addr.sin_family = AF_INET;
  55. addr.sin_addr.s_addr = htonl(ip);
  56. addr.sin_port = htons(port);
  57. return sendto(sock, data, cursor - data, 0, (sockaddr *) &addr, sizeof(addr));
  58. }
  59. uint8 UdpInPacket::read8()
  60. {
  61. uint8 b = 0;
  62. if (cursor <= data + datalen  - sizeof(uint8)) {
  63. b = *cursor;
  64. cursor += sizeof(b);
  65. }
  66. return b;
  67. }
  68. uint16 UdpInPacket::read16()
  69. {
  70. uint16 w = 0;
  71. if (cursor <= data + datalen - sizeof(w)) {
  72. w = ntohs(*(uint16 *) cursor);
  73. cursor += sizeof(w);
  74. }
  75. return w;
  76. }
  77. uint32 UdpInPacket::read32()
  78. {
  79. uint32 dw = 0;
  80. if (cursor <= data + datalen - sizeof(dw)) {
  81. dw = ntohl(*(uint32 *) cursor);
  82. cursor += sizeof(uint32);
  83. }
  84. return dw;
  85. }
  86. const char *UdpInPacket::readString()
  87. {
  88. const char *str = "";
  89. uint16 len = read16();
  90. if (cursor <= data + datalen - len && !cursor[len - 1]) {
  91. str = cursor;
  92. cursor += len;
  93. }
  94. return str;
  95. }
  96. int UdpInPacket::recv(int sock)
  97. {
  98. socklen_t len = sizeof(addr);
  99. datalen = recvfrom(sock, data, MAX_PACKET_SIZE, 0, (sockaddr *) &addr, &len);
  100. if (datalen > 0)
  101. cursor = data + sizeof(UDP_HEADER);
  102. return datalen;
  103. }
  104. void UdpInPacket::decrypt(const char *key)
  105. {
  106. int n = datalen - sizeof(UDP_HEADER);
  107. if (n > 0) {
  108. setkey((char *) key);
  109. char *p = data + sizeof(UDP_HEADER);
  110. while (n > 0) {
  111. dedes(p);
  112. p += 8;
  113. n -= 8;
  114. }
  115. }
  116. }