Exemplo n.º 1
0
// Parse the answers of the child processes transferring files
void parseFileProcessesRequest(int i, char * buffer) {
    // if the fd closed
    if(readline(fileprocesses[i]->in, buffer, MAXBUF-1) == 0) {
        printf("\rChild process MIA...\n");
        senderror(fileprocesses[i]->sender->fd, CMD_FILE, ERR_INTERNAL);
        removeChildProcess(i);
        return;
    }

    if(buffer[3] != '#') {
        printf("\rChild process is sending strange commands... Better check that, bro.\n");
        senderror(fileprocesses[i]->sender->fd, CMD_FILE, ERR_INTERNAL);
        removeChildProcess(i);
        return;
    }

    buffer[3] = 0;
    if(strcmp(buffer, CMD_ERR) == 0) {
        printf("\rChild process failed with error: %s\n", buffer+4);
        senderror(fileprocesses[i]->sender->fd, CMD_FILE, ERR_INTERNAL);
        removeChildProcess(i);
        return;
    }

    if(strcmp(buffer, CMD_PORT) == 0 && buffer[9] == '#') {
        char * msg = malloc(sizeof(char)*(6+strlen(buffer+4)));
        buffer[9] = 0;
        printf("\rChild process waiting on port %s\n", buffer+4);
        sprintf(msg, "%s#%s#%s\n", CMD_PORT, buffer+4, buffer+10);
        sendline(fileprocesses[i]->sender->fd, msg);
        free(msg);
        return;
    }

    if(strcmp(buffer, CMD_READY) == 0) {
        printf("\rFile successfully uploaded !\n");
        int j;
        char * msg = malloc(sizeof(char)*(6+strlen(buffer+4)));
        sprintf(msg, "%s#%s\n", CMD_FILE, buffer+4);
        for(j=0; j<fileprocesses[i]->sender->game->nb_players; j++)
            if(fileprocesses[i]->sender->game->players[j] != fileprocesses[i]->sender)
                sendline(fileprocesses[i]->sender->game->players[j]->fd, msg);
        sprintf(msg, "%05d", fileprocesses[i]->sender->game->nb_players);
        sendack(fileprocesses[i]->out, NULL, msg);
        free(msg);
        return;
    }

    if(strcmp(buffer, CMD_ACK) == 0) {
        printf("\rFile successfully broadcasted !\n");
        sendack(fileprocesses[i]->sender->fd, CMD_FILE, buffer+4);
        removeChildProcess(i);
        return;
    }
}
Exemplo n.º 2
0
void sendlines(int step, struct worker_info *info, bool *toup, bool *todown)
{
	if (step < -1)
		return;
	if (debug) printf("%d sendlines %d\n", info->id, step);
	int idup = (info->number_threads + info->id - 1) % info->number_threads;
	int iddown = (info->id + 1) % info->number_threads;
	sendline(step, idup, toup, info->w, 1);
	sendline(step, iddown, todown, info->w, 0);
	if (debug) printf("%d sendlines done %d\n", info->id, step);
}
Exemplo n.º 3
0
/*
 * Function: pop_quit
 *
 * Purpose: Quit the connection to the server,
 *
 * Arguments:
 * 	server	The server to quit.
 *
 * Return value: 0 for success, non-zero otherwise with error in
 * 	pop_error.
 *
 * Side Effects: The popserver passed in is unusable after this
 * 	function is called, even if an error occurs.
 */
