<?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 &#187; routeros</title>
	<atom:link href="http://lunatic.no/category/routeros/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.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<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>28.02.2012 - <a href="https://github.com/haakonnessjoen/MAC-Telnet/commit/2d1bb7ebafb3cfe9161854d1ac0b05133fa05139">Add comment about \r\n in i18n</a></li>
<li>28.02.2012 - <a href="https://github.com/haakonnessjoen/MAC-Telnet/commit/0918879970d8b399512a9a4553d7989f2434c9a7">Update po template</a></li>
<li>28.02.2012 - <a href="https://github.com/haakonnessjoen/MAC-Telnet/commit/86990fe50070268508caf171c825217c7da21ef5">Cleaned up i18n</a></li>
<li>28.02.2012 - <a href="https://github.com/haakonnessjoen/MAC-Telnet/commit/210b1b5574afb381b87d6d34860009a905ed6848">Added i18n support</a></li>
<li>11.02.2012 - <a href="https://github.com/haakonnessjoen/MAC-Telnet/commit/11ee3dc65c67d6a5c98347287cee50963029d412">Bugfix. Don't die on interfaces without address.</a></li>
</ul>]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2010/10/routeros-mac-telnet-application-for-linux-users/feed/</wfw:commentRss>
		<slash:comments>4</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>Flash ActionScript 3 API for Mikrotik RouterOS 3.x</title>
		<link>http://lunatic.no/2009/05/flash-actionscript-3-api-for-mikrotik-routeros-3-x-2/</link>
		<comments>http://lunatic.no/2009/05/flash-actionscript-3-api-for-mikrotik-routeros-3-x-2/#comments</comments>
		<pubDate>Tue, 05 May 2009 07:08:09 +0000</pubDate>
		<dc:creator>Håkon Nessjøen</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[mikrotik]]></category>
		<category><![CDATA[routeros]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2009/05/flash-actionscript-3-api-for-mikrotik-routeros-3-x-2/"></g:plusone></div>
Lately I have been familiar with the RouterOS series, when we got us a RouterBoard 493. At first I was a bit sceptical since I had never heard of RouterOS or Mikrotik. But as soon as I started testing this nice little hardware/software out, I found out that this was one exceptional nice software. And [...]]]></description>
			<content:encoded><![CDATA[<div style="display:inline;float:right;margin-left:1em"><g:plusone href="http://lunatic.no/2009/05/flash-actionscript-3-api-for-mikrotik-routeros-3-x-2/"></g:plusone></div>
<p>Lately I have been familiar with the RouterOS series, when we got us a RouterBoard 493. At first I was a bit sceptical since I had never heard of RouterOS or Mikrotik. But as soon as I started testing this nice little hardware/software out, I found out that this was one exceptional nice software. And the nice hardware solutions is also nice. You can buy barebone RouterBoards without casing, and put them inside what you want, or buy a RouterOS license and build your own x86 router with standard x86 hardware.</p>
<p>The great thing about these routers is that they are so configurable, and easy to administer. You should all try them out yourself. They are extremely cheap, and extremely full of features. I know a lot of ISP&#8217;s around the world use only mikrotik equiptment in their wireless networks. Since they are very easy to set up with several wireless cards, and they have exceptionally good drivers, where you can fine tune all the parameters.</p>
<p>Anywyas, in addition to supporting configuration over Telnet/SSH/MAC-telnet. They also have a so called RouterOS API. This is a nice little API that gives you computer-friendly access to all the features in the configuration of the box.</p>
<p>Via the API you can read info, set configuration (live), and even listen for events. For example, list out the dBm to all connected WLAN users. Or get notified when someoen pulls a network cable. etc.</p>
<p>The API is <a href="http://wiki.mikrotik.com/wiki/API" target="_blank">&#8220;open documentation&#8221;</a> and you are welcome to create libraries to different languages and post on their site. They already have PHP, Ruby, Perl, Java, etc implementations, but I wanted to be able to create a Flash AIR application to display and configure routers. So I created a Flash ActionScript 3 class for it.</p>
<p>Now I can create nice interfaces displaying live info from the router, and simple(or andvanced) configuration of the device.</p>
<p>The ActionScript classes looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// ApiSocket.as</span>
<span style="color: #009900; font-style: italic;">//</span>
<span style="color: #009900; font-style: italic;">// RouterOS API class</span>
<span style="color: #009900; font-style: italic;">// Author: Håkon Nessjøen</span>
<span style="color: #009900; font-style: italic;">// Date: 2. May 2009</span>
<span style="color: #009900; font-style: italic;">//</span>
<span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.errors</span><span style="color: #000066; font-weight: bold;">.*;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.*;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">ByteArray</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>adobe<span style="color: #000066; font-weight: bold;">.</span>crypto<span style="color: #000066; font-weight: bold;">.</span>MD5<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Socket</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ApiSocket <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Socket</span> <span style="color: #000000;">&#123;</span>
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> RECEIVED<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;received&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> LOGIN<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;loggedin&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> cmd<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> doLogin<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> user<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> password<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> returnData<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> returnPos<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> toread<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> firstRe<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> tag<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> gotDone<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> gotTrap<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> gotFatal<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Boolean</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ApiSocket<span style="color: #000000;">&#40;</span>host<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">,</span> port<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span>host<span style="color: #000066; font-weight: bold;">,</span> port<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			toread = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			doLogin = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">ProgressEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">SOCKET_DATA</span><span style="color: #000066; font-weight: bold;">,</span> socketDataHandler<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> login<span style="color: #000000;">&#40;</span>u<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">,</span> p<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			doLogin = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
			user = u<span style="color: #000066; font-weight: bold;">;</span>
			password = p<span style="color: #000066; font-weight: bold;">;</span>
			sendRequest<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;/login&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> sendRequest<span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">...</span> outData<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			returnData = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Array</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			returnPos = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			firstRe = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			gotDone = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
			gotTrap = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
			gotFatal = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">;</span>
			tag = <span style="color: #990000;">&quot;&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
			cmd = outData<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
			returnData<span style="color: #000000;">&#91;</span>returnPos<span style="color: #000000;">&#93;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> outData<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">ByteArray</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">ByteArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
				<span style="color: #6699cc; font-weight: bold;">var</span> len<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = outData<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&lt;</span> 0x80<span style="color: #000000;">&#41;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span>len<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #0033ff; font-weight: bold;">else</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&lt;</span> 0x4000<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					len <span style="color: #000066; font-weight: bold;">|</span>= 0x8000<span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&lt;</span> 0x200000<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					len <span style="color: #000066; font-weight: bold;">|</span>= 0xC00000<span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&lt;</span> 0x10000000<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					len <span style="color: #000066; font-weight: bold;">|</span>= 0xE0000000<span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span>0xF0<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">24</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">16</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&gt;&gt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&amp;</span> 0xff<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span>
&nbsp;
				<span style="color: #004993;">writeBytes</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">data</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #004993;">writeUTFBytes</span><span style="color: #000000;">&#40;</span>outData<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			<span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> readResponse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> len<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>toread == <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #6699cc; font-weight: bold;">var</span> len1<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span> = <span style="color: #004993;">readUnsignedByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>len1 == <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>gotDone <span style="color: #000066; font-weight: bold;">||</span> gotTrap <span style="color: #000066; font-weight: bold;">||</span> gotFatal<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>doLogin == <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
							<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>returnData<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>ret<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
								<span style="color: #6699cc; font-weight: bold;">var</span> chal<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">ByteArray</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">ByteArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
								<span style="color: #6699cc; font-weight: bold;">var</span> md5<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">ByteArray</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">ByteArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
								<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> returnData<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>ret<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
									chal<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">int</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;0x&quot;</span> <span style="color: #000066; font-weight: bold;">+</span> returnData<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>ret<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substr</span><span style="color: #000000;">&#40;</span>i<span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
								<span style="color: #000000;">&#125;</span>
&nbsp;
								md5<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
								md5<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeUTFBytes</span><span style="color: #000000;">&#40;</span>password<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
								md5<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeBytes</span><span style="color: #000000;">&#40;</span>chal<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
								doLogin<span style="color: #000066; font-weight: bold;">++;</span>
								<span style="color: #009900; font-style: italic;">// Send challenge response</span>
								sendRequest<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;/login&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;=name=&quot;</span> <span style="color: #000066; font-weight: bold;">+</span> user<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;=response=00&quot;</span> <span style="color: #000066; font-weight: bold;">+</span> MD5<span style="color: #000066; font-weight: bold;">.</span>hashBytes<span style="color: #000000;">&#40;</span>md5<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
							<span style="color: #000000;">&#125;</span>
						<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>doLogin == <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
							doLogin = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
	 						<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> ApiEvent<span style="color: #000000;">&#40;</span>ApiSocket<span style="color: #000066; font-weight: bold;">.</span>LOGIN<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;&quot;</span><span style="color: #000066; font-weight: bold;">,</span> returnData<span style="color: #000066; font-weight: bold;">,</span> gotDone <span style="color: #000066; font-weight: bold;">?</span> <span style="color: #990000;">'done'</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000;">&#40;</span>gotFatal <span style="color: #000066; font-weight: bold;">?</span> <span style="color: #990000;">'fatal'</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">'trap'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
						<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #000000;">&#123;</span>
							<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> ApiEvent<span style="color: #000000;">&#40;</span>ApiSocket<span style="color: #000066; font-weight: bold;">.</span>RECEIVED<span style="color: #000066; font-weight: bold;">,</span> tag<span style="color: #000066; font-weight: bold;">,</span> returnData<span style="color: #000066; font-weight: bold;">,</span> gotDone <span style="color: #000066; font-weight: bold;">?</span> <span style="color: #990000;">'done'</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000;">&#40;</span>gotFatal <span style="color: #000066; font-weight: bold;">?</span> <span style="color: #990000;">'fatal'</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #990000;">'trap'</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
						<span style="color: #000000;">&#125;</span>
					<span style="color: #000000;">&#125;</span>
&nbsp;
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">bytesAvailable</span><span style="color: #000000;">&#41;</span>
						readResponse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #0033ff; font-weight: bold;">else</span>
						<span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span>
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>len1 <span style="color: #000066; font-weight: bold;">&gt;</span>= 0xF0<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					len = <span style="color: #004993;">readUnsignedByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					len = <span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">readUnsignedByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					len = <span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">readUnsignedByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					len = <span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">readUnsignedByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>len1 <span style="color: #000066; font-weight: bold;">&gt;</span>= 0xE0<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					len = <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len1 <span style="color: #000066; font-weight: bold;">&amp;</span> <span style="color: #000000; font-weight:bold;">15</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">readUnsignedByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					len = <span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">readUnsignedByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					len = <span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">readUnsignedByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>len1 <span style="color: #000066; font-weight: bold;">&gt;</span>= 0xC0<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					len = <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len1 <span style="color: #000066; font-weight: bold;">&amp;</span> <span style="color: #000000; font-weight:bold;">31</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">readUnsignedByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					len = <span style="color: #000000;">&#40;</span>len <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">readUnsignedByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>len1 <span style="color: #000066; font-weight: bold;">&gt;</span>= 0x80<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					len = <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>len1 <span style="color: #000066; font-weight: bold;">&amp;</span> <span style="color: #000000; font-weight:bold;">63</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&lt;&lt;</span> <span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #004993;">readUnsignedByte</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span> <span style="color: #0033ff; font-weight: bold;">else</span>
					len = len1<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
				toread = len<span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #009900; font-style: italic;">// Calculate how much data of the full length that is available right now</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> slen<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #004993;">bytesAvailable</span> <span style="color: #000066; font-weight: bold;">&gt;</span> toread <span style="color: #000066; font-weight: bold;">?</span> toread <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #004993;">bytesAvailable</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #009900; font-style: italic;">// Calculate how much data that has to be read later</span>
			toread = toread <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #004993;">bytesAvailable</span> <span style="color: #000066; font-weight: bold;">?</span> toread <span style="color: #000066; font-weight: bold;">-</span> <span style="color: #004993;">bytesAvailable</span> <span style="color: #000066; font-weight: bold;">:</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #009900; font-style: italic;">// Read relevant data</span>
			<span style="color: #6699cc; font-weight: bold;">var</span> str<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #004993;">readUTFBytes</span><span style="color: #000000;">&#40;</span>slen<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>toread == <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>str == <span style="color: #990000;">'!re'</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					firstRe<span style="color: #000066; font-weight: bold;">++;</span>
					<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>firstRe <span style="color: #000066; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
						returnPos<span style="color: #000066; font-weight: bold;">++</span>
						returnData<span style="color: #000000;">&#91;</span>returnPos<span style="color: #000000;">&#93;</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #000000;">&#125;</span>
				<span style="color: #000000;">&#125;</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>str == <span style="color: #990000;">'!trap'</span><span style="color: #000000;">&#41;</span>
					gotTrap = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>str == <span style="color: #990000;">'!fatal'</span><span style="color: #000000;">&#41;</span>
					gotFatal = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>str == <span style="color: #990000;">'!done'</span><span style="color: #000000;">&#41;</span>
					gotDone = <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
				<span style="color: #009900; font-style: italic;">// Parse key-value pair</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substr</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> == <span style="color: #990000;">'='</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> tmpPos<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">indexOf</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'='</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> tmpKey<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substr</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span>tmpPos<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					<span style="color: #6699cc; font-weight: bold;">var</span> tmpVal<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substr</span><span style="color: #000000;">&#40;</span>tmpPos<span style="color: #000066; font-weight: bold;">+</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
					returnData<span style="color: #000000;">&#91;</span>returnPos<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span>tmpKey<span style="color: #000000;">&#93;</span> = tmpVal<span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #000000;">&#125;</span>
&nbsp;
				<span style="color: #009900; font-style: italic;">// Reset tag</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substr</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span> == <span style="color: #990000;">'!'</span><span style="color: #000000;">&#41;</span>
					tag = <span style="color: #990000;">&quot;&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
				<span style="color: #009900; font-style: italic;">// Set tag</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substr</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span> == <span style="color: #990000;">'.tag='</span><span style="color: #000000;">&#41;</span>
					tag = str<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">substr</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
				<span style="color: #009900; font-style: italic;">// Are there more packets available</span>
				<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">bytesAvailable</span><span style="color: #000000;">&#41;</span>
					readResponse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> socketDataHandler<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">ProgressEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
			readResponse<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// ApiEvent.as</span>
<span style="color: #009900; font-style: italic;">//</span>
<span style="color: #009900; font-style: italic;">// RouterOS API Event class</span>
<span style="color: #009900; font-style: italic;">// Author: Håkon Nessjøen</span>
<span style="color: #009900; font-style: italic;">// Date: 2. May 2009</span>
<span style="color: #009900; font-style: italic;">//</span>
<span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> ApiEvent <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Event</span> <span style="color: #000000;">&#123;</span>
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> RECEIVED<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;received&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
		static <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> LOGIN<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;loggedin&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> result<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> tag<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> ApiEvent<span style="color: #000000;">&#40;</span><span style="color: #004993;">type</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">,</span> tg<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">,</span> dta<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span><span style="color: #000066; font-weight: bold;">,</span> res<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">type</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">data</span> = dta<span style="color: #000066; font-weight: bold;">;</span>
			result = res<span style="color: #000066; font-weight: bold;">;</span>
			tag = tg<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And here&#8217;s an<br />
<span style="font-size: medium;">Example</span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// Short example that outputs wireless registration-table every second</span>
<span style="color: #0033ff; font-weight: bold;">import</span> ApiSocket<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> ApiEvent<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #6699cc; font-weight: bold;">var</span> sock<span style="color: #000066; font-weight: bold;">:</span>ApiSocket<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> myTimer<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Timer</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Timer</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1000</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
myButton<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span> testAPI<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
myTimer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">TimerEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">TIMER</span><span style="color: #000066; font-weight: bold;">,</span> timedFunction<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> testAPI<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
	sock = <span style="color: #0033ff; font-weight: bold;">new</span> ApiSocket<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;172.17.1.1&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">8728</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	sock<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>ApiEvent<span style="color: #000066; font-weight: bold;">.</span>RECEIVED<span style="color: #000066; font-weight: bold;">,</span> receive<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	sock<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CONNECT</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">connected</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	sock<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>ApiEvent<span style="color: #000066; font-weight: bold;">.</span>LOGIN<span style="color: #000066; font-weight: bold;">,</span> loggedin<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">connected</span><span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Connected. Logging in.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	sock<span style="color: #000066; font-weight: bold;">.</span>login<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;admin&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #990000;">&quot;password&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> loggedin<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span>ApiEvent<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// result can be done, trap or fatal</span>
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">.</span>result == <span style="color: #990000;">'done'</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		myTimer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> timedFunction<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">TimerEvent</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	sock<span style="color: #000066; font-weight: bold;">.</span>sendRequest<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;/interface/wireless/registration-table/print&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">&quot;.tag=mytag&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #339966; font-weight: bold;">function</span> receive<span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">:</span>ApiEvent<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Got Event with tag &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> evt<span style="color: #000066; font-weight: bold;">.</span>tag <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">&quot; result: &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> evt<span style="color: #000066; font-weight: bold;">.</span>result<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">.</span>tag == <span style="color: #990000;">'mytag'</span> <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> evt<span style="color: #000066; font-weight: bold;">.</span>result == <span style="color: #990000;">'done'</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Got rows: &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> evt<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> evt<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">data</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000066; font-weight: bold;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>evt<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">data</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">'mac-address'</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
			<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;  &quot;</span> <span style="color: #000066; font-weight: bold;">+</span> evt<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">data</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#91;</span><span style="color: #990000;">'signal-strength'</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Anyways, the code is nice to use in Flash AIR applications, since Flash AIR doesn&#8217;t have any security limitations to connecting with sockets. But if you were to use this class in a SWF file, you would need to do the right measures to let Flash accept your &#8220;cross domain scripting&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://lunatic.no/2009/05/flash-actionscript-3-api-for-mikrotik-routeros-3-x-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

