Monday, June 22, 2009

OpenSSL: SSL Client/Server Example

I'm experimenting a bit with the new iPhone 3.0 SDK, Core Data, Game Kit, Push Notification Service and so on...

I'm using C, so today a little Example of SSL Client/Server written in C using OpenSSL, I've written a small wrapper for SSL Socket, and here is How to use it.


/* SERVER CODE
* ==============================
*/
SFSocketGlobalInit(); /* Initialize SSL */

/* Alloc Socket, Initialize SSL and Listen */
SFSocket *socket = SFSocketAlloc();
SFSocketInit(socket, CA_FILE, DH_FILE, KEY_FILE, KEY_PASSWORD, NULL);
SFSocketListen(socket, INADDR_ANY, PORT);

do {
SFSocket *clientSocket;
char buffer[64];
int rdSize;

/* Accept Client Connection */
if ((clientSocket = SFSocketAccept(socket)) == NULL)
break;

/* Read Data from Client */
if ((rdSize = SFSocketRead(clientSocket, buffer, 64 - 1)) > 0) {
buffer[rdSize] = '\0';
printf("Client: %s\n", buffer);
}

/* Write to Client */
strcpy(buffer, "Hello Client!");
SFSocketWrite(clientSocket, buffer, strlen(buffer));

/* Disconnect Client */
SFSocketRelease(clientSocket);
} while (1);

/* Close and Release Socket Resources */
SFSocketRelease(socket);


Above you've the simplified server code (without error check!) and below you've the client code. The client try to connects to server, send an "Hello" message and the server reply with other greetings.


/* CLIENT CODE
* ==============================
*/
SFSocketGlobalInit(); /* Initialize SSL */

/* Alloc Socket, Initialize SSL */
SFSocket *socket = SFSocketAlloc();
SFSocketInit(socket, CA_FILE, NULL, KEY_FILE, KEY_PASSWORD, NULL);

/* Connect to Host */
SFSocketConnectToHost(socket, HOSTNAME, PORT);

/* Send Message to Server */
char buffer[64];
strcpy(buffer, "Hello from Client!");
SFSocketWrite(socket, buffer, strlen(buffer));

/* Read Message from Server */
if ((rdSize = SFSocketRead(socket, buffer, 64 - 1)) > 0) {
buffer[rdSize] = '\0';
printf("Server: %s\n", buffer);
}

/* Close and Release Socket Resources */
SFSocketRelease(socket);



Remember that you need to generate, at least, the Authority Certificate, Server Certificate and Clients Certificates. and here is How to do it.


- AUTHORITY Certificate:
openssl genrsa -des3 -out ca.key 1024
openssl req -new -x509 -key ca.key -out ca.crt

- SERVER Certificate
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
openssl x509 -req -in server.csr -out server.crt -sha1 \
-CA ca.crt -CAkey ca.key -CAcreateserial

- CLIENT Certificate
openssl genrsa -des3 -out client.key 1024
openssl req -new -key client.key -out client.csr
openssl x509 -req -in client.csr -out client.crt -sha1 \
-CA ca.crt -CAkey ca.key -CAcreateserial



The Full Source Code is Available Here: SSL Client/Server Example Source Code.

Saturday, June 13, 2009

Qt4 iPhone Like Cut/Copy/Paste Widget

I'm watching the WWDC Keynote podcast, Just finished the Bertrand Serlet Talk, about Snow Leopard. Now before watching Scott Forstall with the new iPhone 3GS, a little break with an example of Cut, Copy and Paste like Widget. (In the Snow Leopard dock demo, I've seen "the same" widget used to do expose)

It's a really simple example but it can be useful to someone to understand how to add actions to a custom shape.

The Source Code is available Here: Qt4 iPhone Like Cut, Copy Paste Source Code.

Thursday, June 11, 2009

Qt4 Geo IP Location

In a time of Google Maps, Geo Location and other services like this... Here an example of Geo IP Location Service (http://www.hostip.info/) is really far from perfect, but it's just an example.

The sample uses QNetworkAccessManager to get the Geo Location of the IP, and Webkit to display the address on the Google Map.

The Source Code is available Here: Qt4 Geo IP Location Source Code.

Friday, May 29, 2009

Tool: IP Packet Sniffer

Today I've implemented a simple IP Packet Sniffer.

Sometimes is useful to know what're the IP packages that transit on your network, for Analyze network problems, Monitor network usage, Debug client/server communications or Debug network protocol implementations.

The tool is very simple, but I think that can be useful to learn something more about IP and TCP packets and Protocols implementation.

Maybe you can extend this Tool to support other Transport Layer Protocol.

Note: You need to run it as Root.

You can find the Source Code Here: IP Packet Sniffer Source Code.

Saturday, May 16, 2009

Qt4 Jump Away Animation

When I've started working on Mac OS X with Core Animation, I was impressed by an Example (I don't remember the link, if someone knows it, post it in the comments! thanks)... Very few lines of code for a great effect.

After a year, or probably more.. this example was came back to my mind, so I've decided to clone using Qt. And below you can see a short Demonstration Video. Your desktop jump away.. full screen is fantastic :)



The Source Code is Available Here: Qt4 Jump Away Animation Source Code.

Friday, May 15, 2009

PyQt4: PyCon3 Photos on Flickr

PyCon3 was finished, Only one week is passed and I already miss it. Guido van Rossum, Alex Martelli, Ariya Hidayat, Raymond Hettinger, David Boddie, Fredrik Lundh... C/C++ and Python Developer not .NET "coders" (I hate even more my boring .NET job).

Following the PyCon week, here a simple example of PyQt4 that allows you to see up to 500 photos tagged PyCon3 on Flickr.

As always the source code is available here: PyQt4 PyCon3 Flickr Source Code.

Monday, May 11, 2009

[TIP] Non Blocking C Read

This is Just a simple code that maybe all the Linux C Developer knows, but if someone is still learning, This is a function that wrap the standard read function adding the non blocking feature. It's Really useful in relation with Sockets.


int nonblock_read (int fd, char *buffer, size_t bufsize, int timeout) {
if (timeout > 0) {
struct timeval tv;
fd_set rfds;

FD_ZERO(&rfds);
FD_SET(fd, &rfds);

tv.tv_sec = 0;
tv.tv_usec = timeout * 1000;

if (select(1, &rfds, NULL, NULL, &tv) <= 0)
return(-ETIME);
}

return(read(fd, buffer, bufsize));
}