Rabu, 16 Juli 2014

Xathrya Sabertooth

Xathrya Sabertooth


Setup Slackware as Basic TFTP Server

Posted: 15 Jul 2014 10:47 PM PDT

TFTP or Trivial File Transfer Protocol is a transfer protocol notable for its simplicity. TFTP is generally used for automated transfer of configuration or boot files between machines in a local environment, such as routers and PXE (Preboot eXecution Environment) mechanism. Compared to FTP, TFTP is extremely limited, providing no authentication and is rarely used interactively by a user.

There are many TFTP implementations, such as:

  • GNU inetutils
  • tftp-hpa
  • atftp
  • tftp-serveretc…

In this article, we will discuss about how to set Slackware as a basic  TFTP server. In specific, we will use tftp-hpa which is included by default. This article is tested using Slackware64 14.0 and tftp-hpa.

Upgrade or Not?

Slackware has shipped a TFTP server by default which is tftp-hpa. The version is 0.49. However we can install the latest tftp-hpa with simple process. If you decided to install the latest version, follow this section. Otherwise you can skip to next section.

Download the latest version of tftp-hpa here: www.kernel.org/pub/software/network/tftp/tftp-hpa/. The latest version is 5.2 which can be downloaded here: www.kernel.org/pub/software/network/tftp/tftp-hpa/tftp-hpa-5.2.tar.xz

Now invoke following series of command to build TFTP (server and client):

tar -Jxf tftp-hpa-5.2.tar.xz  cd tftp-hpa-5.2  ./configure --prefix=/usr  make  cd tftp && strip --strip-unneeded tftp  cd ../tftpd && strip --strip-unneeded tftpd  cd ..

Now install it (make sure you are using root privileges):

make install

Set up

By default, the inetd use /tftpboot directory as root directory of the TFTP server. We can however change the directory to something else. Make sure the directory exists, whichever directory you use.

Suppose we want to use /usr/local/tftpboot as root directory:

mkdir -p /usr/local/tftpboot

Then we give a proper permission:

chmod +777 /usr/local/tftpboot  chown nobody /usr/local/tftpboot

Next we add the following entry to the /etc/inetd.conf file as a single line. Or you can edit it and make sure it’s uncommented.

tftp  dgram   udp     wait    root    /usr/sbin/in.tftpd  in.tftpd -s /usr/local/tftpboot -r blksize

Then, restart the inetd using root privileges.

/etc/rc.d/rc.inetd restart

Testing

Now, let’s testing our TFTP server. In our simple scenario, we will create a plain text and fill something there. Then we will download it via tftp.

echo "Xathrya was testing the TFTP server" >> /usr/local/tftpboot/test.txt

To download it, use:

tftp 127.0.0.1 -c get test.txt

And see in current directory, whether we have successfully download it:

cat test.txt

It should print out “Xathrya was testing the TFTP server”.

Senin, 14 Juli 2014

Xathrya Sabertooth

Xathrya Sabertooth


C++ Reserved Words

Posted: 14 Jul 2014 03:33 AM PDT

C++ reserves some words for its own use and for use in C++ libraries. Thus, we shouldn’t use a reserved word as an identifier in a declaration. Reserved words comes in two categories: keywords and alternative tokens.

This article will list C++ reserved words based on C++11 standard.

C++ Keywords

Keywords are identifiers that form the vocabulary or a programming language. By combining the keyword and following the grammar of C++, we do code with C++. These vocabulary may not be used for other purpose, such as serving as variable names.

The following list show C++’s keywords:

alignasexportsizeof
alignofexternstatic
asmfalsestatic_assert
autofloatstatic_cast
boolforstruct
breakfriendswitch
casegototemplate
catchifthis
charinlinethread_local
char16_tintthrow
char32_tlongtrue
classmutabletry
constnamespacetypedef
constexprnewtypeid
const_castnoexcepttypename
continuenullptrunion
decltypeoperatorunsigned
defaultprivateusing
deleteprotectedvirtual
dopublicvoid
doubleregistervolatile
dynamic_castreinterpret_castwchar_t
elsereturnwhile
enumshort
explicitsigned

Alternative Tokens

