本文整理汇总了C++中address::saddr方法的典型用法代码示例。如果您正苦于以下问题:C++ address::saddr方法的具体用法?C++ address::saddr怎么用?C++ address::saddr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类address
的用法示例。
在下文中一共展示了address::saddr方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SendTo
int SendTo(int sock, const uint8_t *buffer, int len, const address &from, const address &to) {
#ifdef IPV6_PKTINFO
if (from.family() == AF_INET6) {
uint8_t ctlbuf[CMSG_SPACE(sizeof(in6_pktinfo))];
cmsghdr *chdr = (cmsghdr *)ctlbuf;
chdr->cmsg_len = CMSG_LEN(sizeof(in6_pktinfo));
chdr->cmsg_level = IPPROTO_IPV6;
chdr->cmsg_type = IPV6_PKTINFO;
in6_pktinfo *info = (in6_pktinfo *)CMSG_DATA(chdr);
info->ipi6_addr = from.v6()->sin6_addr;
info->ipi6_ifindex = 0;
msghdr msg;
iovec iov;
msg.msg_name = (char *)to.saddr();
msg.msg_namelen = to.addrlen();
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = (char *)ctlbuf;
msg.msg_controllen = sizeof(ctlbuf);
msg.msg_flags = 0;
iov.iov_base = (char *)buffer;
iov.iov_len = len;
return sendmsg(sock, &msg, 0);
}
#endif
return sendto(sock, buffer, len, 0, to.saddr(), to.addrlen());
}
示例2: send_report
int send_report(int type) {
int len;
len = build_report(buffer, bufferLen, type == SSM_REPORT ? STATS_REPORT : type, true);
if (len < 0)
return len;
int res;
if (type == SSM_REPORT) {
if ((res = sendto(mcastSock, buffer, len, 0, ssmProbeAddr.saddr(), ssmProbeAddr.addrlen())) < 0)
d_log(LOG_DEBUG, "Failed to send SSM report: %s", strerror(errno));
else
bytesSent += res;
} else {
for (vector<address>::const_iterator i = redist.begin();
i != redist.end(); ++i) {
char tmp[64];
if (verbose)
d_log(LOG_DEBUG, "Sending Report to %s",
i->to_string(tmp, sizeof(tmp)));
if ((res = sendto(mcastSock, buffer, len, 0, i->saddr(),
i->addrlen())) < 0)
d_log(LOG_DEBUG, "Failed to send report to %s: %s",
i->to_string(tmp, sizeof(tmp)), strerror(errno));
else
bytesSent += res;
}
}
return 0;
}
示例3: send_nprobe
static int send_nprobe(const address &addr, uint32_t &seq) {
int len;
len = build_probe(buffer, bufferLen, seq, get_time_of_day());
seq++;
len = sendto(mcastSock, buffer, len, 0, addr.saddr(), addr.addrlen());
if (len > 0)
bytesSent += len;
return len;
}
示例4: get_local_address_for
address get_local_address_for(const address &remote)
{
int tmpSock = socket(remote.family(), SOCK_DGRAM, 0);
if (tmpSock < 0) {
perror("Failed to create socket to discover local addr");
exit(-1);
}
if (connect(tmpSock, remote.saddr(), remote.addrlen()) != 0) {
perror("Failed to connect multicast socket");
exit(-1);
}
address result(remote.family());
if (result.fromsocket(tmpSock) < 0) {
perror("getsockname");
exit(-1);
}
close(tmpSock);
return result;
}
示例5: RecvMsg
int RecvMsg(int sock, address &from, address &to, uint8_t *buffer, int buflen, int &ttl, uint64_t &ts) {
int len;
struct msghdr msg;
struct iovec iov;
uint8_t ctlbuf[64];
from.set_family(beaconUnicastAddr.family());
msg.msg_name = (char *)from.saddr();
msg.msg_namelen = from.addrlen();
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = (char *)ctlbuf;
msg.msg_controllen = sizeof(ctlbuf);
msg.msg_flags = 0;
iov.iov_base = (char *)buffer;
iov.iov_len = buflen;
len = recvmsg(sock, &msg, 0);
if (len < 0)
return len;
ts = 0;
ttl = 127;
to = beaconUnicastAddr;
if (msg.msg_controllen > 0) {
for (cmsghdr *hdr = CMSG_FIRSTHDR(&msg); hdr; hdr = CMSG_NXTHDR(&msg, hdr)) {
if (hdr->cmsg_level == IPPROTO_IPV6 && hdr->cmsg_type == IPV6_HOPLIMIT) {
ttl = *(int *)CMSG_DATA(hdr);
#ifdef IPV6_PKTINFO
} else if (hdr->cmsg_level == IPPROTO_IPV6 && hdr->cmsg_type == IPV6_PKTINFO) {
if (hdr->cmsg_len == CMSG_LEN(sizeof(in6_pktinfo))) {
in6_pktinfo *pktinfo = (in6_pktinfo *)CMSG_DATA(hdr);
to.set_family(AF_INET6);
to.v6()->sin6_addr = pktinfo->ipi6_addr;
}
#endif
#ifdef IP_RECVTTL
} else if (hdr->cmsg_level == IPPROTO_IP && hdr->cmsg_type == IP_RECVTTL) {
ttl = *(uint8_t *)CMSG_DATA(hdr);
#endif
} else if (hdr->cmsg_level == IPPROTO_IP && hdr->cmsg_type == IP_TTL) {
ttl = *(int *)CMSG_DATA(hdr);
#ifdef SO_TIMESTAMP
} else if (hdr->cmsg_level == SOL_SOCKET && hdr->cmsg_type == SO_TIMESTAMP) {
timeval *tv = (timeval *)CMSG_DATA(hdr);
ts = tv->tv_sec;
ts *= 1000;
ts += tv->tv_usec / 1000;
#endif
}
}
}
if (!ts) {
ts = get_time_of_day();
}
return len;
}
示例6: SetupSocket
int SetupSocket(const address &addr, bool shouldbind, bool ssm) {
int af_family = addr.family();
int level = addr.optlevel();
int sock = socket(af_family, SOCK_DGRAM, 0);
if (sock < 0) {
perror("Failed to create multicast socket");
return -1;
}
int on = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
perror("setsockopt");
return -1;
}
if (shouldbind) {
if (bind(sock, addr.saddr(), addr.addrlen()) != 0) {
perror("Failed to bind multicast socket");
return -1;
}
}
#ifdef SO_TIMESTAMP
if (setsockopt(sock, SOL_SOCKET, SO_TIMESTAMP, &on, sizeof(on)) != 0) {
perror("setsockopt(SO_TIMESTAMP)");
return -1;
}
#endif
int type = level == IPPROTO_IPV6 ?
#ifdef IPV6_RECVHOPLIMIT
IPV6_RECVHOPLIMIT
#else
IPV6_HOPLIMIT
#endif
:
#ifdef IP_RECVTTL
IP_RECVTTL;
#else
IP_TTL;
#endif
if (setsockopt(sock, level, type, &on, sizeof(on)) != 0) {
perror("receiving hop limit/ttl setsockopt()");
return -1;
}
if (!SetHops(sock, addr, defaultTTL)) {
perror("SetHops");
return -1;
}
if (!ssm && addr.is_multicast()) {
if (MulticastListen(sock, addr) != 0) {
perror("Failed to join multicast group");
return -1;
}
}
return sock;
}
示例7: main
int main(int argc, char **argv) {
int res;
srand(time(NULL));
char tmp[256];
if (gethostname(tmp, sizeof(tmp)) != 0) {
perror("Failed to get hostname");
return -1;
}
beaconName = tmp;
parse_arguments(argc, argv);
MulticastStartup();
if (beaconName.empty())
fatal("No name supplied, check `dbeacon -h`.");
if (!probeAddrLiteral.empty()) {
if (!probeAddr.parse(probeAddrLiteral.c_str(), true))
return -1;
probeAddr.to_string(sessionName, sizeof(sessionName));
if (!probeAddr.is_multicast())
fatal("Specified probe addr (%s) is not of a multicast group.",
sessionName);
if (adminContact.empty())
fatal("No administration contact supplied, check `dbeacon -h`.");
mcastListen.push_back(ContentDesc(probeAddr, false));
insert_event(SENDING_EVENT, 100);
insert_event(REPORT_EVENT, 10000);
insert_event(MAP_REPORT_EVENT, 30000);
insert_event(WEBSITE_REPORT_EVENT, 120000);
redist.push_back(probeAddr);
if (useSSM) {
if (probeSSMAddrLiteral.empty()) {
int family = forceFamily;
if (family == AF_UNSPEC) {
family = probeAddr.family();
}
if (family == AF_INET) {
probeSSMAddrLiteral = defaultIPv4SSMChannel;
} else {
probeSSMAddrLiteral = defaultIPv6SSMChannel;
}
}
if (!ssmProbeAddr.parse(probeSSMAddrLiteral.c_str(), true)) {
fatal("Bad address format for SSM channel.");
} else if (!ssmProbeAddr.is_unspecified()) {
insert_event(SSM_SENDING_EVENT, 100);
insert_event(SSM_REPORT_EVENT, 15000);
if (listenForSSM) {
mcastListen.push_back(ContentDesc(ssmProbeAddr, true));
}
}
}
} else {
if (mcastListen.empty())
fatal("Nothing to do, check `dbeacon -h`.");
else
strcpy(sessionName, beaconName.c_str());
}
address local;
local.set_family(probeAddr.family());
mcastSock = SetupSocket(local, false, false);
if (mcastSock < 0)
return -1;
if (beaconUnicastAddr.is_unspecified())
beaconUnicastAddr = get_local_address_for(probeAddr);
if (bind(mcastSock, beaconUnicastAddr.saddr(), beaconUnicastAddr.addrlen()) != 0) {
perror("Failed to bind local socket");
return -1;
}
if (beaconUnicastAddr.fromsocket(mcastSock) < 0) {
perror("getsockname");
return -1;
}
for (McastListen::const_iterator i = mcastListen.begin();
i != mcastListen.end(); ++i) {
int sock = SetupSocket(i->first, true, i->second);
if (sock < 0)
return -1;
//.........这里部分代码省略.........