コード例 #1
0
ファイル: comm.c プロジェクト: klampworks/my-tor-controller
int my_recv(int s, char* j) {

	sleep(1);
	int numbytes = recv(s, j, MAXDATASIZE-1, 0);
	j[numbytes] = '\0';
	clear_socket(s);
	return numbytes;
}
コード例 #2
0
/*
 * Get more LPC sockets structures if we run out
 */
static int more_lpc_sockets()
{
    int i;

    max_lpc_socks += 10;

    if (!lpc_socks)
        lpc_socks = CALLOCATE(10, lpc_socket_t, TAG_SOCKETS, "more_lpc_sockets");
    else
        lpc_socks = RESIZE(lpc_socks, max_lpc_socks, lpc_socket_t, TAG_SOCKETS, "more_lpc_sockets");

    i = max_lpc_socks;
    while (--i >= max_lpc_socks - 10)
        clear_socket(i, 0);

    return max_lpc_socks - 10;
}
コード例 #3
0
ファイル: comm.c プロジェクト: klampworks/my-tor-controller
int my_send(int s, const char *msg) {

	clear_socket(s);

	int len = strlen(msg);
	int r;

	if (msg[len-1] != '\n') {
		
		char *msg2 = alloca(len + 1);
		strncpy(msg2, msg, len);
		msg2[len] = '\n';
		msg2[len+1] = '\0';

		r = send(s, msg2, strlen(msg2), 0);
	} else {
	
		r = send(s, msg, strlen(msg), 0);
	}

	return r;
}
コード例 #4
0
ファイル: socket.c プロジェクト: KaSt/nereamud
/* Recover from a copyover - load players */
void copyover_recover() {     
  CHAR_DATA    *dMob;
  ACCOUNT_DATA *account;
  SOCKET_DATA  *dsock;
  FILE *fp;
  char acct[100];
  char name[100];
  char host[MAX_BUFFER];
  int desc;
      
  log_string("Copyover recovery initiated");

  if ((fp = fopen(COPYOVER_FILE, "r")) == NULL) {  
    log_string("Copyover file not found. Exitting.");
    exit (1);
  }
      
  /* In case something crashes - doesn't prevent reading */
  unlink(COPYOVER_FILE);

  for (;;) {  
    fscanf(fp, "%d %s %s %s\n", &desc, acct, name, host);
    if (desc == -1)
      break;

    // Many thanks to Rhaelar for the help in finding this bug; clear_socket
    // does not like receiving freshly malloc'd data. We have to make sure
    // everything is zeroed before we pass it to clear_socket
    //    dsock = malloc(sizeof(*dsock));
    dsock = calloc(1, sizeof(*dsock));
    clear_socket(dsock, desc);

    dsock->hostname = strdup(host);
    listPut(socket_list, dsock);
    propertyTablePut(sock_table, dsock);

    // load account data
    if((account = get_account(acct)) != NULL)
      socketSetAccount(dsock, account);
    // no luck!
    else {
      close_socket(dsock, FALSE);
      continue;
    }

    // load player data
    if ((dMob = get_player(name)) != NULL) {
      // attach to socket
      charSetSocket(dMob, dsock);
      socketSetChar(dsock, dMob);

      // try putting the character into the game
      // close the socket if we fail.
      if(!try_enter_game(dMob)) {
	// do not bother extracting, since we haven't entered the game yet
	unreference_player(socketGetChar(dsock));
	socketSetChar(dsock, NULL);
	close_socket(dsock, FALSE);
	continue;
      }
    }
    // no luck
    else {
      close_socket(dsock, FALSE);
      continue;
    }
   
    // Write something, and check if it goes error-free
    if (!text_to_socket(dsock, "\n\r <*>  And before you know it, everything has changed  <*>\n\r")) { 
      close_socket(dsock, FALSE);
      continue;
    }
  
    // make sure the socket can be used
    dsock->bust_prompt    =  TRUE;
    dsock->lookup_status  =  TSTATE_DONE;

    // let our modules know we've finished copying over a socket
    hookRun("copyover_complete", hookBuildInfo("sk", dsock));

    // negotiate compression
    text_to_buffer(dsock, (char *) compress_will2);
    text_to_buffer(dsock, (char *) compress_will);
  }
  fclose(fp);

  // now, set all of the sockets' control to the new fSet
  reconnect_copyover_sockets();
}     
コード例 #5
0
ファイル: socket.c プロジェクト: KaSt/nereamud
/* 
 * New_socket()
 *
 * Initializes a new socket, get's the hostname
 * and puts it in the active socket_list.
 */