In addition to keywords, C++ has some alphabetic alternative representations of operators, termed alternative tokens. These, too, are reserved.

Token
Meaning
and&&
and_eq&=
bitand&
bitor|
compl~
not!
not_eq!=
or||
or_eq|=
xor^
xor_eq^=

Notes

The meaning of “auto” keyword has changed in C++11. In C++11 standard, the auto keyword is used to declare a variable with type inferred from the expression or value assigned to it.

In addition to keywords, there are two identifiers with special meaning, which maybe used as names of objects or functions, but have special meaning in certain contexts.

override  final

Also, each name that contains a double underscore __ or begins with an underscore followed by an uppercase letter is always reserved to the implementation and should not be used as an identifier. Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace; such names may be used as identifiers in user-defined namespaces, as names of class members, etc.

The name posix is reserved for a future top-level namespace. The behavior is undefined if a program declares or defines anything in that namespace.(since C++11)

C++11 Concurrency: Part 1 – Introduction to Threading in Standard Library

Posted: 03 Jul 2014 04:12 PM PDT

C++ is growing, as per new standard C++11 now it supports threading. C++11 introduce a new thread library. The new standard define utilities for starting and managing threads. It also contains utilities for synchronization like mutexes and other locks, atomic variables, and other utilities. We will get through this so called C++11 threading by series of articles “C++11 Concurrency”.

To compile the sample code, we need a C++11 compiler. In my case, I use GCC 4.8.1 (on Slackware Linux) or MinGW counterpart (on Windows 8.1). To activated C++11 support features, we need to pass the “-std=c++11″ option to GCC, for example:

g++ filename.cpp -o program -std=c++11

C++ Concurrency

see <atomic>, <condition_variable>, <future>, <mutex>, and <thread> for reference.

Starting from C++11, there are five more headers added to the standard library to support concurrency. They are:

  • atomic
  • condition_variable
  • future
  • mutex
  • thread

These headers are used for multithreading and concurrency. Before the language standard, we would use OS facilities such as POSIX thread or libraries like OpenMP and MPI to enable concurrency using C++.

The <atomic> header defines some definition to allow us using atomic operation. It encapsulate a value whose access is guaranteed to not cause data races and can be used to synchronize memory access among different threads.

The <condition_variable> header defines classes used as condition variable for synchronization.

The <future> header facilitates synchronization access to value.

The <mutex> header is used for allowing mutual exclusion (mutex) of concurrent execution of critical sections of code.

The <thread> header defines thread class, used for spawning threads on C++ and also manage threads as we like.

Starting Threads

see also: Multithreading Support in C++11

Starting a new thread is very easy. We need to create an instance of a std::thread and supply a function and thread will automatically be started and executing the function. The function we talk about can be a function pointer or even a lambda expression.

So first, let’s write a simple code to demonstrate the power of C++ thread.

#include <thread>  #include <iostream>    using std::cout;  using std::endl;  using std::thread;    void hello(){      cout << "Hello from thread " << endl;  }    int main(){      thread t1(hello);      t1.join();        return 0;  }

In the above snippet, we pass a function pointer to thread.

All the threads utilities can be found in the <thread> header. As we see, when constructing a thread instance, we supply a function which will be executed on the fly. The join() method is called to force current thread to wait for the other one. In other word, main thread won’t finish until t1 finish it’s task. If we omit this call, the result is undefined.

We can, however, create more than one thread. To distinguish each of thread, the std::thread class provide a get_id() method which returns an unique id for this thread. We can get a reference to the current thread by std::this_thread variable. Let’s dive in example:

#include <thread>  #include <iostream>  #include <vector>    using std::cout;  using std::endl;  using std::thread;  using std::vector;    void hello(){      cout << "Hello from thread " << this_thread::get_id() << endl;  }    int main(){      vector<thread> threads;        for (int i = 0; i < 5; ++i){          threads.push_back(thread(hello));      }        for (auto& thread : threads){          thread.join();      }        return 0;  }

Sample output:

Hello from thread 2  Hello from thread 4  Hello from thread 3  Hello from thread 5  Hello from thread 6

Our program will start thread one after one and then store them into a vector. This is common strategy to handle several threads.

