comparison src/context.cpp @ 316:f7c5cfb76e86

better smtp verify logging
author Carl Byington <carl@five-ten-sg.com>
date Wed, 21 Sep 2016 16:47:20 -0700
parents ef5a6099cbe7
children e2dc882839f6
comparison
equal deleted inserted replaced
315:6fbe3c81376b 316:f7c5cfb76e86
90 const time_t ERROR_SMTP_SOCKET_TIME = 600; // number of seconds between attempts to open a socket to an smtp server 90 const time_t ERROR_SMTP_SOCKET_TIME = 600; // number of seconds between attempts to open a socket to an smtp server
91 91
92 92
93 int SMTP::writer() { 93 int SMTP::writer() {
94 #ifdef VERIFY_DEBUG 94 #ifdef VERIFY_DEBUG
95 log("writer(%d) sees buffer with %s", buffer); 95 log("writer(%d) sees buffer with '%s'", buffer);
96 log("writer(%d) sees error %d", (int)error);
97 #endif 96 #endif
98 int rs = 0; 97 int rs = 0;
99 if (!error) { 98 if (!error) {
100 int len = strlen(buffer); 99 int len = strlen(buffer);
101 while (rs < len) { 100 while (rs < len) {
109 error = true; 108 error = true;
110 break; 109 break;
111 } 110 }
112 } 111 }
113 } 112 }
113 #ifdef VERIFY_DEBUG
114 log("writer(%d) sees error %d", (int)error);
115 #endif
114 return rs; 116 return rs;
115 } 117 }
116 118
117 119
118 int SMTP::reader() { 120 int SMTP::reader() {
136 break; 138 break;
137 } 139 }
138 } 140 }
139 buffer[pending] = '\0'; 141 buffer[pending] = '\0';
140 #ifdef VERIFY_DEBUG 142 #ifdef VERIFY_DEBUG
141 log("reader(%d) sees buffer with %s", buffer); 143 log("reader(%d) sees buffer with '%s'", buffer);
142 #endif 144 #endif
143 return pending; 145 return pending;
144 } 146 }
145 147
146 148
164 int SMTP::read_response() { 166 int SMTP::read_response() {
165 pending = 0; 167 pending = 0;
166 buffer[pending] = '\0'; 168 buffer[pending] = '\0';
167 while (true) { 169 while (true) {
168 int r = read_line(); 170 int r = read_line();
169 #ifdef VERIFY_DEBUG 171 log("verify::read_response(%d) sees line with '%s'", buffer);
170 log("read_response(%d) sees line with %s", buffer);
171 log("read_response(%d) sees line length %d", r);
172 #endif
173 if (r == 0) return 0; // failed to read any bytes 172 if (r == 0) return 0; // failed to read any bytes
174 if ((r > 4) && (buffer[3] == '-')) { 173 if ((r > 4) && (buffer[3] == '-')) {
175 flush_line(r); 174 flush_line(r);
176 continue; 175 continue;
177 } 176 }
178 log("verify::read_response(%d) sees line with %s", buffer);
179 return atoi(buffer); 177 return atoi(buffer);
180 } 178 }
181 return 0; 179 return 0;
182 } 180 }
183 181
201 return cmd(NULL); 199 return cmd(NULL);
202 } 200 }
203 201
204 202
205 int SMTP::rset() { 203 int SMTP::rset() {
206 int rc = cmd("RSET");
207 efrom[0] = '\0'; 204 efrom[0] = '\0';
208 return rc; 205 return cmd("RSET");
209 } 206 }
210 207
211 208
212 int SMTP::from(const char *f) { 209 int SMTP::from(const char *f) {
213 // the mail from address was originally passed in from sendmail enclosed in 210 // the mail from address was originally passed in from sendmail enclosed in
214 // <>. to_lower_string() removed the <> and converted the rest to lowercase, 211 // <>. to_lower_string() removed the <> and converted the rest to lowercase,
215 // except in the case of an empty return path, which was left as the two 212 // except in the case of an empty return path, which was left as the two
216 // character string <>. 213 // character string <>.
217 if (strncmp(efrom, f, maxlen)) { 214 if (strncmp(efrom, f, maxlen)) {
218 rset();
219 strncpy(efrom, f, maxlen); 215 strncpy(efrom, f, maxlen);
220 efrom[maxlen-1] = '\0'; // ensure null termination 216 efrom[maxlen-1] = '\0'; // ensure null termination
221 init(); 217 init();
222 append("MAIL FROM:<"); 218 append("MAIL FROM:<");
223 if (*f != '<') append(f); 219 if (*f != '<') append(f);
325 #endif 321 #endif
326 break; 322 break;
327 } 323 }
328 } 324 }
329 pthread_mutex_unlock(&mutex); 325 pthread_mutex_unlock(&mutex);
330 if (conn) return conn; 326 if (conn) {
327 int rc = conn->rset();
328 conn->log("verify::getconnection(%d) rset sees %d", rc);
329 if (rc == 250) return conn;
330 put_connection(conn);
331 return NULL;
332 }
331 int sock = NULL_SOCKET; 333 int sock = NULL_SOCKET;
332 if ((time(NULL) - last_err) > ERROR_SMTP_SOCKET_TIME) { 334 if ((time(NULL) - last_err) > ERROR_SMTP_SOCKET_TIME) {
333 // nothing recent, maybe this time it will work 335 // nothing recent, maybe this time it will work
334 hostent *h = gethostbyname(host); 336 hostent *h = gethostbyname(host);
335 if (h) { 337 if (h) {
354 if (sock != NULL_SOCKET) { 356 if (sock != NULL_SOCKET) {
355 struct timeval tv; 357 struct timeval tv;
356 tv.tv_sec = 15; 358 tv.tv_sec = 15;
357 tv.tv_usec = 0; 359 tv.tv_usec = 0;
358 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); 360 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
361 setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
359 conn = new SMTP(sock); 362 conn = new SMTP(sock);
360 conn->set_id(queueid); 363 conn->set_id(queueid);
361 #ifdef VERIFY_DEBUG 364 #ifdef VERIFY_DEBUG
362 conn->log("get_connection(%d) new socket %s", ""); 365 conn->log("get_connection(%d) new socket %s", "");
363 #endif 366 #endif