SOCKET_DATA *new_socket(int sock)
{
  struct sockaddr_in   sock_addr;
  pthread_attr_t       attr;
  pthread_t            thread_lookup;
  LOOKUP_DATA        * lData;
  SOCKET_DATA        * sock_new;
  int                  argp = 1;
  socklen_t            size;

  /* initialize threads */
  pthread_attr_init(&attr);   
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

  /* create and clear the socket */
  sock_new = calloc(1, sizeof(SOCKET_DATA));

  /* attach the new connection to the socket list */
  FD_SET(sock, &fSet);

  /* clear out the socket */
  clear_socket(sock_new, sock);
  sock_new->closed = FALSE;

  /* set the socket as non-blocking */
  ioctl(sock, FIONBIO, &argp);

  /* update the socket list and table */
  listPut(socket_list, sock_new);
  propertyTablePut(sock_table, sock_new);

  /* do a host lookup */
  size = sizeof(sock_addr);
  if (getpeername(sock, (struct sockaddr *) &sock_addr, &size) < 0)
  {
    perror("New_socket: getpeername");
    sock_new->hostname = strdup("unknown");
  }
  else
  {
    /* set the IP number as the temporary hostname */
    sock_new->hostname = strdup(inet_ntoa(sock_addr.sin_addr));

    if (!compares(sock_new->hostname, "127.0.0.1"))
    {
      /* allocate some memory for the lookup data */
      if ((lData = malloc(sizeof(*lData))) == NULL)
      {
        bug("New_socket: Cannot allocate memory for lookup data.");
        abort();
      }

      /* Set the lookup_data for use in lookup_address() */
      lData->buf    =  strdup((char *) &sock_addr.sin_addr);
      lData->dsock  =  sock_new;

      /* dispatch the lookup thread */
      pthread_create(&thread_lookup, &attr, &lookup_address, (void*) lData);
    }
    else sock_new->lookup_status++;
  }

  /* negotiate compression */
  // text_to_buffer(sock_new, (char *) compress_will2);
  // text_to_buffer(sock_new, (char *) compress_will);

  /* send the greeting */
  // text_to_buffer(sock_new, bufferString(greeting));

  /* everything went as it was supposed to */
  return sock_new;
}
コード例 #6
0
ファイル: z_packet.cpp プロジェクト: jtalz/znetlib
zPacket::~zPacket() { clear_ext(); clear_event(); clear_socket(); };
int _handle_client_request(download_clientinfo* clientinfo)
{
	int da_ret = 0;
	int msgType = 0;

	// NULL - checking
	if (!clientinfo) {
		TRACE_DEBUG_MSG("NULL-CHECK");
		return -1;
	}

	switch (msgType = ipc_receive_header(clientinfo->clientfd)) {
	case DOWNLOAD_CONTROL_STOP:
		if (clientinfo->state >= DOWNLOAD_STATE_FINISHED) {
			// clear slot requested by client after finished download
			TRACE_DEBUG_INFO_MSG("request Free slot to main thread");
			clear_socket(clientinfo);
			break;
		}
		TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_STOP");
		da_ret = da_cancel_download(clientinfo->req_id);
		CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
		if (da_ret != DA_RESULT_OK) {
			/* FIXME : need to seperate in detail according to error return values */
			clientinfo->state = DOWNLOAD_STATE_FAILED;
			clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
			TRACE_DEBUG_MSG("Fail to request cancel [%d]", da_ret);
		} else {
			clientinfo->state = DOWNLOAD_STATE_STOPPED;
			clientinfo->err = DOWNLOAD_ERROR_NONE;
			if (clientinfo->requestinfo) {
				if (clientinfo->requestinfo->notification)
					set_downloadedinfo_appfw_notification(clientinfo);
				download_provider_db_requestinfo_remove(clientinfo->
					requestinfo->requestid);
			}
			download_provider_db_history_new(clientinfo);
		}
		ipc_send_stateinfo(clientinfo);
		CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
		break;
	case DOWNLOAD_CONTROL_PAUSE:
		TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_PAUSE");
		da_ret = da_suspend_download(clientinfo->req_id);
		CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
		if (da_ret != DA_RESULT_OK) {
			/* FIXME : need to seperate in detail according to error return values */
			clientinfo->state = DOWNLOAD_STATE_FAILED;
			clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
			TRACE_DEBUG_MSG("Fail to request suspend [%d]", da_ret);
		} else {
			clientinfo->state = DOWNLOAD_STATE_PAUSE_REQUESTED;
			clientinfo->err = DOWNLOAD_ERROR_NONE;
		}
		ipc_send_stateinfo(clientinfo);
		CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
		break;
	case DOWNLOAD_CONTROL_RESUME:
		TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_RESUME");
		da_ret = da_resume_download(clientinfo->req_id);
		CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
		if (da_ret != DA_RESULT_OK) {
			/* FIXME : need to seperate in detail according to error return values */
			clientinfo->state = DOWNLOAD_STATE_FAILED;
			clientinfo->err = DOWNLOAD_ERROR_INVALID_PARAMETER;
			TRACE_DEBUG_MSG("Fail to request resume [%d]", da_ret);
		} else {
			clientinfo->state = DOWNLOAD_STATE_DOWNLOADING;
			clientinfo->err = DOWNLOAD_ERROR_NONE;
		}
		ipc_send_stateinfo(clientinfo);
		CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
		break;
	case DOWNLOAD_CONTROL_GET_STATE_INFO:	// sync call
		TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_GET_STATE_INFO");
		CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
		ipc_send_stateinfo(clientinfo);
		CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
		break;
	case DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO:	// sync call
		TRACE_DEBUG_INFO_MSG("DOWNLOAD_CONTROL_GET_DOWNLOAD_INFO");
		CLIENT_MUTEX_LOCK(&(clientinfo->client_mutex));
		ipc_send_downloadinfo(clientinfo);
		CLIENT_MUTEX_UNLOCK(&(clientinfo->client_mutex));
		break;
	case -1:
	case 0:
		TRACE_DEBUG_MSG("(Closed Socket ) terminate event thread (%d)",
			msgType);
		// bloken socket... it seems the client is dead or closed by agent thread.
		// close socket, this will break the loop. and terminate this thread.
		clear_socket(clientinfo);
		return -1;
	default:
		TRACE_DEBUG_MSG("Unknow message [%d]", msgType);
		return -1;
	}
	return 0;
}