示例#1
0
文件: socket.c 项目: maxott/oml
/** Eventloop callback called when a new connection is received on a listening Socket.
 *
 * This function accept()s the connection, and creates a SocketInt to wrap
 * the underlying system socket. It then runs the user-supplied callback
 * (passed to socket_server_new() when creating the listening Socket) on this
 * new object, passing it the handle (passed to socket_server_new( too))
 *
 * \param source source from which the event was received (e.g., an OComm Channel)
 * \param handle pointer to opaque data passed when creating the listening Socket
 */
static void
on_client_connect(
  SockEvtSource* source,
  //  SocketStatus status,
  void* handle
) {
  (void)source; // FIXME: Check why source parameter is unused
  socklen_t cli_len;

  SocketInt* self = (SocketInt*)handle;

  SocketInt* newSock = initialize(NULL);
  cli_len = sizeof(newSock->servAddr);
  newSock->sockfd = accept(self->sockfd,
                (struct sockaddr*)&newSock->servAddr,
                 &cli_len);
  if (newSock->sockfd < 0) {
    o_log(O_LOG_ERROR, "socket:%s: Error on accept: %s\n",
          self->name, strerror(errno));
    free(newSock);
    return;
  }

  sprintf(newSock->name, "%s-io:%d", self->name, newSock->sockfd);

  if (self->connect_callback) {
    self->connect_callback((Socket*)newSock, self->connect_handle);
  }
}
示例#2
0
文件: socket.c 项目: mytestbed/oml
/** Eventloop callback called when a new connection is received on a listening Socket.
 *
 * This function accept()s the connection, and creates a SocketInt to wrap
 * the underlying system socket. It then runs the user-supplied callback
 * (passed to socket_server_new() when creating the listening Socket) on this
 * new object, passing it the handle (passed to socket_server_new( too))
 *
 * \param source source from which the event was received (e.g., an OComm Channel)
 * \param handle pointer to opaque data passed when creating the listening Socket
 */
static void
on_client_connect(SockEvtSource* source, void* handle)
{
  (void)source; // FIXME: Check why source parameter is unused
  char host[ADDRLEN], serv[SERVLEN];
  size_t namesize;
  *host = 0;
  *serv = 0;

  socklen_t cli_len;
  SocketInt* self = (SocketInt*)handle;
  SocketInt* newSock = socket_initialize(NULL);
  cli_len = sizeof(newSock->servAddr.sa_stor);
  newSock->sockfd = accept(self->sockfd,
                &newSock->servAddr.sa,
                 &cli_len);

  if (newSock->sockfd < 0) {
    o_log(O_LOG_ERROR, "socket(%s): Error on accept: %s\n",
          self->name, strerror(errno));
    oml_free(newSock);
    return;
  }

  /* XXX: Duplicated somewhat with socket_in_new and s_connect */
  if (!getnameinfo(&newSock->servAddr.sa, cli_len,
        host, ADDRLEN, serv, SERVLEN,
        NI_NUMERICHOST|NI_NUMERICSERV)) {
    namesize =  strlen(host) + strlen(serv) + 3 + 1;
    newSock->name = oml_realloc(newSock->name, namesize);
    snprintf(newSock->name, namesize, "[%s]:%s", host, serv);

  } else {
    namesize =  strlen(host) + 4 + 10 + 1; /* XXX: 10 is arbitrarily chosen for the
                                              number of characters in sockfd's decimal
                                              representation */
    newSock->name = oml_realloc(newSock->name, namesize);
    snprintf(newSock->name, namesize, "%s-io:%d", self->name, newSock->sockfd);
    o_log(O_LOG_WARN, "socket(%s): Error resolving new client source, defaulting to %s: %s\n",
        self->name, newSock->name, strerror(errno));
  }

  if (self->connect_callback) {
    self->connect_callback((Socket*)newSock, self->connect_handle);
  }
}