<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Håkon Nessjøen</title>
	<atom:link href="http://lunatic.no/feed/" rel="self" type="application/rss+xml" />
	<link>http://lunatic.no</link>
	<description>Just another life hacker</description>
	<lastBuildDate>Tue, 06 Dec 2011 22:39:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Following status of a file or command</title>
		<link>http://lunatic.no/2011/12/following-status-of-a-file-or-command/</link>
		<comments>http://lunatic.no/2011/12/following-status-of-a-file-or-command/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 22:39:25 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[Tips & tricks]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=284</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2011/12/following-status-of-a-file-or-command/"></g:plusone></div>
A command most people know about in POSIX systems, is &#8220;tail&#8221;. A command showing you the &#8216;tail&#8217;(in most cases, the last lines) of a file or pipe. Or with the option -f, it actually monitors your files, and will show the newly appended lines live. But a command that is not that much known is [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2011/12/following-status-of-a-file-or-command/"></g:plusone></div>
<p>A command most people know about in POSIX systems, is &#8220;tail&#8221;. A command showing you the &#8216;tail&#8217;(in most cases, the last lines) of a file or pipe. Or with the option -f, it actually monitors your files, and will show the newly appended lines live.</p>
<p>But a command that is not that much known is &#8220;watch&#8221;. Say someone is uploading a file to your FTP server, and you want to see the file grow in size. If you want to &#8220;monitor&#8221; how big the file has become without making a &#8220;bash loop&#8221; that clears the screen and runs the command again, often making a flickering output, you can easily just type:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">watch</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">1</span> <span style="color: #c20cb9; font-weight: bold;">du</span> <span style="color: #660033;">-h</span> uploadedfile.zip</pre></div></div>

<p>Watch will now every second run the command &#8220;du -sh uploadedfile.zip&#8221; and display it on your screen, along with the current time.</p>
<p>Or if you want to look at a whole folder being uploaded to:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">watch</span> <span style="color: #660033;">-n</span> <span style="color: #000000;">1</span> <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-lah</span> ~<span style="color: #000000; font-weight: bold;">/</span>upload</pre></div></div>

<p>Would at all times show the latest directory information about your upload folder. Any files growing in size, or any new files appearing would be shown each second. This if of course just a few simple examples of how to use the tool &#8220;watch&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2011/12/following-status-of-a-file-or-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DMX Light control system</title>
		<link>http://lunatic.no/2011/11/dmx-light-control-system/</link>
		<comments>http://lunatic.no/2011/11/dmx-light-control-system/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 22:53:56 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[dmx]]></category>
		<category><![CDATA[hobby]]></category>
		<category><![CDATA[jackd]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=275</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2011/11/dmx-light-control-system/"></g:plusone></div>
Every year, I team up with raider.no to arrange a party in Hurdalen, or around the Oslo area. And one of the things that is important for us (especially me and William), is to create a cool opening show, using our own innovative technology. We don&#8217;t have a lot of money in the organization, so [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2011/11/dmx-light-control-system/"></g:plusone></div>
<p>Every year, I team up with <a href="http://raider.no/" target="_blank">raider.no</a> to arrange a party in Hurdalen, or around the Oslo area. And one of the things that is important for us (especially me and <a href="http://williamviker.net/" target="_blank">William</a>), is to create a cool opening show, using our own innovative technology. We don&#8217;t have a lot of money in the organization, so we tend to also develop stuff that already exists.</p>
<p>This year, we wanted to do the timing of the show entirely in our own system, as we weren&#8217;t satisfied with the Avolites Pearl systems own &#8220;show timing&#8221; system.</p>
<p>So we split up the task in two daemons and a GUI. The two daemons are written in C, and the GUI in Perl (using the Catalyst Framework).<br />
The first deamon is the &#8220;DMX daemon&#8221;, which handles existing DMX data from the Pearl mixer as the rest of the night will be run from this board. This is transferred via network over the ArtNet protocol. It also listens for our own udp DMX-commands, which includes simple operations like, fade (linear), blink, subtract, add, etc. These functions allows external scripts and programs to send simple commands to control the lights. For example &#8220;fade channel 1 from 0 to 255 in 2 seconds&#8221;. Which would then automatically execute, without the client having do anything more. You can also group together a bunch of actions in a &#8220;transaction&#8221;, and then have it execute as soon as you send the &#8220;end transaction&#8221; command. The resulting DMX data is sent to the Enttec DMX dongle connected to this computer. The system is so lightweight, that there was no noticeable delay from using ArtNet->DMXDaemon->EnttecDongle over network, than using the direct DMX output from the board. The nice thing is that, if we want, the show daemon can forcibly stop all data from the Pearl mixer, or even alter the data using add/subtract/max/min commands.</p>
<p>The next daemon is the show daemon, this takes complete scripted shows from the database (created by the GUI), and converts them to commands to be sent to the DMX daemon. This daemon uses (lib)jackd2 to fire events at the exact time according to the sound file playing in a external program like Ardour, which sends timecodes via jackd. The show daemon has functions to group together effects that will be executed at specific timestamps.</p>
<p>Here&#8217;s a link to a <a href='http://lunatic.no/2011/11/dmx-light-control-system/tekniskoversiktdistrictsep17apningsshow/' rel='attachment wp-att-276'>overview</a> of how we wired it all up for the show.</p>
<p>The whole system is kept open-source at <a href="https://github.com/RaiderEMB/timecode-dmx">github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2011/11/dmx-light-control-system/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Variable initialization in C</title>
		<link>http://lunatic.no/2011/11/variable-initialization-in-c/</link>
		<comments>http://lunatic.no/2011/11/variable-initialization-in-c/#comments</comments>
		<pubDate>Sun, 13 Nov 2011 04:46:10 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Tips & tricks]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=263</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2011/11/variable-initialization-in-c/"></g:plusone></div>
