Saturday, June 30, 2012

IPv6


IPv4

IPv4 has been around since the early 1980s. This type of internet layer protocol addressing system is made up of two parts, the network identifier and the host identifier. Using classful networking, the IPv4 addressing range was divided in 5 classes. The first 3 classes had different capacity to be used for different purposes. Class D and class E were the same size, with the former being used for multicast addressing and the latter reserved for future applications. However, at around 1985, another method was devised to divide IP networks better and variably, called Variable Length Subnet Mask. This allowed networks to be divided in a small portion for the hosts, which still allowed growth.


Figure 1: Cisco CRS-3, with100Gbps Ethernet interface and 322 Tbps of interconnect capability

IPv4 Address Depletion

The Internet Assigned Numbers Authority (IANA) is in charge of the IP address structure. Since the 1980s, it was immediately apparent that the pool of available IPv4 addresses was being depleted at a much larger rate than was initially anticipated. Classful networks, Classless Inter-Domain Routing and NAT all were created to solve the problem of IPv4 address exhaustion. However, with all these technologies, something still had to be done and in 1996, IPv6 was born.


Figure 2: Difference between the IPv4 and IPv6 header

IPv6

While IPv4 uses 32 bit addressing, IPv6 uses 128 bits. This means that there are 3.4 x 1038 address. Each living person can have 4.8x1028 addresses for himself. Each subnet in IPv6 has 264 addresses. This means that each subnet has the square size of the whole IPv4 addressing. Of course, this means that a very small number of addresses will be used in each subnet but this will help hierarchical route aggregation.

Security

IPv6 has IPSec built in its architecture. This means that IPv6 was built with security in mind. Although IPSec was engineered for IPv6, it is very common in IPv4 networks. This happened because IPSec was back-engineered to work with IPv4. IPSec was designed to be an integral part of the IPv6 protocol suite, but was later removed and made optional.

IPv6 Address Format

The IPv6 address size is 128 bits. The preferred IPv6 address representation is: xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx where each x is a hexadecimal digit representing 4 bits. IPv6 addresses range from 0000:0000:0000:0000:0000:0000:0000:0000 to ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff.

In addition to this preferred format, IPv6 addresses may be specified in two other shortened formats:
Omit leading zeros - Specify IPv6 addresses by omitting leading zeros. For example, IPv6 address 1050:0000:0000:0000:0005:0600:300c:326b may be written as1050:0:0:0:5:600:300c:326b.

Double colon  - Specify IPv6 addresses by using double colons (::) in place of a series of zeros. For example, IPv6 address ff06:0:0:0:0:0:0:c3 may be written as ff06::c3. Double colons may be used only once in an IP address.

An alternative format for IPv6 addresses combines the colon and dotted notation, so the IPv4 address may be embedded in the IPv6 address. Hexadecimal values are specified for the left-most 96 bits, and decimal values are specified for the right-most 32 bits indicating the embedded IPv4 address. This format ensures compatibility between IPv6 nodes and IPv4 nodes when you are working in a mixed network environment.
These two types of IPv6 addresses use this alternative format:

IPv4–mapped IPv6 address - This type of address is used to represent IPv4 nodes as IPv6 addresses. It allows IPv6 applications to communicate directly with IPv4 applications. For example,0:0:0:0:0:ffff:192.1.56.10 and ::ffff:192.1.56.10/96 (shortened format).

IPv4–compatible IPv6 address - This type of address is used for tunneling. It allows IPv6 nodes to communicate across an IPv4 infrastructure. For example, 0:0:0:0:0:0:192.1.56.10 and::192.1.56.10/96 (shortened format).

All of these formats are valid IPv6 address formats.

IPv6 Day - 6th June, 2012

Instead of swapping an ISP one by one to change from IPv4 to IPv6, a day was organized so that a bulk change will be done. http://www.worldipv6launch.org/measurements/ shows the statistics and changes  which happened in the area of IPv6 since 6th June, nearly a month later.