The threads might interleave. We have no way to control the order of execution of threads. A thread can be preempted at any moment which might lead us to produce following result:

Hello from thread Hello from thread 6  Hello from thread 3  Hello from thread 5  2  Hello from thread 4

So, there is no certainty in which function will be executed first. All decision are taken by OS. The Operating System will decide when resource are available for our threads and execute them on whatever CPU is least occupied.

Aside from function pointer, we can also use lambda expression. Lambda execution is very useful when the executing code is very small thus we don’t necessary need to create a function just for that. We can rewrite the previous code as this one:

#include <thread>  #include <iostream>  #include <vector>    using std::cout;  using std::endl;  using std::thread;  using std::vector;    int main(){      vector<thread> threads;        for (int i = 0; i < 5; ++i){          threads.push_back(thread([]() {              cout << "Hello from thread " << this_thread::get_id() << endl;          }));      }        for (auto& thread : threads){          thread.join();      }        return 0;  }

Threading in Action

Now we will illustrate the power of parallel programming in C++11. We will do something simple yet interesting: parallel merge sort.

Kamis, 10 Juli 2014

Xathrya Sabertooth

Xathrya Sabertooth


Building GCC AVR Toolchain for Slackware64

Posted: 09 Jul 2014 05:19 AM PDT

In this article we will about how to build GCC AVR Toolchain for ARM architecture. The built GCC can be used to compile application into ARM’s machine codes. The toolchain then reside on /usr/local path.

For this article, I use:

  1. Slackware64 14.0
  2. GCC 4.9.0
  3. GDB 7.7
  4. Binutils 2.24
  5. AVR-libc 1.8
  6. GMP 6.0.0
  7. MPFR 3.1.2
  8. MPC 1.0.2

Preparation

We will need some disk space (~2GB should be enough). We also need root access to install it on “system-wide”.

Slackware64 14.0 is optional. You can use any linux distribution you like.

We will create a working directory. We will refer it as $AVRGCC, so $AVRGCC/src should really be something like ~/AVRGCC/src (the ~ means your home directory). So go ahead and create following directory structure:

# set aliases  export AVRGCC=~/AVRGCC  export TARGET=avr  export PREFIX=/usr/local    export BINUTILS_BUILD=${AVRGCC}/build/binutils-build  export GCC_BUILD=${AVRGCC}/build/gcc-build  export GDB_BUILD=${AVRGCC}/build/gdb-build    export BINUTILS_SRC=${AVRGCC}/src/binutils-2.24  export GCC_SRC=${AVRGCC}/src/gcc-4.9.0  export GDB_SRC=${AVRGCC}/src/gdb-7.7  export AVRLIBC_SRC=${AVRGCC}/src/avr-libc-1.8.0    # make base dir, original archives dir (orig), source code dir (src),   # and working / building dir (build)   mkdir ${AVRGCC}{,/{orig,src,build}}    # Make build directory for each component  mkdir ${AVRGCC}/build/{binutils,gcc,gdb}-build

Acquiring the Materials

Download latest packages of GCC, GDB, binutils, and Newlib.You can download it using wget or alternatively download via browser and move it to $ARMGCC/orig. The version we will use is GCC 4.9.0, GDB 7.7, binutils 2.24, and newlib 2.1.0.

cd ${AVRGCC}/orig  wget ftp://ftp.gnu.org/pub/gnu/gcc/gcc-4.9.0/gcc-4.9.0.tar.bz2  wget ftp://ftp.gnu.org/pub/gnu/gdb/gdb-7.7.tar.bz2  wget ftp://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.bz2  wget ftp://ftp.twaren.net/Unix/NonGNU//avr-libc/avr-libc-1.8.0.tar.bz2    wget ftp://ftp.gnu.org/pub/gnu/gmp/gmp-6.0.0a.tar.xz  wget ftp://ftp.gnu.org/pub/gnu/mpc/mpc-1.0.2.tar.gz  wget ftp://ftp.gnu.org/pub/gnu/mpfr/mpfr-3.1.2.tar.gz

Then, extract them to src directory.

