annotate src/dccifd.cpp @ 252:836b7f2357f9

need ntohl() before using masks that are defined in host byte order
author Carl Byington <carl@five-ten-sg.com>
date Sun, 08 Apr 2012 16:10:31 -0700
parents 3fee608becbc
children df7dc6b2b153
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
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
55 DccInterface::DccInterface(const char *port_, mlfiPriv *priv_, int ip, const char *helo_, const char *from)
196
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
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
75 void DccInterface::mlfi_envrcpt(SMFICTX *ctx, const char *envrcpt, bool grey)
196
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
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
83 const char *rdns = getmacro(ctx, "_", "");
196
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(helo); output("\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
97 output(envfrom); output("\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
98 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
99 output(envrcpt);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
100 if (grey) output("\r\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
101 else output("\rdnsblnogrey\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
102 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
103
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
104
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
105 void DccInterface::mlfi_header(SMFICTX *ctx, const char *headerf, const char *headerv)
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
106 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
107 if (dccifd_socket == NULL_SOCKET) Connect();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
108 if ((dccifd_socket != NULL_SOCKET) && (!dccifd_input.empty())) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
109 output(dccifd_input);
227
3fee608becbc Fixes to compile on old systems without memrchr or string::clear().
Carl Byington <carl@five-ten-sg.com>
parents: 214
diff changeset
110 dccifd_input = "";
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
111 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
112
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
113 if (first_header) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
114 output("\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
115 first_header = false;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
116 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
117
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
118 output(headerf);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
119 output(": ");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
120 output(headerv);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
121 output("\r\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
122 }
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 void DccInterface::mlfi_eoh()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
126 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
127 output("\r\n");
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
128 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
129
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
130
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
131 void DccInterface::mlfi_body(const u_char *bodyp, size_t bodylen)
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
132 {
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
133 output((const char *)bodyp, bodylen);
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
134 }
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 void DccInterface::mlfi_eom(bool &grey, int &bulk)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
138 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
139 // AnAnX-DCC-Rhyolite-Metrics: ns.five-ten-sg.com 104; Body=2 Fuz1=2nn
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
140
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
141 close_output(); // signal EOF to DccInterface
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
142 input(); // read what the dcc has to say about this message
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
143 my_syslog(priv, "dcc returned " + escaper(dccifd_output));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
144 grey = false;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
145 bulk = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
146 const int n = dccifd_output.length();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
147 char buf[n+1];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
148 strncpy(buf, dccifd_output.c_str(), n);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
149 buf[n] = '\0';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
150
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
151 int newlines = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
152 int j, i = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
153 while ((i<n) && (newlines < 2)) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
154 switch (buf[i++]) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
155 case 'G' :
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
156 grey = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
157 break;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
158 case '\n' :
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
159 newlines++;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
160 default : ;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
161 }
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 // skip up to and including ;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
165 while ((i<n) && (buf[i++] != ';'));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
166
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
167 // convert to lower, = to space, ctrl-chars to space
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
168 for (j=i; j<n; j++) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
169 buf[j] = tolower(buf[j]);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
170 if (buf[j] == '=') 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 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
173
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
174 while (i<n) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
175 // skip leading blanks
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
176 while ((i<n) && (buf[i] == ' ')) i++;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
177
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
178 // find blank terminator
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
179 for (j=i; (j<n) && (buf[j] != ' '); j++);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
180
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
181 // find anything?
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
182 if (j > i) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
183 // process this token
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
184 buf[j] = '\0';
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
185 //my_syslog(priv, string("dccifd token") + (buf+i));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
186 if (strcmp(buf+i, "bulk") == 0) bulk = dccbulk;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
187 else if (strcmp(buf+i, "many") == 0) bulk = dccbulk;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
188 else if (strcmp(buf+i, "whitelist") == 0) bulk = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
189 else if (isdigit(buf[i])) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
190 int b = atoi(buf+i);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
191 if (b > bulk) bulk = b;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
192 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
193 // skip this token
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
194 i = j+1;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
195 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
196 }
227
3fee608becbc Fixes to compile on old systems without memrchr or string::clear().
Carl Byington <carl@five-ten-sg.com>
parents: 214
diff changeset
197 //char buff[maxlen];
3fee608becbc Fixes to compile on old systems without memrchr or string::clear().
Carl Byington <carl@five-ten-sg.com>
parents: 214
diff changeset
198 //snprintf(buff, sizeof(buff), "dccifd found grey %s bulk %d", ((grey) ? "yes" : "no"), bulk);
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
199 //my_syslog(priv, buff);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
200 }
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 void DccInterface::my_disconnect()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
204 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
205 if (dccifd_socket != NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
206 shutdown(dccifd_socket, SHUT_RDWR);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
207 close(dccifd_socket);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
208 dccifd_socket = NULL_SOCKET;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
209 }
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 void DccInterface::Connect()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
214 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
215 if (err) return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
216
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
217 sockaddr_un server;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
218 memset(&server, '\0', sizeof(server));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
219 server.sun_family = AF_UNIX;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
220 strncpy(server.sun_path, dccifd_port, sizeof(server.sun_path)-1);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
221 dccifd_socket = socket(AF_UNIX, SOCK_STREAM, 0);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
222 if (dccifd_socket != NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
223 bool rc = (connect(dccifd_socket, (sockaddr *)&server, sizeof(server)) == 0);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
224 if (!rc) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
225 my_disconnect();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
226 err = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
227 }
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 size_t DccInterface::my_write(const char *buf, size_t len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
233 if (err) return 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
234 size_t rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
235 while (len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
236 ssize_t ws = write(dccifd_socket, buf, len);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
237 if (ws > 0) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
238 rs += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
239 len -= ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
240 buf += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
241 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
242 else {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
243 // error or peer closed the socket!
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
244 rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
245 err = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
246 break;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
247 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
248 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
249 return rs;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
250 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
251
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
252 size_t DccInterface::my_read(char *buf, size_t len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
253 if (err) return 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
254 size_t rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
255 while (len) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
256 ssize_t ws = read(dccifd_socket, buf, len);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
257 if (ws > 0) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
258 rs += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
259 len -= ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
260 buf += ws;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
261 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
262 else if (ws < 0) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
263 // read error
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
264 rs = 0;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
265 err = true;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
266 break;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
267 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
268 else {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
269 // peer closed the socket, end of file
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 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
273 return rs;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
274 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
275
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
276 void DccInterface::output(const char* buffer, size_t size)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
277 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
278 // if there are problems, fail.
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
279 if (err) return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
280
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
281 // buffer it if not connected yet
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
282 if (dccifd_socket == NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
283 //my_syslog(priv, string("dcc buffered ") + escaper(string(buffer, size)));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
284 dccifd_input.append(buffer, size);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
285 return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
286 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
287
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
288 // write it if we are connected
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
289 //my_syslog(priv, string("dcc write ") + escaper(string(buffer, size)));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
290 my_write(buffer, size);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
291 }
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 void DccInterface::output(const char* buffer)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
295 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
296 output(buffer, strlen(buffer));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
297 }
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 void DccInterface::output(string buffer)
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
301 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
302 output(buffer.c_str(), buffer.size());
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
303 }
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 void DccInterface::close_output()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
307 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
308 if (dccifd_socket != NULL_SOCKET) {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
309 shutdown(dccifd_socket, SHUT_WR);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
310 }
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 void DccInterface::input()
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
315 {
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
316 if ((dccifd_socket == NULL_SOCKET) || err) return;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
317 char buf[maxlen];
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
318 int rs;
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
319 while ((rs = my_read(buf, maxlen))) {
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
320 //my_syslog(priv, string("dcc read ") + escaper(string(buf, rs)));
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
321 dccifd_output.append(buf, rs);
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
322 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
323 my_disconnect();
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
324 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
325
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
326
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
327 const char *DccInterface::getmacro(SMFICTX *ctx, const char *macro, const char *def)
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
328 {
214
82886d4dd71f Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 203
diff changeset
329 const char *rc = smfi_getsymval(ctx, (char*)macro);
196
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
330 if (!rc) rc = def;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
331 return rc;
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
332 }
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
333
ff6d14d75b1e add missing files to cvs
carl
parents:
diff changeset
334