Parsing a packet using structs

When I was analyzing the source code of an opensource software, I found an interesting technique to parse incoming data in a very efficient way, using a struct.

This technique works if the received data has a fixed length and structure; for example it applies very well to data “packets”.

When receiving, the program normally stores incoming data in a buffer in the RAM memory. For example let’s assume that the packet has a length of 14 bytes:

char rx_buffer[14];

Your specific protocol defines the following fields in the packet:

  • address, 5 bytes
  • temperature value, 3 bytes
  • humidity value, 3 bytes
  • TX counter, 1 bytes
  • checksum, 2 bytes

You can define a struct that represents the structure of the packet:

typedef struct {
  uint8_t address[5];
  uint8_t temperature[3];
  uint8_t humidity[3];
  uint8_t tx;
  uint8_t checksum[2];
} my_packet;

The technique consists in applying, as if it were a template, the struct to the buffer:


in this way the received bytes are automatically divided into the different fields:


Therefore, to parse the data you only need to cast the buffer:

my_packet* parsed_data = (my_packet*)rx_buffer;

and access the struct fields:


Padding and packed

You still have a problem: to make it more efficient to access the memory, compliers normally align the variables based on their size. This means that – when a compiler allocates memory for your struct – some empty space (named padding) could be added between the different variables:


The presence of the padding space causes a misalignment if you apply the struct to the buffer with the received data (which are instead continous). Fortunately, it is possible to ask the compiler not to insert padding using the packed attribute:

typedef struct {
  uint8_t address[5];
  uint8_t temperature[3];
  uint8_t humidity[3];
  uint8_t tx;
  uint8_t checksum[2];
}__attribute__((packed)) my_packet;

If you remember to add this attribute, the technique works perfectly!

Java, IllegalFormatConversionException when importing an SSL certificate

Today I faced a strange problem. I needed to import an SSL certificate in a java keystore, using the “classic” keytool command. I was sure that the format of the certificate was correct, but I always received the following error:

java.util.IllegalFormatConversionException: d != java.lang.String


The reason of the error is that – in the latest versions of java – by default the keytool command uses the language of the system (in my case italian).

I had to add the parameter -J-Duser.language=en to the keytool command to made it work correctly:


IIS, how to debug HTTP errors

Today I faced a problem during the setup of a new application: if I opened a browser and entered the URL of the application, I got the following error message


To be able to identify the root cause of the error, I found a new feature added in the latest versions of Microsoft IIS (Internet Information Services): the failed request tracing. Using this tool you can selectively trace all the processing pipeline of a request to identify which step causes the error reported to the user.

Let’s see how to use it. First, verify that this feature is installed on your server using the Server Manager:


Open the IIS Manager tool, select the site which publishes the application and in the Actions pane (on the right) click on Failed Request Tracing…:


Enable the feature and note down the path IIS will save the logs into:


Now you have to specify which requests will be traced. Click on the icon:


then click on Add... in the Actions pane.

Thanks to a wizard you can define the criteria a request must match to be traced. For example in the screenshot below I created a tracing rule for the requests which return error 401.3:


If I reloaded the page with my browser, in the path above I could find the trace logs:


With a double-click on the frxxxxxx files, a page with all the details of the request was displayed:


When I moved to the Request Details tab I was able to identify the folder IIS couldn’t access:


When I set the correct permissions on the NTFS filesystem, the application started to work.


Google Chrome, how to display the website’s SSL certificate

Users of the Google Chrome web browser were able to display the SSL certificate of a website published via https protocol with a click on the address bar. In the latest versions, this functionality has been removed:


You now need some additional steps: first open the developer tools (CTRL-Shift-I):


then select the Security tab and click on View certificate:


