#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
+#include <assert.h>
+
#include <libcitadel.h>
#include "citadel.h"
#include "server.h"
IO->SendBuf.fd = 0;
IO->RecvBuf.fd = 0;
}
-
+ assert(IO->Terminate);
IO->Terminate(IO);
}
eReadState HandleInbound(AsyncIO *IO)
{
eReadState Finished = eBufferNotEmpty;
-
+
while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
if (IO->RecvBuf.nBlobBytesWanted != 0) {
}
if (Finished != eMustReadMore) {
+ assert(IO->ReadDone);
ev_io_stop(event_base, &IO->recv_event);
IO->NextState = IO->ReadDone(IO);
Finished = StrBufCheckBuffer(&IO->RecvBuf);
if ((IO->NextState == eSendReply) ||
(IO->NextState == eSendMore))
{
+ assert(IO->SendDone);
IO->NextState = IO->SendDone(IO);
ev_io_start(event_base, &IO->send_event);
}
case eSendReply:
break;
case eSendMore:
+ assert(IO->SendDone);
IO->NextState = IO->SendDone(IO);
if ((IO->NextState == eTerminateConnection) ||
break;
}
}
- else if (rc < 0)
+ else if (rc < 0) {
+ assert(IO->Timeout);
IO->Timeout(IO);
+ }
/* else : must write more. */
}
static void
AsyncIO *IO = watcher->data;
ev_timer_stop (event_base, &IO->rw_timeout);
+ assert(IO->Timeout);
IO->Timeout(IO);
}
static void
ev_timer_stop (event_base, &IO->conn_fail);
ev_io_stop(loop, &IO->conn_event);
+ assert(IO->ConnFail);
IO->ConnFail(IO);
}
static void
if (nbytes > 0) {
HandleInbound(IO);
} else if (nbytes == 0) {
+ assert(IO->Timeout);
IO->Timeout(IO); /* this is a timeout... */
return;
} else if (nbytes == -1) {
-int event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
+eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
{
int fdflags;
int rc = -1;
CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
// freeaddrinfo(res);
- return -1;
+ return eAbort;
}
fdflags = fcntl(IO->sock, F_GETFL);
if (fdflags < 0) {
"EVENT: unable to get socket flags! %s \n",
strerror(errno));
StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
- return -1;
+ return eAbort;
}
fdflags = fdflags | O_NONBLOCK;
if (fcntl(IO->sock, F_SETFL, fdflags) < 0) {
strerror(errno));
StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
close(IO->sock);
- return -1;
+ return eAbort;
}
/* TODO: maye we could use offsetof() to calc the position of data...
* http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
IO->conn_fail.data = IO;
ev_timer_init(&IO->rw_timeout, IO_Timout_callback, first_rw_timeout, 0);
IO->rw_timeout.data = IO;
-
- rc = connect(IO->sock,
- (struct sockaddr *) &IO->Addr,
- (IO->IP6)? ///HEnt->h_addrtype == AF_INET6)?
- sizeof(struct in6_addr):
- sizeof(struct sockaddr_in));
+ ///struct sockaddr_in *addr = &IO->Addr;
+ if (IO->IP6)
+ rc = connect(IO->sock, &IO->Addr, sizeof(struct in6_addr));
+ else
+ rc = connect(IO->sock, (struct sockaddr_in *)&IO->Addr, sizeof(struct sockaddr_in));
+
if (rc >= 0){
//// freeaddrinfo(res);
set_start_callback(event_base, IO, 0);
ev_timer_start(event_base, &IO->rw_timeout);
- return 0;
+ return IO->NextState;
}
else if (errno == EINPROGRESS) {
ev_io_start(event_base, &IO->conn_event);
ev_timer_start(event_base, &IO->conn_fail);
- return 0;
+ return IO->NextState;
}
else {
CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
+ assert(IO->ConnFail);
IO->ConnFail(IO);
- return -1;
+ return eAbort;
}
+ return IO->NextState;
}
void SetNextTimeout(AsyncIO *IO, double timeout)
ev_timer_again (event_base, &IO->rw_timeout);
}
-void InitEventIO(AsyncIO *IO,
- void *pData,
- double conn_timeout,
- double first_rw_timeout,
- int ReadFirst)
+eNextState InitEventIO(AsyncIO *IO,
+ void *pData,
+ double conn_timeout,
+ double first_rw_timeout,
+ int ReadFirst)
{
IO->Data = pData;
}
IO->IP6 = IO->HEnt->h_addrtype == AF_INET6;
// IO->res = HEnt->h_addr_list[0];
- event_connect_socket(IO, conn_timeout, first_rw_timeout);
+ return event_connect_socket(IO, conn_timeout, first_rw_timeout);
}
eMaxSMTPC
} eSMTP_C_States;
-const double SMTP_C_ConnTimeout = 60.; /* wail 1 minute for connections... */
+const double SMTP_C_ConnTimeout = 300.; /* wail 1 minute for connections... */
const double SMTP_C_ReadTimeouts[eMaxSMTPC] = {
300., /* Greeting... */
30., /* EHLO */
#define SMTP_IS_STATE(WHICH_STATE) (ChrPtr(SendMsg->IO.IOBuf)[0] == WHICH_STATE)
-#define SMTP_DBG_SEND() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: > %s\n", SendMsg->n, ChrPtr(SendMsg->IO.IOBuf))
+#define SMTP_DBG_SEND() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: > %s\n", SendMsg->n, ChrPtr(SendMsg->IO.SendBuf.Buf))
#define SMTP_DBG_READ() CtdlLogPrintf(CTDL_DEBUG, "SMTP client[%ld]: < %s\n", SendMsg->n, ChrPtr(SendMsg->IO.IOBuf))
}
+void SetConnectStatus(AsyncIO *IO)
+{
+
+ SmtpOutMsg *SendMsg = IO->Data;
+ char buf[256];
+ void *src;
+
+ buf[0] = '\0';
+
+ if (IO->IP6) {
+ src = &IO->Addr.sin6_addr;
+ }
+ else {
+ unsigned long psaddr;
+ struct sockaddr_in *addr = (struct sockaddr_in *)&IO->Addr;
+
+ src = &addr->sin_addr.s_addr;
+ memcpy(&psaddr, &addr->sin_addr.s_addr, sizeof(psaddr));
+/// psaddr = ntohl(psaddr);
+/*
+ CtdlLogPrintf(CTDL_DEBUG,
+ "SMTP client[%ld]: connecting to %s [%ld.%ld.%ld.%ld:%d] ...\n",
+ SendMsg->n,
+ SendMsg->mx_host,
+ (psaddr >> 24) & 0xFF,
+ (psaddr >> 16) & 0xFF,
+ (psaddr >> 8) & 0xFF,
+ (psaddr >> 0) & 0xFF,
+ SendMsg->IO.dport);
+
+ SendMsg->MyQEntry->Status = 5;
+ StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
+ "Timeout while connecting %s [%ld.%ld.%ld.%ld:%d] ",
+ SendMsg->mx_host,
+ (psaddr >> 24) & 0xFF,
+ (psaddr >> 16) & 0xFF,
+ (psaddr >> 8) & 0xFF,
+ (psaddr >> 0) & 0xFF,
+ SendMsg->IO.dport);
+
+*/
+ }
+
+ inet_ntop((IO->IP6)?AF_INET6:AF_INET,
+ src,
+ buf, sizeof(buf));
+
+ CtdlLogPrintf(CTDL_DEBUG,
+ "SMTP client[%ld]: connecting to %s [%s]:%d ...\n",
+ SendMsg->n,
+ SendMsg->mx_host,
+ buf,
+ SendMsg->IO.dport);
+
+ SendMsg->MyQEntry->Status = 5;
+ StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
+ "Timeout while connecting %s [%s]:%d ",
+ SendMsg->mx_host,
+ buf,
+ SendMsg->IO.dport);
+}
eNextState mx_connect_relay_ip(AsyncIO *IO)
{
else {
struct sockaddr_in *addr = (struct sockaddr_in*) &IO->Addr;
/* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
- memcpy(&addr->sin_addr,
+ memcpy(&addr->sin_addr,///.s_addr,
&SendMsg->pCurrRelay->Addr,
sizeof(struct in_addr));
addr->sin_port = htons(IO->dport);
}
+ SetConnectStatus(IO);
-
-/*
-
- SendMsg->pCurrRelay->
- if ((SendMsg->pCurrRelay != NULL) && (SendMsg->pCurrRelay->IsIP)) {
- connect = 1;
-
- }
- unsigned long psaddr;
-
-
- memcpy(&psaddr, hostent->h_addr_list[0], sizeof(psaddr));
- psaddr = ntohl(psaddr);
-
- CtdlLogPrintf(CTDL_DEBUG,
- "SMTP client[%ld]: connecting to %s [%ld.%ld.%ld.%ld:%d] ...\n",
- SendMsg->n,
- SendMsg->mx_host,
- (psaddr >> 24) & 0xFF,
- (psaddr >> 16) & 0xFF,
- (psaddr >> 8) & 0xFF,
- (psaddr >> 0) & 0xFF,
- SendMsg->IO.dport);
-
- SendMsg->MyQEntry->Status = 5;
- StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
- "Timeout while connecting %s [%ld.%ld.%ld.%ld:%d] ",
- SendMsg->mx_host,
- (psaddr >> 24) & 0xFF,
- (psaddr >> 16) & 0xFF,
- (psaddr >> 8) & 0xFF,
- (psaddr >> 0) & 0xFF,
- SendMsg->IO.dport);
-
-
- }
-*/
- /// TODO: else fail!
- InitEventIO(IO, SendMsg,
- SMTP_C_ConnTimeout,
- SMTP_C_ReadTimeouts[0],
+ return InitEventIO(IO, SendMsg,
+ SMTP_C_ConnTimeout,
+ SMTP_C_ReadTimeouts[0],
1);
}
{
AsyncIO *IO = (AsyncIO *) Ctx;
SmtpOutMsg *SendMsg = IO->Data;
+ eNextState State = eAbort;
if ((status == ARES_SUCCESS) && (hostent != NULL) ) {
- IO->IP6 = hostent->h_addrtype == AF_INET6;
-
+ IO->IP6 = hostent->h_addrtype == AF_INET6;
+ IO->HEnt = hostent;
+
memset(&IO->Addr, 0, sizeof(struct in6_addr));
if (IO->IP6) {
memcpy(&IO->Addr.sin6_addr.s6_addr,
- IO->HEnt->h_addr_list[0],
+ &hostent->h_addr_list[0],
sizeof(struct in6_addr));
IO->Addr.sin6_family = hostent->h_addrtype;
else {
struct sockaddr_in *addr = (struct sockaddr_in*) &IO->Addr;
/* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
- memcpy(&addr->sin_addr,
- IO->HEnt->h_addr_list[0],
- sizeof(struct in_addr));
+// addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
+ memcpy(&addr->sin_addr.s_addr, hostent->h_addr_list[0], sizeof(uint32_t));
addr->sin_family = hostent->h_addrtype;
addr->sin_port = htons(IO->dport);
}
SendMsg->IO.HEnt = hostent;
- InitEventIO(IO, SendMsg,
- SMTP_C_ConnTimeout,
- SMTP_C_ReadTimeouts[0],
- 1);
+ SetConnectStatus(IO);
+ State = InitEventIO(IO, SendMsg,
+ SMTP_C_ConnTimeout,
+ SMTP_C_ReadTimeouts[0],
+ 1);
}
+ if (State == eAbort)
+ SMTP_C_Terminate(IO);
}
const unsigned short DefaultMXPort = 25;
eNextState get_one_mx_host_ip(AsyncIO *IO)
{
SmtpOutMsg * SendMsg = IO->Data;
-
+ const char *Hostname;
//char *endpart;
//char buf[SIZ];
+ InitC_ares_dns(IO);
- CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
- SendMsg->IO.dport = DefaultMXPort;
+ if (SendMsg->CurrMX) {
+ SendMsg->mx_host = SendMsg->CurrMX->host;
+ SendMsg->CurrMX = SendMsg->CurrMX->next;
+ }
- SendMsg->mx_host = SendMsg->CurrMX->host;
- SendMsg->CurrMX = SendMsg->CurrMX->next;
+ if (SendMsg->pCurrRelay != NULL) Hostname = SendMsg->pCurrRelay->Host;
+ else if (SendMsg->mx_host != NULL) Hostname = SendMsg->mx_host;
+ else Hostname = SendMsg->node;
+
+ CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
CtdlLogPrintf(CTDL_DEBUG,
"SMTP client[%ld]: looking up %s : %d ...\n",
SendMsg->n,
- SendMsg->mx_host,
+ Hostname,
SendMsg->IO.dport);
ares_gethostbyname(SendMsg->IO.DNSChannel,
- SendMsg->mx_host,
+ Hostname,
AF_INET6, /* it falls back to ipv4 in doubt... */
get_one_mx_host_ip_done,
&SendMsg->IO);
+ return IO->NextState;
}
SendMsg->CurrMX = SendMsg->AllMX = IO->VParsedDNSReply;
//// TODO: should we remove the current ares context???
get_one_mx_host_ip(IO);
- return 0;
+ return IO->NextState;
}
SendMsg->MyQEntry->Status = 5;
StrBufPrintf(SendMsg->MyQEntry->StatusMessage,
"No MX hosts found for <%s>", SendMsg->node);
- return 0; ///////TODO: abort!
+ return IO->NextState;
}
- return 0;
+ return eAbort;
}
SendMsg = (SmtpOutMsg *) malloc(sizeof(SmtpOutMsg));
memset(SendMsg, 0, sizeof(SmtpOutMsg));
- SendMsg->IO.sock = (-1);
- SendMsg->n = MsgCount++;
- SendMsg->MyQEntry = MyQEntry;
- SendMsg->MyQItem = MyQItem;
- SendMsg->pCurrRelay = MyQItem->URL;
-
+ SendMsg->IO.sock = (-1);
+ SendMsg->IO.NextState = eReadMessage;
+ SendMsg->n = MsgCount++;
+ SendMsg->MyQEntry = MyQEntry;
+ SendMsg->MyQItem = MyQItem;
+ SendMsg->pCurrRelay = MyQItem->URL;
+
+ SendMsg->IO.dport = DefaultMXPort;
SendMsg->IO.Data = SendMsg;
SendMsg->IO.SendDone = SMTP_C_DispatchWriteDone;
SendMsg->IO.ReadDone = SMTP_C_DispatchReadDone;
if (SMTP_IS_STATE('2')) {
SendMsg->State ++;
- if (SendMsg->pCurrRelay->User == NULL)
+ if ((SendMsg->pCurrRelay == NULL) ||
+ (SendMsg->pCurrRelay->User == NULL))
SendMsg->State ++; /* Skip auth... */
}
/* else we fall back to 'helo' */
else
SMTP_VERROR(5);
}
- if (SendMsg->pCurrRelay->User == NULL)
+ if ((SendMsg->pCurrRelay == NULL) ||
+ (SendMsg->pCurrRelay->User == NULL))
SendMsg->State ++; /* Skip auth... */
return eSendReply;
}
char buf[SIZ];
char encoded[1024];
+ if ((SendMsg->pCurrRelay == NULL) ||
+ (SendMsg->pCurrRelay->User == NULL))
+ SendMsg->State ++; /* Skip auth, shouldn't even come here!... */
+ else {
/* Do an AUTH command if necessary */
sprintf(buf, "%s%c%s%c%s",
SendMsg->pCurrRelay->User, '\0',
strlen(SendMsg->pCurrRelay->Pass) + 2, 0);
StrBufPrintf(SendMsg->IO.SendBuf.Buf,
"AUTH PLAIN %s\r\n", encoded);
-
+ }
SMTP_DBG_SEND();
return eReadMessage;
}
CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
SmtpOutMsg *pMsg = IO->Data;
FinalizeMessageSend(pMsg);
- return 0;
+ return eAbort;
}
eNextState SMTP_C_Timeout(AsyncIO *IO)
{
SmtpOutMsg *pMsg = IO->Data;
StrBufPlain(IO->ErrMsg, CKEY(ReadErrors[pMsg->State]));
FinalizeMessageSend(pMsg);
- return 0;
+ return eAbort;
}
eNextState SMTP_C_ConnFail(AsyncIO *IO)
{
CtdlLogPrintf(CTDL_DEBUG, "SMTP: %s\n", __FUNCTION__);
SmtpOutMsg *pMsg = IO->Data;
FinalizeMessageSend(pMsg);
- return 0;
+ return eAbort;
}
int ParseURL(ParsedURL **Url, StrBuf *UrlStr, short DefaultPort)
{
- const char *pch, *pStartHost, *pEndHost, *pPort, *pCredEnd, *pUserEnd;
+ const char *pch, *pEndHost, *pPort, *pCredEnd, *pUserEnd;
ParsedURL *url = (ParsedURL *)malloc(sizeof(ParsedURL));
memset(url, 0, sizeof(ParsedURL));
* http://username:passvoid@[ipv6]:port/url
*/
url->URL = NewStrBufDup(UrlStr);
- pStartHost = pch = ChrPtr(url->URL);
+ url->Host = pch = ChrPtr(url->URL);
url->LocalPart = strchr(pch, '/');
if (url->LocalPart != NULL) {
if ((*(url->LocalPart + 1) == '/') &&
(*(url->LocalPart + 2) == ':')) { /* TODO: find default port for this protocol... */
- pStartHost = url->LocalPart + 3;
- url->LocalPart = strchr(pStartHost, '/');
+ url->Host = url->LocalPart + 3;
+ url->LocalPart = strchr(url->Host, '/');
}
}
if (url->LocalPart == NULL) {
pCredEnd = NULL;
if (pCredEnd != NULL)
{
- url->User = pStartHost;
- pStartHost = pCredEnd + 1;
+ url->User = url->Host;
+ url->Host = pCredEnd + 1;
pUserEnd = strchr(url->User, ':');
if (pUserEnd > pCredEnd)
}
pPort = NULL;
- if (*pStartHost == '[') {
- pStartHost ++;
- pEndHost = strchr(pStartHost, ']');
+ if (*url->Host == '[') {
+ url->Host ++;
+ pEndHost = strchr(url->Host, ']');
if (pEndHost == NULL) {
free(url);
return 0; /* invalid syntax, no ipv6 */
url->af = AF_INET6;
}
else {
- pPort = strchr(pStartHost, ':');
+ pPort = strchr(url->Host, ':');
if (pPort != NULL)
pPort ++;
}
if (pPort != NULL)
url->Port = atol(pPort);
- url->IsIP = inet_pton(url->af, pStartHost, &url->Addr);
+ url->IsIP = inet_pton(url->af, url->Host, &url->Addr);
return 1;
}
/*