Nutella (Cup)cake

2012 January 15
tags: , ,
by Pedro Kiefer

Sábado chuvoso, nada para fazer, então nada melhor do que ir para a cozinha e fazer alguma delícia! Queríamos testar a receita de cupcake de Nutella deste blog, compramos todos os ingredientes, mas acabamos esquecendo das forminhas. Então mudamos um pouco a receita e fizemos um bolo de Nutella!

Bolo de Nutella


Bolo de Nutella

Segue abaixo a receita. Ah, na receita original eles usam self-rising flour, que não tínhamos, então adicionamos fermento à receita original.

Bolo de Nutella

  • 1 xícara de farinha
  • 1 1/4 colher de sopa de fermento
  • 1/4 colher de sopa de sal
  • 4 colheres de sopa de manteiga sem sal
  • 1 colher de chá de extrato de baunilha
  • 1/2 xícara de açucar
  • 350 gramas de Nutella
  • 4 ovos
  • 4 colheres de sopa de leite

Preaqueça o forno em 180ºC.

Misture a farinha, o fermento e o sal e reserve.

Com a batedeira, em outro recipiente, bata a manteiga até ficar cremosa. Adicione o extrato de baunilha e bata até incorporar. Adicione o açucar e bata bem. Bata a Nutella, cuidando para raspar as laterais do pote para a massa ficar homogênea. Adicione um ovo por vez e bata até ficar cremoso. Bata metade da mistura de farinha e em seguida adicione o leite. Adicione o restante da farinha e bata ligeiramente.

Coloque a massa em uma forma de uns 25cm de diâmetro, untada com manteiga e farinha. Asse por certa de 30 minutos. Logo que retirar do forno, coloque pedaços de chocolate meio-amargo por cima e espere derreter para logo em seguida espalhar sobre o bolo.

Bom apetite!

Asterisk, OpenVPN and QoS

2011 September 28
by Pedro Kiefer

Installing a VoIP system is nowadays an easy task, just install Asterisk, have a few SIP clients and you have an ‘instant’ telephone system. But your system will not be as reliable as the one offered by any telecom company. Why? Quality of Service, or for short, QoS.

Telecom companies use sophisticated hierarchies of systems to deliver the needed QoS. Backbones uses SDH systems, where one can guarantee the bandwidth and throughput for any kind of data. So if you specify that a voice packet should be delivered in 10ms, it will get delivered in that time span. Now when it comes to IP networks, you have no guarantee that your packet will be delivered in that time frame, which is a good thing when you’re downloading files, opening web pages, and so on. But when it comes to voice and video streaming, it’s a real mess. So you must create some QoS rules for your packets.

Asterisk has this real nice feature for aggregating multiple servers so that it works as a single phone network. The only problem is that this feature is not really secure, so as to mitigate that, one can always create VPN’s (Virtual Private Networks). But how does that impact your QoS solution? Well, depends on what kind, or how you configure your VPN, with OpenVPN it’s quite simple.

Just as a reminder, for the rest of the article when I say QoS, I really mean the QoS of the gateway of your network. The gateway is the one place that will enforce the needed quality of service (okay, on bigger networks you will have multiple routers which will need to be configured for QoS too).

Don’t get too excited with QoS, even though you did everything by the book that doesn’t mean that your ISP will use TOS field the same way you did. By that I mean, you won’t solve any problem with QoS if the problem is not on how you route packets to the internet. If you have full control of your link and all the router in between your networks, you’re a lucky guy!

The Network

We have some computers, servers, and IP phones on each network. The OpenVPN tunnel server doesn’t need to be the same as the gateway, as long as you export the correct ports to that server. Make sure you also add the correct gateway for the packets that should be tunneled (ie. packets for the network 10.2.1.0 that originates on the 10.1.1.0 network). On the image, the tunnel is represented by the red lines.

A sample network using Asterisk and OpenVPN

OpenVPN