cd ${AVRGCC}/src    tar -jxf ${AVRGCC}/orig/gcc-4.9.0.tar.bz2  tar -jxf ${AVRGCC}/orig/gdb-7.7.tar.bz2  tar -jxf ${AVRGCC}/orig/binutils-2.24.tar.bz2  tar -jxf ${AVRGCC}/orig/avr-libc-1.8.0.tar.bz2

Next we need to download some prerequisites, especially GMP (GNU Multiple Precision), MPC (Multiple-Precision Complex), and MPFR (Multiple-Precision Floating Point Reliably) which is used for computation. The prerequisites can be downloaded by invoking following command:

The MPFR, GMP, and MPC is not the latest version, but at least we can build GCC. Supposing your working directory at /home/xathrya/AVRGCC, to obtain MPDR, GMP, and MPC invoke following command:

cd ${GCC_SRC}  ./contrib/download_prerequisites

However, the version of GMP, MPC, MPFR, PPL, CLOOG, and ISL is not the latest version. We can switch to the latest version by download the latest version and create symbolic link to it.

cd ${GCC_SRC}    rm {gmp,mpc,mpfr}    tar -Jxf ${AVRGCC}/orig/gmp-6.0.0a.tar.xz  tar -zxf ${AVRGCC}/orig/mpc-1.0.2.tar.gz  tar -zxf ${AVRGCC}/orig/mpfr-3.1.2.tar.gz    ln -s gmp-6.0.0 gmp  ln -s mpc-1.0.2 mpc  ln -s mpfr-3.1.2 mpfr

Building the Toolchains

Building stage is pretty simple. Honestly speaking, the tricky part is about how GCC and built concept and ordering them in correct order.

Binutils

First, built binutils. This will provide lots of useful tools in building ARM libraries and binaries (like objdump, ld, ranlib, etc). Invoke following commands:

cd ${BINUTILS_BUILD}  ${BINUTILS_SRC}/configure --target=${TARGET} --prefix=${PREFIX} \      --enable-interwork --enable-multilib --enable-shared   make configure-host  make -j4 all  make install

You will notice a few configure options that are really critical for getting things to work properly.

  • –target=avr Says we want a compiler to generate binaries for the avr platform.
  • –enable-interwork This allows for assembling Thumb and ARM code mixed into the same binaries (for those chips that support that)
  • –enable-multilib Multilib allows the use of libraries that are compiled multiple times for different targets/build types
  • –enable-shared – enable the creation of the shared libraries.

GCC

Next we will build the GCC. But please note that we are only intereset on C and C++ so we will ignore other language. To built GCC, do this:

cd ${GCC_BUILD}  ${GCC_SRC}/configure --target=${TARGET} --prefix=${PREFIX} --enable-interwork \     --enable-multilib --enable-languages="c,c++" --with-dwarf2 --disable-libssp \     --enable-c99 --enable-long-long --enable-__cxa_atexit --enable-shared  make -j4 all-gcc   make -j4 all-target-libgcc    make install-gcc install-target-libgcc

Now, the important points are:

  • –enable-languages=”c,c++” - means build C and C++ only.
  • –with-newlib – use Newlib instead of the standard C libraries.
  • –with-headers – this adds a header inlude path during build time and will allow the build to find the newlib header files
  • –enable-__cxa_atexit – allows use of __cxa_atexit, rather than atexit, to register C++ destructors for local statics and global objects and is essential for fully standards-compliant handling of destructors. It also affects the C++ ABI and therefore results in C++ shared libraries and C++ programs that are interoperable with other Linux distributions.
  • –enable-c99 – enable C99 support for C programs.
  • –enable-long-long – enables long long support in the compiler.

avr-libc

Now build our avr-libc

cd ${AVRLIBC_SRC}  ./configure --host=avr --build=`./config.guess` --prefix=${PREFIX}  make  make install

GDB

Optionally you can build GDB. To do so, invoke following:

cd ${GDB_BUILD}  ${GDB_SRC}/configure --target=${TARGET} --prefix=${PREFIX} \    --enable-interwork --enable-multilib  make -j4 all   make install

And that’s it. You should have them now.

Testing

The toolchain can be tested with the minimal code here:

int main()  {     return 0;  }