int
pop_quit (popserver server)
{
  int ret = 0;

  if (server->file >= 0)
    {
      if (pop_retrieve_flush (server))
	{
	  ret = -1;
	}

      if (sendline (server, "QUIT") || getok (server))
	{
	  ret = -1;
	}

      close (server->file);
    }

  free (server->buffer);
  free ((char *) server);

  return (ret);
}
Exemplo n.º 4
0
// Send message to a player
void send_to_player(player p, char * buffer) {
    int j;

    // Not in a game
    if(!p->game) {
        senderror(p->fd, CMD_SEND_TO_PLAYER, ERR_NOT_IN_GAME);
        return;
    }

    // get the name of the receiver
    char * pointer = strchr(buffer+4, '#');
    if(strlen(buffer+4) < 3 || !pointer) {
        senderror(p->fd, NULL, ERR_NOT_RECOGNIZED);
        return;
    }
    *pointer = 0;

    for(j=0; j<p->game->nb_players; j++)
        if(strcmp(p->game->players[j]->name, buffer+4) == 0) {
            char * msg = malloc((strlen(pointer+1)+strlen(p->name)+7)*sizeof(char));
            sprintf(msg, "%s#%s#%s\n", CMD_SEND_TO_PLAYER, p->name, pointer+1);
            sendline(p->game->players[j]->fd, msg);
            free(msg);
            return;
        }

    senderror(p->fd, CMD_SEND_TO_PLAYER, ERR_NO_SUCH_PLAYER);
}
Exemplo n.º 5
0
int
pop_multi_first (popserver server, const char *command, char **response)
{
  if (server->in_multi)
    {
      strcpy (pop_error,
	      "Already in multi-line query in pop_multi_first");
      return (-1);
    }

  if (sendline (server, command) || (pop_getline (server, response) < 0))
    {
      return (-1);
    }

  if (0 == strncmp (*response, "-ERR", 4))
    {
      snprintf (pop_error, ERROR_MAX, "%s", *response);
      return (-1);
    }
  else if (0 == strncmp (*response, "+OK", 3))
    {
      for (*response += 3; **response == ' '; (*response)++) /* empty */;
      server->in_multi = true;
      return (0);
    }
  else
    {
      strcpy (pop_error,
	      "Unexpected response from server in pop_multi_first");
      return (-1);
    }
}
Exemplo n.º 6
0
// send an available game id
void getAvailableId(player p) {
    int j,k;

    int r=rand()%99999;
    int id;
    for(k=0;k<99999;k++) {
        int exist = 0;
        id = (r+k)%99999+1;
        for(j=0;j<nb_game; j++)
            if(games[j]->id == id) {
                exist = 1;
                break;
            }
        if(!exist)
            break;
    }

    // No available id found
    if(k == 99999) {
        sendline(p->fd, "ERR#WTF ?? No more ids available ?!\n");
        return;
    }

    char msg[6];
    sprintf(msg, "%05d\n", id);
    sendack(p->fd, CMD_CREATE, msg);
}
Exemplo n.º 7
0
// bind to a game
void bindToGame(player p, char * buffer) {
    int j;
    buffer[9] = 0;

    // already in a game
    if(p->game) {
        senderror(p->fd, NULL, ERR_ALREADY_IN_GAME);
        return;
    }

    // id of the game
    int id = atoi(buffer+4);

    // search for the game
    game g = NULL;
    for(j=0; j<nb_game; j++)
        if(games[j]->id == id) {
            g = games[j];
            break;
        }

    // if it doesn't exist, 
    if(!g) {
        senderror(p->fd, CMD_BIND, ERR_GAME_DOESNT_EXIST);
        return;
    }

    // update the name of the player
    strncpy(p->name, buffer+10, MAX_NAME_SIZE);

    // if game is already full
    if(g->nb_players >= MAX_PLAYER_GAME) {
        printf("\rToo many players in this game !\n");
        senderror(p->fd, CMD_BIND, ERR_TOO_MANY_PLAYERS_IN_GAME);
        return;
    }

    // check if the name is already used
    for(j=0; j<g->nb_players; j++)
        if(strcmp(g->players[j]->name, p->name) == 0) {
            printf("\rName already used !\n");
            senderror(p->fd, CMD_BIND, ERR_NAME_USED);
            return;
        }

    // send notification to players in the game
    char * msg = malloc(sizeof(char)*(6+strlen(p->name)));
    sprintf(msg, "%s#%s\n", CMD_CONNECTED, p->name);
    for(j=0;j<g->nb_players; j++)
        if(g->players[j] != p)
            sendline(g->players[j]->fd, msg);
    free(msg);

    // update player, game and table of games
    p->game = g;
    g->players[g->nb_players] = p;
    g->nb_players++;
    sendack(p->fd, CMD_BIND, buffer+4);
}
Exemplo n.º 8
0
//start worker "ReadInput"
void ReadInput::start()
{
    int i=0;
    while(i<10000) {
        QThread::msleep(10);
        sendline();
        i++;

    }
    emit signalReadInputEnded();
}
Exemplo n.º 9
0
/*---------------------------------------------------------------------------*/
void
shell_prompt(char *str)
{
  char *line;
  line = alloc_line();
  if(line != NULL) {
    strncpy(line, str, TELNETD_CONF_LINELEN);
    /*    petsciiconv_toascii(line, TELNETD_CONF_LINELEN);*/
    sendline(line);
  }
}
Exemplo n.º 10
0
Arquivo: pop.c Projeto: Sunmonds/emacs
/*
 * Function: pop_stat
 *
 * Purpose: Issue the STAT command to the server and return (in the
 * 	value parameters) the number of messages in the maildrop and
 * 	the total size of the maildrop.
 *
 * Return value: 0 on success, or non-zero with an error in pop_error
 * 	in failure.
 *
 * Side effects: On failure, may make further operations on the
 * 	connection impossible.
 */
