- int dbgiter; char dbgbuf[2048]; int dbgbuflen = 0;
-#endif
-
- /* NOTE: do not call syslog() (unless you are about to return) before
- * we take a copy of the h_addr_list.
- */
-
- /* only one set of connection targets can be used. assert this */
- if (argaddr == NULL && hent == NULL) {
- syslog (LOG_ERR, "oops! both NULL in try_to_connect");
- return EX_SOFTWARE;
- } else if (argaddr != NULL && hent != NULL) {
- syslog (LOG_ERR, "oops! both non-NULL in try_to_connect");
- return EX_SOFTWARE;
- }
-
- /* take a copy of the h_addr_list part of the struct hostent */
- if (hent != NULL) {
- memset (inaddrlist, 0, sizeof(inaddrlist));
-
- for (hostnum=0; hent->h_addr_list[hostnum] != 0; hostnum++) {
-
-#ifdef DO_CONNECT_DEBUG_SYSLOGS
- dbgbuflen += snprintf (dbgbuf+dbgbuflen, 2047-dbgbuflen,
- "[%d %lx: %d.%d.%d.%d]",
- hostnum, hent->h_addr_list[hostnum],
- hent->h_addr_list[hostnum][0],
- hent->h_addr_list[hostnum][1],
- hent->h_addr_list[hostnum][2],
- hent->h_addr_list[hostnum][3]);
-#endif
-
- if (hostnum > 255) {
- syslog (LOG_ERR, "too many address in hostent (%d), ignoring others",
- hostnum);
- break;
- }
-
- if (hent->h_addr_list[hostnum] == NULL) {
- /* shouldn't happen */
- syslog (LOG_ERR, "hent->h_addr_list[hostnum] == NULL! foo!");
- return EX_SOFTWARE;
- }
-
-#ifdef DO_CONNECT_DEBUG_SYSLOGS
- dbgbuflen += snprintf (dbgbuf+dbgbuflen, 2047-dbgbuflen,
- "[%d: %d.%d.%d.%d] ", sizeof (struct in_addr),
- hent->h_addr_list[hostnum][0],
- hent->h_addr_list[hostnum][1],
- hent->h_addr_list[hostnum][2],
- hent->h_addr_list[hostnum][3]);
-#endif
-
- memcpy ((void *) &(inaddrlist[hostnum]),
- (void *) hent->h_addr_list[hostnum],
- sizeof (struct in_addr));
- }
-
-#ifdef DO_CONNECT_DEBUG_SYSLOGS
- syslog (LOG_DEBUG, "dbg: %d %s", hostnum, dbgbuf); dbgbuflen = 0;
-#endif
- }
-
-
-#ifdef DO_CONNECT_DEBUG_SYSLOGS
- for (dbgiter = 0; dbgiter < hostnum; dbgiter++) {
- syslog (LOG_DEBUG, "dbg: host addr %d/%d = %lx at %lx", dbgiter, hostnum,
- inaddrlist[dbgiter].s_addr, &(inaddrlist[dbgiter]));
- }
-#endif
-
- hent = NULL; /* cannot use hent after this point, syslog() may overwrite it */
-
-#ifdef DO_CONNECT_DEBUG_SYSLOGS
- syslog (LOG_DEBUG, "dbg: socket");
-#endif
-
- if(-1 == (mysock = socket(PF_INET,SOCK_STREAM,0)))
- {
- origerr = errno; /* take a copy before syslog() */
- syslog (LOG_ERR, "socket() to spamd failed: %m");
- switch(origerr)
- {
- case EPROTONOSUPPORT:
- case EINVAL:
- return EX_SOFTWARE;
- case EACCES:
- return EX_NOPERM;
- case ENFILE:
- case EMFILE:
- case ENOBUFS:
- case ENOMEM:
- return EX_OSERR;
- default:
- return EX_SOFTWARE;
- }
- }
-
-#ifdef USE_TCP_NODELAY
- /* TODO: should this be up above the connect()? */
- value = 1; /* make this explicit! */
- if(-1 == setsockopt(mysock,0,TCP_NODELAY,&value,sizeof(value)))
- {
- switch(errno)
- {
- case EBADF:
- case ENOTSOCK:
- case ENOPROTOOPT:
- case EFAULT:
- syslog (LOG_ERR, "setsockopt() to spamd failed: %m");
- close (mysock);
- return EX_SOFTWARE;
-
- default:
- break; /* ignored */
- }
- }
-#endif
-
- for (numloops=0; numloops < MAX_CONNECT_RETRIES; numloops++) {
-
-#ifdef DO_CONNECT_DEBUG_SYSLOGS
- syslog (LOG_DEBUG, "dbg: connect() to spamd %d", numloops);
-#endif
-
- if (argaddr != NULL) {
- addr = (struct sockaddr_in *) argaddr; /* use the one provided */
-
-#ifdef DO_CONNECT_DEBUG_SYSLOGS
- syslog (LOG_DEBUG, "dbg: using argaddr");
-#endif
-
- } else {
- /* cycle through the addrs in hent */
- memset(&addrbuf, 0, sizeof(addrbuf));
- addrbuf.sin_family=AF_INET;
- addrbuf.sin_port=htons(hent_port);
-
- if (sizeof(addrbuf.sin_addr) != sizeof(struct in_addr)) { /* shouldn't happen */
- syslog (LOG_ERR,
- "foo! sizeof(sockaddr.sin_addr) != sizeof(struct in_addr)");
- return EX_SOFTWARE;
- }
-
-#ifdef DO_CONNECT_DEBUG_SYSLOGS
- syslog (LOG_DEBUG, "dbg: cpy addr %d/%d at %lx",
- numloops%hostnum, hostnum, &(inaddrlist[numloops % hostnum]));
-#endif
-
- memcpy (&addrbuf.sin_addr, &(inaddrlist[numloops % hostnum]),
- sizeof(addrbuf.sin_addr));
- addr = &addrbuf;
-
-#ifdef DO_CONNECT_DEBUG_SYSLOGS
- syslog (LOG_DEBUG, "dbg: conn addr %d/%d = %lx",
- numloops%hostnum, hostnum, addrbuf.sin_addr.s_addr);