And finally you can see the certificate used by the web site (in this example,


WinRM, exceeded MaxEnvelopeSize quota

Today, while I was configuring an Exchange 2010 server, I got the following error:

The WinRM […] was notified that the request size exceeded the configured MaxEnvelopeSize quota


The MaxEnvelopeSize parameter defines the maximum size (in Kb) of a single SOAP request. Sometimes you may need to increase the value to be able to run a big script/command (via powershell or using the GUI).

The default maximum size is 500Kb and you can show the actual value with the command:

winrm get winrm/config


To change the value, run the following command (in the example I’m increasing the value to 1000Kb):


If you got the error from the Exchange management console, you probably need also to increase the value in the web.config file (InstallDir\ClientAccess\PowerShell). If the parameter is not already present, it can be added:


Microsoft Exchange without using Outlook

If you have a mailbox hosted on a Microsoft Exchange server, you’re probably using Outlook (or its web variant) as mail client. Thanks to a suggestion by a colleague, I’ve just discovered a way to access your Exchange mailbox (including calendar and contacts) using any mail clients.

You only need to install on your PC an opensource software, DavMail Gateway


DavMail indeed acts as a “translator”: on a side it communicates with the Exchange server using the language (protocol) normally spoken by Outlook (WebDav, EWS – Exchange Web Services), while on the other side it offers the standard mail protocols: POP/IMAP (for incoming emails) and SMTP (for outgoing emails):


Let’s see how to use it; as mail client I chose Thunderbird, in its portable version (which doesn’t require installation) but as I said you can use the mail client you prefer.

First download the latest version of DavMail. DavMail is available on different platforms, I chose the Windows version, again the one that doesn’t require installation:


Once unzipped the archive in a new folder, run the program with a double clink on davmail.exe (you can also run the program in console mode or install it as a Windows service):


The program shows its icon in the systray, in the right-bottom corner of your screen, next to the clock. You can configure it choosing Settings… from the menu (right-click the icon to display it):


Type the address of the webmail (OWA) of your Exchange server. Depending on how the server is configured, you may need to choose a particular protocol; most of the time the default setting (Auto) is ok. Note the port numbers of the different services (for example IMAP on port 1143…): you’ll need them later to configure your mail client.


When DavMail is configured and running, you can move to your mail client. When configuring a new account, type localhost both for incoming and outgoing server and enter the correct port number for the different protocols:


If everything was configured correctly, you can now manage your Exchange mailbox using your preferred mail program!

Websphere MQ – Copy messages between queues

If you administer a Websphere MQ server, it may happen that someone ask you to manage the messages: create a backup, move messages from a queue to another one…

The main administration tool for WMQ, MQ Explorer, doesn’t support those operations; you can only browse the queues or send test messages:


I recently discovered a tool, freely released by IBM, named qload.  This tool allows to move/copy messages between queues or transfer them between a queue and a text file.

qload’s source code is available on Github, in the ibm-messaging repository.

Let’s see some examples of its usage:

  • move all the messages from queue IN.QUEUE to queue OUT.QUEUE, qmanager QM.TEST
  • copy all the messages from queue IN.QUEUE to queue OUT.QUEUE,qmanager QM.TEST
./qload -m QM.TEXT -i IN.QUEUE -o OUT.QUEUE
  • save all the messages in queue IN.QUEUE, qmanager QM.TEST, to the file msgs.txt
./qload -m QM.TEXT -i IN.QUEUE -f msgs.txt
  • load all the messages from file msgs.txt to queue IN.QUEUE,qmanager QM.TEST
./qload -m QM.TEXT -o IN.QUEUE -f msgs.txt

It’s of course possible to filter the messages. For example the parameter -r allows to specify the number of the message or the range of messages to be included in the action:

./qload -m QM.TEXT -i IN.QUEUE -f msgs.txt -r 10..15

saves in the file msgs.txt only messages with index between 10 and 15.

Eagle – PCB quote generator

The PCBShopper website compares the prices of the most used PCB services.

Jeremy Ruhland published on his Github’s repository an Eagle script (ulpuser language program) that automatically gets the required information from an Eagle project and compiles the PCBShopper web form. Let’s see how to use it:

- first click on pcbshopper.ulp and display his source code (RAW button).


- save the ulp file in the ulp subfolder of your Eagle installation:


- open your Eagle project, move to the Board window and choose File – Run ULP…. Choose the pcbshopper.ulp file saved before:


- configure the required PCB (copper size, quantity…), then click on Click here to open


- the PCBShopper website will be automatically opened with all the fields pre-filled, you only have to click on Get Prices:


- to get the quotes, the cheapest ones on the top:


Eagle – How to place pins in a circle

I’m working with Eagle to prepare a PCB for a Nixie clock. I wasn’t able to find a library for the Nixie I chose so I had to create it from scratch.

Nixie’s datasheet has the following drawing about pins disposition:


Let’s see how to draw it in Eagle, thanks to an ULP (User Language Program).

Choose File – Run ULP…


Click on cmd-draw.ulp:


Choose Pad, then enter the values for the radius (0.19 mils) and the angle between two pads (25.714°) as explained in the datasheet. You can also define shape and size for the pads:


Click on OK and confirm the generated script with OK again:


The result:




Windows, how to clean up unused COM ports

It certainly happened to you that, when you connect a new USB device to your Windows PC, the operating system assigns the device a COM port with a high number because of the previous ones are in use:


Windows indeed keeps the COM port reserved even if the device is not physically connected to the computer. You can clear some old reservations with the following steps:

- run a command prompt as administrator:


- type the following commands:

set devmgr_show_nonpresent_devices=1
start devmgmt.msc


- choose View – Show hidden devices:


- you can now remove (uninstall) old devices and free the associated COM ports: