smarty template cache syncing daemon (PoC)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.1KB

  1. /***************************************************************************
  2. * Copyright (C) 06/2006 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <net/if.h>
  26. #include <netinet/in.h>
  27. #include <arpa/inet.h>
  28. #include "configfile.h"
  29. #include "logging.h"
  30. #define DEFAULT_DEVICE "eth0"
  31. #define DEFAULT_GROUP "224.0.0.1"
  32. #define DEFAULT_PORT 2000
  33. static struct sockaddr_in dest_addr;
  34. int mcast_init()
  35. {
  36. struct ip_mreq multiaddr;
  37. struct ifreq ifr;
  38. // open socket
  39. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  40. if (sockfd == -1) {
  41. log_print(LOG_ERROR, "mcast_init: socket()");
  42. return -1;
  43. }
  44. // get device flags
  45. char *mcastdev = config_get_string("global", "mcastdev", DEFAULT_DEVICE);
  46. strncpy(ifr.ifr_name, mcastdev, sizeof(ifr.ifr_name));
  47. if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) != 0) {
  48. log_print(LOG_WARN, "mcast_init: ioctl(SIOCGIFFLAGS)");
  49. close(sockfd);
  50. return -1;
  51. }
  52. // check device flags
  53. if ((ifr.ifr_flags & (IFF_UP | IFF_MULTICAST)) != (IFF_UP | IFF_MULTICAST)) {
  54. log_print(LOG_WARN, "mcast_init: device %s not up");
  55. close(sockfd);
  56. return -1;
  57. }
  58. if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, ifr.ifr_name, sizeof(ifr.ifr_name)) < 0) {
  59. log_print(LOG_WARN, "mcast_init: setsockopt(SO_BINDTODEVICE)");
  60. // close(sockfd);
  61. // return -1;
  62. }
  63. char *mcastgroup = config_get_string("global", "mcastgroup", DEFAULT_GROUP);
  64. int mcastport = config_get_int("global", "mcastport", DEFAULT_PORT);
  65. dest_addr.sin_family = AF_INET;
  66. inet_aton(mcastgroup, &dest_addr.sin_addr);
  67. dest_addr.sin_port = htons(mcastport);
  68. if (bind(sockfd, (struct sockaddr*)&dest_addr, sizeof(dest_addr)) < 0) {
  69. log_print(LOG_WARN, "mcast_init: bind()");
  70. close(sockfd);
  71. return -1;
  72. }
  73. // get interface address
  74. if (ioctl(sockfd, SIOCGIFADDR, &ifr) != 0) {
  75. log_print(LOG_WARN, "mcast_init: ioctl(SIOCGIFADDR)");
  76. close(sockfd);
  77. return -1;
  78. }
  79. // check address family
  80. if (ifr.ifr_addr.sa_family != AF_INET) {
  81. log_print(LOG_WARN, "mcast_init: unknown address family");
  82. close(sockfd);
  83. return -1;
  84. }
  85. char loop = 1;
  86. if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop))) {
  87. log_print(LOG_WARN, "mcast_init: setsockopt(IP_MULTICAST_LOOP)");
  88. close(sockfd);
  89. return -1;
  90. }
  91. inet_aton(mcastgroup, &multiaddr.imr_multiaddr);
  92. multiaddr.imr_interface.s_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
  93. if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
  94. &multiaddr, sizeof(multiaddr)) == -1) {
  95. log_print(LOG_WARN, "mcast_init: setsockopt(IP_ADD_MEMBERSHIP)");
  96. close(sockfd);
  97. return -1;
  98. }
  99. return sockfd;
  100. }
  101. int mcast_send(int sock, char *buf, int len)
  102. {
  103. return sendto(sock, buf, len, MSG_NOSIGNAL, (const struct sockaddr*)&dest_addr, sizeof(dest_addr));
  104. }
  105. void msock_close(int sock)
  106. {
  107. close(sock);
  108. }