I don’t intend to give a full how-to on OpenVPN, just a basic configuration, with a highlight on how to get QoS for the tunneled packets. Besides that, configuring OpenVPN is really simple.

First you have to create your own Certificate Authority (CA). You can use something like tinyca or minica, or the command line version, described here. Remember that you will need one certificate per client. After that is just a matter of writing a really simple text file. Below are a sample configuration, known to work well integrating two Asterisk servers.

Server

# OpenVPN server
# Listen to local ip address only
local 10.1.1.2

# Should be exported on the router
port 1194
proto udp
dev tun

# SSL/TLS CA and keys
ca ca.crt
cert server.network.crt
key server.network.key

# Diffie Hellman Parameters
dh dh1024.pem

# Server tunnel
server 10.3.1.0 255.255.255.0
ifconfig-pool-persist ipp.txt

push "route 10.1.1.0 255.255.255.0"
route 10.2.1.0 255.255.255.0
client-config-dir client-configs
keepalive 10 120

# Drop privileges
user nobody
group nogroup

# Persist
persist-key
persist-tun

# Logs
verb 5
status /var/log/openvpn.log

# Fork to the background
daemon

Client

The highlighted line is the one which will make the QoS work for the encrypted packets. If you think that passing the TOS (Type of service) is a security fault, don’t panic, just create another tunnel for passing your sensitive data – and that’s really easy to do with OpenVPN.

# OpenVPN client
client

# Interface for tunnel
# Protocol and Port
dev tun0
proto udp
port 1194

# SSL/TLS CA and keys
ca /etc/openvpn/certs/ca.crt
cert /etc/openvpn/certs/remote1.mynetwork.crt
key /etc/openvpn/keys/remote1.mynetwork.key

# Symmetric cipher
cipher BF-CBC

# Remote server to connect to. Can be domain name or IP address.
remote remote1.mynetwork.com

# Check if the tunnel went down and restart it.
# 10 is the ping interval number and 120 is the timeout to restart.
keepalive 10 120
route 10.1.1.0 255.255.255.0

# This is need so we can apply QoS to the tunnel
passtos

# Drop privileges
user nobody
group nogroup

# Use a persistent key and tunnel interface.
persist-tun
persist-key

# Log to file instead of syslog
log-append /var/log/openvpn.log
verb 4

# Fork to the background
daemon

If you can ping the remote server, using the internal IP address, then your tunnel is up and running.

Asterisk

I suppose that you already know how to configure an Asterisk server, if you don’t you can follow my guide (it’s a bit outdated, I might update it soon).

Getting IAX2 working is really simple too, so I won’t describe it. If you’re using FreePBX, you can follow this guide. Remember to use the internal IP’s from your network.

Make sure your asterisk installation is tagging the correct TOS for the packets. On my FreePBX install it already had the correct configuration set on /etc/asterisk/sip_general_additional.conf. Check your asterisk configuration for the following lines:

tos_sip=cs3
tos_audio=ef
tos_video=af41

This tags your voice data as Expedited Forwarding, normal SIP packets get Class Selector 3 and video data gets Assured Forwarding, Class 4, with drop precedence 1. More on what all this means shortly.

QoS

Getting the right choice of tools for your specific QoS application is a hard problem. You can have some traffic shaping algorithms, congestion avoidance mechanisms and quite a few packet scheduling algorithms. I’m not an expert on how all these different types of algorithms work, or what is the best solution for your case. I’m just putting together some information that I think is relevant. One can always read all the RFC’s about QoS.

First things first, the mentioned TOS field is now called DSCP (Differentiated Services Code Point), it replaces the TOS field and is specified for IPv4 and IPv6 (for reference RFC2474 is the specification). It tries to maintain backward compatibility with the TOS field. Most networks use the following traffic classes:

  • Default PHB — which is typically best-effort traffic
  • Expedited Forwarding (EF) PHB — dedicated to low-loss, low-latency traffic
  • Assured Forwarding (AF) PHB — gives assurance of delivery under prescribed conditions
  • Class Selector PHBs — which maintain backward compatibility with the IP Precedence field.