Figure 3: Banner for World IPv6 Launch

Monday, June 25, 2012

A Chair and Sending Emails


This second post of Second Life takes me to creating some objects and stuff using the scripting language and the object builder. It is a bit frustrating at first but then it can get quite ok. The first thing I attempted to do was to build a chair and make my avatar sit on it. The first problem I had was that when I did the legs of the chair, another avatar came and removed it. Asking her why, she said that I was not allowed to build there and she took me to another place.

Creating a Chair

I used cylinders to make the legs and a cube to create the seat and the back of the chair. Although it is simple, it is a cute chair :) I used co-ordinates to create the legs of the chair so that I would know exactly how large it is and it would be easy to create the seat. I made the back legs longer so that I would use them for the back of the chair. Then I created two cubes, one for the seat and one for the back of the chair. Using the arrows I could move the parts and then dial in the exact number in the coordinates to create a smooth even chair. It took me nearly 1 hour to create this chair, but I was very happy with the end result. Also, the way I created the chair the avatar sat down immediately.


Figure 1: The Chair


Figure 2: The Avatar Sitting Down

Creating an object to send emails

Anyone doing programming knows that you need to setup a proper SMTP server with all the necessary details and settings to be able to send an email from a website or from an application. However, it was very easy to send emails in Second Life. using only a function with 2 lines of code, I was immediately sending emails when the object was touched. This was done by using the code below:

touch_start(integer total_number)
{
     llEmail("danielborgmt@gmail.com","Hey, You touched me","I have been touched on Second Life");
     llSay(0, "You Touched Me! - I sent an email to my owner!");
}

This shows how easy it is to send emails from Second Life. After touching the box I created with the script above, using the technique shown below, I got an email from the Second Life Object. The email indicates the name of the object, the Region name and also the local position.


Figure 3: Touching the Object


Figure 4: Email received from the Object 

Thursday, June 21, 2012

What on earth is Second Life????



Figure 1: Second Life Logo

Second Life is a 3D world where everyone you see is  a real person and every place you visit is built by people just like you and me.

Second Life is an online virtual world. It was developed by Linden Lab and was launched in 2003. Residents can use a number of client applications, or Viewers, to connect with each other through avatars in this virtual world. The world, known as grid, can be explored by thee residents to meet other residents, participate in events and more. It can also be used as a trading place. After reaching top popularity in the late 2000s, it has declined in popularity.

Second Life has a 3D modeling tool which is based on simple geometric shapes which allow residents to build virtual objects. It also has a procedural scripting language, Linden Scripting Language or LSL. This language can be used to add interactivity to objects, such as sending emails, dancing, and more.

Never in my life I have resisted something as much as I resisted Second Life. Using Second Life is an essential part for this module and it is compulsory, meaning it cannot be avoided. Personally, I do not see any point of learning this language as I cannot see myself making any use of this knowledge. After all, this product is decreasing its popularity. Personally I do not think that this will help me find a better job and I find it an utter waste of time.

However, my opinion does not count and I still have to work on it. Next week I will continue by explaining the interface of Second Life and how to navigate around.


Figure 2: Residents as Avatars hanging out

My question about this is how useful such a virtual world is. Talking to some of my colleagues, while working with some of the largest casinos in the world, I noticed that some of the casinos actually have a similar environment for their players. Players in such casinos choose an avatar to play 3 Dimensional poker for example. It might not be that boring after all......


Figure 3: An Avatar in a Live Casino environment

Monday, June 18, 2012

Mobile Application Development


The world of smart phones totally changed the way we live, as we constantly have a mini computer in our pockets. Several sizes, form factors and platforms are available and choosing your smart phone is certainly not easy, unless you are either a brainwashed Apple fan. The most common OSs for mobile phones are Android (36.9%), iOS (28.5%), RIM (18.1%) and Symbian (13.1%). Bada, WinMobile and Windows Phone 7 occupy the rest (3.4%).


