annotate src/dccifd.cpp @ 200:c7efbf2ffd8b

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