Beispiel #1
0
ndn_Error
ArduinoYunTcpTransportLite::connect
  (const char* host, int port, ElementListenerLite& elementListener)
{
  ndn_ElementReader_reset(&elementReader_, &elementListener);

  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  if (client_.connect(host, port) != 1)
    return NDN_ERROR_SocketTransport_cannot_connect_to_socket;

  return NDN_ERROR_success;
}
Beispiel #2
0
ndn_Error ndn_SocketTransport_connect
  (struct ndn_SocketTransport *self, ndn_SocketType socketType, const char *host,
   unsigned short port, struct ndn_ElementListener *elementListener)
{
  int socketDescriptor;

  ndn_ElementReader_reset(&self->elementReader, elementListener);

  if (socketType == SOCKET_UNIX) {
    struct sockaddr_un address;
    memset(&address, 0, sizeof(struct sockaddr_un));

    if ((socketDescriptor = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
      return NDN_ERROR_SocketTransport_cannot_connect_to_socket;

    address.sun_family = AF_UNIX;
    strcpy(address.sun_path, host);
    if (connect(socketDescriptor, (struct sockaddr *)&address,
                SUN_LEN(&address)) == -1) {
      close(socketDescriptor);
      return NDN_ERROR_SocketTransport_cannot_connect_to_socket;
    }
  }
  else {
    struct addrinfo hints;
    char portString[10];
    struct addrinfo *serverInfo;
    struct addrinfo *p;

    if (self->socketDescriptor >= 0) {
      close(self->socketDescriptor);
      self->socketDescriptor = -1;
    }

    ndn_memset((uint8_t *)&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    if (socketType == SOCKET_TCP)
      hints.ai_socktype = SOCK_STREAM;
    else if (socketType == SOCKET_UDP)
      hints.ai_socktype = SOCK_DGRAM;
    else
      return NDN_ERROR_unrecognized_ndn_SocketTransport;

    sprintf(portString, "%d", port);

    if (getaddrinfo(host, portString, &hints, &serverInfo) != 0)
      return NDN_ERROR_SocketTransport_error_in_getaddrinfo;

    // loop through all the results and connect to the first we can
    for(p = serverInfo; p != NULL; p = p->ai_next) {
      if ((socketDescriptor = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
        continue;

      if (connect(socketDescriptor, p->ai_addr, p->ai_addrlen) == -1) {
        close(socketDescriptor);
        continue;
      }

      break;
    }

    if (p == NULL) {
      freeaddrinfo(serverInfo);
      return NDN_ERROR_SocketTransport_cannot_connect_to_socket;
    }

    freeaddrinfo(serverInfo);
  }

  self->socketDescriptor = socketDescriptor;
  return NDN_ERROR_success;
}