Figure 1: Top Mobile Operating Systems


Figure 2: Operating Systems in Comparison (January 2012)


Figure 3: Smart Phone Manufacturer Comparison

One Application, 5 Operating Systems

Deciding on which platform to start developing can be tricky. There are an unlimited number of devices out there to cater for all but sticking to just one platform many times is not good enough. Thank god for PhoneGap, Appcelerator Titanium and Rhodes.

Android developers use Java to build applications for Android devices. iOS developers use Objective-C to develop applications for iPhones and iPADs. Immediately we can see that the code is not interchangeable since a different language is used. Also, learning Java and Objective-C has a steep learning curve. How about using JavaScript, CSS3 and HTML5 instead. These three applications allow you to create an application using JavaScript (or JQuery), CSS3 and HTML5. These technologies are very fast and also quite easy to use.


Figure 4: Appcelerator Titanium, PhoneGap and Rhodes

How is it done?

PhoneGap allows the developer to build applications once using web standards. Then, using PhoneGap, a mobile developer can get access to native APIs and then allows the developer to deploy to as many devices as he wants. Appcelerator uses nearly the same cycle. However, Rhodes makes use of Ruby to create the applications, rather than the other web technologies. This makes Rhodes less popular as there are a smaller number of developers using Ruby than the other languages.

Android Market vs AppStore

These two are the location people use to download applications for Android and IOS respectively. It turns out that although Android owns more than 50% of total smartphone market share, developers have, thus far, been reluctant to flock to the Android platform with their apps. In fact, there are more than 4 times as many iOS developers as there are Android developers. Why, you may ask? Quite simply, developers have long known that Apple device owners are closely locked into the Apple ecosystem, with credit cards on file. Many a study has been done, and the results are unanimous – Apple device owners are more willing to pay for apps. That being said, there are many apps in the Google Play Store that are also on Apple’s App Store – but are free, or ad supported.

Downloads and Fragmentation

The most frustration for developers on the Android platform has come from one evil word, which is fragmentation. As noted in the image above, thanks to The Next Web, iOS versions are adopted much faster and in far higher numbers than any recent version of Android. Ice Cream Sandwich, however,  still has not passed the 3% mark of all Android devices. Furthermore, according to GigaOm, 45% of iPhone and iPod Touch users purchased at least one paid app every month. Only 19% of Android users bought a paid app every month. In addition, iOS users almost double Android users in the total number of apps downloaded per month.


Figure 5: Fragmentation

Conclusion

In the end, this war between Android and IOS will never be over and it will never be a seamless job to produce apps for both Android and IOS. It is still a matter of personal taste and what the developer wants to achieve with the application he or she is going to develop

Saturday, May 19, 2012

Coursework 2 - Part 3


Today I am going to talk about the last part of my course work. After nearly finishing my coursework, I got an idea to make the game more interesting. I added a feature that checks if a player beats the current high score, and the current high score was achieved by another player, the game sends an email to the other player to tell him or her that her score was beaten.

Modifying the database

Even though this was not very difficult to do, it took me a whole week to manage to sort it out. First, I added a field in the database of the users to add the email address of the player. This requires the email to be added manually to the database as a new user cannot add an email address in the login form. This was done without because the scope of the login page was not to create a fully featured login form with a registration form.  So, I added an email address to each record in the database. For testing purposes, I inserted my email address in all the fields so that I would not bug other people with my emails.

Checking if the highest score was beaten by another player

Then, I started to modify the function which compares the high scores. After opening the connection with the database, I made a query to obtain the username, the high score and the email of the person who is at the top of the hall of fame.  The query which I used is shown below:

select username, highscore, email
from cannon_game.login    
order by highscore desc limit 1;

