|
Lines 129-224
Link Here
|
| 129 |
return 0; |
129 |
return 0; |
| 130 |
}; |
130 |
}; |
| 131 |
|
131 |
|
| 132 |
int LANProtocol::lanReadDataFromServer() |
132 |
// The next two functions should probably go into some common |
|
|
133 |
// class, because they're also needed by lisa |
| 134 |
int |
| 135 |
LANProtocol::commonReadDataFromServer(int fd, char **bufp) |
| 133 |
{ |
136 |
{ |
| 134 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer() host: "<<m_currentHost<<" port: "<<m_port<<endl; |
|
|
| 135 |
connectToHost(m_currentHost.latin1(), m_port); |
| 136 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer() connected"<<endl; |
| 137 |
|
| 138 |
int receivedBytes(0); |
137 |
int receivedBytes(0); |
| 139 |
char* receiveBuffer(0); |
138 |
char* receiveBuffer(0); |
| 140 |
char tmpBuf[64*1024]; |
139 |
|
| 141 |
int bytesRead(0); |
140 |
*bufp = NULL; |
| 142 |
do |
141 |
while (1) { |
| 143 |
{ |
142 |
fd_set rset; |
| 144 |
fd_set tmpFDs; |
|
|
| 145 |
FD_ZERO(&tmpFDs); |
| 146 |
FD_SET(m_iSock,&tmpFDs); |
| 147 |
timeval tv; |
143 |
timeval tv; |
|
|
144 |
|
| 145 |
FD_ZERO(&rset); |
| 146 |
FD_SET(fd, &rset); |
| 148 |
tv.tv_sec = 1; |
147 |
tv.tv_sec = 1; |
| 149 |
tv.tv_usec = 0; |
148 |
tv.tv_usec = 0; |
| 150 |
select(m_iSock+1,&tmpFDs,0,0,&tv); |
149 |
select(fd+1, &rset, 0, 0, &tv); |
| 151 |
if (FD_ISSET(m_iSock,&tmpFDs)) |
150 |
if (FD_ISSET(fd, &rset)) { |
| 152 |
{ |
151 |
char tmpBuf[4096]; |
| 153 |
bytesRead=read(tmpBuf,64*1024); |
152 |
int bytesRead; |
|
|
153 |
|
| 154 |
bytesRead = ::read(fd, tmpBuf, sizeof(tmpBuf)); |
| 154 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer: read "<<bytesRead<<" bytes"<<endl; |
155 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer: read "<<bytesRead<<" bytes"<<endl; |
| 155 |
|
156 |
|
| 156 |
if (bytesRead>0) |
157 |
if (bytesRead < 0) { |
| 157 |
{ |
158 |
if (receiveBuffer) |
| 158 |
char *newBuf=new char[receivedBytes+bytesRead]; |
159 |
delete [] receiveBuffer; |
| 159 |
if (receiveBuffer!=0) memcpy(newBuf,receiveBuffer,receivedBytes); |
160 |
return -1; |
| 160 |
memcpy(newBuf+receivedBytes,tmpBuf,bytesRead); |
161 |
} |
| 161 |
receivedBytes+=bytesRead; |
162 |
|
| 162 |
if (receiveBuffer!=0) delete [] receiveBuffer; |
163 |
if (bytesRead == 0) |
| 163 |
receiveBuffer=newBuf; |
164 |
break; |
| 164 |
}; |
165 |
|
| 165 |
}; |
166 |
char *newBuf = new char[receivedBytes + bytesRead]; |
| 166 |
} while (bytesRead>0); |
167 |
if (receiveBuffer!=0) |
| 167 |
closeDescriptor(); |
168 |
memcpy(newBuf, receiveBuffer, receivedBytes); |
| 168 |
if ((bytesRead<0) || (receivedBytes<4)) |
169 |
memcpy(newBuf + receivedBytes, tmpBuf, bytesRead); |
| 169 |
{ |
170 |
if (receiveBuffer!=0) |
| 170 |
delete [] receiveBuffer; |
171 |
delete [] receiveBuffer; |
| 171 |
error(ERR_INTERNAL_SERVER,i18n("Received unexpected data from %1").arg(m_currentHost)); |
172 |
receiveBuffer = newBuf; |
| 172 |
return 0; |
173 |
receivedBytes += bytesRead; |
| 173 |
}; |
174 |
} |
|
|
175 |
} |
| 174 |
|
176 |
|
| 175 |
UDSEntry entry; |
177 |
*bufp = receiveBuffer; |
|
|
178 |
return receivedBytes; |
| 179 |
} |
| 180 |
|
| 181 |
// Data sent by the server contains several lines separated by NUL |
| 182 |
// characters. Each of this lines is newline terminated. |
| 183 |
int |
| 184 |
LANProtocol::commonParseData(const char *data, size_t len, UDSEntry &entry) |
| 185 |
{ |
| 186 |
const char *endOfData = data + len; |
| 187 |
unsigned long hostAddress; |
| 188 |
QString hostName; |
| 176 |
|
189 |
|
| 177 |
char *currentBuf=receiveBuffer; |
190 |
while (data < endOfData) { |
| 178 |
int bytesLeft=receivedBytes; |
191 |
char *nextLine; |
| 179 |
int tmpIP; |
192 |
|
| 180 |
//this should be large enough for a name |
193 |
if (!(nextLine = (char *) memchr(data, 0, endOfData - data))) |
| 181 |
char tmpName[1024]; |
|
|
| 182 |
//this should be large enough for the hostname |
| 183 |
char tmpHostname[512]; |
| 184 |
while (bytesLeft>0) |
| 185 |
{ |
| 186 |
if ((memchr(currentBuf,0,bytesLeft)==0) || (memchr(currentBuf,int('\n'),bytesLeft)==0)) |
| 187 |
{ |
| 188 |
delete [] receiveBuffer; |
| 189 |
error(ERR_INTERNAL_SERVER,i18n("Received unexpected data from %1").arg(m_currentHost)); |
| 190 |
return 0; |
194 |
return 0; |
| 191 |
}; |
195 |
|
| 192 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer: processing "<<currentBuf; |
196 |
// We now know it's NUL terminated so we can treat it like any other |
| 193 |
sscanf(currentBuf,"%u %s\n",&tmpIP,tmpName); |
197 |
// string |
| 194 |
//since we check for 0 and \n with memchr() we can be sure |
198 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer: processing "<<data; |
| 195 |
//at this point that tmpBuf is correctly terminated |
199 |
|
| 196 |
int length=strlen(currentBuf)+1; |
200 |
QString line(data); |
| 197 |
bytesLeft-=length; |
201 |
int idx; |
| 198 |
currentBuf+=length; |
202 |
|
| 199 |
if ((bytesLeft==0) && (strstr(tmpName,"succeeded")!=0) && ((tmpIP==0) ||(tmpIP==1))) |
203 |
data = nextLine + 1; |
| 200 |
{ |
204 |
if ((idx = line.find(' ')) < 0) |
|
|
205 |
continue; |
| 206 |
hostAddress = line.left(idx).toULong(); |
| 207 |
line.remove(0, idx+1); |
| 208 |
|
| 209 |
if ((idx = line.find('\n')) < 0 || idx > 512) |
| 210 |
continue; |
| 211 |
line.truncate(idx); |
| 212 |
hostName = line; |
| 213 |
|
| 214 |
if (data >= endOfData |
| 215 |
&& hostName.contains("succeeded") |
| 216 |
&& (hostAddress == 0 || hostAddress == 1)) { |
| 201 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer: succeeded"<<endl; |
217 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer: succeeded"<<endl; |
| 202 |
} |
218 |
} else { |
| 203 |
else |
219 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer: listing host: "<<hostName<<" with ip: "<<hostAddress<<endl; |
| 204 |
{ |
|
|
| 205 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer: listing host: "<<tmpName<<" with ip: "<<tmpIP<<endl; |
| 206 |
UDSAtom atom; |
220 |
UDSAtom atom; |
| 207 |
|
221 |
|
| 208 |
atom.m_uds = KIO::UDS_NAME; |
222 |
atom.m_uds = KIO::UDS_NAME; |
| 209 |
if (m_shortHostnames) |
223 |
|
| 210 |
{ |
224 |
// Remove eveything after the first dot it user |
| 211 |
if (inet_addr(tmpName)!=-1) |
225 |
// requested short hostnames |
| 212 |
atom.m_str=tmpName; |
226 |
if (m_shortHostnames |
| 213 |
else |
227 |
&& inet_addr(hostName.ascii()) == -1 |
| 214 |
{ |
228 |
&& (idx = hostName.find('.')) > 0) |
| 215 |
sscanf(tmpName,"%[^.]",tmpHostname); |
229 |
hostName.truncate(idx); |
| 216 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer: Hostname of " << tmpName << " is " << tmpHostname << "\n"; |
230 |
atom.m_str = hostName; |
| 217 |
atom.m_str = tmpHostname; |
|
|
| 218 |
}; |
| 219 |
} |
| 220 |
else |
| 221 |
atom.m_str = tmpName; |
| 222 |
|
231 |
|
| 223 |
entry.append( atom ); |
232 |
entry.append( atom ); |
| 224 |
atom.m_uds = KIO::UDS_SIZE; |
233 |
atom.m_uds = KIO::UDS_SIZE; |
|
Lines 232-240
Link Here
|
| 232 |
atom.m_long = S_IFDIR; // it is always a directory |
241 |
atom.m_long = S_IFDIR; // it is always a directory |
| 233 |
entry.append( atom ); |
242 |
entry.append( atom ); |
| 234 |
listEntry(entry,false); |
243 |
listEntry(entry,false); |
| 235 |
}; |
244 |
} |
|
|
245 |
} |
| 246 |
|
| 247 |
return 1; |
| 248 |
} |
| 249 |
|
| 250 |
int LANProtocol::lanReadDataFromServer() |
| 251 |
{ |
| 252 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer() host: "<<m_currentHost<<" port: "<<m_port<<endl; |
| 253 |
connectToHost(m_currentHost.latin1(), m_port); |
| 254 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer() connected"<<endl; |
| 255 |
|
| 256 |
int receivedBytes(0); |
| 257 |
char* receiveBuffer(0); |
| 258 |
|
| 259 |
receivedBytes = commonReadDataFromServer(m_iSock, &receiveBuffer); |
| 260 |
closeDescriptor(); |
| 261 |
|
| 262 |
if (receivedBytes < 4) |
| 263 |
{ |
| 264 |
error(ERR_INTERNAL_SERVER,i18n("Received unexpected data from %1").arg(m_currentHost)); |
| 265 |
return 0; |
| 236 |
}; |
266 |
}; |
| 237 |
|
267 |
|
|
|
268 |
UDSEntry entry; |
| 269 |
|
| 270 |
if (!commonParseData(receiveBuffer, receivedBytes, entry)) { |
| 271 |
delete [] receiveBuffer; |
| 272 |
error(ERR_INTERNAL_SERVER,i18n("Received unexpected data from %1").arg(m_currentHost)); |
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 238 |
listEntry( entry, true ); // ready |
276 |
listEntry( entry, true ); // ready |
| 239 |
delete [] receiveBuffer; |
277 |
delete [] receiveBuffer; |
| 240 |
return 1; |
278 |
return 1; |
|
Lines 248-253
Link Here
|
| 248 |
sockaddr_un addr; |
286 |
sockaddr_un addr; |
| 249 |
memset((char*)&addr,0,sizeof(addr)); |
287 |
memset((char*)&addr,0,sizeof(addr)); |
| 250 |
addr.sun_family=AF_LOCAL; |
288 |
addr.sun_family=AF_LOCAL; |
|
|
289 |
|
| 290 |
// XXX This is not a good idea. Please use the standard |
| 291 |
// KDE socket locations for this; connecting to |
| 292 |
// /tmp/whatnot is insecure. --okir |
| 251 |
QCString socketname="/tmp/resLisa-"; |
293 |
QCString socketname="/tmp/resLisa-"; |
| 252 |
|
294 |
|
| 253 |
struct passwd *user = getpwuid( getuid() ); |
295 |
struct passwd *user = getpwuid( getuid() ); |
|
Lines 302-338
Link Here
|
| 302 |
|
344 |
|
| 303 |
int receivedBytes(0); |
345 |
int receivedBytes(0); |
| 304 |
char* receiveBuffer(0); |
346 |
char* receiveBuffer(0); |
| 305 |
char tmpBuf[64*1024]; |
|
|
| 306 |
int bytesRead(0); |
| 307 |
do |
| 308 |
{ |
| 309 |
fd_set tmpFDs; |
| 310 |
FD_ZERO(&tmpFDs); |
| 311 |
FD_SET(sockFD,&tmpFDs); |
| 312 |
timeval tv; |
| 313 |
tv.tv_sec = 1; |
| 314 |
tv.tv_usec = 0; |
| 315 |
select(sockFD+1,&tmpFDs,0,0,&tv); |
| 316 |
if (FD_ISSET(sockFD,&tmpFDs)) |
| 317 |
{ |
| 318 |
bytesRead=::read(sockFD,tmpBuf,64*1024); |
| 319 |
kdDebug(7101)<<"RLANProtocol::readDataFromServer: read "<<bytesRead<<" bytes"<<endl; |
| 320 |
|
347 |
|
| 321 |
if (bytesRead>0) |
348 |
receivedBytes = commonReadDataFromServer(sockFD, &receiveBuffer); |
| 322 |
{ |
|
|
| 323 |
char *newBuf=new char[receivedBytes+bytesRead]; |
| 324 |
if (receiveBuffer!=0) memcpy(newBuf,receiveBuffer,receivedBytes); |
| 325 |
memcpy(newBuf+receivedBytes,tmpBuf,bytesRead); |
| 326 |
receivedBytes+=bytesRead; |
| 327 |
if (receiveBuffer!=0) delete [] receiveBuffer; |
| 328 |
receiveBuffer=newBuf; |
| 329 |
}; |
| 330 |
}; |
| 331 |
} while (bytesRead>0); |
| 332 |
::close(sockFD); |
349 |
::close(sockFD); |
| 333 |
|
350 |
|
| 334 |
|
351 |
if (receivedBytes < 4) |
| 335 |
if ((bytesRead<0) || (receivedBytes<4)) |
|
|
| 336 |
{ |
352 |
{ |
| 337 |
delete [] receiveBuffer; |
353 |
delete [] receiveBuffer; |
| 338 |
error(ERR_CANNOT_OPEN_FOR_READING,socketname); |
354 |
error(ERR_CANNOT_OPEN_FOR_READING,socketname); |
|
Lines 342-406
Link Here
|
| 342 |
|
358 |
|
| 343 |
UDSEntry entry; |
359 |
UDSEntry entry; |
| 344 |
|
360 |
|
| 345 |
char *currentBuf=receiveBuffer; |
361 |
if (!commonParseData(receiveBuffer, receivedBytes, entry)) { |
| 346 |
int bytesLeft=receivedBytes; |
362 |
delete [] receiveBuffer; |
| 347 |
int tmpIP; |
363 |
error(ERR_INTERNAL_SERVER,i18n("Received unexpected data from %1").arg(socketname)); |
| 348 |
//this should be large enough for a name |
364 |
return 0; |
| 349 |
char tmpName[1024]; |
365 |
} |
| 350 |
//this should be large enough for the hostname |
|
|
| 351 |
char tmpHostname[512]; |
| 352 |
while (bytesLeft>0) |
| 353 |
{ |
| 354 |
if ((memchr(currentBuf,0,bytesLeft)==0) || (memchr(currentBuf,int('\n'),bytesLeft)==0)) |
| 355 |
{ |
| 356 |
delete [] receiveBuffer; |
| 357 |
error(ERR_INTERNAL_SERVER,i18n("Received unexpected data from %1").arg(socketname)); |
| 358 |
return 0; |
| 359 |
}; |
| 360 |
kdDebug(7101)<<"RLANProtocol::readDataFromServer: processing "<<currentBuf; |
| 361 |
sscanf(currentBuf,"%u %s\n",&tmpIP,tmpName); |
| 362 |
//since we check for 0 and \n with memchr() we can be sure |
| 363 |
//at this point that tmpBuf is correctly terminated |
| 364 |
int length=strlen(currentBuf)+1; |
| 365 |
bytesLeft-=length; |
| 366 |
currentBuf+=length; |
| 367 |
if ((bytesLeft==0) && (strstr(tmpName,"succeeded")!=0) && ((tmpIP==0) ||(tmpIP==1))) |
| 368 |
{ |
| 369 |
kdDebug(7101)<<"RLANProtocol::readDataFromServer: succeeded"<<endl; |
| 370 |
} |
| 371 |
else |
| 372 |
{ |
| 373 |
kdDebug(7101)<<"RLANProtocol::readDataFromServer: listing host: "<<tmpName<<" with ip: "<<tmpIP<<endl; |
| 374 |
UDSAtom atom; |
| 375 |
|
| 376 |
atom.m_uds = KIO::UDS_NAME; |
| 377 |
if (m_shortHostnames) |
| 378 |
{ |
| 379 |
if (inet_addr(tmpName)!=-1) |
| 380 |
atom.m_str=tmpName; |
| 381 |
else |
| 382 |
{ |
| 383 |
sscanf(tmpName,"%[^.]",tmpHostname); |
| 384 |
kdDebug(7101)<<"LANProtocol::lanReadDataFromServer: Hostname of " << tmpName << " is " << tmpHostname << "\n"; |
| 385 |
atom.m_str = tmpHostname; |
| 386 |
}; |
| 387 |
} |
| 388 |
else |
| 389 |
atom.m_str = tmpName; |
| 390 |
entry.append( atom ); |
| 391 |
atom.m_uds = KIO::UDS_SIZE; |
| 392 |
atom.m_long = 1024; |
| 393 |
entry.append(atom); |
| 394 |
atom.m_uds = KIO::UDS_ACCESS; |
| 395 |
atom.m_long = S_IRUSR | S_IRGRP | S_IROTH ; |
| 396 |
//atom.m_long = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
| 397 |
entry.append(atom); |
| 398 |
atom.m_uds = KIO::UDS_FILE_TYPE; |
| 399 |
atom.m_long = S_IFDIR; // it is always a directory |
| 400 |
entry.append( atom ); |
| 401 |
listEntry(entry,false); |
| 402 |
}; |
| 403 |
}; |
| 404 |
|
366 |
|
| 405 |
listEntry( entry, true ); // ready |
367 |
listEntry( entry, true ); // ready |
| 406 |
delete [] receiveBuffer; |
368 |
delete [] receiveBuffer; |
|
Lines 436-442
Link Here
|
| 436 |
delete hostInfo; |
398 |
delete hostInfo; |
| 437 |
return 0; |
399 |
return 0; |
| 438 |
} |
400 |
} |
| 439 |
memcpy(&ip, hp->h_addr, hp->h_length); |
401 |
memcpy(&ip, hp->h_addr, sizeof(ip)); |
| 440 |
|
402 |
|
| 441 |
for (int i=0; i<KIOLAN_MAX; i++) |
403 |
for (int i=0; i<KIOLAN_MAX; i++) |
| 442 |
{ |
404 |
{ |