当前位置: 首页>>代码示例>>C++>>正文


C++ SockAddr::ToString方法代码示例

本文整理汇总了C++中SockAddr::ToString方法的典型用法代码示例。如果您正苦于以下问题:C++ SockAddr::ToString方法的具体用法?C++ SockAddr::ToString怎么用?C++ SockAddr::ToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SockAddr的用法示例。


在下文中一共展示了SockAddr::ToString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: SendData

  /**
   *
   *
   * @param message
   * @param message_size
   * @param port
   * @param outPrefix [in] - If not null, then every response line will be
   *                  prefixed with this string.
   *
   * @return bool
   */
  static bool SendData(const char *message, size_t message_size, const SockAddr &connectAddr, const char *outPrefix = NULL)
  {
    size_t totalLength;

    vector<char> buffer(max(MaxReplyLineSize,  MaxCommandSize));
    Socket sendSocket;
    FileHandle fileHandle;
    uint32_t magic;

    if (!sendSocket.OpenTCP(connectAddr.Type()))
    {
      fprintf(stderr, "Error creating %s socket: %s\n",
              Addr::TypeToString(connectAddr.Type()),
              SystemErrorToString(sendSocket.GetLastError()));
      return false;
    }


    if (!sendSocket.Connect(connectAddr))
    {
      fprintf(stderr, "Error connecting to beacon on %s: %s\n",
              connectAddr.ToString(),
              SystemErrorToString(sendSocket.GetLastError()));
      return false;
    }

    // Assemble the message
    magic = htonl(MagicMessageNumber);
    memcpy(&buffer.front(), &magic, sizeof(uint32_t));
    totalLength = message_size + sizeof(uint32_t);
    if (totalLength > MaxCommandSize)
    {
      fputs("Command too long. Not Sent.\n", stderr);
      return false;
    }

    memcpy(&buffer[sizeof(uint32_t)], message, message_size);

    // Send our message.
    if (!sendSocket.Send(&buffer.front(), totalLength))
    {
      fprintf(stderr, "Error sending command to beacon: %s\n", SystemErrorToString(sendSocket.GetLastError()));
      return false;
    }

    // Use stdio for simpler reading of the socket.
    fileHandle = fdopen(sendSocket, "r");
    if (!fileHandle.IsValid())
    {
      perror("Error opening socket file: ");
      return false;
    }

    // Read until done
    while (fgets(&buffer.front(), buffer.size(), fileHandle))
    {
      if (outPrefix)
        fputs(outPrefix, stdout);

      fputs(&buffer.front(), stdout);
    }

    if (ferror(fileHandle))
    {
      perror("\nConnection failed. Partial completion may have occurred: \n");
      return true;
    }

    return true;
  }
开发者ID:JakeMont,项目名称:OpenBFDD,代码行数:81,代码来源:bfdd-control.cpp

示例2: handleListenSocket

  void Beacon::handleListenSocket(Socket &socket)
  {
    SockAddr sourceAddr;
    IpAddr destIpAddr, sourceIpAddr;
    uint8_t ttl;
    BfdPacket packet;
    bool found;
    Session *session = NULL;

    if (!m_packet.DoRecvMsg(socket))
    {
      gLog.ErrnoError(m_packet.GetLastError(), "Error receiving on BFD listen socket");
      return;
    }

    sourceAddr = m_packet.GetSrcAddress();
    if (!LogVerify(sourceAddr.IsValid()))
      return;
    sourceIpAddr = IpAddr(sourceAddr);

    destIpAddr = m_packet.GetDestAddress();
    if (!destIpAddr.IsValid())
    {
      gLog.LogError("Could not get destination address for packet from %s.", sourceAddr.ToString());
      return;
    }

    ttl = m_packet.GetTTLorHops(&found);
    if (!found)
    {
      gLog.LogError("Could not get ttl for packet from %s.", sourceAddr.ToString());
      return;
    }

    LogOptional(Log::Packet, "Received bfd packet %zu bytes from %s to %s", m_packet.GetDataSize(), sourceAddr.ToString(), destIpAddr.ToString());

    //
    // Check ip specific stuff. See draft-ietf-bfd-v4v6-1hop-11.txt
    //

    // Port
    if (m_strictPorts)
    {
      if (sourceAddr.Port() < bfd::MinSourcePort) // max port is max value, so no need to check
      {
        LogOptional(Log::Discard, "Discard packet: bad source port %s to %s", sourceAddr.ToString(), destIpAddr.ToString());
        return;
      }
    }

    // TTL assumes that all control packets are from neighbors.
    if (ttl != 255)
    {
      gLog.Optional(Log::Discard, "Discard packet: bad ttl/hops %hhu", ttl);
      return;
    }

    if (!Session::InitialProcessControlPacket(m_packet.GetData(), m_packet.GetDataSize(), packet))
    {
      gLog.Optional(Log::Discard, "Discard packet");
      return;
    }

    // We have a (partially) valid packet ... now find the correct session.
    if (packet.header.yourDisc != 0)
    {
      DiscMapIt found = m_discMap.find(packet.header.yourDisc);
      if (found == m_discMap.end())
      {
        if (gLog.LogTypeEnabled(Log::DiscardDetail))
          Session::LogPacketContents(packet, false, true, sourceAddr, destIpAddr);

        gLog.Optional(Log::Discard, "Discard packet: no session found for yourDisc <%u>.", packet.header.yourDisc);
        return;
      }
      session = found->second;
      if (session->GetRemoteAddress() != sourceIpAddr)
      {
        if (gLog.LogTypeEnabled(Log::DiscardDetail))
          Session::LogPacketContents(packet, false, true, sourceAddr, destIpAddr);

        LogOptional(Log::Discard, "Discard packet: mismatched yourDisc <%u> and ip <from %s to %s>.", packet.header.yourDisc, sourceAddr.ToString(), destIpAddr.ToString());
        return;
      }
    }
    else
    {
      // No discriminator
      session = findInSourceMap(sourceIpAddr, destIpAddr);
      if (NULL == session)
      {
        // No session yet .. create one !?
        if (!m_allowAnyPassiveIP && m_allowedPassiveIP.find(sourceIpAddr) == m_allowedPassiveIP.end())
        {
          if (gLog.LogTypeEnabled(Log::DiscardDetail))
            Session::LogPacketContents(packet, false, true, sourceAddr, destIpAddr);

          LogOptional(Log::Discard, "Ignoring unauthorized bfd packets from %s",  sourceAddr.ToString());
          return;
        }
//.........这里部分代码省略.........
开发者ID:JakeMont,项目名称:OpenBFDD,代码行数:101,代码来源:Beacon.cpp


注:本文中的SockAddr::ToString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。