/[svn]/hopm/trunk/src/irc.c
ViewVC logotype

Diff of /hopm/trunk/src/irc.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 5362 by michael, Sun Jan 11 18:51:07 2015 UTC revision 5363 by michael, Mon Jan 12 19:19:11 2015 UTC
# Line 26  Line 26 
26  #include <string.h>  #include <string.h>
27  #include <sys/types.h>  #include <sys/types.h>
28  #include <sys/socket.h>  #include <sys/socket.h>
29    #include <netdb.h>
30  #include <netinet/in.h>  #include <netinet/in.h>
31  #include <arpa/inet.h>  #include <arpa/inet.h>
32  #include <poll.h>  #include <poll.h>
# Line 82  static char          IRC_RAW[MSGLENMAX]; Line 83  static char          IRC_RAW[MSGLENMAX];
83  static unsigned int  IRC_RAW_LEN    = 0;         /* Position of IRC_RAW                   */  static unsigned int  IRC_RAW_LEN    = 0;         /* Position of IRC_RAW                   */
84  static int           IRC_FD         = 0;         /* File descriptor for IRC client        */  static int           IRC_FD         = 0;         /* File descriptor for IRC client        */
85    
86  static struct sockaddr_in IRC_SVR;              /* Sock Address Struct for IRC server    */  static struct sockaddr_storage IRC_SVR;          /* Sock Address Struct for IRC server    */
 static struct in_addr IRC_LOCAL;                /* Sock Address Struct for Bind          */  
87    
88  static time_t IRC_LAST = 0;                      /* Last full line of data from irc server*/  static time_t IRC_LAST = 0;                      /* Last full line of data from irc server*/
89  static time_t IRC_LASTRECONNECT = 0;             /* Time of last reconnection */  static time_t IRC_LASTRECONNECT = 0;             /* Time of last reconnection */
# Line 168  irc_cycle(void) Line 168  irc_cycle(void)
168  static void  static void
169  irc_init(void)  irc_init(void)
170  {  {
171    struct sockaddr_in bsaddr;    const void *address = NULL;
   struct in_addr *irc_host;  
172    
173    if (IRC_FD)    if (IRC_FD)
174      close(IRC_FD);      close(IRC_FD);
175    
176    memset(&IRC_SVR, 0, sizeof(IRC_SVR));    memset(&IRC_SVR, 0, sizeof(IRC_SVR));
   memset(&IRC_LOCAL, 0, sizeof(IRC_LOCAL));  
   memset(&bsaddr, 0, sizeof(bsaddr));  
177    
178    /* Resolve IRC host. */    /* Resolve IRC host. */
179    if ((irc_host = firedns_resolveip4(IRCItem->server)) == NULL)    if ((address = firedns_resolveip6(IRCItem->server)))
180    {    {
181      log_printf("IRC -> firedns_resolveip4(\"%s\"): %s", IRCItem->server,      struct sockaddr_in6 *in = (struct sockaddr_in6 *)&IRC_SVR;
                firedns_strerror(fdns_errno));  
     exit(EXIT_FAILURE);  
   }  
182    
183    IRC_SVR.sin_family = AF_INET;      IRC_SVR.ss_family = AF_INET6;
184    IRC_SVR.sin_port = htons(IRCItem->port);      in->sin6_port = htons(IRCItem->port);
185    IRC_SVR.sin_addr = *irc_host;      memcpy(&in->sin6_addr, address, sizeof(in->sin6_addr));
186      }
187      else if ((address = firedns_resolveip4(IRCItem->server)))
188      {
189        struct sockaddr_in *in = (struct sockaddr_in *)&IRC_SVR;
190    
191    if (IRC_SVR.sin_addr.s_addr == INADDR_NONE)      IRC_SVR.ss_family = AF_INET;
192        in->sin_port = htons(IRCItem->port);
193        memcpy(&in->sin_addr, address, sizeof(in->sin_addr));
194      }
195      else
196    {    {
197      log_printf("IRC -> Unknown error resolving remote host (%s)",      log_printf("IRC -> firedns_resolveip(\"%s\"): %s", IRCItem->server,
198                 IRCItem->server);                 firedns_strerror(fdns_errno));
199      exit(EXIT_FAILURE);      exit(EXIT_FAILURE);
200    }    }
201    
202    /* Request file desc for IRC client socket */    /* Request file desc for IRC client socket */
203    IRC_FD = socket(AF_INET, SOCK_STREAM, 0);    IRC_FD = socket(IRC_SVR.ss_family, SOCK_STREAM, 0);
204    
205    if (IRC_FD == -1)    if (IRC_FD == -1)
206    {    {
# Line 209  irc_init(void) Line 211  irc_init(void)
211    /* Bind */    /* Bind */
212    if (!EmptyString(IRCItem->vhost))    if (!EmptyString(IRCItem->vhost))
213    {    {
214      if (inet_pton(AF_INET, IRCItem->vhost, &IRC_LOCAL.s_addr) <= 0)      struct addrinfo hints, *res;
215    
216        memset(&hints, 0, sizeof(hints));
217    
218        hints.ai_family = AF_UNSPEC;
219        hints.ai_socktype = SOCK_STREAM;
220        hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
221    
222        if (getaddrinfo(IRCItem->vhost, NULL, &hints, &res))
223      {      {
224        log_printf("IRC -> bind(): %s is an invalid address", IRCItem->vhost);        log_printf("IRC -> bind(): %s is an invalid address", IRCItem->vhost);
225        exit(EXIT_FAILURE);        exit(EXIT_FAILURE);
226      }      }
227        else if (bind(IRC_FD, res->ai_addr, res->ai_addrlen))
     bsaddr.sin_addr.s_addr = IRC_LOCAL.s_addr;  
     bsaddr.sin_family = AF_INET;  
     bsaddr.sin_port = htons(0);  
   
     if (bind(IRC_FD, (struct sockaddr *)&bsaddr, sizeof(bsaddr)))  
228      {      {
229        log_printf("IRC -> bind(): error binding to %s: %s", IRCItem->vhost, strerror(errno));        log_printf("IRC -> bind(): error binding to %s: %s", IRCItem->vhost, strerror(errno));
230        exit(EXIT_FAILURE);        exit(EXIT_FAILURE);
231      }      }
232    
233        freeaddrinfo(res);
234    }    }
235  }  }
236    

Legend:
Removed from v.5362  
changed lines
  Added in v.5363

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28