masqmail-0.2

view src/readsock.c @ 179:ec3fe72a3e99

Fixed an important bug with folded headers! g_strconcat() returns a *copy* of the string, but hdr->value still pointed to the old header (which probably was a memory leak, too). If the folded part had been quite small it was likely that the new string was at the same position as the old one, thus making everything go well. But if pretty long headers were folded several times it was likely that the new string was allocated somewhere else in memory, thus breaking things. In result mails to lots of recipients (folded header) were frequently only sent to the ones in the first line. Sorry for the inconvenience.
author meillo@marmaro.de
date Fri, 03 Jun 2011 09:52:17 +0200
parents f671821d8222
children
line source
1 /* MasqMail
2 Copyright (C) 2000 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include <signal.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <setjmp.h>
23 #include <unistd.h>
24 #include <ctype.h>
26 #include "readsock.h"
27 /*#include "masqmail.h"*/
29 jmp_buf jmp_timeout;
31 static void
32 sig_timeout_handler(int sig)
33 {
34 longjmp(jmp_timeout, 1);
35 }
37 static struct sigaction old_sa_alrm;
39 static void
40 alarm_on(int timeout)
41 {
42 struct sigaction sa;
44 sa.sa_handler = sig_timeout_handler;
45 sigemptyset(&(sa.sa_mask));
46 sa.sa_flags = 0;
47 sigaction(SIGALRM, &sa, &old_sa_alrm);
49 if (timeout > 0)
50 alarm(timeout);
51 }
53 static void
54 alarm_off()
55 {
56 alarm(0);
58 sigaction(SIGALRM, &old_sa_alrm, NULL);
59 }
61 static void
62 _read_chug(FILE * in)
63 {
64 int c = 0;
66 c = fgetc(in);
67 while (isspace(c) && (c != EOF))
68 c = fgetc(in);
69 ungetc(c, in);
70 }
72 static int
73 _read_line(FILE * in, char *buf, int buf_len, int timeout)
74 {
75 int p = 0;
76 int c = 0;
78 c = fgetc(in);
79 while ((c != '\n') && (c != EOF) && (p < buf_len - 1)) {
80 buf[p++] = c;
81 c = fgetc(in);
82 }
84 buf[p] = '\0';
86 if (c == EOF)
87 return -1;
88 else if (p >= buf_len) {
89 ungetc(c, in);
90 return -2;
91 }
93 buf[p++] = c; /* \n */
94 buf[p] = '\0';
96 return p;
97 }
99 int
100 read_sockline(FILE * in, char *buf, int buf_len, int timeout, unsigned int flags)
101 {
102 int p = 0;
104 if (setjmp(jmp_timeout) != 0) {
105 alarm_off();
106 return -3;
107 }
109 alarm_on(timeout);
111 /* strip leading spaces */
112 if (flags & READSOCKL_CHUG) {
113 _read_chug(in);
114 }
116 p = _read_line(in, buf, buf_len, timeout);
118 alarm_off();
120 if (p > 1) {
121 /* here we are sure that buf[p-1] == '\n' */
122 if (flags & READSOCKL_CVT_CRLF) {
123 if ((buf[p - 2] == '\r') && (buf[p - 1] == '\n')) {
124 buf[p - 2] = '\n';
125 buf[p - 1] = 0;
126 p--;
127 }
128 }
129 }
130 return p;
131 }
133 int
134 read_sockline1(FILE * in, char **pbuf, int *buf_len, int timeout, unsigned int flags)
135 {
136 int p = 0, size = *buf_len;
137 char *buf;
139 if (setjmp(jmp_timeout) != 0) {
140 alarm_off();
141 return -3;
142 }
144 alarm_on(timeout);
146 /* strip leading spaces */
147 if (flags & READSOCKL_CHUG) {
148 _read_chug(in);
149 }
151 if (!*pbuf)
152 *pbuf = g_malloc(size);
153 buf = *pbuf;
155 while (1) {
156 int pp;
158 pp = _read_line(in, buf, size, timeout);
159 if (pp == -2) {
160 *pbuf = realloc(*pbuf, *buf_len + size);
161 buf = *pbuf + *buf_len;
162 *buf_len += size;
163 p += size;
164 } else {
165 if (pp > 0)
166 p += pp;
167 else
168 p = pp;
169 break;
170 }
171 }
173 alarm_off();
175 if (p > 1) {
176 buf = *pbuf;
177 /* here we are sure that buf[p-1] == '\n' */
178 if (flags & READSOCKL_CVT_CRLF) {
179 if ((buf[p - 2] == '\r') && (buf[p - 1] == '\n')) {
180 buf[p - 2] = '\n';
181 buf[p - 1] = '\0';
182 p--;
183 }
184 }
185 }
186 return p;
187 }