connect.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: connect.cc,v 1.7 2001/02/20 07:03:18 jgg Exp $
  4. /* ######################################################################
  5. Connect - Replacement connect call
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include "connect.h"
  10. #include <apt-pkg/error.h>
  11. #include <apt-pkg/fileutl.h>
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <unistd.h>
  15. // Internet stuff
  16. #include <netinet/in.h>
  17. #include <sys/socket.h>
  18. #include <arpa/inet.h>
  19. #include <netdb.h>
  20. #include "rfc2553emu.h"
  21. /*}}}*/
  22. static string LastHost;
  23. static int LastPort = 0;
  24. static struct addrinfo *LastHostAddr = 0;
  25. static struct addrinfo *LastUsed = 0;
  26. // RotateDNS - Select a new server from a DNS rotation /*{{{*/
  27. // ---------------------------------------------------------------------
  28. /* This is called during certain errors in order to recover by selecting a
  29. new server */
  30. void RotateDNS()
  31. {
  32. if (LastUsed != 0 && LastUsed->ai_next != 0)
  33. LastUsed = LastUsed->ai_next;
  34. else
  35. LastUsed = LastHostAddr;
  36. }
  37. /*}}}*/
  38. // DoConnect - Attempt a connect operation /*{{{*/
  39. // ---------------------------------------------------------------------
  40. /* This helper function attempts a connection to a single address. */
  41. static bool DoConnect(struct addrinfo *Addr,string Host,
  42. unsigned long TimeOut,int &Fd,pkgAcqMethod *Owner)
  43. {
  44. // Show a status indicator
  45. char Name[NI_MAXHOST];
  46. char Service[NI_MAXSERV];
  47. Name[0] = 0;
  48. Service[0] = 0;
  49. getnameinfo(Addr->ai_addr,Addr->ai_addrlen,
  50. Name,sizeof(Name),Service,sizeof(Service),
  51. NI_NUMERICHOST|NI_NUMERICSERV);
  52. Owner->Status("Connecting to %s (%s)",Host.c_str(),Name);
  53. /* If this is an IP rotation store the IP we are using.. If something goes
  54. wrong this will get tacked onto the end of the error message */
  55. if (LastHostAddr->ai_next != 0)
  56. {
  57. char Name2[NI_MAXHOST + NI_MAXSERV + 10];
  58. snprintf(Name2,sizeof(Name2),"[IP: %s %s]",Name,Service);
  59. Owner->SetFailExtraMsg(string(Name2));
  60. }
  61. else
  62. Owner->SetFailExtraMsg("");
  63. // Get a socket
  64. if ((Fd = socket(Addr->ai_family,Addr->ai_socktype,
  65. Addr->ai_protocol)) < 0)
  66. return _error->Errno("socket","Could not create a socket for %s (f=%u t=%u p=%u)",
  67. Name,Addr->ai_family,Addr->ai_socktype,Addr->ai_protocol);
  68. SetNonBlock(Fd,true);
  69. if (connect(Fd,Addr->ai_addr,Addr->ai_addrlen) < 0 &&
  70. errno != EINPROGRESS)
  71. return _error->Errno("connect","Cannot initiate the connection "
  72. "to %s:%s (%s).",Host.c_str(),Service,Name);
  73. /* This implements a timeout for connect by opening the connection
  74. nonblocking */
  75. if (WaitFd(Fd,true,TimeOut) == false)
  76. return _error->Error("Could not connect to %s:%s (%s), "
  77. "connection timed out",Host.c_str(),Service,Name);
  78. // Check the socket for an error condition
  79. unsigned int Err;
  80. unsigned int Len = sizeof(Err);
  81. if (getsockopt(Fd,SOL_SOCKET,SO_ERROR,&Err,&Len) != 0)
  82. return _error->Errno("getsockopt","Failed");
  83. if (Err != 0)
  84. {
  85. errno = Err;
  86. return _error->Errno("connect","Could not connect to %s:%s (%s).",Host.c_str(),
  87. Service,Name);
  88. }
  89. return true;
  90. }
  91. /*}}}*/
  92. // Connect - Connect to a server /*{{{*/
  93. // ---------------------------------------------------------------------
  94. /* Performs a connection to the server */
  95. bool Connect(string Host,int Port,const char *Service,int DefPort,int &Fd,
  96. unsigned long TimeOut,pkgAcqMethod *Owner)
  97. {
  98. if (_error->PendingError() == true)
  99. return false;
  100. // Convert the port name/number
  101. char ServStr[300];
  102. if (Port != 0)
  103. snprintf(ServStr,sizeof(ServStr),"%u",Port);
  104. else
  105. snprintf(ServStr,sizeof(ServStr),"%s",Service);
  106. /* We used a cached address record.. Yes this is against the spec but
  107. the way we have setup our rotating dns suggests that this is more
  108. sensible */
  109. if (LastHost != Host || LastPort != Port)
  110. {
  111. Owner->Status("Connecting to %s",Host.c_str());
  112. // Free the old address structure
  113. if (LastHostAddr != 0)
  114. {
  115. freeaddrinfo(LastHostAddr);
  116. LastHostAddr = 0;
  117. LastUsed = 0;
  118. }
  119. // We only understand SOCK_STREAM sockets.
  120. struct addrinfo Hints;
  121. memset(&Hints,0,sizeof(Hints));
  122. Hints.ai_socktype = SOCK_STREAM;
  123. Hints.ai_protocol = 0;
  124. // Resolve both the host and service simultaneously
  125. while (1)
  126. {
  127. int Res;
  128. if ((Res = getaddrinfo(Host.c_str(),ServStr,&Hints,&LastHostAddr)) != 0 ||
  129. LastHostAddr == 0)
  130. {
  131. if (Res == EAI_NONAME || Res == EAI_SERVICE)
  132. {
  133. if (DefPort != 0)
  134. {
  135. snprintf(ServStr,sizeof(ServStr),"%u",DefPort);
  136. DefPort = 0;
  137. continue;
  138. }
  139. return _error->Error("Could not resolve '%s'",Host.c_str());
  140. }
  141. return _error->Error("Something wicked happened resolving '%s:%s' (%i)",
  142. Host.c_str(),ServStr,Res);
  143. }
  144. break;
  145. }
  146. LastHost = Host;
  147. LastPort = Port;
  148. }
  149. // When we have an IP rotation stay with the last IP.
  150. struct addrinfo *CurHost = LastHostAddr;
  151. if (LastUsed != 0)
  152. CurHost = LastUsed;
  153. while (CurHost != 0)
  154. {
  155. if (DoConnect(CurHost,Host,TimeOut,Fd,Owner) == true)
  156. {
  157. LastUsed = CurHost;
  158. return true;
  159. }
  160. close(Fd);
  161. Fd = -1;
  162. // Ignore UNIX domain sockets
  163. do
  164. {
  165. CurHost = CurHost->ai_next;
  166. }
  167. while (CurHost != 0 && CurHost->ai_family == AF_UNIX);
  168. /* If we reached the end of the search list then wrap around to the
  169. start */
  170. if (CurHost == 0 && LastUsed != 0)
  171. CurHost = LastHostAddr;
  172. // Reached the end of the search cycle
  173. if (CurHost == LastUsed)
  174. break;
  175. if (CurHost != 0)
  176. _error->Discard();
  177. }
  178. if (_error->PendingError() == true)
  179. return false;
  180. return _error->Error("Unable to connect to %s %s:",Host.c_str(),ServStr);
  181. }
  182. /*}}}*/