util.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #ifndef QEMU_NET_UTIL_H
  25. #define QEMU_NET_UTIL_H
  26. /*
  27. * Structure of an internet header, naked of options.
  28. */
  29. struct ip {
  30. #if HOST_BIG_ENDIAN
  31. uint8_t ip_v:4, /* version */
  32. ip_hl:4; /* header length */
  33. #else
  34. uint8_t ip_hl:4, /* header length */
  35. ip_v:4; /* version */
  36. #endif
  37. uint8_t ip_tos; /* type of service */
  38. uint16_t ip_len; /* total length */
  39. uint16_t ip_id; /* identification */
  40. uint16_t ip_off; /* fragment offset field */
  41. #define IP_DF 0x4000 /* don't fragment flag */
  42. #define IP_MF 0x2000 /* more fragments flag */
  43. #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
  44. uint8_t ip_ttl; /* time to live */
  45. uint8_t ip_p; /* protocol */
  46. uint16_t ip_sum; /* checksum */
  47. struct in_addr ip_src, ip_dst; /* source and dest address */
  48. } QEMU_PACKED;
  49. static inline bool in6_equal_net(const struct in6_addr *a,
  50. const struct in6_addr *b,
  51. int prefix_len)
  52. {
  53. if (memcmp(a, b, prefix_len / 8) != 0) {
  54. return 0;
  55. }
  56. if (prefix_len % 8 == 0) {
  57. return 1;
  58. }
  59. return a->s6_addr[prefix_len / 8] >> (8 - (prefix_len % 8))
  60. == b->s6_addr[prefix_len / 8] >> (8 - (prefix_len % 8));
  61. }
  62. #define TCPS_CLOSED 0 /* closed */
  63. #define TCPS_LISTEN 1 /* listening for connection */
  64. #define TCPS_SYN_SENT 2 /* active, have sent syn */
  65. #define TCPS_SYN_RECEIVED 3 /* have send and received syn */
  66. /* states < TCPS_ESTABLISHED are those where connections not established */
  67. #define TCPS_ESTABLISHED 4 /* established */
  68. #define TCPS_CLOSE_WAIT 5 /* rcvd fin, waiting for close */
  69. /* states > TCPS_CLOSE_WAIT are those where user has closed */
  70. #define TCPS_FIN_WAIT_1 6 /* have closed, sent fin */
  71. #define TCPS_CLOSING 7 /* closed xchd FIN; await FIN ACK */
  72. #define TCPS_LAST_ACK 8 /* had fin and close; await FIN ACK */
  73. /* states > TCPS_CLOSE_WAIT && < TCPS_FIN_WAIT_2 await ACK of FIN */
  74. #define TCPS_FIN_WAIT_2 9 /* have closed, fin is acked */
  75. #define TCPS_TIME_WAIT 10 /* in 2*msl quiet wait after close */
  76. int net_parse_macaddr(uint8_t *macaddr, const char *p);
  77. #endif /* QEMU_NET_UTIL_H */