net_tcpchksum.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /****************************************************************************
  2. * net/utils/net_tcpchksum.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <nuttx/net/netdev.h>
  25. #include "utils/utils.h"
  26. #ifdef CONFIG_NET_TCP
  27. /****************************************************************************
  28. * Public Functions
  29. ****************************************************************************/
  30. /****************************************************************************
  31. * Name: tcp_chksum, tcp_ipv4_chksum, and tcp_ipv6_chksum
  32. *
  33. * Description:
  34. * Calculate the TCP checksum of the packet in d_buf and d_appdata.
  35. *
  36. * The TCP checksum is the Internet checksum of data contents of the
  37. * TCP segment, and a pseudo-header as defined in RFC793.
  38. *
  39. * Note: The d_appdata pointer that points to the packet data may
  40. * point anywhere in memory, so it is not possible to simply calculate
  41. * the Internet checksum of the contents of the d_buf buffer.
  42. *
  43. * Returned Value:
  44. * The TCP checksum of the TCP segment in d_buf and pointed to by
  45. * d_appdata.
  46. *
  47. ****************************************************************************/
  48. #ifdef CONFIG_NET_IPv4
  49. uint16_t tcp_ipv4_chksum(FAR struct net_driver_s *dev)
  50. {
  51. return ipv4_upperlayer_chksum(dev, IP_PROTO_TCP);
  52. }
  53. #endif /* CONFIG_NET_IPv4 */
  54. #ifdef CONFIG_NET_IPv6
  55. uint16_t tcp_ipv6_chksum(FAR struct net_driver_s *dev)
  56. {
  57. return ipv6_upperlayer_chksum(dev, IP_PROTO_TCP, IPv6_HDRLEN);
  58. }
  59. #endif /* CONFIG_NET_IPv6 */
  60. #if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6)
  61. uint16_t tcp_chksum(FAR struct net_driver_s *dev)
  62. {
  63. if (IFF_IS_IPv6(dev->d_flags))
  64. {
  65. return tcp_ipv6_chksum(dev);
  66. }
  67. else
  68. {
  69. return tcp_ipv4_chksum(dev);
  70. }
  71. }
  72. #endif
  73. #endif /* CONFIG_NET_TCP */