int
pop_stat (popserver server, int *count, int *size)
{
  char *fromserver;
  char *end_ptr;

  if (server->in_multi)
    {
      strcpy (pop_error, "In multi-line query in pop_stat");
      return (-1);
    }

  if (sendline (server, "STAT") || (pop_getline (server, &fromserver) < 0))
    return (-1);

  if (strncmp (fromserver, "+OK ", 4))
    {
      if (0 == strncmp (fromserver, "-ERR", 4))
	{
	  strncpy (pop_error, fromserver, ERROR_MAX);
	}
      else
	{
	  strcpy (pop_error,
		  "Unexpected response from POP server in pop_stat");
	  pop_trash (server);
	}
      return (-1);
    }

  errno = 0;
  *count = strtol (&fromserver[4], &end_ptr, 10);
  /* Check validity of string-to-integer conversion. */
  if (fromserver + 4 == end_ptr || *end_ptr != ' ' || errno)
    {
      strcpy (pop_error, "Unexpected response from POP server in pop_stat");
      pop_trash (server);
      return (-1);
    }

  fromserver = end_ptr;

  errno = 0;
  *size = strtol (fromserver + 1, &end_ptr, 10);
  if (fromserver + 1 == end_ptr || errno)
    {
      strcpy (pop_error, "Unexpected response from POP server in pop_stat");
      pop_trash (server);
      return (-1);
    }

  return (0);
}
Exemplo n.º 11
0
/*
 * Function: pop_reset
 *
 * Purpose: Reset the server to its initial connect state
 *
 * Arguments:
 * 	server	The server.
 *
 * Return value: 0 for success, non-0 with error in pop_error
 * 	otherwise.
 *
 * Side effects: Closes the connection on error.
 */