That is what EF, CS3 and AF41 means, just a common way of signalling that your packet is important, or not that much. But just tagging your packets won’t get you far. For now, you’ve got your Asterisk correctly tagging the packets, and your tunnel to preserve them. Time to add the magic to classify and prioritise the packets!

Linux Traffic Control

Linux has the tc tool for configuring and setting up a QoS policy. With it you can configure different kinds of queueing disciplines and classes. This queues acts directly on net devices, so you have to configure it per device. In the example below we have an ADSL modem on ppp0 device.

TC allows you to configure classful and classless disciples, each one supporting different scheduling algorithms. We will use Hierarchy Token Bucket (HTB) for the classful packets (the ones that got tagged by Asterisk), and Stochastic Fairness Queueing (SFQ) for the classless packets. After getting your queues configured you have to inform iptables that it should use the queue, that’s basically setting up some CLASSIFY targets. You definitely can add some MARK rules to tag your packets, but we don’t need it, Asterisk is doing that job for us.

First we will configure what is the maximum bandwidth allowed, in this case we have an 1000kbps uplink that we want to add a QoS policy. The following table illustrates the QoS policy required for the network. As we are using an asymmetric connection, we will limit the upload bandwidth to 95% of the nominal speed.

Class Nominal rate Maximum rate Priority Packets
Real time 47.5kbps 95kbps 0 ICMP, SYN, RST, ACK
High 522.5kbps 950kbps 1 EF and CS3 packets
Regular 190kbps 950kbps 2 Regular traffic, HTTP, SSH, etc
Bulk 190kbps 950kbps 3  
QoS Policy

With the queues in place you just have to add the necessary iptable rules. The rules will classify the packets that have the DSCP tag using the same classes that you defined using tc. That’s it, your QoS is now in place. Just make sure you add and remove the rules according to the status of your link (in this case ppp0). The script bellow is called by /etc/ppp/ip-up.d and /etc/ppp/ip-down.d, with the start and stop targets respectively.

# !/bin/bash
# 20110916 - Leonardo Santos <leonardo at aligera dot com dot br>
# Initial version. It only uses the iptables target CLASSIFY.
# For the QoS to work, Asterisk has to tag the packets with the right DSCP.
# The OpenVPN tunnel must be passing along the DSCP field, and not blanking it out.
#
PATH=$PATH:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin

# uplink in kbps
UPLINK=1000
DEV=ppp0

CEIL=$(($UPLINK*95/100))

CLASS_RT="10"
CLASS_HIGH="11"
CLASS_REG="12"
CLASS_BULK="13"

do_iptables() {
        iptables -$1 POSTROUTING -t mangle -p icmp -j CLASSIFY --set-class 1:$CLASS_RT
        iptables -$1 POSTROUTING -t mangle -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j CLASSIFY --set-class 1:$CLASS_RT
        iptables -$1 POSTROUTING -t mangle -p udp -m dscp --dscp-class cs3 -j CLASSIFY --set-class 1:$CLASS_HIGH
        iptables -$1 POSTROUTING -t mangle -p udp -m dscp --dscp-class ef -j CLASSIFY --set-class 1:$CLASS_HIGH
}
add_rules() {
        tc qdisc add dev $DEV root handle 1: htb default $CLASS_BULK
        tc class add dev $DEV parent 1: classid 1:1 htb rate ${CEIL}kbit ceil ${CEIL}kbit
        tc class add dev $DEV parent 1:1 classid 1:$CLASS_RT   htb rate $((1*$CEIL/20))kbit  ceil $(($CEIL/10))kbit prio 0
        tc class add dev $DEV parent 1:1 classid 1:$CLASS_HIGH htb rate $((11*$CEIL/20))kbit ceil ${CEIL}kbit       prio 1
        tc class add dev $DEV parent 1:1 classid 1:$CLASS_REG  htb rate $((4*$CEIL/20))kbit  ceil ${CEIL}kbit       prio 2
        tc class add dev $DEV parent 1:1 classid 1:$CLASS_BULK htb rate $((4*$CEIL/20))kbit  ceil ${CEIL}kbit       prio 3
        tc qdisc add dev $DEV parent 1:$CLASS_HIGH handle 120: sfq perturb 10
        tc qdisc add dev $DEV parent 1:$CLASS_BULK handle 130: sfq perturb 10
        do_iptables A
}
del_rules() {
        tc qdisc del dev $DEV root
        do_iptables D
}
show_status() {
        tc -s -d class show dev $DEV
        tc -s -d qdisc show dev $DEV
}
case $1 in
        start)
                add_rules
        ;;
        stop)
                del_rules
        ;;
        status)
                show_status
        ;;
        restart)
                del_rules
                add_rules
        ;;
        *)
                echo "Usage: $0 {start|stop|restart|status}"
                exit 1
        ;;