I&#8217;m feel that I am starting to get the hold of C programming. But every now and then, I get these &#8220;ahaaa&#8221; moments, that I am a bit embarrassed about. One of these &#8220;aha&#8221;&#8216;s I experienced today is how/when C handles initialization of variables where you don&#8217;t explicitly initialize them yourself. Consider this code: 1 [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2011/11/variable-initialization-in-c/"></g:plusone></div>
<p>I&#8217;m feel that I am starting to get the hold of C programming. But every now and then, I get these &#8220;ahaaa&#8221; moments, that I am a bit embarrassed about. One of these &#8220;aha&#8221;&#8216;s I experienced today is how/when C handles initialization of variables where you don&#8217;t explicitly initialize them yourself.</p>
<p>Consider this code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> var1<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">**</span>argv<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> argc<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">static</span> <span style="color: #993333;">int</span> var2<span style="color: #339933;">;</span>
  <span style="color: #993333;">int</span> var3<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here i deliberately did not initialize any of the variables to make an example. The first two variables will automatically be initialized with 0 before your main() function is executed. The last variable lives in the stack and will not be initialized unless you explicitly do it yourself. So it will probably have a &#8216;random&#8217; value. Thats kind of handy to know about ;) I feel like this probably is one of the first things you usually learn about C, but I had totally missed this. Nice to know. Also if you use Valgrind to check your code, it can be very good at following uninitialized stack variables. It will actually follow uninitialized memory bit-wise.</p>
<p>First I thought the memory was initialized to 0 at compile-time, but after some reading, I learnt that the compiler records the amount of uninitialized memory for global and static variables, and stores the amount of data required in the BSS segment of the program. This way the executable will not grow in size in line with the amount of uninitialized data. For this reason some people call the BSS segment for the &#8220;Better Save Space&#8221; segment.</p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2011/11/variable-initialization-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>X applications and Socks proxy via SSH</title>
		<link>http://lunatic.no/2011/11/x-applications-and-socks-proxy-via-ssh/</link>
		<comments>http://lunatic.no/2011/11/x-applications-and-socks-proxy-via-ssh/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 20:29:26 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[Tips & tricks]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=249</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2011/11/x-applications-and-socks-proxy-via-ssh/"></g:plusone></div>
