void client_register(char msg[], int sockfd) {
	int i, portno;
	char* token, hostname;
	char ip[15];

	char delimiter[2] = " ";
	token = strtok(msg, ":");
	token = strtok(NULL, delimiter);

	strcpy(ip, token);
	token = strtok(NULL, delimiter);

	portno = atoi(token);

	/////checking for duplicate connection////
	for (i = 1; i < 5; i++) {

		if ((strcmp(table[i].ip_address, ip) == 0)
				&& (table[i].portno == portno)) {
			write(table[i].clientfd, "DUPLICATE CONNECTION", 18);
			return;

		}
	}

	/*****finding empty entry*******/
	for (i = 1; i < 5; i++) {
		if (table[i].id == -1)
			break;
	}
	/*******************************/

	/*******checking if server list full*****/

	if (i == 5) {
		///means table full
		write(sockfd, "SERVER FULL", 40); //if full send message full
		return;

	}

	/*****************************************/

	/*adding the new client to list*/
	table[i].id = i + 1;
	table[i].clientfd = sockfd;
	token = strtok(NULL, delimiter);
	strcpy(table[i].host_name, token);
	strcpy(table[i].ip_address, ip);
	table[i].portno = portno;
	/******************************/

	/*******displaying the updated table********/
	display_table();
	write(sockfd, "REGISTRATION COMPLETE", 40);
	send_table(); //sending the updated table to all existing clients
}
Example #2
0
XmemError XmemSendTable(XmemTableId table, void *buf, int elems,
			int offset, int upflag)
{
	if (!libinitialized)
		return XmemErrorNOT_INITIALIZED;

	if (markers_mask & XmemMarkersDISABLE) {
		return routines.SendTable(table, buf, elems, phys_eloff(offset),
					upflag);
	}

	if (markers_mask & XmemMarkersATOMIC)
		return send_table_atomic(table, buf, elems, offset, upflag);

	return send_table(table, buf, elems, offset, upflag);
}
Example #3
0
/** Firmware FSM event handler for finished measurement */
inline static
firmware_state_t firmware_handle_measurement_finished(const firmware_state_t pstate)
{
  switch (pstate) {
  case STP_MEASURING:
    /* end measurement */
    cli();
    on_measurement_finished();
    send_table(PACKET_VALUE_TABLE_DONE);
    return STP_DONE;
    break;
  default:
    send_text_P(PSTR("invalid state transition"));
    wdt_soft_reset();
    break;
  }
}
void server() {
	initialize();
	int server_sockfd, client_sockfd;
	int server_len, client_len;
	struct sockaddr_in server_address;
	struct sockaddr_in client_address;
	int result;
	char option[1024];

	server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
	server_address.sin_family = AF_INET;
	server_address.sin_addr.s_addr = htonl(INADDR_ANY);
	server_address.sin_port = htons(PORTNO);
	server_len = sizeof(server_address);
	bind(server_sockfd, (struct sockaddr *) &server_address, server_len);
	listen(server_sockfd, 5);
// Creating a queue of connections and initializing readfds to sockfd.
	FD_ZERO(&readfds);
	FD_SET(server_sockfd, &readfds);
	FD_SET(0, &readfds);

	while (1) // waiting for client requests
	{

		int fd;

		int nread;
		char ch[1024];

		testfds = readfds;

		result = select(FD_SETSIZE, &testfds, (fd_set *) 0, (fd_set *) 0,
				(struct timeval *) 0);
		if (result <= 0) {
			perror("Error");
			exit(1);
		}

		if (FD_ISSET(0, &testfds)) {

			fgets(option, 1024, stdin);
			option[strlen(option) - 1] = '\0';
			sparse_command(option);

		}

		else {

			for (fd = 0; fd < FD_SETSIZE; fd++) {
				if (FD_ISSET(fd, &testfds)) {
					if (fd == server_sockfd)

					{
						client_sockfd = accept(server_sockfd,
								(struct sockaddr *) &client_address,
								&client_len);

						FD_SET(client_sockfd, &readfds);

					}

					else {
						memset(ch, 0, 1024);
						ioctl(fd, FIONREAD, &nread);
						if (nread == 0)

						{
							remove_client(fd);
							close(fd);
							FD_CLR(fd, &readfds);
							send_table();

						} else {

							read(fd, &ch, sizeof(ch));
							char temp[1024];
							bzero(temp, 1024);
							strcpy(temp, ch);
							char* reader2;
							reader2 = strtok(temp, ":");

							/**registering the client***/
							if (strstr(ch, "Register") != NULL) {

								client_register(ch, fd);
								memset(ch, 0, 1024);

							}

							else {

								bzero(ch, 1024);
								bzero(temp, 1024);
							}
						}

					}

				}
			}

		}
	}
}
Example #5
0
void ripng_router::data_available(uint32_t) {
	sockaddr_in6 from;

	int res = m_sock.recvfrom(buffer, sizeof(buffer), &from);
	if (res <= 0) {
		// XXX
		return;
	}

	/* ignore messages from self */
	if (g_mrd->has_address(from.sin6_addr))
		return;

	if (ntohs(from.sin6_port) != 522)
		return;

	if (res < (int)sizeof(ripng_header))
		return;

	if (((res - sizeof(ripng_header)) % sizeof(ripng_rte)) != 0)
		return;

	interface *intf = get_interface_by_index(from.sin6_scope_id);
	if (!intf)
		return;

	ripng_header *hdr = (ripng_header *)buffer;
	if (hdr->version != 1 && hdr->zero != 0)
		return;

	if (hdr->command == RIPNG_REQUEST) {
		send_table(intf);
	} else if (hdr->command == RIPNG_RESPONSE) {
		ripng_rte *rte = (ripng_rte *)(buffer + sizeof(ripng_header));

		int rtecount = (res - sizeof(ripng_header)) / sizeof(ripng_rte);

		for (int i = 0; i < rtecount; i++, rte++) {
			if (rte->metric < 1 || rte->metric > 16)
				continue;
			if (rte->prefixlen > 128)
				continue;
			inet6_addr prefix(rte->prefix, rte->prefixlen);
			if (prefix.type() & inet6_addr::multicast)
				continue;

			mrib_def::prefix *ex = g_mrd->mrib().get_prefix(prefix, this);

			int metric = rte->metric + 1;

			if (ex) {
				ripng_prefix *rpi = (ripng_prefix *)ex;

				if (rpi->metric >= metric) {
					rpi->lastupdate = tval::now();
					if (metric < rpi->metric) {
						rpi->metric = metric;
						rpi->nexthop = from.sin6_addr;
						rpi->intf = intf;
					}
				} else if (rte->metric == RIP_INFINITY) {
					if (rpi->nexthop == from.sin6_addr
						&& rpi->metric < RIP_INFINITY) {
						rpi->lastupdate = tval::now();
						rpi->metric = rte->metric;
					}
				} else {
					/* dont mrib::update_prefix */
					continue;
				}

				g_mrd->mrib().update_prefix(rpi);

				continue;
			}

			if (metric < RIP_INFINITY) {
				ripng_prefix *pinfo = new ripng_prefix(this);
				if (pinfo) {
					pinfo->distance = 120;
					pinfo->metric = metric;
					pinfo->nexthop = from.sin6_addr;
					pinfo->lastupdate = tval::now();
					pinfo->intf = intf;
					pinfo->metric = metric;
					g_mrd->mrib().install_prefix(prefix, pinfo);
				}
			}
		}
	}
}
Example #6
0
/** Firmware FSM event handler for receiving a command packet from the host
 *
 * \param pstate current FSM state
 * \param cmd the command we are to handle
 * \return new state
 *
 * Implicit parameters via global variables:
 *   personality_param_sram[0..sizeof(personality_param_sram)-2] param+token data
 *   personality_param_sram[sizeof(personality_param_sram)-1] size of param+token data
 */
inline static
firmware_state_t firmware_handle_command(const firmware_state_t pstate,
                                         const uint8_t cmd)
{
  /* temp vars */
  const frame_cmd_t c = (frame_cmd_t)cmd;

  uprintf("EAT PACKET: %c", cmd);

  switch (pstate) {
  case STP_READY:
    switch (c) {
    case FRAME_CMD_PERSONALITY_INFO:
      send_personality_info();
      /* fall through */
    case FRAME_CMD_ABORT:
    case FRAME_CMD_INTERMEDIATE:
    case FRAME_CMD_STATE:
      send_state_P(PSTR_READY);
      return STP_READY;
      break;
    case FRAME_CMD_PARAMS_TO_EEPROM:
      /* The param length has already been checked by the frame parser */
      send_state_P(PSTR("PARAMS_TO_EEPROM"));
      eeprom_update_block(&pparam_sram, &pparam_eeprom,
                          sizeof(pparam_eeprom));
      send_state_P(PSTR_READY);
      return STP_READY;
      break;
    case FRAME_CMD_PARAMS_FROM_EEPROM:
      params_copy_from_eeprom_to_sram();
      send_eeprom_params_in_sram();
      send_state_P(PSTR_READY);
      return STP_READY;
      break;
    case FRAME_CMD_MEASURE:
      /* The param length has already been checked by the frame parser */
      general_personality_start_measurement_sram();
      send_state_P(PSTR_MEASURING);
      return STP_MEASURING;
      break;
    case FRAME_CMD_RESET:
      send_state_P(PSTR_RESET);
      wdt_soft_reset();
      break;
    }
    break;
  case STP_MEASURING:
    switch (c) {
    case FRAME_CMD_INTERMEDIATE:
      /** The value table will be updated asynchronously from ISRs
       * like ISR(ADC_vect) or ISR(TIMER1_foo), i.e. independent from
       * this main loop.  This will cause glitches in the intermediate
       * values as the values in the table often consist of more than
       * a single 8bit machine word.  However, we have decided that
       * for *intermediate* results, those glitches are acceptable.
       *
       * Keeping interrupts enabled has the additional advantage that
       * the measurement continues during send_table(), so we need not
       * concern ourselves with pausing the measurement timer, or with
       * making sure we properly reset the hardware which triggered
       * our ISR within the appropriate time range or anything
       * similar.
       *
       * If you decide to bracket the send_table() call with a
       * cli()/sei() pair, be aware that you need to solve the issue
       * of resetting the hardware properly. For example, with the
       * adc-int-mca personality, you would need to reset the peak
       * hold capacitor on resume if an event has been detected by the
       * analog circuit while we had interrupts disabled and thus
       * ISR(ADC_vect) could not reset the peak hold capacitor.
       */
      send_table(PACKET_VALUE_TABLE_INTERMEDIATE);
      send_state_P(PSTR_MEASURING);
      return STP_MEASURING;
      break;
    case FRAME_CMD_PERSONALITY_INFO:
      send_personality_info();
      /* fall through */
    case FRAME_CMD_PARAMS_TO_EEPROM:
    case FRAME_CMD_PARAMS_FROM_EEPROM:
    case FRAME_CMD_MEASURE:
    case FRAME_CMD_RESET:
    case FRAME_CMD_STATE:
      send_state_P(PSTR_MEASURING);
      return STP_MEASURING;
      break;
    case FRAME_CMD_ABORT:
      send_state_P(PSTR_DONE);
      cli();
      on_measurement_finished();
      send_table(PACKET_VALUE_TABLE_ABORTED);
      send_state_P(PSTR_DONE);
      return STP_DONE;
      break;
    }
    break;
  case STP_DONE:
    switch (c) {
    case FRAME_CMD_PERSONALITY_INFO:
      send_personality_info();
      /* fall through */
    case FRAME_CMD_STATE:
      send_state_P(PSTR_DONE);
      return STP_DONE;
      break;
    case FRAME_CMD_RESET:
      send_state_P(PSTR_RESET);
      wdt_soft_reset();
      break;
    default:
      send_table(PACKET_VALUE_TABLE_RESEND);
      send_state_P(PSTR_DONE);
      return STP_DONE;
      break;
    }
    break;
  }
  send_text_P(PSTR("STP_ERROR"));
  wdt_soft_reset();
}