esac

I would like to thank Leonardo Santos for putting the script together and letting me publish it, and for being a good friend.

Working with LCD glyphs

2011 May 31
by Pedro Kiefer

Reading a diff today I found this piece of code for defining a font for a matrix LCD display. The code is interesting, it lets the developer see what the font looks like, so fixing your alphabet is really easy.

unsigned char font5x7[][8] =
{
/* z */
 ,{
   ________,
   ________,
   XXXXX___,
   ___X____,
   __X_____,
   _X______,
   XXXXX___,
   ________}

/* s */
 ,{
   ________,
   ________,
   _XXX____,
   X_______,
   _XX_____,
   ___X____,
   XXX_____,
   ________}
}

But something is fishy here, how do the compiler understand ________ as being 0×00, or 0xFF? So I went on to see the included header… and ouch, this is what I found.

#define	_XX_____	0x60
#define	_XX____X	0x61
#define	_XX___X_	0x62
#define	_XX___XX	0x63
#define	_XX__X__	0x64
#define	_XX__X_X	0x65
#define	_XX__XX_	0x66
#define	_XX__XXX	0x67
#define	_XX_X___	0x68
#define	_XX_X__X	0x69
#define	_XX_X_X_	0x6a
#define	_XX_X_XX	0x6b
#define	_XX_XX__	0x6c
#define	_XX_XX_X	0x6d
#define	_XX_XXX_	0x6e
#define	_XX_XXXX	0x6f

This is ugly as code and pretty as ASCII art. When we are coding we want beautiful code, but not pretty ASCII art. Let that to all the artists, they do better art than we do. So, how do we fix the code? Simple, macros to the rescue!

#define _	0
#define X	1
#define b(a,b,c,d,e,f,g,h)	(a << 7| b << 6 | c << 5 | d << 4 | e << 3 | f << 2 | g << 1 | h)

With this we let the compiler do the dirty job of creating all those values. Using the macros above, the code becomes easier to maintain and read. Just remember to undef the macros after using it, as you don’t want all your X’s, _’s and b’s being changed!

unsigned char font5x7[][8] = {
	/* z */ {
	b(_,_,_,_,_,_,_,_),
	b(_,_,_,_,_,_,_,_),
	b(X,X,X,X,X,_,_,_),
	b(_,_,_,X,_,_,_,_),
	b(_,_,X,_,_,_,_,_),
	b(_,X,_,_,_,_,_,_),
	b(X,X,X,X,X,_,_,_),
	b(_,_,_,_,_,_,_,_)
	},

	/* s */ {
	b(_,_,_,_,_,_,_,_),
	b(_,_,_,_,_,_,_,_),
	b(_,X,X,X,_,_,_,_),
	b(X,_,_,_,_,_,_,_),
	b(_,X,X,_,_,_,_,_),
	b(_,_,_,X,_,_,_,_),
	b(X,X,X,_,_,_,_,_),
	b(_,_,_,_,_,_,_,_)
	},
};

