Porting IP protocol to Arduino

I am working on porting a scaled down version of a ip/socket based protocol that my company wrote for a *nix based operating system.
Despite the system being quite old it was written fairly well for portability. It uses Message IDs and has structures associated with them. I am porting just a small
subset of the message IDs to keep it as simple as possible. I have the basic connection, ack, nack and keep alive messages working. But when I call the function below the program crashes upon server.available() in the beginning of the program.

We have structures and we use macros to create tables of the elements. There are Pack and Unpack functions. I have used the Pack function with no problem, it is the Unpack function that is causing the issue.

acMidString - raw incoming ascii data
pvMid - message struct
Table - element table for that specific struct(offsets)

int FepMidUnpack(const char *acMidString, void *pvMID, const ST_FEP_MID_TABLE *pstTable, int iUseFieldIndex)
{
  const ST_FEP_MID_TABLE *pstField = pstTable;
  char acFieldNumStr[16];
  int iFieldNum = 1;
  int iRes = 1;
  int iStringOfs = sizeof(ST_FEP_HDR_AC_W100);

  while ((pstField->iSize != -1) && (iRes == 1))
  {
    if (iUseFieldIndex)
    {
      sprintf(acFieldNumStr, "%02d", iFieldNum);
      if (strncmp(&acMidString[iStringOfs], acFieldNumStr, 2) != 0)
      {
        iRes = -1;
      }
      iStringOfs += 2;
    }

    if (iRes == 1)
    {
      strncpy((char *) pvMID + pstField->iOfs, &acMidString[iStringOfs], pstField->iSize);
      iStringOfs += pstField->iSize;
    }
    pstField++;
    iFieldNum++;

  }
  return iRes;
}

Here is my program and ram memory usage before and after including the call:
Program: 19432 bytes (59.3% Full)
Data: 1631 bytes (79.6% Full)
After
Program: 19524 bytes (59.6% Full)
Data: 1697 bytes (82.9% Full)

Is the problem that I am getting too close to the RAM cap? I would like to verify the source of the problem before upgrading to a Mega.

Any Ideas would be great.

Thanks

sprintf() is a pretty large function because it has to implement a scripting language. You might save space by using:

acFieldNumStr[0] = '0' + ((iFieldNum/10)%10);
acFieldNumStr[1] = '0' + (iFieldNum%10);
acFieldNumStr[2] = '\0';

I tried the sprintf fix and it only seemed to alter the program code by about 24 bytes and it did not affect the crash.

But I did some more digging and it looks like the crash is happening on the client.available() function in my read function.
Don't know if that helps at all.

Thanks

I commented out all of the function except for a return 0 and the crash still happens.
Could this be happening because of how I pass the data to the function?

FepMidUnpack(pstMidIn->acRaw, &pstMidIn->unMids.stMid0200, FEP_MID_TABLE(FEP_MID_0200), NO);

Thanks

We can't tell what is happening from the snippets of code you are posting. Post all of it.

Could this be happening because of how I pass the data to the function?

Yes, it could. If the function expects a certain number of bytes of data to be pushed on the stack, and there are more, or fewer, bytes, then when the function returns, and pops values off the stack to get to the return pointer, the return pointer won't be where it is expected to be, so the program will return to somewhere else, possibly not a valid address, and crash.