The record obtained is inserted into the variable $row and from it I obtained a value for the $topuser, $topscore and $topemail, which are the details of the player with the highest score in the hall of fame. Then an if statement checks if the current score is larger than the $topscore value from the database. If the score is higher than the topscore, the difference is calculated. Then, another if statement checks if the current player is the topuser. If it is not the same user, then it means that the person who was first, now moves to second place. Then, a function called email is called and it is passed the values for topuser, topemail and the score difference. Then the function continues to update the scores accordingly like it used to do before.

Testing this function

Knowing that emailing would not work immediately, I tested this function a bit primitively, using echo. I tested by using different users to play the game and trying to achieve a better score every time. I was not playing the game to obtain the highest possible score. I tried to keep scores low so I would not have to reset the scores from the database because I would not be able to beat them. By echoing the variables values I knew that the function to check if the high score was beaten by another player was working great.

Email function

I created the email function and I passed to it the variables $topuser, $topemail and $scoredifference. To send emails you need a to address to send emails to, a from address, a subject and a body. The to email was obtained from the database and passed to the function. I used my personal email as the from email address. Then I crafted a subject and a body message using the user name and the score difference. The code below shows the first part of the email function:

     $to = $topemail;
     $from = "danielborgmt@gmail.com";
     $subject = ucwords($topuser).", be careful!!";
     $body = ucwords($topuser).", your score was beaten by "                   .$scoredifference." points!! Make sure to beat his score to          stay in the first place!!!";

The function ucwords() is used to capitalize the first letter of the string passed to it. Then you need an SMTP server to be able to send emails. This gave me a lot of problems and took me nearly a week to solve. I talked about this in my previous blog. After trying a lot of solutions, I managed to find a working script which worked nearly immediately. The script was a PEAR extension and I used the following code to be able to use it:

     $er = error_reporting(0);
     require_once "Mail.php";

SMTP Server Settings