By the way, you can apply this idea for creating small graphics on code. It’s easy and self-documenting. Happy hacking!

Update:
I just remembered the section Making a Glyph from Bit Patterns from Expert C Programming (buy this book if you don’t have it yet!), it gives a solution similar to mine. The macros defined there are:

#define _ )*2
#define X )*2 + 1
#define s ((((((((0

So the code looks like this:

unsigned char font5x7[][8] = {
	/* z */ {
	s _ _ _ _ _ _ _ _,
	s _ _ _ _ _ _ _ _,
	s X X X X X _ _ _,
	s _ _ _ X _ _ _ _,
	s _ _ X _ _ _ _ _,
	s _ X _ _ _ _ _ _,
	s X X X X X _ _ _,
	s _ _ _ _ _ _ _ _,
	},
	/* s */ {
	s _ _ _ _ _ _ _ _,
	s _ _ _ _ _ _ _ _,
	s _ X X X _ _ _ _,
	s X _ _ _ _ _ _ _,
	s _ X X _ _ _ _ _,
	s _ _ _ X _ _ _ _,
	s X X X _ _ _ _ _,
	s _ _ _ _ _ _ _ _,
	}
};

Estrada sem fim

2011 April 19
by Pedro Kiefer

Acordamos em Etretat com muitos quilômetros pela frente, um bom trecho de carro e outro tanto de trem. Tomamos nosso café da manhã, comprado numa boulangerie, olhando as falésias na beira da praia. Nem o vento congelante atrapalho o momento. Acertamos o hotel e partimos para a estrada, eram pelo menos 3h de viagem até Lille. As auto-estradas francesas são muito boas, valem o que se paga de pedágio.

Utilizamos pouco o GPS, somente para nos guiarmos até a auto-estrada e depois para chegarmos em Lille. Dessa vez analisamos toda a rota antes de partimos para a estrada, foi bem melhor assim. Às 15h chegamos na Gare de Flanders, em Lille. Próximo trem para Brugge era às 17h, tínhamos tempo para descansar da estrada, almoçarmos e comprarmos as passagens.

Foi a primeira viagem de trem da Nanda, ela adorou a experiência, parecia eu na minha primeira viagem de trem. Chegamos às 18h33 em Brugge, logo estávamos no hotel, bastou pegarmos o primeiro ônibus em direção a praça central. Aproveitamos o final do dia, afinal só escurece às 21h, para conhecer um pouco da cidade. Nos assustamos com o preço dos restaurantes, preços para turistas. Acabamos jantando no Quick, era a melhor opção e o mais perto do hotel.

Etretat

2011 April 19
by Pedro Kiefer

Outra ótima dica que recebemos: visitem Etretat. Saímos cedo de Granville, rumo a Etretat e suas falésias. Foram uns 240km, passando pelas gigantescas pontes que existem em Le Havre. Como anotamos os principais pontos da rota, só utilizamos o GPS nos pontos mais críticos. A sinalização nas estradas é muito boa, tendo um mapa e planejando a rota antes é muito fácil de se locomover de carro pela França.

Chegamos em Etretat e já era hora de almoçarmos, antes de irmos para praia ver as falésias. Comemos um crepe de salmão, nos instalamos no hotel e partimos para a praia, que estava há 3 quadras dali. Chegando na praia nos deparamos com as incríveis falésias! A cidade foi criada bem no meio do vale entre as falésias. Na praia não há areia, somente pedras, de todos os tamanhos e formatos. A sensação de andar no meio de brita é engraçada, o pé afunda.

Subimos nas falésias dos dois lados da praia. A vista é impressionante. Definitivamente deve ser incluído numa visita pelo interior da frança. O tempo estava ótimo, apesar de nublado, não havia vento e a temperatura estava amena. A cidade deve fica explodindo de turistas no verão.

A noite começou a chover e ventar, logo o frio virou congelante novamente. Jantamos batatas para nos aquecermos.

Bad Behavior has blocked 72 access attempts in the last 7 days.