PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/aiccu/common/heartbeat.c

#
C | 296 lines | 200 code | 39 blank | 57 comment | 38 complexity | 1928ad3aaa484e4a8d8c42d3b9098c53 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /**********************************************************
  2. SixXS - Automatic IPv6 Connectivity Configuration Utility
  3. ***********************************************************
  4. Copyright 2003-2005 SixXS - http://www.sixxs.net
  5. ***********************************************************
  6. common/heartbeat.c - Heartbeat Code
  7. ***********************************************************
  8. $Author: jeroen $
  9. $Id: heartbeat.c,v 1.9 2006-12-21 14:08:50 jeroen Exp $
  10. $Date: 2006-12-21 14:08:50 $
  11. **********************************************************/
  12. #include "heartbeat.h"
  13. #include "aiccu.h"
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <stdlib.h>
  17. #include <stdarg.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include <time.h>
  21. #include <signal.h>
  22. #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
  23. #include <net/if.h>
  24. #endif
  25. /**************************************
  26. Functions
  27. **************************************/
  28. /* Get a socket and determine the new IP address */
  29. SOCKET heartbeat_socket(
  30. uint32_t *address_changed,
  31. int bStaticTunnel,
  32. const char *sIPv4Interface,
  33. char **sIPv4Local,
  34. const char *sIPv4POP,
  35. const char *sIPv4LocalResolve)
  36. {
  37. SOCKET sockfd;
  38. struct sockaddr sa;
  39. socklen_t socklen;
  40. char local_ipv4[NI_MAXHOST];
  41. #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
  42. struct ifreq interface;
  43. #endif
  44. struct addrinfo hints, *res, *ressave;
  45. D(dolog(LOG_DEBUG, "heartbeat_socket() - Address is %s\n", *sIPv4Local));
  46. if (address_changed) *address_changed = 0;
  47. /* Get ourselves a nice IPv4 socket */
  48. sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  49. if (sockfd < 0)
  50. {
  51. dolog(LOG_ERR, "Couldn't open a socket for determining current IPv4 address\n");
  52. return -1;
  53. }
  54. fcntl(sockfd, F_SETFD, FD_CLOEXEC);
  55. #if defined(SOL_SOCKET) && defined(SO_BINDTODEVICE)
  56. /*
  57. * We don't have to bind to a device if this
  58. * is a static tunnel, this allows running
  59. * the heartbeat client as non-root.
  60. */
  61. if (!bStaticTunnel)
  62. {
  63. /*
  64. * Only allowed as root, but we need root rights anyways
  65. * to (re)configure the tunnel
  66. */
  67. /* Bind to the underlying IPv4 device */
  68. memset(&interface, 0, sizeof(interface));
  69. strncpy(interface.ifr_ifrn.ifrn_name, sIPv4Interface, IFNAMSIZ);
  70. if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE,
  71. (char *)&interface, sizeof(interface)) == -1)
  72. {
  73. dolog(LOG_ERR, "Couldn't bind to device \"%s\"\n", sIPv4Interface);
  74. close(sockfd);
  75. /* We return a -1, thus the app will keep beating */
  76. return -1;
  77. }
  78. }
  79. #else
  80. /* Make compiler happy and 'use' the param */
  81. sIPv4Interface = sIPv4Interface;
  82. #endif
  83. /*
  84. * connect to the remote port
  85. * this causes us to be able to use normal write()
  86. * and also gives us the ability to find out the local IP
  87. */
  88. memset(&hints, 0, sizeof(struct addrinfo));
  89. hints.ai_family = AF_INET;
  90. hints.ai_socktype = SOCK_DGRAM;
  91. /* Get the POP IPv4 into a sockaddr */
  92. if (getaddrinfo(sIPv4POP, HEARTBEAT_PORT, &hints, &res) != 0)
  93. {
  94. dolog(LOG_ERR, "Couldn't resolve POP ip %s\n", sIPv4POP);
  95. closesocket(sockfd);
  96. /* We return a -1, thus the app will keep beating */
  97. return -1;
  98. }
  99. ressave = res;
  100. while (res)
  101. {
  102. if (connect(sockfd, res->ai_addr, (unsigned int)res->ai_addrlen) == 0) break;
  103. res = res->ai_next;
  104. }
  105. freeaddrinfo(ressave);
  106. if (res == NULL)
  107. {
  108. dolog(LOG_ERR, "Failed to connect() to remote side\n");
  109. closesocket(sockfd);
  110. /* We return a -1, thus the app will keep beating */
  111. return -1;
  112. }
  113. /* Normal operation, find out our local IPv4 address */
  114. if (sIPv4LocalResolve == NULL)
  115. {
  116. /* Figure out our local IP */
  117. socklen = sizeof(sa);
  118. if (getsockname(sockfd, &sa, &socklen) == -1)
  119. {
  120. dolog(LOG_WARNING, "Couldn't get local socketaddress\n");
  121. closesocket(sockfd);
  122. return -1;
  123. }
  124. if (getnameinfo((struct sockaddr *)&sa, sizeof(sa),
  125. local_ipv4, sizeof(local_ipv4),
  126. NULL, 0,
  127. NI_NUMERICHOST) != 0)
  128. {
  129. dolog(LOG_WARNING, "Couldn't get local IP\n");
  130. closesocket(sockfd);
  131. return -1;
  132. }
  133. }
  134. else
  135. {
  136. /*
  137. * this causes us to be able to use normal write()
  138. * and also gives us the ability to find out the local IP
  139. */
  140. memset(&hints, 0, sizeof(struct addrinfo));
  141. hints.ai_family = AF_INET;
  142. hints.ai_socktype = SOCK_DGRAM;
  143. /* Get the POP IPv4 into a sockaddr */
  144. if (getaddrinfo(sIPv4LocalResolve, NULL, &hints, &res) != 0)
  145. {
  146. dolog(LOG_ERR, "Couldn't resolve POP IPv4 %s\n", sIPv4POP);
  147. /* We return a -1, thus the app will keep beating */
  148. return -1;
  149. }
  150. ressave = res;
  151. while (res)
  152. {
  153. if (getnameinfo(res->ai_addr, (socklen_t)res->ai_addrlen,
  154. local_ipv4, sizeof(local_ipv4),
  155. NULL, 0,
  156. NI_NUMERICHOST) == 0)
  157. {
  158. break;
  159. }
  160. dolog(LOG_WARNING, "Couldn't get local IP\n");
  161. res = res->ai_next;
  162. }
  163. freeaddrinfo(ressave);
  164. }
  165. /* Did the IPv4 address change? */
  166. if (*sIPv4Local == NULL ||
  167. strcmp(*sIPv4Local, local_ipv4) != 0)
  168. {
  169. if (*sIPv4Local) free(*sIPv4Local);
  170. *sIPv4Local = strdup(local_ipv4);
  171. dolog(LOG_DEBUG, "heartbeat_socket() - IPv4 : %s\n", *sIPv4Local);
  172. if (!bStaticTunnel)
  173. {
  174. /* Run a script to change the address */
  175. if (address_changed) *address_changed = 1;
  176. }
  177. }
  178. D(else dolog(LOG_DEBUG, "heartbeat_socket() - Address stays %s\n", *sIPv4Local));
  179. /* Return it */
  180. return sockfd;
  181. }
  182. /* Send a heartbeat */
  183. int heartbeat_send(SOCKET sockfd, char *sIPv4Local, char *sIPv6Local, char *sPassword, bool bBehindNAT)
  184. {
  185. struct MD5Context md5;
  186. unsigned char *p, our_digest[20], *pn = our_digest, buf[1000];
  187. time_t time_tee;
  188. int i;
  189. time_tee = time(NULL);
  190. /* Create the string to send including our password */
  191. snprintf((char *)buf, sizeof(buf), "HEARTBEAT TUNNEL %s %s %ld %s",
  192. sIPv6Local,
  193. (bBehindNAT ? "sender" : sIPv4Local),
  194. (long int)time_tee, sPassword);
  195. /* Generate a MD5 */
  196. MD5Init(&md5);
  197. MD5Update(&md5, buf, (unsigned int)strlen((char *)buf));
  198. MD5Final(our_digest, &md5);
  199. /* Overwrite it without password */
  200. p = buf;
  201. p += snprintf((char *)buf, sizeof(buf)-17, "HEARTBEAT TUNNEL %s %s %ld ",
  202. sIPv6Local,
  203. (bBehindNAT == 1 ? "sender" : sIPv4Local),
  204. (long int)time_tee);
  205. /* append the digest */
  206. for (i = 0; i < 16; i++)
  207. {
  208. snprintf((char *)p, 3, (const char *)"%02x", *pn++);
  209. p+=2;
  210. }
  211. *p = '\0';
  212. /* Send the heartbeat */
  213. send(sockfd, (const char *)buf, (unsigned int)strlen((const char *)buf),0);
  214. dolog(LOG_DEBUG, "[HB] %s\n", buf);
  215. return 0;
  216. }
  217. char *heartbeat_getlocalIP(struct TIC_Tunnel *hTunnel)
  218. {
  219. bool address_changed = false;
  220. char *ipv4_local = NULL;
  221. SOCKET sockfd = heartbeat_socket(&address_changed, 0, "",
  222. &ipv4_local,
  223. hTunnel->sIPv4_POP,
  224. NULL);
  225. if (sockfd >= 0) closesocket(sockfd);
  226. dolog(LOG_DEBUG, "Local IPv4 address: %s\n", ipv4_local);
  227. return ipv4_local;
  228. }
  229. /*
  230. * Other code can call this every once in a while
  231. * and it will take care of everything ("everything?" "everything!")
  232. */
  233. void heartbeat_beat(struct TIC_Tunnel *hTunnel)
  234. {
  235. uint32_t address_changed = 0;
  236. SOCKET sockfd = -1;
  237. D(dolog(LOG_DEBUG, "heartbeat_beat() - Beating from %s\n", hTunnel->sIPv4_Local);)
  238. sockfd = heartbeat_socket(&address_changed, 0, "",
  239. &hTunnel->sIPv4_Local,
  240. hTunnel->sIPv4_POP,
  241. NULL);
  242. if (sockfd >= 0)
  243. {
  244. if (address_changed == 1)
  245. {
  246. D(dolog(LOG_DEBUG, "heartbeat_beat() - Address changed to %s\n", hTunnel->sIPv4_Local);)
  247. aiccu_reconfig(hTunnel);
  248. }
  249. heartbeat_send(sockfd,
  250. hTunnel->sIPv4_Local,
  251. hTunnel->sIPv6_Local,
  252. hTunnel->sPassword,
  253. 1);
  254. closesocket(sockfd);
  255. sockfd = (SOCKET)-1;
  256. }
  257. }