The error_reporting(0) is used to hide errors for using methods which were not called using a STRICT method. This was done at the top of the php file. After enabling SSL on my web server, I added the details of my Gmail account to use Gmail's SMTP server. Then I called the function to send the email to the details of the player who was first using my Gmail SMTP server. The code is shown below:

     $host = "ssl://smtp.gmail.com";
     $port = "465";
     $username = "danielborgmt";
     $password = "mypassword";
     $headers = array ('From' => $from, 'To' => $to, 'Subject' =>                         $subject);
     $smtp = Mail::factory('smtp', array ('host' => $host,'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
     $mail = $smtp->send($to, $headers, $body);
          
     if (PEAR::isError($mail)) {
          echo("<p>" . $mail->getMessage() . "</p>");
     }

This code allowed me to send emails properly to the player who lost the first place.


Figure 1: Sample Email from PHP Script


Figure 2: Email details

This concludes my second coursework.

Wednesday, May 16, 2012

PHP and Sending Emails


As we know, send emails from PHP scripts can sometimes be vital. We see this in practice nearly every day, especially with registration forms. Most of the times, when we register on a new website, we receive an email from the company which requires us to follow a link to verify that it was a person who submitted the request. This is also the case when we are resetting a password. We receive an email with a random password or to follow a link to create a new password.

Several things exist to send emails from PHP. Among them we find the normal mail() function, the sendmail application, and other scripts which allow us to send emails.

SMTP

To send an email, the SMTP (Simple Mail Transfer Protocol) Protocol is used. This protocol uses port 25 for communication. This means that port 25 must not be blocked on our firewall in the first place. Several forms of SMTP servers exist, and we might not have one running on our local machine. There are several setups which we might come across:

  • SMTP Server without authentication
  • SMTP Server with authentication
  • SMTP Server with authentication and SSL


SMTP Servers without authentication are become rarer, due to the fact that these are many times used by spammers and thus were blacklisted by ISPs as spamming engines. SMTP Servers with authentication require a username and a password to allow mail to pass through. Many times, they use the username and password used for the incoming mail server. SSL is a form of encryption which increases the level of security as the messages are not sent in plain text format.

Gmail

One good common SMTP server is the Gmail SMTP Server. This allows users who have an account with Gmail to authenticate and send emails through the server. Gmail uses SSL and this means that it uses port 465 instead of port 25. SSL is not always enabled in PHP, so it is useful to turn it on.

Enabling SSL in PHP

SSL is required by Gmail and other SMTP servers using SSL. This means that we need to enable it in PHP. There are a few easy steps to enable SSL in XAMPP which I am using for my testing environment. The first thing is to stop the Apache service. Then copy libeay32.dll and ssleay32.dll from the php directory to the apache/bin directory, overwriting the existing copies. Then, make sure that there is an entry for the open SSL extension in the php.ini file. The entry should be like this: extension=php_openssl.dll, without the semicolon in the front. Then start the Apache service again. The service should start with SSL support.

PHP Mail Function

The PHP mail() function is a function used to send emails. This takes 5 parameters:

  • to
  • subject
  • message
  • headers (optional)
  • parameters (optional)


This function uses the SMTP configuration found in the php.ini file. This mainly consists of SMTP server, smtp_port, sendmail_from and sendmail_path. The SMTP server takes the address of the SMTP server, the smtp_port takes the port which the SMTP is configured to work on. The sendmail_from indicated the email address which is shown as the from address, while the sendmail_path is the installation path of the sendmail application. The code snippet below shows a sample of the mail function

<?php
$to = "someone@example.com";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "someonelse@example.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>

PHP Scripts

Several additional scripts exist apart from the mail() function in PHP. Several people and groups of people went a step forward and created complex mail functions to add more features to this important task in PHP. During my research to send emails from PHP, due to the fact that mail() function was not working, I found many advanced projects. Each project had its own way to send emails and its own classes which needed importing to the actual PHP function which I was using. As a matter of fact, I ended up using one of these methods.

Pear and Mail() Script

After a lot of problems with other functions and setups, I managed to find a working script. I am not saying in any way that all of the other scripts contained bugs, far from it, but I did not manage to make them work. I used a Pear extension which actually managed to send emails. I downloaded version 1.2.0 of their scripts which I placed in the directory of my PHP file. I imported the file in my PHP file and then was able to call the method. After setting up values for to, from, subject and body, I was able to setup the server part in the code and then call the send function, as shown below:

            $er = error_reporting(0);
     require_once "Mail.php";

$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "danielborgmt";
$password = "mypassword";
$headers = array
     ('From' => $from,
     'To' => $to,
     'Subject' => $subject);
$smtp = Mail::factory('smtp', array
     ('host' => $host,
     'port' => $port,
     'auth' => true,
     'username' => $username,
     'password' => $password));
$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
     echo("<p>" . $mail->getMessage() . "</p>");
}

The first line shows a function which disables errors caused by not following the strict procedures. This function's result ended up in my testing mailbox :)

My Issues with PHP and Sending Emails

I experienced several issues when setting up this application. Most of them were errors due to not understanding exactly how the PHP sendmail function works, and other small issues with settings for the SMTP servers. The list below shows a number of issues I had:

  • did not properly setup php.ini and incorrect path for sendmail
  • incorrect settings in sendmail.ini
  • incorrect mail server address, incorrect username format, incorrect password
  • incorrect server port
  • using SMTP server with Authentication without providing a username and password
  • disabled SSL
  • closed ports on the Windows Firewall


After a whole week of struggling and research, I managed to overcome these difficulties. The picture below shows a sample email I sent from PHP.


Figure 1: Sample Email from PHP Script

Figure 2: Email details

Wednesday, May 9, 2012

More on PHP


PHP and MySQL

MySQL is the most common open-source database system. It is very popular across the globe and has very good integration with PHP. Data is stored in tables within a particular database. Tables are in turn made up of rows and columns.  SQL on the other hand is a query language used to interact between an application and the database. Each type of database has its particular SQL language, so it can be a bit tricky to work with different database servers. For the sake of this blog, I will only speak about MySQL.
The first thing to do when interfacing between PHP and MySQL is to open a connection. This can be done using the mysql_connect() function which takes the servername, the username and the password as parameters, among others. Most of the times, this connection is stored in a string variable, as shown below:

<?php
      $con = mysql_connect("localhost","peter","abc123");
      if (!$con)
      {
            die('Could not connect: ' . mysql_error());
      }
     
      // some code
      mysql_close($con);
?>

The last line before the closing tag shows the connection being closed. This is an important step because malicious users can make use of open connections. Also, open connections will increase the load on the server. The code in the if statement is executed if the connection fail. The mysql_error() shows the exact reason for the failure. This is very important to troubleshoot the issue.

All the queries work this same from PHP as if they were executed in MySQL Workbench, so I will not go into INSERT and UPDATE statements.

Selecting Data

Although the SQL command for selecting data is still the same, the results returned need to be manipulated to be used for the developer's needs. The code below is to select all the rows in the table and display them one after the other.

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

while($row = mysql_fetch_array($result))
  {
  echo $row['FirstName'] . " " . $row['LastName'];
  echo "<br />";
  }

The first line above shows the code used to select a Database from the server. This is important because a database server might have more than one database. The result variable holds the data returned by the mysql_query () function. The function mysql_fetch_array() returns the next row in the recordset. The while loop is used to loop through all the records in the recordset. Each row is an array of columns and the value of each row can either be accessed by the field name or by the index. $row['0'] and $row['FirstName'] will return the same value.

Show Results in HTML

The results can then be formatted by also echoing the HTML tags. This can be seen in the example below:
echo "<table border='1'>
<
tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

This will create an HTML table dynamically with all the records in the table.

PHP and ODBC

ODBC is an API which allows PHP to connect to a data source.  With such a connection, PHP can connect to any database or computer on the network. In this example I will setup a connection with a MS Access Database. To create an ODBC connection to a MS Access Database, these steps have to be followed:

  1. Open the Administrative Tools icon in your Control Panel.
  2. Double-click on the Data Sources (ODBC) icon inside.
  3. Choose the System DSN tab.
  4. Click on Add in the System DSN tab.
  5. Select the Microsoft Access Driver. Click Finish.
  6. In the next screen, click Select to locate the database.
  7. Give the database a Data Source Name (DSN).
  8. Click OK.

This process required access to the location of the database. Next, we have to setup a connection to the ODBC. the odbc_connect() function is used to accomplish this task. This function takes 4 parameters: data source name, username, password and optional cursor type.  The code below shows this:

$conn=odbc_connect('northwind','','');
$sql="SELECT * FROM customers";
$rs=odbc_exec($conn,$sql);

Retrieving Records

The function odbc_fetch_row() function can be used to return records from the result-set. This function takes the OSBC result identifier and an optional row number. To read fields from a record, the odbc_result() function is used and this function takes the ODBC result identifier and a field number or name.

<?php
$conn=odbc_connect('northwind','','');
if (!$conn)
  {exit("Connection Failed: " . $conn);}
$sql="SELECT * FROM customers";
$rs=odbc_exec($conn,$sql);
if (!$rs)
  {exit("Error in SQL");}
echo "<table><tr>";
echo "<th>Companyname</th>";
echo "<th>Contactname</th></tr>";
while (odbc_fetch_row($rs))
  {
  $compname=odbc_result($rs,"CompanyName");
  $conname=odbc_result($rs,"ContactName");
  echo "<tr><td>$compname</td>";
  echo "<td>$conname</td></tr>";
  }
odbc_close($conn);
echo "</table>";
?>

The php code above shows the whole process using a MS Access Database and ODBC instead of MySQL. Last but not least, the connection will be closed. As we can see, the code is very similar to the mysql_fetch_array() function used before.