rev |
line source |
meillo@367
|
1 /*
|
meillo@367
|
2 ** MasqMail
|
meillo@367
|
3 ** Copyright (C) 2000 Oliver Kurth
|
meillo@367
|
4 **
|
meillo@367
|
5 ** This program is free software; you can redistribute it and/or modify
|
meillo@367
|
6 ** it under the terms of the GNU General Public License as published by
|
meillo@367
|
7 ** the Free Software Foundation; either version 2 of the License, or
|
meillo@367
|
8 ** (at your option) any later version.
|
meillo@367
|
9 **
|
meillo@367
|
10 ** This program is distributed in the hope that it will be useful,
|
meillo@367
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
meillo@367
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
meillo@367
|
13 ** GNU General Public License for more details.
|
meillo@367
|
14 **
|
meillo@367
|
15 ** You should have received a copy of the GNU General Public License
|
meillo@367
|
16 ** along with this program; if not, write to the Free Software
|
meillo@367
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
meillo@0
|
18 */
|
meillo@0
|
19
|
meillo@0
|
20 #include <signal.h>
|
meillo@0
|
21 #include <stdio.h>
|
meillo@0
|
22 #include <stdlib.h>
|
meillo@0
|
23 #include <setjmp.h>
|
meillo@0
|
24 #include <unistd.h>
|
meillo@0
|
25 #include <ctype.h>
|
meillo@15
|
26
|
meillo@0
|
27 #include "readsock.h"
|
meillo@15
|
28 /*#include "masqmail.h"*/
|
meillo@0
|
29
|
meillo@0
|
30 jmp_buf jmp_timeout;
|
meillo@0
|
31
|
meillo@10
|
32 static void
|
meillo@10
|
33 sig_timeout_handler(int sig)
|
meillo@0
|
34 {
|
meillo@10
|
35 longjmp(jmp_timeout, 1);
|
meillo@0
|
36 }
|
meillo@0
|
37
|
meillo@0
|
38 static struct sigaction old_sa_alrm;
|
meillo@0
|
39
|
meillo@10
|
40 static void
|
meillo@10
|
41 alarm_on(int timeout)
|
meillo@0
|
42 {
|
meillo@10
|
43 struct sigaction sa;
|
meillo@0
|
44
|
meillo@10
|
45 sa.sa_handler = sig_timeout_handler;
|
meillo@10
|
46 sigemptyset(&(sa.sa_mask));
|
meillo@10
|
47 sa.sa_flags = 0;
|
meillo@10
|
48 sigaction(SIGALRM, &sa, &old_sa_alrm);
|
meillo@0
|
49
|
meillo@10
|
50 if (timeout > 0)
|
meillo@10
|
51 alarm(timeout);
|
meillo@0
|
52 }
|
meillo@0
|
53
|
meillo@10
|
54 static void
|
meillo@10
|
55 alarm_off()
|
meillo@0
|
56 {
|
meillo@10
|
57 alarm(0);
|
meillo@0
|
58
|
meillo@10
|
59 sigaction(SIGALRM, &old_sa_alrm, NULL);
|
meillo@0
|
60 }
|
meillo@0
|
61
|
meillo@10
|
62 static void
|
meillo@366
|
63 _read_chug(FILE *in)
|
meillo@0
|
64 {
|
meillo@10
|
65 int c = 0;
|
meillo@0
|
66
|
meillo@10
|
67 c = fgetc(in);
|
meillo@10
|
68 while (isspace(c) && (c != EOF))
|
meillo@10
|
69 c = fgetc(in);
|
meillo@10
|
70 ungetc(c, in);
|
meillo@0
|
71 }
|
meillo@0
|
72
|
meillo@10
|
73 static int
|
meillo@366
|
74 _read_line(FILE *in, char *buf, int buf_len, int timeout)
|
meillo@0
|
75 {
|
meillo@10
|
76 int p = 0;
|
meillo@10
|
77 int c = 0;
|
meillo@0
|
78
|
meillo@10
|
79 c = fgetc(in);
|
meillo@10
|
80 while ((c != '\n') && (c != EOF) && (p < buf_len - 1)) {
|
meillo@10
|
81 buf[p++] = c;
|
meillo@10
|
82 c = fgetc(in);
|
meillo@10
|
83 }
|
meillo@0
|
84
|
meillo@15
|
85 buf[p] = '\0';
|
meillo@0
|
86
|
meillo@10
|
87 if (c == EOF)
|
meillo@10
|
88 return -1;
|
meillo@10
|
89 else if (p >= buf_len) {
|
meillo@10
|
90 ungetc(c, in);
|
meillo@10
|
91 return -2;
|
meillo@10
|
92 }
|
meillo@0
|
93
|
meillo@10
|
94 buf[p++] = c; /* \n */
|
meillo@15
|
95 buf[p] = '\0';
|
meillo@0
|
96
|
meillo@10
|
97 return p;
|
meillo@0
|
98 }
|
meillo@0
|
99
|
meillo@10
|
100 int
|
meillo@366
|
101 read_sockline(FILE *in, char *buf, int buf_len, int timeout, unsigned int flags)
|
meillo@0
|
102 {
|
meillo@10
|
103 int p = 0;
|
meillo@0
|
104
|
meillo@10
|
105 if (setjmp(jmp_timeout) != 0) {
|
meillo@10
|
106 alarm_off();
|
meillo@10
|
107 return -3;
|
meillo@10
|
108 }
|
meillo@0
|
109
|
meillo@10
|
110 alarm_on(timeout);
|
meillo@0
|
111
|
meillo@10
|
112 /* strip leading spaces */
|
meillo@10
|
113 if (flags & READSOCKL_CHUG) {
|
meillo@10
|
114 _read_chug(in);
|
meillo@10
|
115 }
|
meillo@0
|
116
|
meillo@10
|
117 p = _read_line(in, buf, buf_len, timeout);
|
meillo@0
|
118
|
meillo@10
|
119 alarm_off();
|
meillo@0
|
120
|
meillo@10
|
121 if (p > 1) {
|
meillo@10
|
122 /* here we are sure that buf[p-1] == '\n' */
|
meillo@10
|
123 if (flags & READSOCKL_CVT_CRLF) {
|
meillo@10
|
124 if ((buf[p - 2] == '\r') && (buf[p - 1] == '\n')) {
|
meillo@10
|
125 buf[p - 2] = '\n';
|
meillo@10
|
126 buf[p - 1] = 0;
|
meillo@10
|
127 p--;
|
meillo@10
|
128 }
|
meillo@10
|
129 }
|
meillo@10
|
130 }
|
meillo@10
|
131 return p;
|
meillo@0
|
132 }
|
meillo@0
|
133
|
meillo@10
|
134 int
|
meillo@367
|
135 read_sockline1(FILE *in, char **pbuf, int *buf_len, int timeout,
|
meillo@367
|
136 unsigned int flags)
|
meillo@0
|
137 {
|
meillo@10
|
138 int p = 0, size = *buf_len;
|
meillo@10
|
139 char *buf;
|
meillo@0
|
140
|
meillo@10
|
141 if (setjmp(jmp_timeout) != 0) {
|
meillo@10
|
142 alarm_off();
|
meillo@10
|
143 return -3;
|
meillo@10
|
144 }
|
meillo@0
|
145
|
meillo@10
|
146 alarm_on(timeout);
|
meillo@0
|
147
|
meillo@10
|
148 /* strip leading spaces */
|
meillo@10
|
149 if (flags & READSOCKL_CHUG) {
|
meillo@10
|
150 _read_chug(in);
|
meillo@10
|
151 }
|
meillo@0
|
152
|
meillo@10
|
153 if (!*pbuf)
|
meillo@27
|
154 *pbuf = g_malloc(size);
|
meillo@10
|
155 buf = *pbuf;
|
meillo@0
|
156
|
meillo@10
|
157 while (1) {
|
meillo@10
|
158 int pp;
|
meillo@0
|
159
|
meillo@10
|
160 pp = _read_line(in, buf, size, timeout);
|
meillo@10
|
161 if (pp == -2) {
|
meillo@10
|
162 *pbuf = realloc(*pbuf, *buf_len + size);
|
meillo@10
|
163 buf = *pbuf + *buf_len;
|
meillo@10
|
164 *buf_len += size;
|
meillo@10
|
165 p += size;
|
meillo@10
|
166 } else {
|
meillo@10
|
167 if (pp > 0)
|
meillo@10
|
168 p += pp;
|
meillo@10
|
169 else
|
meillo@10
|
170 p = pp;
|
meillo@10
|
171 break;
|
meillo@10
|
172 }
|
meillo@10
|
173 }
|
meillo@0
|
174
|
meillo@10
|
175 alarm_off();
|
meillo@10
|
176
|
meillo@10
|
177 if (p > 1) {
|
meillo@10
|
178 buf = *pbuf;
|
meillo@10
|
179 /* here we are sure that buf[p-1] == '\n' */
|
meillo@10
|
180 if (flags & READSOCKL_CVT_CRLF) {
|
meillo@10
|
181 if ((buf[p - 2] == '\r') && (buf[p - 1] == '\n')) {
|
meillo@10
|
182 buf[p - 2] = '\n';
|
meillo@15
|
183 buf[p - 1] = '\0';
|
meillo@10
|
184 p--;
|
meillo@10
|
185 }
|
meillo@10
|
186 }
|
meillo@10
|
187 }
|
meillo@10
|
188 return p;
|
meillo@0
|
189 }
|