symonnet.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* $Id: symonnet.c,v 1.15 2005/10/16 15:27:01 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2004 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <netdb.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <time.h>
  37. #include "conf.h"
  38. #include "error.h"
  39. #include "data.h"
  40. #include "symon.h"
  41. #include "net.h"
  42. /* Fill a mux structure with inet details */
  43. void
  44. connect2mux(struct mux * mux)
  45. {
  46. struct sockaddr_storage sockaddr;
  47. int family;
  48. bzero((void *) &sockaddr, sizeof(sockaddr));
  49. get_mux_sockaddr(mux, SOCK_DGRAM);
  50. family = mux->sockaddr.ss_family;
  51. get_inaddrany_sockaddr(&sockaddr, family, SOCK_DGRAM, "0");
  52. if ((mux->symuxsocket = socket(family, SOCK_DGRAM, 0)) == -1)
  53. fatal("could not obtain socket: %.200s", strerror(errno));
  54. if (bind(mux->symuxsocket, (struct sockaddr *) & sockaddr, SS_LEN(&sockaddr)) == -1)
  55. fatal("could not bind socket: %.200s", strerror(errno));
  56. info("sending packets to udp %.200s", mux->name);
  57. }
  58. /* Send data stored in the mux structure to a mux */
  59. void
  60. send_packet(struct mux * mux)
  61. {
  62. if (sendto(mux->symuxsocket, (void *) &mux->packet.data,
  63. mux->offset, 0, (struct sockaddr *) & mux->sockaddr,
  64. SS_LEN(&mux->sockaddr))
  65. != mux->offset) {
  66. mux->senderr++;
  67. }
  68. if (mux->senderr >= SYMON_WARN_SENDERR) {
  69. warning("%d updates to mux(%.200s) lost due to send errors",
  70. mux->senderr, mux->name);
  71. mux->senderr = 0;
  72. }
  73. }
  74. /* Prepare a packet for data */
  75. void
  76. prepare_packet(struct mux * mux)
  77. {
  78. time_t t = time(NULL);
  79. bzero(&mux->packet, sizeof(mux->packet));
  80. mux->packet.header.symon_version = SYMON_PACKET_VER;
  81. mux->packet.header.timestamp = t;
  82. /* symonpacketheader is always first stream */
  83. mux->offset =
  84. setheader((char *) &mux->packet.data,
  85. &mux->packet.header);
  86. }
  87. /* Put a stream into the packet for a mux */
  88. void
  89. stream_in_packet(struct stream * stream, struct mux * mux)
  90. {
  91. mux->offset +=
  92. (streamfunc[stream->type].get) /* call getter of stream */
  93. (&mux->packet.data[mux->offset], /* packet buffer */
  94. sizeof(mux->packet.data) - mux->offset, /* maxlen */
  95. stream);
  96. }
  97. /* Ready a packet for transmission, set length and crc */
  98. void
  99. finish_packet(struct mux * mux)
  100. {
  101. mux->packet.header.length = mux->offset;
  102. mux->packet.header.crc = 0;
  103. /* fill in correct header with crc = 0 */
  104. setheader((char *) &mux->packet.data, &mux->packet.header);
  105. /* fill in correct header with crc */
  106. mux->packet.header.crc = crc32(&mux->packet.data, mux->offset);
  107. setheader((char *) &mux->packet.data, &mux->packet.header);
  108. }