18 ipa_t ipa = inet_addr(ips);
19 if (ipa == INADDR_NONE)
return 0;
29 struct in_addr ipa_struct;
30 ipa_struct.s_addr = htonl(ipa);
31 string s = inet_ntoa(ipa_struct);
43 if (!Util::readInt(in,adr[0]) || !Util::verify(in,
'.') ||
44 !Util::readInt(in,adr[1]) || !Util::verify(in,
'.') ||
45 !Util::readInt(in,adr[2]) || !Util::verify(in,
'.') ||
46 !Util::readInt(in,adr[3]))
48 ipa = ((adr[0] & 0xff) << 24) | ((adr[1] & 0xff) << 16) |
49 ((adr[2] & 0xff) << 8) | (adr[3] & 0xff);
58 hostent* host = gethostbyname(hostName);
59 if (host == NULL)
return 0;
60 return ntohl(*((ipa_t*) &host->h_addr_list[0][0]));
68 if (gethostname(myName, 1000) != 0)
return 0;
79 bzero(sap,
sizeof(sockaddr_in));
80 sap->sin_family = AF_INET;
81 sap->sin_addr.s_addr = (ipa == 0 ? INADDR_ANY : htonl(ipa));
82 sap->sin_port = htons(port);
91 ipa = ntohl(sap->sin_addr.s_addr);
92 ipp = ntohs(sap->sin_port);
100 sockaddr_in sa; socklen_t len =
sizeof(sa);
101 if (getsockname(sock, (
struct sockaddr *) &sa, &len) < 0)
103 return ntohs(sa.sin_port);
111 sockaddr_in sa; socklen_t len =
sizeof(sa);
112 if (getsockname(sock, (
struct sockaddr *) &sa, &len) < 0)
114 return ntohl(sa.sin_addr.s_addr);
122 sockaddr_in sa; socklen_t len =
sizeof(sa);
123 if (getpeername(sock, (
struct sockaddr *) &sa, &len) < 0)
125 return ntohl(sa.sin_addr.s_addr);
134 if ((flags = fcntl(sock, F_GETFL, 0)) < 0)
return false;
136 if ((flags = fcntl(sock, F_SETFL, flags)) < 0)
return false;
144 return socket(AF_INET,SOCK_DGRAM,0);
151 return socket(AF_INET,SOCK_STREAM,0);
165 int x = bind(sock,(
struct sockaddr *) &sa,
sizeof(sa));
194 sockaddr_in sa; socklen_t len =
sizeof(sa);
195 sock = accept(sock,(
struct sockaddr *) &sa, &len);
196 if (sock < 0)
return -1;
210 return (connect(sock,(
struct sockaddr *) &sa,
sizeof(sa)) == 0);
224 return sendto(sock,buf,leng,0,(
struct sockaddr *) &sa,
sizeof(sa));
235 return recv(sock,buf,leng,0);
251 socklen_t socklen =
sizeof(sa);
252 int nbytes = recvfrom(sock,buf,leng,0,(
struct sockaddr *) &sa,&socklen);
264 ps.fd = sock; ps.events = POLLIN;
265 return poll(&ps, 1, 0) == 1;
273 int dAvail; socklen_t daSize =
sizeof(dAvail);
275 if (getsockopt(sock, SOL_SOCKET, SO_NREAD, &dAvail, &daSize) == -1)
277 if (ioctl(sock, SIOCINQ, &dAvail ) == -1)
289 int sbSize; socklen_t sbSizeSize =
sizeof(sbSize);
290 if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sbSize, &sbSizeSize) != 0)
292 int dQueued; socklen_t dqSize =
sizeof(dQueued);
294 if (getsockopt(sock, SOL_SOCKET, SO_NWRITE, &dQueued, &dqSize) != 0)
296 if (ioctl(sock, SIOCOUTQ, &dQueued ) == -1)
299 return sbSize - dQueued;
311 if (
dataAvail(sock) < (
int)
sizeof(uint32_t))
return false;
313 int nbytes = recv(sock, (
void *) &temp,
sizeof(uint32_t), 0);
314 if (nbytes !=
sizeof(uint32_t))
315 Util::fatal(
"Np4d::recvInt: can't receive integer");
320 bool Np4d::recvIntBlock(
int sock, uint32_t& val) {
321 char temp[
sizeof(uint32_t)];
322 int nbytes;
int rem =
sizeof(uint32_t);
324 nbytes = recv(sock, (
void *) &temp[
sizeof(uint32_t)-rem],
sizeof(uint32_t), 0);
325 if(nbytes < 0)
return nbytes;
329 if (nbytes !=
sizeof(uint32_t))
330 Util::fatal(
"Np4d::recvInt: can't receive integer");
331 val = *((uint32_t*) temp);
343 if (
spaceAvail(sock) < (
int)
sizeof(uint32_t))
return false;
345 int nbytes = send(sock, (
void *) &val,
sizeof(uint32_t), 0);
346 if (nbytes !=
sizeof(uint32_t))
347 Util::fatal(
"Np4d::sendInt: can't send integer");
351 bool Np4d::sendIntBlock(
int sock, uint32_t val) {
353 int nbytes = send(sock, (
void *) &val,
sizeof(uint32_t), 0);
354 if (nbytes !=
sizeof(uint32_t))
355 Util::fatal(
"Np4d::sendInt: can't send integer");
369 int vecSiz = length *
sizeof(uint32_t);
370 if (
dataAvail(sock) < vecSiz)
return false;
371 uint32_t buf[length];
372 int nbytes = recv(sock,(
void *) buf, vecSiz, 0);
373 if (nbytes != vecSiz)
374 Util::fatal(
"Np4d::recvIntVec: can't receive vector");
375 for (
int i = 0; i < length; i++) vec[i] = ntohl(buf[i]);
387 socklen_t vecSiz = length *
sizeof(uint32_t);
388 if (
spaceAvail(sock) < (int) vecSiz)
return false;
389 uint32_t buf[length];
390 for (
int i = 0; i < length; i++) buf[i] = htonl(vec[i]);
391 int nbytes = send(sock, (
void *) buf, vecSiz, 0);
392 if (nbytes != (
int) vecSiz)
393 Util::fatal(
"Np4d::sendIntVec: can't send vector");
418 int nbytes = recv(sock,(
void *) &length,
sizeof(uint32_t), MSG_PEEK);
419 if (nbytes <= 0)
return nbytes;
420 if (nbytes !=
sizeof(uint32_t))
return -1;
421 length = ntohl(length);
422 if (
dataAvail(sock) < ((
int) (length +
sizeof(uint32_t))))
return -1;
423 nbytes = recv(sock,(
void *) &length,
sizeof(uint32_t), 0);
424 length = min<int>(ntohl(length), buflen);
425 nbytes = recv(sock,(
void *) buf, length, 0);
429 int Np4d::recvBufBlock(
int sock,
char* buf,
int buflen) {
430 int rem =
sizeof(uint32_t);
char lenBuf[
sizeof(uint32_t)];
int nbytes;
432 nbytes = recv(sock,&lenBuf[
sizeof(uint32_t)-rem],
434 if(nbytes < 0)
return nbytes;
438 uint32_t length = *((uint32_t *) lenBuf);
439 length = ntohl(length);
440 length = min<int>(length, buflen);
443 nbytes = recv(sock,(
void *) &buf[length-rem], length, 0);
444 if(nbytes < 0)
return nbytes;
451 int Np4d::sendBuf(
int sock,
char* buf,
int buflen) {
452 if (
spaceAvail(sock) < buflen + (
int)
sizeof(uint32_t))
454 buflen = htonl(buflen);
455 int nbytes = send(sock, (
void *) &buflen,
sizeof(uint32_t), 0);
456 if (nbytes !=
sizeof(uint32_t))
457 Util::fatal(
"Np4d::sendBuf: can't send buffer");
458 buflen = ntohl(buflen);
459 nbytes = send(sock, (
void *) buf, buflen, 0);
460 if (nbytes != buflen)
461 Util::fatal(
"Np4d::sendBuf: can't send buffer");
465 int Np4d::sendBufBlock(
int sock,
char* buf,
int buflen) {
466 int length = htonl(buflen);
467 int nbytes = send(sock, (
void *) &length,
sizeof(uint32_t), 0);
468 if (nbytes !=
sizeof(uint32_t))
469 Util::fatal(
"Np4d::sendBuf: can't send buffer");
470 nbytes = send(sock, (
void *) buf, buflen, 0);
471 if (nbytes != buflen)
472 Util::fatal(
"Np4d::sendBuf: can't send buffer");
483 const char *p; p = s.c_str();
484 int numLeft = s.size();
485 while (numLeft > 0) {
486 int nbytes = write(sock,(
void *) p,numLeft);
488 if (nbytes < 0 && errno == EINTR)
492 numLeft -= nbytes; p += nbytes;