annotate src/dccifd.cpp @ 209:0fc1899391b6 stable-6-0-17

use autoconf to package the standard gnu documentation
author Carl Byington <carl@five-ten-sg.com>
date Wed, 05 Mar 2008 18:18:52 -0800
parents 92a5c866bdfa
children 82886d4dd71f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
1 /*
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
2
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
4 the GPL version 3 or any later version at your choice available at
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
5 http://www.gnu.org/licenses/gpl-3.0.txt
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
6
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
7 */
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
8
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
9 #include "includes.h"
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
10 #include <errno.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
11 #include <fcntl.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
12 #include <poll.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
13 #include <signal.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
14 #include <string>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
15 #include <sys/types.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
16 #include <sys/wait.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
17 #include <unistd.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
18
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
19 // needed for socket io
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
20 #include <sys/ioctl.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
21 #include <net/if.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
22 #include <arpa/inet.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
23 #include <netinet/in.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
24 #include <netinet/tcp.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
25 #include <netdb.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
26 #include <sys/socket.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
27 #include <sys/un.h>
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
28
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
29
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
30 const int maxlen = 1000; // used for snprintf buffers
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
31 extern int NULL_SOCKET;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
32 const char *options = "header\n";
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
33
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
34
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
35 ////////////////////////////////////////////////
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
36 // helper to convert syslog control chars
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
37 //
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
38 string escaper(string v);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
39 string escaper(string v)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
40 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
41 size_t n = v.length();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
42 char buf[n+1];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
43 strncpy(buf, v.c_str(), n);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
44 for (size_t i=0; i<n; i++) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
45 if (buf[i] == '\r') buf[i] = 'r';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
46 if (buf[i] == '\n') buf[i] = 'n';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
47 if ((unsigned char)(buf[i]) < ' '){
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
48 buf[i] = '.';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
49 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
50 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
51 return string(buf, n);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
52 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
53
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
54
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
55 DccInterface::DccInterface(char *port_, mlfiPriv *priv_, int ip, char *helo_, char *from)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
56 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
57 err = false;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
58 first_recipient = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
59 first_header = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
60 priv = priv_;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
61 ip4 = ip;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
62 helo = helo_;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
63 envfrom = from;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
64 dccifd_port = port_;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
65 dccifd_socket = NULL_SOCKET;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
66 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
67
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
68
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
69 DccInterface::~DccInterface()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
70 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
71 my_disconnect();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
72 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
73
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
74
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
75 void DccInterface::mlfi_envrcpt(SMFICTX *ctx, char *envrcpt, bool grey)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
76 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
77 if (first_recipient) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
78 first_recipient = false;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
79 char adr[sizeof "255.255.255.255 "];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
80 adr[0] = '\0';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
81 inet_ntop(AF_INET, (const u_char *)&ip4, adr, sizeof(adr));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
82 // Validated sending site's address
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
83 char *rdns = getmacro(ctx, "_", "");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
84 char buf[maxlen+1];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
85 if (*rdns == '[') rdns = "";
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
86 else {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
87 int n = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
88 while ((n < maxlen) && rdns[n] && (rdns[n] != ' ')) n++;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
89 strncpy(buf, rdns, n);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
90 buf[n] = '\0';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
91 rdns = buf;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
92 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
93 output(options);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
94 output(adr); output("\r");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
95 output(rdns); output("\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
96 //output("4.3.2.1\r\n"); // !! not local whitelisting
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
97 output(helo); output("\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
98 output(envfrom); output("\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
99 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
100 output(envrcpt);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
101 if (grey) output("\r\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
102 else output("\rdnsblnogrey\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
103 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
104
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
105
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
106 void DccInterface::mlfi_header(SMFICTX *ctx, char* headerf, char* headerv)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
107 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
108 if (dccifd_socket == NULL_SOCKET) Connect();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
109 if ((dccifd_socket != NULL_SOCKET) && (!dccifd_input.empty())) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
110 output(dccifd_input);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
111 dccifd_input.clear();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
112 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
113
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
114 if (first_header) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
115 output("\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
116 first_header = false;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
117 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
118
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
119 output(headerf);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
120 output(": ");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
121 output(headerv);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
122 output("\r\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
123 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
124
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
125
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
126 void DccInterface::mlfi_eoh()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
127 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
128 output("\r\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
129 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
130
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
131
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
132 void DccInterface::mlfi_body(u_char *bodyp, size_t bodylen)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
133 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
134 output((char *)bodyp, bodylen);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
135 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
136
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
137
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
138 void DccInterface::mlfi_eom(bool &grey, int &bulk)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
139 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
140 // AnAnX-DCC-Rhyolite-Metrics: ns.five-ten-sg.com 104; Body=2 Fuz1=2nn
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
141
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
142 close_output(); // signal EOF to DccInterface
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
143 input(); // read what the dcc has to say about this message
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
144 my_syslog(priv, "dcc returned " + escaper(dccifd_output));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
145 grey = false;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
146 bulk = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
147 const int n = dccifd_output.length();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
148 char buf[n+1];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
149 strncpy(buf, dccifd_output.c_str(), n);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
150 buf[n] = '\0';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
151
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
152 int newlines = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
153 int j, i = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
154 while ((i<n) && (newlines < 2)) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
155 switch (buf[i++]) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
156 case 'G' :
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
157 grey = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
158 break;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
159 case '\n' :
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
160 newlines++;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
161 default : ;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
162 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
163 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
164
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
165 // skip up to and including ;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
166 while ((i<n) && (buf[i++] != ';'));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
167
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
168 // convert to lower, = to space, ctrl-chars to space
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
169 for (j=i; j<n; j++) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
170 buf[j] = tolower(buf[j]);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
171 if (buf[j] == '=') buf[j] = ' ';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
172 if (buf[j] < ' ') buf[j] = ' ';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
173 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
174
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
175 while (i<n) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
176 // skip leading blanks
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
177 while ((i<n) && (buf[i] == ' ')) i++;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
178
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
179 // find blank terminator
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
180 for (j=i; (j<n) && (buf[j] != ' '); j++);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
181
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
182 // find anything?
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
183 if (j > i) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
184 // process this token
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
185 buf[j] = '\0';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
186 //my_syslog(priv, string("dccifd token") + (buf+i));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
187 if (strcmp(buf+i, "bulk") == 0) bulk = dccbulk;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
188 else if (strcmp(buf+i, "many") == 0) bulk = dccbulk;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
189 else if (strcmp(buf+i, "whitelist") == 0) bulk = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
190 else if (isdigit(buf[i])) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
191 int b = atoi(buf+i);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
192 if (b > bulk) bulk = b;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
193 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
194 // skip this token
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
195 i = j+1;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
196 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
197 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
198 char buff[maxlen];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
199 snprintf(buff, sizeof(buff), "dccifd found grey %s bulk %d", ((grey) ? "yes" : "no"), bulk);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
200 //my_syslog(priv, buff);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
201 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
202
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
203
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
204 void DccInterface::my_disconnect()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
205 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
206 if (dccifd_socket != NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
207 shutdown(dccifd_socket, SHUT_RDWR);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
208 close(dccifd_socket);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
209 dccifd_socket = NULL_SOCKET;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
210 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
211 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
212
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
213
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
214 void DccInterface::Connect()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
215 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
216 if (err) return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
217
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
218 sockaddr_un server;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
219 memset(&server, '\0', sizeof(server));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
220 server.sun_family = AF_UNIX;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
221 strncpy(server.sun_path, dccifd_port, sizeof(server.sun_path)-1);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
222 dccifd_socket = socket(AF_UNIX, SOCK_STREAM, 0);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
223 if (dccifd_socket != NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
224 bool rc = (connect(dccifd_socket, (sockaddr *)&server, sizeof(server)) == 0);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
225 if (!rc) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
226 my_disconnect();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
227 err = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
228 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
229 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
230 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
231
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
232
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
233 size_t DccInterface::my_write(const char *buf, size_t len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
234 if (err) return 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
235 size_t rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
236 while (len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
237 ssize_t ws = write(dccifd_socket, buf, len);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
238 if (ws > 0) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
239 rs += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
240 len -= ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
241 buf += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
242 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
243 else {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
244 // error or peer closed the socket!
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
245 rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
246 err = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
247 break;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
248 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
249 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
250 return rs;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
251 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
252
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
253 size_t DccInterface::my_read(char *buf, size_t len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
254 if (err) return 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
255 size_t rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
256 while (len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
257 ssize_t ws = read(dccifd_socket, buf, len);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
258 if (ws > 0) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
259 rs += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
260 len -= ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
261 buf += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
262 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
263 else if (ws < 0) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
264 // read error
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
265 rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
266 err = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
267 break;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
268 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
269 else {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
270 // peer closed the socket, end of file
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
271 break;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
272 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
273 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
274 return rs;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
275 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
276
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
277 void DccInterface::output(const char* buffer, size_t size)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
278 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
279 // if there are problems, fail.
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
280 if (err) return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
281
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
282 // buffer it if not connected yet
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
283 if (dccifd_socket == NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
284 //my_syslog(priv, string("dcc buffered ") + escaper(string(buffer, size)));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
285 dccifd_input.append(buffer, size);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
286 return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
287 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
288
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
289 // write it if we are connected
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
290 //my_syslog(priv, string("dcc write ") + escaper(string(buffer, size)));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
291 my_write(buffer, size);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
292 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
293
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
294
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
295 void DccInterface::output(const char* buffer)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
296 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
297 output(buffer, strlen(buffer));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
298 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
299
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
300
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
301 void DccInterface::output(string buffer)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
302 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
303 output(buffer.c_str(), buffer.size());
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
304 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
305
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
306
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
307 void DccInterface::close_output()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
308 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
309 if (dccifd_socket != NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
310 shutdown(dccifd_socket, SHUT_WR);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
311 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
312 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
313
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
314
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
315 void DccInterface::input()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
316 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
317 if ((dccifd_socket == NULL_SOCKET) || err) return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
318 char buf[maxlen];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
319 int rs;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
320 while (rs = my_read(buf, maxlen)) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
321 //my_syslog(priv, string("dcc read ") + escaper(string(buf, rs)));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
322 dccifd_output.append(buf, rs);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
323 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
324 my_disconnect();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
325 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
326
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
327
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
328 char *DccInterface::getmacro(SMFICTX *ctx, char *macro, char *def)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
329 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
330 char *rc = smfi_getsymval(ctx, macro);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
331 if (!rc) rc = def;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
332 return rc;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
333 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
334
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
335