int
pop_reset (popserver server)
{
  if (pop_retrieve_flush (server))
    {
      return (-1);
    }

  if (sendline (server, "RSET") || getok (server))
    return (-1);

  return (0);
}
Exemplo n.º 12
0
/*---------------------------------------------------------------------------*/
static void
sendopt(u8_t option, u8_t value)
{
  char *line;
  line = alloc_line();
  if(line != NULL) {
    line[0] = TELNET_IAC;
    line[1] = option;
    line[2] = value;
    line[3] = 0;
    sendline(line);
  }
}
Exemplo n.º 13
0
int wctx(long flen)
{
  int thisblklen;
  int sectnum, attempts, firstch;
  long charssent;
  
//DSERIAL.println("\nwctx");

  charssent = 0;  
  firstsec=TRUE;  
  thisblklen = blklen;
  vfile(F("wctx:file length=%ld"), flen);

  while ((firstch=readline(Rxtimeout))!=NAK && firstch != WANTCRC
    && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
    ;
  if (firstch==CAN) {
    zperr("Receiver CANcelled");
    return ERROR;
  }
  if (firstch==WANTCRC)
    Crcflg=TRUE;
  if (firstch==WANTG)
    Crcflg=TRUE;
  sectnum=0;
  for (;;) {
    if (flen <= (charssent + 896L))
      thisblklen = 128;
    if ( !filbuf(txbuf, thisblklen))
      break;
    if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
      return ERROR;
    charssent += thisblklen;
  }
  //fclose(in);
  fout.close();
  attempts=0;
  do {
    purgeline();
    sendline(EOT);
    //fflush(stdout);
    ++attempts;
  }
  while ((firstch=(readline(Rxtimeout)) != ACK) && attempts < Tx_RETRYMAX);
  if (attempts == Tx_RETRYMAX) {
    zperr("No ACK on EOT");
    return ERROR;
  }
  else
    return OK;
}
Exemplo n.º 14
0
/*
 * Function: pop_noop
 *
 * Purpose: Send a noop command to the server.
 *
 * Argument:
 * 	server	The server to send to.
 *
 * Return value: 0 on success, non-zero with error in pop_error
 * 	otherwise.
 *
 * Side effects: Closes connection on error.
 */
int
pop_noop (popserver server)
{
  if (server->in_multi)
    {
      strcpy (pop_error, "In multi-line query in pop_noop");
      return (-1);
    }

  if (sendline (server, "NOOP") || getok (server))
    return (-1);

  return (0);
}
Exemplo n.º 15
0
// broadcast a message
void broadcastMessage(player p, char * buffer) {
    int j;

    if(!p->game) {
        senderror(p->fd, buffer, ERR_NOT_IN_GAME);
        return;
    }

    char * msg = malloc((strlen(buffer+4)+strlen(p->name)+7)*sizeof(char));
    sprintf(msg, "%s#%s#%s\n", buffer, p->name, buffer+4);
    for(j=0; j < p->game->nb_players; j++)
        if ( p->game->players[j] != p )
            sendline(p->game->players[j]->fd, msg);
    free(msg);
}
Exemplo n.º 16
0
/* Function: pop_delete
 *
 * Purpose: Delete a specified message.
 *
 * Arguments:
 * 	server	Server from which to delete the message.
 * 	message	Message to delete.
 *
 * Return value: 0 on success, non-zero with error in pop_error
 * 	otherwise.
 */
int
pop_delete (popserver server, int message)
{
  if (server->in_multi)
    {
      strcpy (pop_error, "In multi-line query in pop_delete");
      return (-1);
    }

  sprintf (pop_error, "DELE %d", message);

  if (sendline (server, pop_error) || getok (server))
    return (-1);

  return (0);
}
Exemplo n.º 17
0
// Send a message to the game master
void send_to_game_master(player p, char * buffer) {
    // Not in a game
    if(!p->game) {
        senderror(p->fd, CMD_SEND_GM, ERR_NOT_IN_GAME);
        return;
    }

    // No game master in this game
    if(!p->game->game_master) {
        senderror(p->fd, CMD_SEND_GM, ERR_NO_GM);
        return;
    }

    char * msg = malloc((strlen(buffer+4)+strlen(p->name)+7)*sizeof(char));
    sprintf(msg, "%s#%s#%s\n", CMD_SEND_GM, p->name, buffer+4);
    sendline(p->game->game_master->fd, msg);
    free(msg);
}
Exemplo n.º 18
0
/*
 * Function: pop_last
 *
 * Purpose: Find out the highest seen message from the server.
 *
 * Arguments:
 * 	server	The server.
 *
 * Return value: If successful, the highest seen message, which is
 * 	greater than or equal to 0.  Otherwise, a negative number with
 * 	the error explained in pop_error.
 *
 * Side effects: Closes the connection on error.
 */
int
pop_last (popserver server)
{
  char *fromserver;

  if (server->in_multi)
    {
      strcpy (pop_error, "In multi-line query in pop_last");
      return (-1);
    }

  if (sendline (server, "LAST"))
    return (-1);

  if (pop_getline (server, &fromserver) < 0)
    return (-1);

  if (! strncmp (fromserver, "-ERR", 4))
    {
      snprintf (pop_error, ERROR_MAX, "%s", fromserver);
      return (-1);
    }
  else if (strncmp (fromserver, "+OK ", 4))
    {
      strcpy (pop_error, "Unexpected response from server in pop_last");
      pop_trash (server);
      return (-1);
    }
  else
    {
      char *end_ptr;
      int count;
      errno = 0;
      count = strtol (&fromserver[4], &end_ptr, 10);
      if (fromserver + 4 == end_ptr || errno)
	{
	  strcpy (pop_error, "Unexpected response from server in pop_last");
	  pop_trash (server);
	  return (-1);
	}
      return count;
    }
}
Exemplo n.º 19
0
static int fetch_with_import(struct transport *transport,
			     int nr_heads, struct ref **to_fetch)
{
	struct child_process fastimport;
	struct helper_data *data = transport->data;
	int i;
	struct ref *posn;
	struct strbuf buf = STRBUF_INIT;

	get_helper(transport);

	if (get_importer(transport, &fastimport))
		die("Couldn't run fast-import");

	for (i = 0; i < nr_heads; i++) {
		posn = to_fetch[i];
		if (posn->status & REF_STATUS_UPTODATE)
			continue;

		strbuf_addf(&buf, "import %s\n", posn->name);
		sendline(data, &buf);
		strbuf_reset(&buf);
	}
	disconnect_helper(transport);
	finish_command(&fastimport);
	free(fastimport.argv);
	fastimport.argv = NULL;

	for (i = 0; i < nr_heads; i++) {
		char *private;
		posn = to_fetch[i];
		if (posn->status & REF_STATUS_UPTODATE)
			continue;
		if (data->refspecs)
			private = apply_refspecs(data->refspecs, data->refspec_nr, posn->name);
		else
			private = strdup(posn->name);
		read_ref(private, posn->old_sha1);
		free(private);
	}
Exemplo n.º 20
0
static int fetch_with_fetch(struct transport *transport,
			    int nr_heads, struct ref **to_fetch)
{
	struct helper_data *data = transport->data;
	int i;
	struct strbuf buf = STRBUF_INIT;

	standard_options(transport);

	for (i = 0; i < nr_heads; i++) {
		const struct ref *posn = to_fetch[i];
		if (posn->status & REF_STATUS_UPTODATE)
			continue;

		strbuf_addf(&buf, "fetch %s %s\n",
			    sha1_to_hex(posn->old_sha1), posn->name);
	}

	strbuf_addch(&buf, '\n');
	sendline(data, &buf);

	while (1) {
		recvline(data, &buf);

		if (!prefixcmp(buf.buf, "lock ")) {
			const char *name = buf.buf + 5;
			if (transport->pack_lockfile)
				warning("%s also locked %s", data->name, name);
			else
				transport->pack_lockfile = xstrdup(name);
		}
		else if (!buf.len)
			break;
		else
			warning("%s unexpectedly said: '%s'", data->name, buf.buf);
	}
	strbuf_release(&buf);
	return 0;
}
Exemplo n.º 21
0
void
shell_output_P(PGM_P str1, PGM_P str2)
{
	static unsigned len;
	char *line;

	line = alloc_line();
	if (line != NULL) {
		len = strlen_P(str1);
		strncpy_P(line, str1, TELNETD_CONF_LINELEN);
		if (len < TELNETD_CONF_LINELEN)
			strncpy_P(line + len, str2, TELNETD_CONF_LINELEN - len);
		len = strlen(line);
		if (len < TELNETD_CONF_LINELEN - 2) {
			line[len] = ISO_cr;
			line[len + 1] = ISO_nl;
			line[len + 2] = 0;
		}
		/*    petsciiconv_toascii(line, TELNETD_CONF_LINELEN);*/
		sendline(line);
	}
}
Exemplo n.º 22
0
static int wctx(struct zm_fileinfo *zi)
{
    register size_t thisblklen;
    register int sectnum, attempts, firstch;

    firstsec = TRUE;
    thisblklen = blklen;

    while ((firstch = READLINE_PF(Rxtimeout)) != NAK && firstch != WANTCRC && firstch != WANTG && firstch != TIMEOUT && firstch != CAN);
    if (firstch == CAN) {
        return ERROR;
    }
    if (firstch == WANTCRC)
        Crcflg = TRUE;
    if (firstch == WANTG)
        Crcflg = TRUE;
    sectnum = 0;
    for (;;) {
        if (zi->bytes_total <= (zi->bytes_sent + 896L))
            thisblklen = 128;
        if (!filbuf(txbuf, thisblklen))
            break;
        if (wcputsec(txbuf, ++sectnum, thisblklen) == ERROR)
            return ERROR;
        zi->bytes_sent += thisblklen;
    }
    fclose(input_f);
    attempts = 0;
    do {
        purgeline(io_mode_fd);
        sendline(EOT);
        flushmo();
        ++attempts;
    } while ((firstch = (READLINE_PF(Rxtimeout)) != ACK) && attempts < RETRYMAX);
    if (attempts == RETRYMAX) {
        return ERROR;
    } else
        return OK;
}
Exemplo n.º 23
0
static int disconnect_helper(struct transport *transport)
{
	struct helper_data *data = transport->data;
	struct strbuf buf = STRBUF_INIT;

	if (data->helper) {
		if (debug)
			fprintf(stderr, "Debug: Disconnecting.\n");
		if (!data->no_disconnect_req) {
			strbuf_addf(&buf, "\n");
			sendline(data, &buf);
		}
		close(data->helper->in);
		close(data->helper->out);
		fclose(data->out);
		finish_command(data->helper);
		free((char *)data->helper->argv[0]);
		free(data->helper->argv);
		free(data->helper);
		data->helper = NULL;
	}
	return 0;
}
Exemplo n.º 24
0
const char *usersockname(const char *file)
{
  static char buf[UNIX_PATH_MAX];
  char filebuf[UNIX_PATH_MAX];
  char sock_dir[UNIX_PATH_MAX];

  if (file != NULL && file[0] == '/') {
    return file;
  }

  if (file == NULL) {
    strlcpy(filebuf, "backtick", UNIX_PATH_MAX);
  } else {
    strlcpy(filebuf, file, UNIX_PATH_MAX);
  }

  if (!get_ud_path(sock_dir, sizeof(sock_dir))) {
    sendline("uim-fep cannot create directory");
    /* exit(EXIT_FAILURE); */
  }
  snprintf(buf, sizeof(buf), "%s/%s", sock_dir, filebuf);

  return buf;
}
Exemplo n.º 25
0
static int wcputsec(char *buf, int sectnum, size_t cseclen)
{
    int checksum, wcj;
    char *cp;
    unsigned oldcrc;
    int firstch;
    int attempts;

    firstch = 0;                /* part of logic to detect CAN CAN */

    for (attempts = 0; attempts <= RETRYMAX; attempts++) {
        Lastrx = firstch;
        sendline(cseclen == 1024 ? STX : SOH);
        sendline(sectnum);
        sendline(-sectnum - 1);
        oldcrc = checksum = 0;
        for (wcj = cseclen, cp = buf; --wcj >= 0;) {
            sendline(*cp);
            oldcrc = updcrc((0377 & *cp), oldcrc);
            checksum += *cp++;
        }
        if (Crcflg) {
            oldcrc = updcrc(0, updcrc(0, oldcrc));
            sendline((int) oldcrc >> 8);
            sendline((int) oldcrc);
        } else
            sendline(checksum);

        flushmo();
        if (Optiong) {
            firstsec = FALSE;
            return OK;
        }
        firstch = READLINE_PF(Rxtimeout);
      gotnak:
        switch (firstch) {
        case CAN:
            if (Lastrx == CAN) {
              cancan:
                return ERROR;
            }
            break;
        case TIMEOUT:
            continue;
        case WANTCRC:
            if (firstsec)
                Crcflg = TRUE;
        case NAK:
            continue;
        case ACK:
            firstsec = FALSE;
            Totsecs += (cseclen >> 7);
            return OK;
        case ERROR:
            break;
        default:
            break;
        }
        for (;;) {
            Lastrx = firstch;
            if ((firstch = READLINE_PF(Rxtimeout)) == TIMEOUT)
                break;
            if (firstch == NAK || firstch == WANTCRC)
                goto gotnak;
            if (firstch == CAN && Lastrx == CAN)
                goto cancan;
        }
    }
Exemplo n.º 26
0
// printSensorReading(char *sensorName)
// Description: Connect to server adhering to Three Mile Island protocol and
//              print sensor readings server sends.
// Input: String of which sensor to get reading.
// Output: None
void printSensorReading(char *sensorName)
{
  /*
   Connect to redirect server to determine which sensor to connect to.
  */
  //int sockfd = connectToHost("threemileisland.info", 0xbaad);
  int sockfd = connectToHost("threemileisland.cs.dartmouth.edu", 0xbaad);
  
  if (sockfd < 0)
  {
    return;
  }
  
  sendline(sockfd, "AUTH secretpassword");
  
  char hostname[64];
  int port;
  char auth[32];
  
  if (recvlinef(sockfd, "CONNECT %64s %d %32s", hostname, &port, auth) != 3)
  {
    fprintf(stderr, "Failed to read connect data.\n");
    shutdown(sockfd, SHUT_RDWR);
    return;
  }
  
  shutdown(sockfd, SHUT_RDWR);
  close(sockfd);
  
  /*
    Connect to the specified sensor we were told to to retrieve reading.
  */
  int sensorfd = connectToHost(hostname, port);
  
  if (sensorfd < 0)
  {
    return;
  }
 
  //sendline(sensorfd, "AUTH %s", auth);
  sendline(sensorfd, "AUTH networks");
 
  char value[64];
  
  if (recvlinef(sensorfd, "%64s", value) != 1)
  {
    fprintf(stderr, "Failed to read auth data.\n");
    shutdown(sensorfd, SHUT_RDWR);
    return;
  }
 
  if (strcmp(value, "SUCCESS") != 0)
  {
    fprintf(stderr, "Authentication failed.");
    printf("Authstr: AUTH %s\n",value);
    shutdown(sensorfd, SHUT_RDWR);
    return;
  }
  
  sendline(sensorfd, sensorName);
  
  time_t timestamp;
  int reading;
  char units[16];
  
  if (recvlinef(sensorfd, "%d %d %16s", &timestamp, &reading, units) != 3)
  {
    fprintf(stderr, "Failed to read sensor data.\n");
    shutdown(sensorfd, SHUT_RDWR);
    return;
  }
  
  char formatted_time[64];
  strftime(formatted_time, 64, "%c", localtime(&timestamp));
  
  printf("\n\tThe last %s was taken %s and was %d %s\n\n\n", sensorName, formatted_time, reading, units);
  
  sendline(sensorfd, "CLOSE");
  
  shutdown(sensorfd, SHUT_RDWR);
  close(sensorfd);
}
Exemplo n.º 27
0
static void xchgline(struct helper_data *helper, struct strbuf *buffer)
{
	sendline(helper, buffer);
	recvline(helper, buffer);
}
Exemplo n.º 28
0
int wcputsec(char *buf,int sectnum,int cseclen)
{
  int checksum, wcj;
  char *cp;
  unsigned oldcrc;
  int firstch;
  uint8_t attempts;

  firstch=0;      /* part of logic to detect CAN CAN */

  if (Verbose>2)
    fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 );
  else if (Verbose>1)
    fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );
  for (attempts=0; attempts <= Tx_RETRYMAX; attempts++) {
    Lastrx= firstch;
    sendline(cseclen==1024?STX:SOH);
    sendline(sectnum);
    sendline(-sectnum -1);
    oldcrc=checksum=0;
    for (wcj=cseclen,cp=buf; --wcj>=0; ) {
      sendline(*cp);
      oldcrc=updcrc((0377& *cp), oldcrc);
      checksum += *cp++;
    }
    if (Crcflg) {
      oldcrc=updcrc(0,updcrc(0,oldcrc));
      sendline((int)oldcrc>>8);
      sendline((int)oldcrc);
    }
    else
      sendline(checksum);

    if (Optiong) {
      firstsec = FALSE; 
      return OK;
    }
    firstch = readline(Rxtimeout);
gotnak:
    switch (firstch) {
    case CAN:
      if(Lastrx == CAN) {
cancan:
        zperr("Cancelled");  
        return ERROR;
      }
      break;
    case TIMEOUT:
      zperr("Timeout on sector ACK"); 
      continue;
    case WANTCRC:
      if (firstsec)
        Crcflg = TRUE;
    case NAK:
      zperr("NAK on sector"); 
      continue;
    case ACK: 
      firstsec=FALSE;
      Totsecs += (cseclen>>7);
      return OK;
    case ERROR:
      zperr("Got burst for sector ACK"); 
      break;
    default:
      zperr("Got %02x for sector ACK", firstch); 
      break;
    }
    for (;;) {
      Lastrx = firstch;
      if ((firstch = readline(Rxtimeout)) == TIMEOUT)
        break;
      if (firstch == NAK || firstch == WANTCRC)
        goto gotnak;
      if (firstch == CAN && Lastrx == CAN)
        goto cancan;
    }
  }
Exemplo n.º 29
0
/*
 * Function: pop_open (char *host, char *username, char *password,
 * 		       int flags)
 *
 * Purpose: Establishes a connection with a post-office server, and
 * 	completes the authorization portion of the session.
 *
 * Arguments:
 * 	host	The server host with which the connection should be
 * 		established.  Optional.  If omitted, internal
 * 		heuristics will be used to determine the server host,
 * 		if possible.
 * 	username
 * 		The username of the mail-drop to access.  Optional.
 * 		If omitted, internal heuristics will be used to
 * 		determine the username, if possible.
 * 	password
 * 		The password to use for authorization.  If omitted,
 * 		internal heuristics will be used to determine the
 * 		password, if possible.
 * 	flags	A bit mask containing flags controlling certain
 * 		functions of the routine.  Valid flags are defined in
 * 		the file pop.h
 *
 * Return value: Upon successful establishment of a connection, a
 * 	non-null popserver will be returned.  Otherwise, null will be
 * 	returned, and the string variable pop_error will contain an
 * 	explanation of the error.
 */
popserver
pop_open (char *host, char *username, char *password, int flags)
{
  int sock;
  popserver server;

  /* Determine the user name */
  if (! username)
    {
      username = getenv ("USER");
      if (! (username && *username))
	{
	  username = getlogin ();
	  if (! (username && *username))
	    {
	      struct passwd *passwd;
	      passwd = getpwuid (getuid ());
	      if (passwd && passwd->pw_name && *passwd->pw_name)
		{
		  username = passwd->pw_name;
		}
	      else
		{
		  strcpy (pop_error, "Could not determine username");
		  return (0);
		}
	    }
	}
    }

  /*
   *  Determine the mail host.
   */

  if (! host)
    {
      host = getenv ("MAILHOST");
    }

#ifdef HESIOD
  if ((! host) && (! (flags & POP_NO_HESIOD)))
    {
      struct hes_postoffice *office;
      office = hes_getmailhost (username);
      if (office && office->po_type && (! strcmp (office->po_type, "POP"))
	  && office->po_name && *office->po_name && office->po_host
	  && *office->po_host)
	{
	  host = office->po_host;
	  username = office->po_name;
	}
    }
#endif

#ifdef MAILHOST
  if (! host)
    {
      host = MAILHOST;
    }
#endif

  if (! host)
    {
      strcpy (pop_error, "Could not determine POP server");
      return (0);
    }

  /* Determine the password */
#ifdef KERBEROS
#define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
#else
#define DONT_NEED_PASSWORD 0
#endif

  if ((! password) && (! DONT_NEED_PASSWORD))
    {
      if (! (flags & POP_NO_GETPASS))
	{
	  password = getpass ("Enter POP password:"******"Could not determine POP password");
	  return (0);
	}
    }
  if (password)			/* always true, detected 20060515 */
    flags |= POP_NO_KERBEROS;
  else
    password = username;	/* dead code, detected 20060515 */
  /** "kpop" service is  never used: look for 20060515 to see why **/

  sock = socket_connection (host, flags);
  if (sock == -1)
    return (0);

  server = (popserver) malloc (sizeof (struct _popserver));
  if (! server)
    {
      strcpy (pop_error, "Out of memory in pop_open");
      return (0);
    }
  server->buffer = (char *) malloc (GETLINE_MIN);
  if (! server->buffer)
    {
      strcpy (pop_error, "Out of memory in pop_open");
      free ((char *) server);
      return (0);
    }

  server->file = sock;
  server->data = 0;
  server->buffer_index = 0;
  server->buffer_size = GETLINE_MIN;
  server->in_multi = false;
  server->trash_started = false;

  if (getok (server))
    return (0);

  /*
   * I really shouldn't use the pop_error variable like this, but....
   */
  if (strlen (username) > ERROR_MAX - 6)
    {
      pop_close (server);
      strcpy (pop_error,
	      "Username too long; recompile pop.c with larger ERROR_MAX");
      return (0);
    }
  sprintf (pop_error, "USER %s", username);

  if (sendline (server, pop_error) || getok (server))
    {
      return (0);
    }

  if (strlen (password) > ERROR_MAX - 6)
    {
      pop_close (server);
      strcpy (pop_error,
	      "Password too long; recompile pop.c with larger ERROR_MAX");
      return (0);
    }
  sprintf (pop_error, "PASS %s", password);

  if (sendline (server, pop_error) || getok (server))
    {
      return (0);
    }

  return (server);
}
Exemplo n.º 30
0
/*
 * Function: pop_list
 *
 * Purpose: Performs the POP "list" command and returns (in value
 * 	parameters) two malloc'd zero-terminated arrays -- one of
 * 	message IDs, and a parallel one of sizes.
 *
 * Arguments:
 * 	server	The pop connection to talk to.
 * 	message	The number of the one message about which to get
 * 		information, or 0 to get information about all
 * 		messages.
 *
 * Return value: 0 on success, non-zero with error in pop_error on
 * 	failure.
 *
 * Side effects: On failure, may make further operations on the
 * 	connection impossible.
 */
int
pop_list (popserver server, int message, int **IDs, int **sizes)
{
  int how_many, i;
  char *fromserver;

  if (server->in_multi)
    {
      strcpy (pop_error, "In multi-line query in pop_list");
      return (-1);
    }

  if (message)
    how_many = 1;
  else
    {
      int count, size;
      if (pop_stat (server, &count, &size))
	return (-1);
      how_many = count;
    }

  *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
  *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
  if (! (*IDs && *sizes))
    {
      strcpy (pop_error, "Out of memory in pop_list");
      return (-1);
    }

  if (message)
    {
      sprintf (pop_error, "LIST %d", message);
      if (sendline (server, pop_error))
	{
	  free ((char *) *IDs);
	  free ((char *) *sizes);
	  return (-1);
	}
      if (pop_getline (server, &fromserver) < 0)
	{
	  free ((char *) *IDs);
	  free ((char *) *sizes);
	  return (-1);
	}
      if (strncmp (fromserver, "+OK ", 4))
	{
	  if (! strncmp (fromserver, "-ERR", 4))
	    snprintf (pop_error, ERROR_MAX, "%s", fromserver);
	  else
	    {
	      strcpy (pop_error,
		      "Unexpected response from server in pop_list");
	      pop_trash (server);
	    }
	  free ((char *) *IDs);
	  free ((char *) *sizes);
	  return (-1);
	}
      (*IDs)[0] = atoi (&fromserver[4]);
      fromserver = strchr (&fromserver[4], ' ');
      if (! fromserver)
	{
	  strcpy (pop_error,
		  "Badly formatted response from server in pop_list");
	  pop_trash (server);
	  free ((char *) *IDs);
	  free ((char *) *sizes);
	  return (-1);
	}
      (*sizes)[0] = atoi (fromserver);
      (*IDs)[1] = (*sizes)[1] = 0;
      return (0);
    }
  else
    {
      if (pop_multi_first (server, "LIST", &fromserver))
	{
	  free ((char *) *IDs);
	  free ((char *) *sizes);
	  return (-1);
	}
      for (i = 0; i < how_many; i++)
	{
	  if (pop_multi_next (server, &fromserver) <= 0)
	    {
	      free ((char *) *IDs);
	      free ((char *) *sizes);
	      return (-1);
	    }
	  (*IDs)[i] = atoi (fromserver);
	  fromserver = strchr (fromserver, ' ');
	  if (! fromserver)
	    {
	      strcpy (pop_error,
		      "Badly formatted response from server in pop_list");
	      free ((char *) *IDs);
	      free ((char *) *sizes);
	      pop_trash (server);
	      return (-1);
	    }
	  (*sizes)[i] = atoi (fromserver);
	}
      if (pop_multi_next (server, &fromserver) < 0)
	{
	  free ((char *) *IDs);
	  free ((char *) *sizes);
	  return (-1);
	}
      else if (fromserver)
	{
	  strcpy (pop_error,
		  "Too many response lines from server in pop_list");
	  free ((char *) *IDs);
	  free ((char *) *sizes);
	  return (-1);
	}
      (*IDs)[i] = (*sizes)[i] = 0;
      return (0);
    }
}