I have often been very thankful of people who blog/share about things they find out on their own, and want to help others find it out quicker, or just to make little known, but awesome, features more visible. So I thought, maybe I should start to write these small posts about stuff i find, or [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2011/11/x-applications-and-socks-proxy-via-ssh/"></g:plusone></div>
<p>I have often been very thankful of people who blog/share about things they find out on their own, and want to help others find it out quicker, or just to make little known, but awesome, features more visible. So I thought, maybe I should start to write these small posts about stuff i find, or stuff I want others to know about. So here goes the first.</p>
<h3>Remote X applications</h3>
<p>Have you ever wanted to run an x application from a distant server, but don&#8217;t want to bother with installing VNC or installing anything at all? There is this little known feature in the standard OpenSSH client, which will proxy your X display connection. It&#8217;s as easy as running this from a terminal window:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-X</span> myuser<span style="color: #000000; font-weight: bold;">@</span>remote.server.net</pre></div></div>

<p>Now, after you have authenticated, you can start x applications. For example xclock, or kdevelop or other nifty stuff, and it comes right to your local X windows.</p>
<p>&nbsp;</p>
<h3>Remote connections using SOCKS</h3>
<p>When you develop against remote web services, people often restrict your access by your servers IP address. This way only your web server can connect to the web services. But sometimes the only documentation you get from them are the wsdl pages on their server, or maybe they even expect you to have a web-browser on your server to browse the documentation on their web services server. Then you have a problem right? And links/lynx isn&#8217;t always web 2.0 enough. Thankfully I recently found this nice Socks feature in OpenSSH. Just connect as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-D</span> <span style="color: #000000;">8080</span> myuser<span style="color: #000000; font-weight: bold;">@</span>my.webserver.net</pre></div></div>

<p>Now when you are logged in, you automatically have a Socks server running at 127.0.0.1:8080. Go to your favourite browser, and go to proxy settings, and enter 127.0.0.1 port 8080 under the &#8220;Socks&#8221; configuration, and save. Now all your web-browsing is tunneled through your web server, and you can easily open the documentation you wanted.</p>
<p>If you have an application that does not know of Socks proxying, and still want to &#8216;tunnel&#8217; your data through your remote server, you can of course always use the more known port forwarding functions in OpenSSH (-L, and -R).</p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2011/11/x-applications-and-socks-proxy-via-ssh/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MAC-address based Telnet server in Linux</title>
		<link>http://lunatic.no/2010/12/mac-address-based-telnet-server-in-linux/</link>
		<comments>http://lunatic.no/2010/12/mac-address-based-telnet-server-in-linux/#comments</comments>
		<pubDate>Sun, 26 Dec 2010 00:40:48 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[mikrotik]]></category>
		<category><![CDATA[routeros]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=200</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/12/mac-address-based-telnet-server-in-linux/"></g:plusone></div>
My previous post was about RouterOS Mac-Telnet application for Linux users where I talked about the MAC-Telnet client I created for Linux users. Since then, I have both started porting it to OS X and to create a daemon version of MAC-Telnet, called &#8220;mactelnetd&#8221;. You can look at the different branches at github.com. To use [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/12/mac-address-based-telnet-server-in-linux/"></g:plusone></div>
<p>My previous post was about <a title="Permanent Link to RouterOS Mac-Telnet application for Linux users" rel="bookmark" href="/2010/10/routeros-mac-telnet-application-for-linux-users/">RouterOS Mac-Telnet application for Linux users</a> where I talked about the MAC-Telnet client I created for Linux users.</p>
<p>Since then, I have both started porting it to OS X and to create a daemon version of MAC-Telnet, called &#8220;mactelnetd&#8221;. You can look at the different branches at <a title="MAC-Telnet at github.com" href="http://github.com/haakonnessjoen/MAC-Telnet" target="_blank">github.com</a>.</p>
<p>To use mactelnetd, install MAC-Telnet from the github link, using either the latest .deb file from the download section (if you are on Ubuntu or Debian), or download the sources and compile it yourself. You can find instructions for this on my previous post. you first need to edit the /etc/mactelnetd.users file. This file consists of the users and their respective passwords for logging into the server. I wish we wouldn&#8217;t have to write the passwords in plain text, but the authentication method used in the mac-telnet protocol demands that we know the actual password, unlike ssh/regular telnet, where the password can be stored as a hash sum.</p>
<p>The format of /etc/mactelnetd.users is like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Users file for MAC-Telnetd</span>
<span style="color: #666666; font-style: italic;"># Use existing usernames from your system</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Format:</span>
<span style="color: #666666; font-style: italic;">#username:password</span>
haakon:mypass</pre></div></div>

<p>Here I have activated the user account haakon, with apssword &#8220;mypass&#8221;. It is important that the user names that you enter into this file is existing user names from your Linux machine. The mactelnetd daemon will check /etc/passwd for home directory, and what shell to spawn for the user, as any regular terminal server.</p>
<p>When you have created/modified this file to your wishes, you can start the server daemon:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> mactelnetd</pre></div></div>

<p>If there were no error messages, it spawned successfully and you can watch it&#8217;s log entries via syslog.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">tail</span> <span style="color: #660033;">-f</span> <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>log<span style="color: #000000; font-weight: bold;">/</span>syslog
Dec <span style="color: #000000;">26</span> 01:06:08 haakon-kontorpc mactelnetd<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">10183</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>: Bound to 0.0.0.0:<span style="color: #000000;">20561</span>
Dec <span style="color: #000000;">26</span> 01:<span style="color: #000000;">31</span>:<span style="color: #000000;">17</span> haakon-kontorpc mactelnetd<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">10183</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>: <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">28780</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> New connection from <span style="color: #000000;">0</span>:c:<span style="color: #000000;">42</span>:3e:<span style="color: #000000;">20</span>:c2.
Dec <span style="color: #000000;">26</span> 01:<span style="color: #000000;">31</span>:<span style="color: #000000;">17</span> haakon-kontorpc mactelnetd<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">10391</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>: <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">28780</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> User haakon logged in.</pre></div></div>

<p>Here you see the results of me logging into my Linux Desktop machine from a MikroTik Router using the RouterOS command &#8220;tool mac-telnet 1c:6f:65:2c:98:b7&#8243;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>admin<span style="color: #000000; font-weight: bold;">@</span>Holmenveien<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> tool mac-telnet 1c:6f:<span style="color: #000000;">65</span>:2c:<span style="color: #000000;">98</span>:b7
Login: haakon
Password:
Trying 1C:6F:<span style="color: #000000;">65</span>:2C:<span style="color: #000000;">98</span>:B7...
Connected to 1C:6F:<span style="color: #000000;">65</span>:2C:<span style="color: #000000;">98</span>:B7
Linux haakon-kontorpc 2.6.35-<span style="color: #000000;">23</span>-generic-pae <span style="color: #666666; font-style: italic;">#41-Ubuntu SMP Wed Nov 24 10:35:46 UTC 2010 i686 GNU/Linux</span>
Ubuntu <span style="color: #000000;">10.10</span>
&nbsp;
Welcome to Ubuntu<span style="color: #000000; font-weight: bold;">!</span>
<span style="color: #000000; font-weight: bold;">*</span> Documentation:  https:<span style="color: #000000; font-weight: bold;">//</span>help.ubuntu.com<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
haakon<span style="color: #000000; font-weight: bold;">@</span>haakon-kontorpc:~$ <span style="color: #c20cb9; font-weight: bold;">w</span>
01:<span style="color: #000000;">32</span>:<span style="color: #000000;">52</span> up <span style="color: #000000;">24</span> days, <span style="color: #000000;">22</span>:<span style="color: #000000;">48</span>,  <span style="color: #000000;">2</span> <span style="color: #c20cb9; font-weight: bold;">users</span>,  load average: <span style="color: #000000;">0.53</span>, <span style="color: #000000;">0.61</span>, <span style="color: #000000;">0.57</span>
USER     TTY      FROM              LOGIN<span style="color: #000000; font-weight: bold;">@</span>   IDLE   JCPU   PCPU WHAT
haakon   tty7     :<span style="color: #000000;">0</span>               01Dec10 24days <span style="color: #000000;">26</span>:29m  3.17s gnome-session
haakon   pts<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">0</span>    <span style="color: #000000;">0</span>:c:<span style="color: #000000;">42</span>:3e:<span style="color: #000000;">20</span>:c2  01:<span style="color: #000000;">32</span>    0.00s  0.15s  0.00s <span style="color: #c20cb9; font-weight: bold;">w</span></pre></div></div>

<p>If you want to shut down the mactelnetd server, simply write &#8220;sudo killall mactelnetd&#8221; in your shell.</p>
<p><strong>FUN TIP:</strong> If you want &#8220;fancy&#8221; terminal promt like RouterOS has, with syntax coloring on your Linux machine (logging in via console, xterm, ssh or mactelnet), you can install the shell &#8220;<a title="Friendly Interactive SHell" href="http://en.wikipedia.org/wiki/Friendly_interactive_shell" target="_blank">fish</a>&#8220;, and then change your user settings to use it as the default shell by issuing:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> usermod <span style="color: #660033;">--shell</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>fish myusername</pre></div></div>

<p><strong>NB:</strong> Be sure that the file /usr/bin/fish exists, and change &#8220;myusername&#8221; with your username, and remember to test logging into a second console, just to be sure you did this correctly, before logging out of your current terminal. To revert, set /bin/bash as your shell again, or what ever shell you previously used.</p>
<p>Here&#8217;s a direct link to the latest Ubuntu/Debian package at the time of writing:</p>
<p><a href="https://github.com/downloads/haakonnessjoen/MAC-Telnet/mactelnet_0.2-1ubuntu1_i386.deb">mactelnet_0.2-1ubuntu1_i386.deb</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2010/12/mac-address-based-telnet-server-in-linux/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RouterOS Mac-Telnet application for Linux users</title>
		<link>http://lunatic.no/2010/10/routeros-mac-telnet-application-for-linux-users/</link>
		<comments>http://lunatic.no/2010/10/routeros-mac-telnet-application-for-linux-users/#comments</comments>
		<pubDate>Fri, 01 Oct 2010 10:28:53 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[mikrotik]]></category>
		<category><![CDATA[routeros]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=180</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/10/routeros-mac-telnet-application-for-linux-users/"></g:plusone></div>
Earlier, I wrote about a Wireshark plugin for dissecting Mac-Telnet packets. Now I have created an open source application for connecting to a RouterOS router via its MAC address from Linux without having to install Wine. At the time of writing the project is still in &#8220;alpha&#8221; stage. But it is fully functional. Though it [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/10/routeros-mac-telnet-application-for-linux-users/"></g:plusone></div>
<p>Earlier, I wrote about a <a href="http://lunatic.no/2010/06/dissecting-mikrotiks-mac-telnet-packets/">Wireshark plugin</a> for dissecting Mac-Telnet packets. Now I have created an open source application for connecting to a RouterOS router via its MAC address from Linux without having to install Wine. At the time of writing the project is still in &#8220;alpha&#8221; stage. But it is fully functional. Though it will probably be rewritten soon. I feel that the second time you write the same application, it tends to be more robust and thought through.. The first time is more of a proof of concept, and I let myself do a lot of short-cuts, just to be able to see it working.</p>
<p>The source code can be found at <a href="http://github.com/haakonnessjoen/MAC-Telnet">github</a>. Just find the &#8220;download source&#8221; link at the top of the page.</p>
<p>To compile it, simply untar it, and compile it with &#8220;make&#8221; .<br />
Because it needs to alter the frame headers, it uses RAW sockets, which means that you will need root access to use this tool.</p>
<p>To find the MAC address of connected routers, try the included tool mndp. It might take up to 2 minutes to find all routers, since it is a passive tool that waits for the routers to broadcast their info on the network:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># ./mndp</span>
Searching <span style="color: #000000; font-weight: bold;">for</span> MikroTik routers... Abort with CTRL+C.
  <span style="color: #000000;">0</span>:c:<span style="color: #000000;">42</span>:<span style="color: #000000;">43</span>:<span style="color: #000000;">58</span>:a5 HMG</pre></div></div>

<p>HMG is my name of the found router, set in the Identity section of the router.</p>
<p>Here are some mac-telnet usage information:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># ./mactelnet --help</span>
Usage: .<span style="color: #000000; font-weight: bold;">/</span>mactelnet <span style="color: #000000; font-weight: bold;">&lt;</span>ifname<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;</span>MAC<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;</span>username<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>password<span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
Parameters:
  ifname    Network interface that the RouterOS resides on. <span style="color: #7a0874; font-weight: bold;">&#40;</span>ex: eth0<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  MAC       MAC-Address of the RouterOS device. Use mndp to discover them.
  username  Your username.
  password  Your password.</pre></div></div>

<p><strong>Tip:</strong> Log out using CTRL+D on your keyboard.</p>
<p>Usage example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># sudo ./mactelnet eth0 0:c:42:43:58:a5 admin mysecretpass</span>
Connecting to <span style="color: #000000;">0</span>:c:<span style="color: #000000;">42</span>:<span style="color: #000000;">43</span>:<span style="color: #000000;">58</span>:a5...done
&nbsp;
&nbsp;
  MMM      MMM       KKK                          TTTTTTTTTTT      KKK
  MMMM    MMMM       KKK                          TTTTTTTTTTT      KKK
  MMM MMMM MMM  III  KKK  KKK  RRRRRR     OOOOOO      TTT     III  KKK  KKK
  MMM  MM  MMM  III  KKKKK     RRR  RRR  OOO  OOO     TTT     III  KKKKK
  MMM      MMM  III  KKK KKK   RRRRRR    OOO  OOO     TTT     III  KKK KKK
  MMM      MMM  III  KKK  KKK  RRR  RRR   OOOOOO      TTT     III  KKK  KKK
&nbsp;
  MikroTik RouterOS <span style="color: #000000;">4.0</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>c<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #000000;">1999</span>-<span style="color: #000000;">2009</span>       http:<span style="color: #000000; font-weight: bold;">//</span>www.mikrotik.com<span style="color: #000000; font-weight: bold;">/</span>
&nbsp;
&nbsp;
<span style="color: #7a0874; font-weight: bold;">&#91;</span>admin<span style="color: #000000; font-weight: bold;">@</span>HMG<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>[UPDATE]<br />
<a href="http://www.omniflux.com/">Omni Flux</a> sent me a patch that speeds up the MNDP discovery time by sending out a MNDP request before waiting for replies. Thanks!</p>
<p>He also informed me that his tests showed that you could send all the mac-telnet traffic to- and from- the broadcast address. This means that you would not need socket_raw (read: root privileges) capabillities to be able to send data to the router.</p>
<p>So the latest version of mactelnet, now also as a binary .deb package below, can be used without root privileges, unless you don&#8217;t want all data from your session to be broadcasted on your local network. You can optionally use the &#8220;old&#8221; method, where it uses the destination routers mac address as destination instead of broadcast.</p>
<p>Heres the latest &#8220;stable&#8221;: v0.1 version<br />
<a href="http://github.com/haakonnessjoen/MAC-Telnet/tarball/v0.1">Source tarball</a><br />
<a href="http://github.com/downloads/haakonnessjoen/MAC-Telnet/mactelnet_0.1-0ubuntu1_i386.deb">Ubuntu/Debian binary package</a></p>
<h2>Last commits on MAC-Telnet at GitHub</h2><ul><li>21.11.2011 - <a href="https://github.com/haakonnessjoen/MAC-Telnet/commit/1ac6a78d69634e5c54bb8b6182995d1d84a9cf71">Fix Makefile to include LDFLAGS and LIBS</a></li>
<li>21.11.2011 - <a href="https://github.com/haakonnessjoen/MAC-Telnet/commit/6a9192d4d4f1d5c6849de158a707f4cc759c3ea5">Removed librt as mandatory library. Only needed for kfreebsd.</a></li>
<li>13.11.2011 - <a href="https://github.com/haakonnessjoen/MAC-Telnet/commit/a5b710a45671c1b9504130d83d3ebe7fe8a65c5a">Added support for reloading list of interfaces/addresses on HUP signal, and fixed notification to connections of TERM signal.</a></li>
<li>13.11.2011 - <a href="https://github.com/haakonnessjoen/MAC-Telnet/commit/4d3923c1cc947fbf1a6b23e2adf6915c47aac8d1">Fix bug in interface enumerator</a></li>
<li>13.11.2011 - <a href="https://github.com/haakonnessjoen/MAC-Telnet/commit/e78481d2e5a4a3ea133980403041579b048ede74">Code &quot;speedup&quot;.</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2010/10/routeros-mac-telnet-application-for-linux-users/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Simple ORM php-library for mySQL</title>
		<link>http://lunatic.no/2010/08/simple-orm-php-library-for-mysql/</link>
		<comments>http://lunatic.no/2010/08/simple-orm-php-library-for-mysql/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 17:51:54 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[hobby]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=132</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/08/simple-orm-php-library-for-mysql/"></g:plusone></div>
I have been using Akelos both for professional and personal uses for a long time now. But sometimes I only need the ORM part of Akelos, and not the whole MVC shebang. So for a while ago, I made a ~40 lines php script that abstracted mysql queries in a easy to use class, much [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/08/simple-orm-php-library-for-mysql/"></g:plusone></div>
<p>I have been using Akelos both for professional and personal uses for a long time now. But sometimes I only need the ORM part of Akelos, and not the whole MVC shebang. So for a while ago, I made a ~40 lines php script that abstracted mysql queries in a easy to use class, much alike how Akelos&#8217; AKActiveRecord works. But just much much simpler.</p>
<p>Then for a few days ago I thought, why not extend (rewrite) it, and add support for relations and serialization, etc; but still keep it simple enough to be one single small include for my small projects, with only two lines of configuration needed?</p>
<p>So here is initial version under LGPL license: <a href='http://lunatic.no/wp-content/uploads/2010/08/SimpleActiveRecord-v0.1.tar.gz'>SimpleActiveRecord-v0.1.tar.gz</a>.</p>
<p>In the tarball above, you will find everything you need in the file &#8220;simpleactiverecord.php&#8221;, but included is also a test suite using the simpletest library.</p>
<p>To use the orm functions, all you need is the simpleactiverecord.php. But if you are going to extend it, or check that all the functionality is intact, you can use the included tests to check that the all the desired functionality is intact after your changes. To run the tests, just invoke ./test.sh from the command line. (if you are using debian/ubuntu, you need to have the php-cli package installed to do this)</p>
<p>To begin; I will add some phpDoc in the future! It&#8217;s not a finished project.</p>
<p><strong>Examples</strong></p>
<p>Here are some examples and explanations of how you can use this simple ORM:</p>
<p>First the mysql database I use for these examples:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`users`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`customer_id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`username`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`meta`</span> text<span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`customer_id`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`customer_id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`users`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'someuser'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'N;'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`customers`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`name`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`customers`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'CustomerName 1'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`blogposts`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`user_id`</span> <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`title`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`body`</span> text<span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`created_at`</span> datetime <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`updated_at`</span> <span style="color: #993333; font-weight: bold;">TIMESTAMP</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">CURRENT_TIMESTAMP</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #993333; font-weight: bold;">CURRENT_TIMESTAMP</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #ff0000;">`user_id`</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`user_id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`blogposts`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Blogpost 1'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'This is my first blog'</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'2010-08-20 12:54:02'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'Blogpost 2'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'This is my second blog'</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'2010-08-20 12:54:09'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>And then some code to test the functionality:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
&nbsp;
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'simpleactiverecord.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">extends</span> SimpleActiveRecord <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$has_many</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'blogposts'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Blogpost'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$belongs_to</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'customer'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Customer'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$serialize</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'meta'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Customer <span style="color: #000000; font-weight: bold;">extends</span> SimpleActiveRecord <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$has_many</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'users'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Blogpost <span style="color: #000000; font-weight: bold;">extends</span> SimpleActiveRecord <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$belongs_to</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'user'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'User'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
SimpleDbAdapterWrapper<span style="color: #339933;">::</span><span style="color: #004000;">setAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mysqlAdapter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
SimpleDbAdapterWrapper<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ormtest'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'passord'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ormtestdb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'someuser'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* or $user-&gt;findFirstBy('field', 'value'); */</span>
&nbsp;
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Username: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Customer: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">customer</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">blogposts</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$blogpost</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// Dumps the whole blogpost object</span>
	<span style="color: #b1b100;">print</span> <span style="color: #000088;">$blogpost</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Or fetch data from it manually:</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Blog title: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$blogpost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;Blog written by: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$blogpost</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">print</span> <span style="color: #0000ff;">&quot;---<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This gave me the follwoing output:</p>
<p>
<pre>Username: someuser
Customer: CustomerName 1

Blogpost(1)
	Id: 1
	User_id: 1
	Title: Blogpost 1
	Body: This is my first blog
	Created_at:
	Updated_at: 2010-08-20 14:54:02
	User: (reference to a User object)
Blog title: Blogpost 1
Blog written by: someuser
---
Blogpost(2)
	Id: 2
	User_id: 1
	Title: Blogpost 2
	Body: This is my second blog
	Created_at:
	Updated_at: 2010-08-20 14:54:09
	User: (reference to a User object)
Blog title: Blogpost 2
Blog written by: someuser
---</pre>
</p>
<p><strong>Saving / Updating</strong></p>
<p>If I want to change something in for user with id 1 I can simply write:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'newusername'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will update sql with the new values of your user object.</p>
<p><strong>Relations</strong></p>
<p>As you can see in the example; we have three databases with relations to eachother. To relate the models to eachother, all you have to do is to define the relationship in the &#8216;magic&#8217; variables called $belongs_to and $has_many. In the example, I have related the Customer model to the User model, by saying &#8220;$has_many = array(&#8216;users&#8217; => &#8216;User&#8217;);&#8221; which means that the User table has a column called customer_id, which points to the &#8220;Customer&#8221; model. This means that if you have a Customer object, you can access an array of all the users to this customer, by using the $customerobject->users property. This will automatically &#8220;lazy load&#8221; all the users for your selected customer.</p>
<p><strong>Custom table names or field names</strong></p>
<p>If your users table haven&#8217;t defined the customer id field as &#8220;customer_id&#8221; which was automatically guessed, you can define it by saying: &#8220;var $has_many = array(&#8216;users:custom_customer_id&#8217; => &#8216;User&#8217;);&#8221;. Now instead of searching for customer_id in the Users table, it will search for the correct customer id in the custom_customer_id field of the users table. The same goes to the $belongs_to relations.</p>
<p>Also, if you do not define the primary key of your tables as &#8216;id&#8217;. Maybe you have your table&#8217;s primary key named &#8216;user_id&#8217;, you can define this in your model definition. The same goes if you define for example a User class, but the table is names &#8216;my_users&#8217;; you can explicitly define both like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">extends</span> SimpleActiveRecord <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$tableName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'my_users'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$primaryKey</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'user_id'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Serialization</strong></p>
<p>A nifty feature of this library is also automatic serialization. As you can see in the definition of the User class in the first example, i have told the library that the field &#8216;meta&#8217; is a serialized field. This means that it wil automatically be serialized with php&#8217;s &#8220;serialize()&#8221; function before it is saved to SQL. This enables you to add php variables and objects to your object for later use when you load the object from sql again. Let me give an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'is_logged_in'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meta</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'something_useful'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'this'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'useful'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'information'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'about'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'someuser'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will then save the serialized content of meta to sql. The following SQL query happened behind the curtains:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">UPDATE</span> <span style="color: #ff0000;">`users`</span> <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #ff0000;">`customer_id`</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`username`</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'someuser'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">`meta`</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'a:2:{s:12:<span style="color: #000099; font-weight: bold;">\&quot;</span>is_logged_in<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:1;s:16:<span style="color: #000099; font-weight: bold;">\&quot;</span>something_useful<span style="color: #000099; font-weight: bold;">\&quot;</span>;a:6:{i:0;s:4:<span style="color: #000099; font-weight: bold;">\&quot;</span>this<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:1;s:2:<span style="color: #000099; font-weight: bold;">\&quot;</span>is<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:2;s:6:<span style="color: #000099; font-weight: bold;">\&quot;</span>useful<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:3;s:11:<span style="color: #000099; font-weight: bold;">\&quot;</span>information<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:4;s:5:<span style="color: #000099; font-weight: bold;">\&quot;</span>about<span style="color: #000099; font-weight: bold;">\&quot;</span>;i:5;s:8:<span style="color: #000099; font-weight: bold;">\&quot;</span>someuser<span style="color: #000099; font-weight: bold;">\&quot;</span>;}}'</span> <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #ff0000;">`id`</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">LIMIT</span> <span style="color: #cc66cc;">1</span></pre></div></div>

<p>If we load user from sql again now and print the meta field:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meta</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We can see that it has correctly remembered the structure of our meta variable.</p>
<pre>Array
(
    [is_logged_in] => 1
    [something_useful] => Array
        (
            [0] => this
            [1] => is
            [2] => useful
            [3] => information
            [4] => about
            [5] => someuser
        )

)</pre>
<p>You can also have more than one serialized field by specifying it in the class definition:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$serialize</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'meta,metafield2'</span><span style="color: #339933;">;</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$serialize</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'meta'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'metafield2'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Setting / getting multiple keys at once</strong></p>
<p>To set a whole bunch of variables in the object at once, you can use setAttributes(). This function sets the corresponding fields to the values in the associated array given as argument:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'newname'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'customer_id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// or fetch all data from a model as an array:</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Searches</strong></p>
<p>You can also search for specific fields, or specify a WHERE statement manually. For example. If you are searching for all customers with a specific customer name, you can do the following:<br />
$customer = new Customer();<br />
$customers = $customer->findBy(&#8216;name&#8217;, &#8216;CustomerName&#8217;);<br />
print_r($customers);</p>
<p>This would give you an array of Customer objects. You probably notice that I have defined $customer as an empty instance of the Customer object, before using it to find the customers and wonered why.. The reason for this is that PHP &lt; 5.3.0 doesn&#8217;t support <a href="http://php.net/manual/en/language.oop5.late-static-bindings.php">late static bindings</a>. And I would like to support &lt; 5.3.0 since a lot of distributions haven&#8217;t moved to 5.3.x yet.</p>
<p>There is also a function called findFirstBy(), which works in a similar matter, but gives you only 1 object.</p>
<p>The find() and findFirst() functions allow you to define the full WHERE clause yourself. But remember to escape your data when you use these two functions. These are the only two functions that does not automatically escape your input.</p>
<p><strong>Model code</strong></p>
<p>Inside your model (the class definition of your table), you should add functions to handle tasks for your model that is more advanced than setting a variable and saving. For example in a User model, you might want to encrypt the users password just before the object is saved to sql.</p>
<p>Things like this can be done by defining beforeSave(), afterLoad() and beforeDestroy() functions in your model. They do as you think they do. And if your beforeSave() or beforeDestroy() functions return a false boolean, the save- or delete-operation will be aborted!</p>
<p><strong>Exceptions</strong></p>
<p>The class only has two custom exceptions, SqlErrorException and InvalidDbAdapterException. I think the names describes themselves pretty good.<br />
Remember to catch them. InvalidDbAdapterException usually only arrives if your dbAdapter is uncomplete. You should not be able to get this error with for the included mysqlAdapter.</p>
<p><strong>Thats all folks!</strong></p>
<p>Until I add phpDoc documentation, there&#8217;s only one way to learn about all the features included. There&#8217;s a lot of functions that is not mentioned here. But you can easily find about them by reading the source code. It shouldn&#8217;t be all too cryptic to understand.</p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2010/08/simple-orm-php-library-for-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dissecting Mikrotiks Mac-Telnet packets</title>
		<link>http://lunatic.no/2010/06/dissecting-mikrotiks-mac-telnet-packets/</link>
		<comments>http://lunatic.no/2010/06/dissecting-mikrotiks-mac-telnet-packets/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 19:11:43 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[routeros]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=112</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/06/dissecting-mikrotiks-mac-telnet-packets/"></g:plusone></div>
I was searching on the Internet if there were any MAC-Telnet clients for linux/posix, since using terminal.exe in wine is problematic sometimes, as wine gives it access to only one of the NICs in your computer. I didn&#8217;t find any clients, but I found this nice reverse engineering of the protocol, which interested me, because [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/06/dissecting-mikrotiks-mac-telnet-packets/"></g:plusone></div>
<div id="attachment_117" class="wp-caption alignleft" style="width: 310px"><a href="http://lunatic.no/wp-content/uploads/2010/06/Screenshot-mactelnet-test.png"><img class="size-medium wp-image-117" title="Screenshot of Mac-Telnet dissecter" src="http://lunatic.no/wp-content/uploads/2010/06/Screenshot-mactelnet-test-300x205.png" alt="Screenshot of Mac-Telnet dissecter" width="300" height="205" /></a><p class="wp-caption-text">Mac-Telnet traffic in Wireshark</p></div>
<p>I was searching on the Internet if there were any MAC-Telnet clients for linux/posix, since using terminal.exe in wine is problematic sometimes, as wine gives it access to only one of the NICs in your computer.</p>
<p>I didn&#8217;t find any clients, but I found this nice reverse engineering of the protocol, which interested me, because I&#8217;ve always wondered how it was made, just not enough to check myself. But this guy has done his homework and then some in reverse engineering of Mikrotiks protocol.</p>
<p>You can find his page here: <a href="http://www.omniflux.com/devel/" target="_blank">http://www.omniflux.com/devel/</a></p>
<p>This gave me an Idea to create a MAC-Telnet client myself, and I started some testing with perl to check if I was able to send the correct packets to my mikrotik router. At first you might think; thats easy! All packets via the mac-telnet protocol uses UDP packets, with both source- and destintation port: 20561.</p>
<p>But you can&#8217;t send these packets with your normal socket wrapper library, because you need to set the mac-addresses in the network frame to something other than the source/destination IPs corresponding ARP entry. To go into details: When you send Mac-Telnet packets as a client, you need to send your packets going from your ip to ip 255.255.255.255, but with the router you want to telnet as the destination mac address in the ethernet frame. (even though the ip is set to 255.255.255.255).</p>
<p>To test this in perl, I used Net::RawIP from CPAN, which worked just like I wanted to. The only problem is that you need to have root access to be able to do raw packets.</p>
<p>Anyways, I looked at the packet I sent with my test script, and the response from the RouterBoard with Wireshark. Even though I got it to work, I felt I was having problems reading the packets very easily: Reading omniflux&#8217;s protocol description, and then looking at the hex dump of the packet. So I decided to make a dissector plugin for Wireshark (ethereal), so I could debug the packets easier.</p>
<p>Here is the finished source code for the Wireshark plugin (GPLv2): <a href="http://lunatic.no/wp-content/uploads/2010/06/mactelnet.diff_.txt">mactelnet.diff</a></p>
<p>Just patch this in from the root of the wireshark sources.</p>
<p>[Update: It is now added in the wireshark svn trunk]</p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2010/06/dissecting-mikrotiks-mac-telnet-packets/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>More verbose logging of queue-call attempts in Asterisk</title>
		<link>http://lunatic.no/2010/02/more-verbose-logging-of-queue-call-attempts-in-asterisk/</link>
		<comments>http://lunatic.no/2010/02/more-verbose-logging-of-queue-call-attempts-in-asterisk/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 19:12:54 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[asterisk]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=106</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/02/more-verbose-logging-of-queue-call-attempts-in-asterisk/"></g:plusone></div>
Not much to write about lately, but many of you, me included, use queue_log to generate important statistics about a company&#8217;s queue calls. It is important to see how many times a user have been called, how many times they answered, how long the calls last, etc. But one thing missing, has been how many [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/02/more-verbose-logging-of-queue-call-attempts-in-asterisk/"></g:plusone></div>
<p>Not much to write about lately, but many of you, me included, use queue_log to generate important statistics about a company&#8217;s queue calls. It is important to see how many times a user have been called, how many times they answered, how long the calls last, etc.</p>
<p>But one thing missing, has been how many calls failed because of telephony failure, or configuration errors. So i&#8217;ve uploaded this:</p>
<p><a title="0016925: [patch] app_queue: Log failed attempts to call members" href="https://issues.asterisk.org/view.php?id=16925" target="_blank">https://issues.asterisk.org/view.php?id=16925</a></p>
<p>This patch makes app_queue more verbose to queue_log, reporting problems calling out, as well as marking them as &#8220;congestion&#8221; with a new parameter to the RINGNOANSWER queue_log entry. (also to the AMI event)</p>
<p>More information in the issue tracker.</p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2010/02/more-verbose-logging-of-queue-call-attempts-in-asterisk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Asterisk, DAHDI and TDMoE</title>
		<link>http://lunatic.no/2010/02/asterisk-dahdi-and-tdmoe/</link>
		<comments>http://lunatic.no/2010/02/asterisk-dahdi-and-tdmoe/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 13:40:07 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[asterisk]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://lunatic.no/?p=95</guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/02/asterisk-dahdi-and-tdmoe/"></g:plusone></div>
A few years ago, I read this wiki page at voip-info.org, where I was informed of this nifty thing called TDMoE. TDMoE is TDM over Ethernet. This nifty DAHDI driver (dahdi_dynamic_eth), provides any dahdi signalling via Ethernet. So if you need to have PRI signalling between two asterisk servers in the same room. Look no [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2010/02/asterisk-dahdi-and-tdmoe/"></g:plusone></div>
<p>A few years ago, I read this wiki page at voip-info.org, where I was informed of this nifty thing called TDMoE. TDMoE is <a title="Time Division Multiplexing" href="http://en.wikipedia.org/wiki/Time-division_multiplexing" target="_blank">TDM</a> over Ethernet. This nifty DAHDI driver (dahdi_dynamic_eth), provides <span style="text-decoration: underline;">any</span> dahdi signalling via Ethernet. So if you need to have PRI signalling between two asterisk servers in the same room. Look no further! Instead of buying expensive T1/E1 cards, to be able to talk PRI, you can just use your ethernet cards. TDMoE uses your network interface card to talk to the other asterisk box. To do this, both machines needs to know eachothers MAC addresses. Why? Because TDMoE uses it&#8217;s own ethernet type. Packets between the boxes goes through the ethernet via ethernet type 0xd00d. Inside the ethernet frame, lies a dahdi_dynamic packet, which in turn has the packet/frame from your chosen signalling standard.</p>
<p>But beware. You <span style="text-decoration: underline;">should</span> have DAHDI compatible hardware in at least one of the asterisk boxes. This is because of timing, so you can use the timing from that card, to time the TDMoE. TDM packets needs to be sent every 1ms at a very stable rate. A normal computer without extra hardware cannot acheive 100% the correct timing requirements of DAHDI signalling protocols on it&#8217;s own. The kernel module dahdi_dummy (former ztdummy) can give you a signal close to ~99.4%(my results, testing with dahdi_test -v) of what the timing should be.</p>
<p>In my testing results, TDMoE works nice with dahdi_dummy, but I am unsure if this is stable over time. So better have some DAHDI hardware somewhere.</p>
<p>Before we start, I need to say that, by no means think that I am an expert at DAHDI or TDMoE, or that everything I write here is correct. I take no responsibility of what you do with the information on this post, or the correctness of the information here. If you see something that is plain wrong, please inform me, and I&#8217;ll update the post :)</p>
<p>So suddenly I came to the point where something like TDMoE would be very nice to have, and it worked like a charm. But on the &#8216;tutorials&#8217; out there, almost all of them use the &#8216;em&#8217; signalling. So I thought I could write a bit of what you need to do, and what I did to get PRI signalling to work over ethernet with dahdi_dummy as timing source. So, to set up this, you first need to compile and install libpri, dahdi-kernel, and dahdi-tools on both machines. And then recompile asterisk, so they get dahdi and pri support.</p>
<p>After I did this, in my box with dahdi_dummy loaded I edited /etc/dahdi/system.conf and added the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">dynamic</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">eth,eth1/00:00:01:00:00:00/0,31,0</span>
<span style="color: #000099;">echocanceller</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">mg2,1-15,17-31</span>
<span style="color: #000099;">bchan</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">1-15,17-31</span>
<span style="color: #000099;">dchan</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">16</span>
<span style="color: #000099;">alaw</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">1-15,17-31</span></pre></div></div>

<p>Where 00:00:01:00:00:00 is replaced by the MAC address of the <span style="text-decoration: underline;">other</span> server, and eth1 reflects the network interface I&#8217;d like to use for this. Then in /etc/asterisk/chan_dahdi.conf, I added the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>channels<span style="">&#93;</span></span>
<span style="color: #000099;">context</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">default</span>
<span style="color: #000099;">usecallerid</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">hidecallerid</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">no</span>
<span style="color: #000099;">callwaiting</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">usecallingpres</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">callwaitingcallerid</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">threewaycalling</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">transfer</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">facilityenable</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">canpark</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">cancallforward</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">callreturn</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">echocancel</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">echocancelwhenbridged</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">relaxdtmf</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">immediate</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">no</span>
&nbsp;
<span style="color: #000099;">switchtype</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">euroisdn</span>
<span style="color: #000099;">context</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">inbound</span>
<span style="color: #000099;">group</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">0</span>
<span style="color: #000099;">echocancel</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">signalling</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">pri_net</span>
<span style="color: #000099;">channel</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">&gt;1-15,17-31</span></pre></div></div>

<p>Here you see I set the signalling type to &#8216;pri_net&#8217;, which means that this will be the network side of the pri signalling. Let&#8217;s call this the pri_net server. Then i configured dahdi by modprobing dahdi, modprobing dahdi_dummy, and running dahdi_cfg -vvv</p>
<p>If everything worked well, you should also be able to do dahdi_test -v, and see the precision to be close to 100%. You can now configure the other server. In /etc/dahdi/system.conf on this server, I had the same setup as the pri_net server, but here you put the MAC address of the pri_net server. So they point to eachother.</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">dynamic</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">eth,eth0/00:00:01:00:00:01/0,31,1</span>
<span style="color: #000099;">echocanceller</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">mg2,1-15,17-31</span>
<span style="color: #000099;">bchan</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">1-15,17-31</span>
<span style="color: #000099;">dchan</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">16</span>
<span style="color: #000099;">alaw</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">1-15,17-31</span></pre></div></div>

<p>Remember to change the MAC adress.</p>
<p>Then in /etc/asterisk/chan_dahdi.conf on this server, I had:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>channels<span style="">&#93;</span></span>
<span style="color: #000099;">context</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">default</span>
<span style="color: #000099;">usecallerid</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">hidecallerid</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">no</span>
<span style="color: #000099;">callwaiting</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">usecallingpres</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">callwaitingcallerid</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">threewaycalling</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">transfer</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">facilityenable</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">canpark</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">cancallforward</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">callreturn</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">echocancel</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">echocancelwhenbridged</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">relaxdtmf</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">immediate</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">no</span>
&nbsp;
<span style="color: #000099;">switchtype</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">euroisdn</span>
<span style="color: #000099;">context</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">inbound</span>
<span style="color: #000099;">group</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">0</span>
<span style="color: #000099;">echocancel</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">yes</span>
<span style="color: #000099;">signalling</span><span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">pri_cpe</span>
<span style="color: #000099;">channel</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;">&gt;1-15,17-31</span></pre></div></div>

<p>Now here I set signalling to be pri_cpe, which means the customer side of the PRI line.</p>
<p>Now all you need to do is to modprobe dahdi, run dahdi_cfg -vv and hope for the best, and start asterisk.</p>
<p>If you type dahdi show status in asterisk, you should se a &#8220;OK&#8221; in the Alarms section, on your span. If you see RED, they don&#8217;t see eachother. Or if it&#8217;s Yellow, i think you have link, but the timing is not correct.</p>
<p>If you have problems with timing, you can check tcpdump -i eth0 ether proto 0xd00d, and you should see a buckload of packets flowing in. Also try to use the dahdi_test tool on both machines to compare timing. the pri_cpe machine will get the timing over ethernet, so it may be a bit off, if your network is not perfect.</p>
<p>I can now successfully make calls with DAHDI/g0/number with callerid etc. working spotless. :) Hope you can too!</p>
<p>[Update Jun 29, 2010: I've uploaded a TDMoE <a href="https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=4906" target="_blank">dissector patch</a> for <a href="http://www.wireshark.org/" target="_blank">Wireshark</a>]</p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2010/02/asterisk-dahdi-and-tdmoe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

