tech_log: June 2009

Thursday, June 25, 2009

On structure assignment (pack your ETH header structures!)

Program like this :

#include <stdio.h>
#include <stddef.h>


int main()
{
    int i, j;
    char a[] = { 0x0, 0x1, 0x2, 0x4, 0x5, 0x6, 0x7, 0x8};

    typedef struct
    {
        char c;
        short s;
        int k;
    } s_t;

    s_t str;

    printf ("offsetof(str, c) = %d\n", offsetof(s_t, c));
    printf ("offsetof(str, s) = %d\n", offsetof(s_t, s));
    printf ("offsetof(str, k) = %d\n", offsetof(s_t, k));

    str = *( (s_t *)a );

    printf("str.c = %#x\n", str.c);
    printf("str.s = %#x\n", str.s);
    printf("str.k = %#x\n", str.k);

    return 0;
}

Will give output like this :

drasko@bg:~/stuff$ ./struct_ass
offsetof(str, c) = 0
offsetof(str, s) = 2
offsetof(str, k) = 4
str.c = 0
str.s = 0x402
str.k = 0x8070605
drasko@bg:~/stuff$

If, however, structure was packed, like this :

typedef struct
    {
        char c;
        short s;
        int k;
    } __attribute__ ((__packed__)) s_t;

Result would be different :

drasko@bg:~/stuff$ ./struct_ass
offsetof(str, c) = 0
offsetof(str, s) = 1
offsetof(str, k) = 3
str.c = 0
str.s = 0x201
str.k = 0x7060504
drasko@bg:~/stuff$

So - advice for casting your ETH packet stream into ETH header-like pkt_hdr struct for easier manipulation of memcers in ETH frame (like src and dst MAC addr and ethtype) :
pack your header struct to kill the padding!

Monday, June 22, 2009

expected '=', ',', ';', 'asm' or '__attribute__'

error: expected '=', ',', ';', 'asm' or '__attribute__' before 'foo_t'

This error message can appear on compilation of .c file, whose very first function is :

foo_t bar(void)
{
   /* ... */
}

This signalize that ';' is missing before your function and adding it would surely solve the problem, like this :

; foo_t bar(void)
{
   /* ... */
}

But that is only placebo. Real problem comes actually from the missing ';' in include file, foo.h, that is included and unrolled by preprocessor right before the definition of the bar() in your .c file. For example, last lines of foo.h might be :

/* ... */
typedef baz_t (* handler)(int arg1, int arg2)          <--- MISSING ';'
#endif /* FOO_H */


Vi - Hidden missing tab in Makefile

Makefile:<line_num>: *** missing separator.  Stop.

Most probably because of missing TAB in the begining of the line. you know this, you verified that you are starting line by pressing TAB in your Vi, and still you are getting this error. Why?

Try
:set et?

and if it shows:
expandtab

then this is an answer. Expandtab is transfering your tabs into spaces (and this is very often demanded Vi setup by project coding standard).

Solution:
In instert mode press CTRL+v, <tab>
This will insert real tab char.

Verify that line begins with real <TAB> char by issuing Vi command :
:set list

Neat and compact itoa() implementation

char* itoa(int val, int base)
{
    static char buf[32] = {0};
    int i = 30;
    for(; val && i ; --i, val /= base)
        buf[i] = "0123456789abcdef"[val % base];
   
    return &buf[i+1];
}

Sunday, June 14, 2009

cyrillic in LaTeX

Marx:/home/drasko/alp# apt-get install texlive-lang-cyrillic

\documentclass{article}
%\usepackage{ucs}
\usepackage[utf8]{inputenc}
\usepackage[T2A]{fontenc}
%\usepackage[english, serbian]{babel}
\title{Србија, бато, Србија}
\author{Драшко ДРАШКОВИЋ}
\date{19 јануар 2009}
\begin{document}
\maketitle
Успех!
\end{document}




Sunday, June 7, 2009

xmame settings on Linux

Place roms in dir.
In order to launch xmame, change /etc/xmame/xmamerc
### Data files/directories ###
#rompath                 /usr/share/games/xmame/roms
rompath                 /home/drasko/mame/roms

Go to the roms dir and :
$ drasko@Marx:~/mame/roms$ xmame.SDL -ef 3 -fullscreen kungfum.zip

Only xmame.SDL can do fullscreen, pure xmame can not.

Use shift+home or shift+insert for scaling if not in fullscreen mode.

Saturday, June 6, 2009

Advanced Linux Programming Book


www.advancedlinuxprogramming.com

French translation :
http://www.advancedlinuxprogramming-fr.org/doku.php

Get tex sources :
$ svn co svn://svn.tuxfamily.org/svnroot/advancedlp/advancedlp/trunk

resolving of ! LaTeX Error: File `sectsty.sty' not found. :
# apt-get install texlive-latex-extra

In the sources there is ruines.png missing.
!pdfTeX error: pdflatex (file ../img/ruines.png): cannot find image file
Resolving that error:
$ cd fr/trunk/fr/src/img
drasko@Marx:~/alp/fr/trunk/fr/src/img$ ls
codesourcery.png
drasko@Marx:~/alp/fr/trunk/fr/src/img$ cp codesourcery.png ruines.png
(I just pathceh it, to have some .png at all in order to build)


KOS - The Kid Operating System

Nice project for self education on OS :
http://kos.enix.org/

$ mkdir kos
$ cd kos
$ cvs -d :pserver:anonymous@kos.enix.org:/var/cvs/kos login
$ cvs -z3 -d :pserver:anonymous@kos.enix.org:/var/cvs/kos co .
(in order to download all dirs)
$ cd kos-doc
$ su
# apt-get install kile
(easiest way to get all you need for Latex + nice editor,
but that will not be enough, because texlive-latex-base comes with a little bug: kvoptins.sty missing
https://bugs.launchpad.net/ubuntu/+source/texlive-base/+bug/114299 )
# apt-get install texlive-latex-recommended
(to get them)
$ make ps

Happy starting!