Пример #1
0
int
handleReceivedExplictUDPData(int pa_socket, struct sockaddr_in *pa_pstFromAddr,
    EIP_UINT8* pa_buf, unsigned int pa_length, int *pa_nRemainingBytes)
{
  int nRetVal = 0;
  struct S_Encapsulation_Data sEncapData;
  /* eat the encapsulation header*/
  /* the structure contains a pointer to the encapsulated data*/
  /* returns how many bytes are left after the encapsulated data*/
  *pa_nRemainingBytes = createEncapsulationStructure(pa_buf, pa_length,
      &sEncapData);

  if (SUPPORTED_OPTIONS_MASK == sEncapData.nOptions) /*TODO generate appropriate error response*/
    {
      if (*pa_nRemainingBytes >= 0) /* check if the message is corrupt: header size + claimed payload size > than what we actually received*/
        {
          /* full package or more received */
          sEncapData.nStatus = OPENER_ENCAP_STATUS_SUCCESS;
          nRetVal = 1;
          /* most of these functions need a reply to be send */
          switch (sEncapData.nCommand_code)
            {
          case (COMMAND_LISTSERVICES):
            handleReceivedListServicesCmd(&sEncapData);
            break;

          case (COMMAND_LISTIDENTITY):
            handleReceivedListIdentityCmdUDP(pa_socket, pa_pstFromAddr,
                &sEncapData);
            nRetVal = EIP_OK; /* as the response has to be delayed do not send it now */
            break;

          case (COMMAND_LISTINTERFACES):
            handleReceivedListInterfacesCmd(&sEncapData);
            break;

            /* Fhe following commands are not to be sent via UDP */
          case (COMMAND_NOP):
          case (COMMAND_REGISTERSESSION):
          case (COMMAND_UNREGISTERSESSION):
          case (COMMAND_SENDRRDATA):
          case (COMMAND_SENDUNITDATA):
          default:
            sEncapData.nStatus = OPENER_ENCAP_STATUS_INVALID_COMMAND;
            sEncapData.nData_length = 0;
            break;
            }
          /* if nRetVal is greater then 0 data has to be sent */
          if (0 < nRetVal)
            {
              nRetVal = encapsulate_data(&sEncapData);
            }
        }
    }

  return nRetVal;
}
Пример #2
0
/* This read file function read file character by character */
void read_file(char * file_name)
{
	FILE *fp;
	// MAX_LINE_LENGTH = 1024; MAX_TIMESTAMP_LENGTH = 10; MAX_AMOUNT_LENGTH = 10
	char ch, type, timestamp[MAX_TIMESTAMP_LENGTH + 1], amount[MAX_AMOUNT_LENGTH + 1], description[MAX_LINE_LENGTH];
	// status indicate which kind data currently read, 0:type; 1:timestamp; 2:amount; 3:description
	int status = 0, timestamp_i, amount_i, amount_t, description_i, description_max;
	// Doubly-linked circular list
	My402List list;
	memset(&list, 0, sizeof(My402List));
	(void)My402ListInit(&list);

	if((fp = fopen(file_name,"r")) == NULL)
		print_error("cannot open file");

	while((ch = fgetc(fp)) != EOF)
	{
		switch(status)
		{
			case 0: // read type
				if(ch != '-' && ch != '+')
					print_error("undefined transcation type");
				else
				{
					type = ch;
					ch = fgetc(fp);
					if(ch != '\t' && ch != EOF)
						print_error("wrong format of file");
					else
						status++;
				}
				break;
			case 1: // read timestamp
				timestamp_i = 0;
				while(ch != '\t' && ch != EOF)
				{
					if(timestamp_i >= MAX_TIMESTAMP_LENGTH)
						print_error("timestamp is too large");
					if(ch < '0' || ch > '9')
						print_error("timestamp must be number");
					timestamp[timestamp_i++] = ch;
					ch = fgetc(fp);
				}
				timestamp[timestamp_i] = '\0';
				status++;
				break;
			case 2: // read amount
				amount_i = 0;
				amount_t = 0;
				while(ch != '\t' && ch != EOF)
				{
					if(amount_i >= MAX_AMOUNT_LENGTH)
						print_error("amount is too large");
					if(ch >= '0' && ch <= '9')
					{
						amount[amount_i++] = ch;
						ch = fgetc(fp);
					}
					else if(ch == '.')
					{
						if(++amount_t > 1)
							print_error("wrong format of amount");
						else
						{
							// skip the '.' symbol
							// amount[amount_i++] = ch;
							ch = fgetc(fp);
						}
					}
					else
						print_error("wrong format of amount");
				}
				amount[amount_i] = '\0';
				status++;
				break;
			case 3: // read description
				description_i = 0;
				description_max = MAX_LINE_LENGTH - amount_i - timestamp_i;
				while(ch != '\n' && ch != EOF)
				{
					if(description_i >= description_max)
						print_error("description is too long");
					if(ch == '\t')
						print_error("wrong format of file");
					description[description_i++] = ch;
					ch = fgetc(fp);
				}
				description[description_i] = '\0';

				encapsulate_data(type, timestamp, amount, description);

				status = 0;
				break;
			default:
				print_error("parse file error");
				break;
		}
	}
	fclose(fp);
}