Librenix
Headlines | Linux | Apps | Coding | BSD | Admin | News
Information for Linux System Administration 

Space Tyrant: A threaded game server project in C

Up
vote
Down

[Update, June 25, 2005: A Space Tyrant home page has been created as a central index to the various ST articles, links, and files.]

[Update, March 21, 2007: A Space Tyrant has its own website! It's small but growing and will provide quick access to the latest code and developments in the ST universe.]

Space Tyrant: Today we kick off a new multithreaded, network socket programming project which we will call Space Tyrant. Our mission is to write an open source, multiplayer, networked, strategy game in the C programming language. The goal of this project is to make a solid code base which implements a simple space trading game upon which other games can then be built. The game will be a subset of The Last Resort (TLR) that currently runs at [offline]. This project will be a learning exercise for me as well as for any interested readers. The current state of the source code will be released with each article update.

The game design: While my TLR game consists of over 25,000 lines of C source code and supports a web interface as well as telnet and a graphical client, this code will be far smaller and simpler. It will initially only support telnet and will implement a far simpler game design.

Players will be able to telnet into the game, create an account, and play in a universe that contains ports, planets, as well as other players. Each player will be issued a starship, some cargo holds, and an amount of starship fuel. Additional fuel will be issued hourly and will accumulate in the starship. Fuel will be used to move the ship between sectors -- locations within the game universe -- and to dock with ports. Once a ship runs out of fuel it can't move at all until new fuel is issued.

Players will be able to buy and sell commodities (Iron, alcohol, and hardware) between the three different kinds of ports. Each port type will sell one of the three commodities and buy the other two. Prices will be based on supply and demand with rarely-used ports offering the better prices.

With the money players earn trading they will be able to buy more cargo holds to make their ships more efficient for trading. They will also be able to buy fighters -- small military drones -- that can be used to attack other ships or deployed to guard a sector and its contents. The fighters carried with a ship will guard it against attacks from other players.

Games will run for a predetermined length of time, then reset and start anew.

The programming model: Now, on to the software design. I've compared and considered various models for the server design. TLR is based on the forking model using inetd or xinetd to handle the listening and forking. While the forking model is inherently distributable to multiple processors, it introduces inefficiencies (forking multiple processes) and makes interprocess communications more difficult and slower.

Next, I considered a non-blocking, single process model. In this approach, one process handles everything in a single thread. It would use non-blocking IO (read and write functions that never wait for completion but, rather, return immediately if they aren't ready to read or write actual data). The thttpd web server is an example of a non-blocking, single process server. It's extremely fast and efficient. However, this model is quite complicated to code, and, I believe would make it more likely to introduce subtle timing bugs.

Next, I considered a pure multithreaded, single process model with a thread for each player. While appealing in many ways, this model would require the same kind of coordination between threads that the forking model requires between processes. Such interprocess communication would be simplified in that the various threads share memory, but the coordination issues otherwise remain the same.

Last, I considered another multithreaded model, this time with only IO threads for each user and a single thread that implements all game logic. While that one central thread might someday be a bottleneck that limits scalability on large SMP systems, it does distribute the IO on any additional processors that might be present, and requires minimal coordination. In short, this model combines the logic simplicity of the non-blocking single process model with the coding simplicity of the threaded model, while separating the IO from the main logic. There will also be two other simple threads in this model. There will be a thread that listens for new connections and spawns the IO threads for each new connection. There will also be a thread that writes the data to disk periodically.

This is the approach that I intend to take for this project. The code will be written for both Linux and Mac OS X.

More info: I have set up an email address for programmers following this series to provide recommendations, bug reports, and other feedback. Send email about this project to spacetyrant [at] librenix.com.
mail this link | permapage | score:9590 | -Ray, March 18, 2005 (Updated: July 26, 2008)

Tutorial: Introduction to Linux files

Up
vote
Down

This newbie-level Linux tutorial is an introduction to handling files from the Linux command line. It will cover finding files, determining their type, renaming, copying, examining their attributes, reading their contents, and, in the case of binary files, how to get clues to learn something more about them. Further reading will be suggested for editing files since that topic is beyond the scope of this article.

Getting Started
The reader of this tutorial is expected to have access to a Linux system and to perform the example commands as we progress through the tutorial. Once logged in to your Linux system, open a terminal session. Under Red Hat Linux, terminal is found in the 'system tools' section of the menu. (Your system may, alternatively, use a terminal program called 'konsole', 'xterm', or 'shell'. Look around your system for a menu with 'tools' or 'utilities' in the name if necessary.)

ls: Listing files
Let's start with the ls command. ls is an abbreviation for list files. Type ls now, then press the 'enter' key to see the names of the files in your current directory. The results from my 'tmp' directory are listed in bold below:

 $ ls /tmp
tardir.0.log
$
Note that I said 'your current directory'. To get the a listing of files in another directory, enter ls [dir] where [dir] is the name of the directory you wish to look at. For example, to see the file names in your top level directory, '/', type the following:
 $ ls /
bin dev home mnt
proc sbin tmp var boot etc
initrd lib opt root sys usr

$
For more information on the files, use one or more of the ls command line switches. Here I use ls with the -l switch for a 'long' listing:
 $ ls -l
total 14
-rw-r--r-- 1 root root 9649
Mar 28 02:47 tardir.0.log

$
Note that with the -l switch we get the file permissions, the inode links, the owner and group names, the file size in bytes, and the timestamp of the file as well in addition to the name. The ls command has many more options. Type man ls for a full list of options.

file: What is this file?
Linux also provides a handy command to help determine what type of files you are dealing with:

 $ file tardir.0.log
tardir.0.log: ASCII text
$
The Linux (and Unix) file command knows about, and can detect, many different file types. In our example, file tells us that tardir.0.log is a simple ASCII text file.

less: Paging through a file
Now, to actually look at the contents of a text file, we have many options. The most common is the more command and a more elaborate, newer command is less. I like less because it lets you use the arrow keys for scrolling and the pgup/pgdn keys for paging through the file. The following is a condensed page from the command less tardir.0.log:

 home/tfr/
home/tfr/doc/
home/tfr/doc/1-WAY
home/tfr/doc/0
home/tfr/doc/ADMIN
[ . . . ]
home/tfr/doc/BAT
home/tfr/doc/BATT
:
From the ':' prompt we can page or scroll forward or backward. We can also type /star to search for the next occurrance of the string 'star'. Enter man more or man less for more information on the more or less commands, respectively.

mv: Renaming a file
Now, suppose we want to rename a file. Under Linux (and Unix) we 'move' it with the mv command as follows:

 $ ls
tardir.0.log
$ mv tardir.0.log tar.log
$ ls
tar.log
$
Note that the mv command only produces output when there is an error. In this case, we encountered no error so mv quietly performed its work.

cp: Copying files
To make an actual copy of a file, we use the cp command. For example, to make a backup copy of tar.log named tar.log.2, we enter the following:

 $ cp tar.log tar.log.2
$ ls
tar.log tar.log.2
$
Again, we get no output to the screen when the cp command is used without error. We had to use the ls command to see the result of the command. Enter man cp for more details of the cp command.

strings: Looking for text in a binary file
Now, to actually look inside an unknown binary file for text strings there is a command called, appropriately enough, strings. For example, if we run the strings command on the 'echo' program, we get, in part, the following:

 $ strings /bin/echo
Copyright (C) 2004 Free
Software Foundation, Inc.
Written by %s, %s, %s,
%s, %s, %s, %s,
%s, %s, and others.

$
Type man strings for more information.

grep: Finding particular strings in a file
To look for a particular text string in a file, we use the grep command:

 $ grep html tar.log
home/tfr/timeout.html
home/tfr/hello.html

$
And, of course, man grep will retrieve additional instructions for the grep command.

find: Finding files by name
To find all files with a particular name on your system, use the find command. For example, to find files named 'echo', enter the following:

 $ find / -name 'echo'
/bin/echo
/etc/xinetd.d/echo

$
Further, to find all files in the /var filesystem with the string 'echo' in their names, enter this:
 $ find /var -name '*echo*' 
/var/mod/mod_echo.html
/var/mod/mod_echo.html.en

$

More information...
To get started editing text files try this tiny vi tutorial. After going through the quick tutorial, you can click the contents button and reach an advanced vi tutorial as well as other vi information.

For information on moving around in a Linux filesystem try this Introduction to Linux in ten commands. That article also provides additional examples on some of the commands covered here.

mail this link | permapage | score:9587 | -Ray, April 2, 2005

Scripting: Bash Array Tutorial

Up
vote
Down

An excellent introduction to bash arrays including 15 examples...
$ cat arraymanip.sh
#! /bin/bash
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'

echo ${Unix[1]}

$./arraymanip.sh
Red hat
read more...
permapage | score:9581 | -Ray, June 7, 2010

Hacker Haiku

Up
vote
Down

They said it couldn't be done;
that hacker experiences couldn't be expressed in haiku.
Or, perhaps they said it shouldn't be done?

Too late. Here are my top ten Hacker Haikus:

  • The Hacker
    Don't fear the hacker
    he just enjoys the challenge
    ...of cleaning your disk!
  • Gates' Law
    Great chips run high speeds
    but bloatware slows to crawl
    heat is main output
  • The Script Kiddie
    My server was hacked
    SSH had a hole
    now my box is 0wned
  • Turbo Mode
    Overclock your box
    it is very fast ... at first
    warranty now void
  • The Ghost Ship Bug
    The bug seemed so small
    Game database was trashed
    I must start new game
  • Email Dilemma
    Mail is killer app
    spam overflows my quota
    I need new mailbox
  • Code Red II
    My website was so grand
    until the day it changed
    to an odd demand
  • The Email Virus
    Don't open that file!
    it has a strange subject line...
    you mail all your friends
  • The Buggy OS
    My box crashed again
    the registry was corrupt
    Slackware brought it up
  • The IT Shuffle
    One group gets upgrade
    next group can't read their files
    Now they upgrade too
mail this link | permapage | score:9575 | -Ray, June 2, 2003 (Updated: June 3, 2003)

MiniLesson: An introduction to Linux in ten commands

Up
vote
Down

This tutorial is the first in a series of introductory Linux lessons. This first article will cover navigating around a Linux filesystem along with a brief passage -- with examples -- on using ten of the most essential GNU/Linux commands.

You should have access to a Linux system in order to perform the example commands as we progress through the tutorial. If you don't have a dedicated Linux box, you can use a Live Linux CD-ROM-based distribution such as Knoppix. Knoppix will let you run Linux directly from the CD without modifying anything on your hard drive.

Once you're logged in to a Linux system, open a terminal session. Each of the commands covered here will be typed directly into a command line terminal window. Under Red Hat Linux, terminal is found in the 'system tools' section of the menu. (Your system may, alternatively, have a terminal program called 'konsole', 'xterm', or 'shell'. Look around your system for a menu with 'tools' or 'utilities' in the name if necessary.)

pwd
The first command we will use is 'pwd' -- which stands for 'print working directory'. The pwd command shows you your current position within the Linux filesystem. The position is known as your 'current working directory'. Type pwd now. The example below shows my command prompt and the pwd command followed by the output from the pwd command:
 [rayy@barton0 rayy]$ pwd
/home/rayy
[rayy@barton0 rayy]$
From the output (/home/rayy) we can tell that I am in my 'home directory' -- the directory where I keep my personal files and the directory where I always start out in a new session.

ls
The ls command lets you list files. For example, here is the (shortened) output of an ls command on my system:
 [rayy@barton0 code]$ ls
artdir countdir machine
tsardir sortdir
[rayy@barton0 code]$
Alternatively, you can get a 'long listing' that shows file sizes, timestamp, ownership, and permissions as follows:
 [rayy@barton0 code]$ ls -l
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 artdir
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 countdir
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 machine
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 sortdir
drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 tsardir
[rayy@barton0 code]$
You can also supply a target directory to the ls command. For example, to view the contents of the /tmp directory, I enter the following:
 [rayy@barton0 code]$ ls /tmp
flp kde-rayy mcop-rayy
[rayy@barton0 code]$
For more information on the ls command you can reference the manual page for ls with the following command:
 [rayy@barton0 code]$ man ls
cd
This next command, 'cd', lets you change your current working directory. for example, you can change your current working directory to /usr/bin by entering the following command:
 [rayy@barton0 rayy]$ cd /usr/bin
[rayy@barton0 bin]$
Note that after I entered the cd command, my command prompt changed to reflect the change in the last node of my current working directory. Your command prompt may not be configured to do that.

Change your current working directory to /usr/bin now and enter the ls command.
 [rayy@barton0 code]$ cd /usr/bin
[rayy@barton0 bin]$ ls
addr2line mcheck
addr2name.awk mcomp
addresses mcookie
.
.
.
[rayy@barton0 bin]$
The preceding is a partial listing. There are many, many files in the /usr/bin directory on most Linux systems.

file
If you have a background in Windows or are familiar with DOS, you are used to file extensions that signify the file type. Linux (and Unix) have no such requirement. That is, an executable program can be named anything. Therefore, a handy command is supplied with Linux named 'file'. For example, I have a file named 'sample.c' in my code directory. I can learn a bit about that file by entering the following command:
 [rayy@barton0 code]$ file sample.c
sample.c: C++ program text
[rayy@barton0 code]$
Alternatively, I can use the '*' wildcard -- which represents all filenames -- to examine all of my code files at once. The following is a shortened example:
 [rayy@barton0 code]$ file *
bbsdir: directory
code.tar: GNU tar archive
genart.c: ASCII C program text
sample.c: C++ program text
xor: ELF 32-bit LSB executable
[rayy@barton0 code]$
The file command can be very useful to avoid minor annoyances -- such as when using one of the following three commands.

cat
The cat command is useful for concatenating multiple files -- or just for dumping a single text file to the screen. Before you use the cat command to dump a file to the screen, use the file command to make sure it's some variety of text file such as ascii text, commands/text, C source code, html/text, etc. The following is a shortened example of using file and cat to identify and dump a text file:
 [rayy@barton0 code]$ file xor.c
xor.c: ASCII C program text
[rayy@barton0 code]$ cat xor.c
main()
{
int x;
unsigned char buff[128],
.
.
.
[rayy@barton0 code]$
more
The more command is useful when a text file is larger than a single screen. The following is a shortened example of using more to view a large C program:
 [rayy@barton0 code]$ more xor.c
main()
{
int x;
unsigned char buff[128],
.
.
.
printf("n%sn",buff1);
printf("n%sn",buff2);
}
--More--(29%)
Note the '--More--(29%)' at the end of the screen. That means that 29% of the file is above that line, implying that another 71% of the file is below. Press the space bar to page through the file, a screenful at a time. Press the b key to back up. If you finish looking before reaching the end of the file, press the q key to quit.

grep
The grep command, short for 'get regular expression and print', is useful for finding occurances of a particular string in a text file. To find the 'printf' statements in the example C program above, enter the following command:
 [rayy@barton0 code]$ grep printf xor.c
printf("n%sn",buff1);
printf("n%sn",buff2);
[rayy@barton0 code]$
The grep command has far more capability than I describe here and, as usual, enter
 [rayy@barton0 code]$ man grep
for more information.

cp
The cp command will let you copy files. Unlike the commands used above, this one includes a hazard; if you copy filename1 to filename2 and filename2 already exists, you will destroy the original filename2 file. Use cp with caution!

To make a duplicate copy of my xor.c file I could enter the following command:
 [rayy@barton0 code]$ cp xor.c xor.c.bak
[rayy@barton0 code]$ ls xor.c*
xor.c xor.c.bak
[rayy@barton0 code]$
Note that the cp command returned no output -- I had to enter an ls command to see the results of the copy. [By adding the * wildcard to the original filename, I asked for a listing of all files that started with xor.c -- including those with no additional characters in the name.]

rm
The rm command is used for removing files. To remove the duplicate file I created in the cp command example, I would enter the following:
 [rayy@barton0 code]$ rm xor.c.bak
[rayy@barton0 code]$ ls xor.c*
xor.c
[rayy@barton0 code]$
Again, note the absense of any feedback from the rm command. I had to enter an ls command to verify that the xor.c.bak file had really been removed.

As with other commands, rm can remove multiple files at once when used with wildcards or with the -r (recursive) option. See the man page for more information on rm.

mkdir/rmdir
Ok, this is really two commands, but they are complementary. Use the mkdir command to make a new directory and use the rmdir command to remove an empty directory. For example:
 [rayy@barton0 tmp]$ mkdir testdir
[rayy@barton0 tmp]$ ls
testdir
[rayy@barton0 tmp]$ rmdir testdir
[rayy@barton0 tmp]$ ls
[rayy@barton0 tmp]$
In the preceding series of commands I first created a new directory named 'testdir'. I then used the ls command to verify its presence. Then, I removed 'testdir' and verified that it was gone by using ls again.

For more information on the commands covered in this article, take a look at the general commands man pages over at the LinuxQuestions.org website.
mail this link | permapage | score:9563 | -Ray, February 19, 2004 (Updated: April 18, 2007)

Expect Script Examples

Up
vote
Down

Expressions, if statements, for loops, and while loops examples are covered in this mini-tutorial:
This article explains the following in the expect scripting language.
  • Expressions – arithmetic operation
  • if construct in expect
  • looping constructs
read more...
permapage | score:9544 | -Ray, January 21, 2011

Librenix T-Shirts and Coffee Mugs!

Up
vote
Down

For today's example of my (semi)elite C programming skilz, I submit for your inspection the Librenix T-Shirts! Yes, I created the images on these shirts and coffee mugs entirely with C code. While the code isn't up to the standards *cough* of my open source Space Tyrant project, at least the output is colorful and not entirely textual!


click either image to see the T-Shirts, Coffee Mugs, etc.

(If you like the images but don't care for 'librenix' on your shirt, these same styles are available for all 50 US state names as well as with the signs of the zodiac here)

(and here are some modern prints)
mail this link | permapage | score:9529 | -Ray, June 6, 2010 (Updated: May 13, 2014)

Microsoft to push unlicensed users to Linux

Up
vote
Down

Microsoft has long had a conflict of interest about software piracy. By pretending not to notice, they encouraged the use of unlicensed Microsoft software, thereby letting dependence on their formats, packages, and protocols grow. The time is approaching when that will change.

Microsoft has historically made much noise and took little action against unlicensed users of its software. In the case of some developing countries, the reason was obvious. Let them develop a US-style de facto Microsoft business standard and they then become owned by Microsoft.

We've all seen Windows users circulate simple text-only notes in Microsoft Word .doc files. While it may be annoying to those without .doc capabilities (including users of older versions of Word, itself), it is a beautiful thing from Microsoft's point of view. It perpetuates their monopoly while forcing upgrades among the faithful, all in the same simple act. The widespread use of proprietary formats tends to lead to even more use of those same formats.

However, as Microsoft's markets in the US approach the saturation point -- and start to recede -- they are faced with a dilemma. Do they try desperately to hold on to as much market share as possible, or do they cash in while accepting -- and accelerating -- the inevitable decline in share?

I think Microsoft will be increasingly choosing the 'cash in' option as the pressure rises to keep earnings high. The first victims of this gradual policy shift will be business and government users in developed countries with strong IP protection laws.

Next, in approximate order, comes consumers in developed countries and business / government users in rapidly developing countries -- especially those countries seeking easy access to western markets. Last to pay up will be students and consumers in the poorest developing countries.

But, for all of you still getting a free ride from Microsoft, the good times will inevitably come to an end. They are simply waiting until you, and your compatriots, are too invested in the knowlege, skills, and standards of Microsoft products to quit. Then, they will charge you.

If you are an unlicensed Windows user who can't afford to someday become a profit center in the vast Microsoft empire, you should consider the alternatives. I recommend you start by downloading and burning a live Linux CD of Knoppix, booting it up on your Windows box, and trying it out. It's free and since it runs straight from the CD, you don't need to install it on your hard drive.
mail this link | permapage | score:9524 | -Ray, August 1, 2005

Space Tyrant Index Page: Linux game server development project

Up
vote
Down

This page links to the various articles that have been published about Space Tyrant, a multithreaded, network socket programming Linux game server project in C.

[Update: 07Jun2014 You can play a development version of an upcoming MMORPG in text mode at telnet://textMMOde.com:2323
or, on a command line, enter:
telnet textmmode.com 2323
to log in and play. This game is based on Space Tyrant and the scenario is based on a modified Starship Traders.]

[Update: 03Jun2014 Starshiptraders, the parent of Space Tyrant, and the descendant of Czarwars, Lives Again]

Read below for the history and overview or just click these links for the articles:Space Tyrant is a C language Linux game server development project. It started out in February, 2005 as a humble echo server with high ambitions. On March 18, ST had its design and intentions announced to the world.
Today we kick off a new multithreaded, network socket programming project which we will call Space Tyrant. Our mission is to write an open source, multiplayer, networked, strategy game in the C programming language. The goal of this project is to make a solid code base which implements a simple space trading game upon which other games can then be built. The game will be a subset of The Last Resort (TLR) that currently runs at Ioresort.com. This project will be a learning exercise for me as well as for any interested readers. The current state of the source code will be released with each article update.
Source code to ST, then just an echo server, was not released.

Then, on March 27, 2005, ST was promoted to a crude chatserver.
This is the first code release of Space Tyrant. This is an early stage of development and, at this point, only implements the listening thread, the two IO threads for each player connection, and a skeletal game logic thread that does little beyond proof-of-concept code.
Next, on May 30, 2005, ST started to resemble some sort of incomplete text game.
It’s now possible to connect to the game via telnet and to create an account, log in, and be issued a ship. Once you’re logged in, there is a universe to explore filled with ports for buying and selling goods and planets for scooping free goods. From those trading activities you can earn money, called microbots. Other than trading to earn more money, you only use your microbots to buy fighters -- which you can use to attack other players or the neutral fighters that guard some sectors.
As of this release, the source code was placed under the GPL, version 2.

On June 26, 2005, Space Tyrant was released with many shiny new abilities. Players can now deploy fighters to guard sectors, navigate with the aid of an autopilot, rank the various players in the universe, and sleep peacefully knowing their alternate universe is being backed up constantly by a sporty new backup thread. Yes, good times are surely upon us.
If you’re looking for a planet, type the ‘L’ command that you would normally use to land on a planet in your sector. In the absence of a planet, the L key will engage the autopilot which will search for the nearest planet and give you a ‘/’ command to autowarp to it.
There is normally a copy of the development version of the server running on my decTOP on port 23. To access it, type (or click, if it works for you) the following command:

(telnet to my ST server)

The current development version of the code is usually at http://librenix.com/st/st.c but the individual articles link to the specific versions discussed in each article.

You can mail me about the project at spacetyrant [at] librenix.com -- or you can just telnet into Space Tyrant, as listed above, and send me a radio message. I'm likely to get that faster. ;)


The Space Tyrant project has a new website, SpaceTyrant.com, where the latest source code is always available. Also, new articles about ST programming and about ST gameplay will be posted there as they come available.


Information on The Last Resort follows. TLR is one of the two games that Space Tyrant is intended to replace someday. Space Tyrant has a long way to go before it replicates TLR's 25,000 lines of C code. So far, it has reused only about 40 lines of TLR code, a handy 'bit-plane' sort dating back to 1998.

  • TLR Survival Manual
  • Is this game for you? read more...
  • mail this link | permapage | score:9516 | -Ray, June 25, 2005 (Updated: June 7, 2014)

    Online Ruby Interpreter

    Up
    vote
    Down

    Try Ruby online with this online tutorial / interpreter. It runs in your browser.
    Ruby is a programming language from Japan (available at ruby-lang.org) which is revolutionizing the web. The beauty of Ruby is found in its balance between simplicity and power.

    Try out Ruby code at the prompt above. In addition to Ruby's builtin methods, the following commands are available:
    read more...
    permapage | score:9513 | -Ray, November 30, 2005

    Tutorial: Linux Dialog Boxes

    Up
    vote
    Down

    Dialog lets you create dialog boxes from Unix/Linux shell scripts...
    'dialog' is a utility for building console-based 'front ends' in UNIX like operating systems.

    In this brief tutorial I am mentioning the usage of few important basic controls available with this 'dialog' utility and later I have created a very simple front end application in UNIX bash scripting using dialog.
    read more...
    permapage | score:9497 | -Ray, January 1, 2010

    Tutorial: UDP socket based client server C programs

    Up
    vote
    Down

    This short and sweet tutorial has the example C source code for both a UDP client and server.
    This article explains how to write a simple UDP client/server system in C for the Linux or Unix platform. Writing client-server applications using UDP sockets is considered very easy, yet we sometimes miss some essential steps, which results in spending more time in intense debugging sessions. I went through one such experience. It is quite hard to remember all the socket API details quickly without having to wade through tons of man pages, so this page lists the standard code for UDP Client Server program. I hope it will be useful for others, too.
    [The original article has gone offline and the link has been replaced by a working page. -Ed.] read more...
    mail this link | permapage | score:9473 | -Ray, February 23, 2004 (Updated: August 24, 2008)

    Pattern matching in shell scripting

    Up
    vote
    Down

    This article is excerpted from the book Beginning Portable Shell Scripting.
    Shell programming is heavily dependent on string processing. The term string is used generically to refer to any sequence of characters; typical examples of strings might be a line of input or a single argument to a command. Users enter responses to prompts, file names are generated, and commands produce output. Recurring throughout this is the need to determine whether a given string conforms to a given pattern; this process is called pattern matching. The shell has a fair amount of built-in pattern matching functionality.
    read more...
    mail this link | permapage | score:9469 | -Ray, January 1, 2009

    Beneficial Computer Viruses

    Up
    vote
    Down

    An article on last week's front page of SecurityPortal entitled Reflections on the Strange, Perplexing, Interminable, and Most Lamentable Phenomenon Known as the Viral Wars contains an alarming suggestion.

    It proposes that we..."Develop antiviral viruses (antibodies) that are polymorphic and mobile. Roaming the Internet they would seek out and destroy new viral strains. (SARC is doing some interesting work in this area. More needs to be done though.)". There are several problems with this idea.

    First, it ignores the basic problem with viruses -- that they run on other people's computers without authorization. Presumably, a "beneficial" virus would propagate by similarly illegitimate means, but carry a "good" payload rather than a destructive one. For that reason, I believe that anyone who releases such a "good" virus should be charged with the same offense as one releasing a "bad" virus, although with reduced penalties for its perhaps lesser damage and less deliberately destructive intent.

    Note that the good virus would not only propagate and execute without permission, but would also consume network bandwidth, processor cycles, memory, and disk space. Resulting, inevitably, in the denial of the system owner from using those resources. That is what is commonly called a "denial of service" attack, or DoS.

    Next, there is the non-trivial problem of identifying malicious programs. Identifying a known, existing virus is easy in comparison to programmatically distinguishing between unknown good and bad code. Since many legitimate programs contain several ways to damage or remove files, the simple ability to delete and modify files cannot alone identify a program as bad. So, perhaps the good virus would limit itself to wiping out only programs that it could (somehow) identify as capable of replication by combining its own code with that of another program. That would surely be inconvenient for the makers of self-extracting archive software. But, assuming that that obstacle could be overcome, how would a good virus tell another good virus from a bad one? Both behave similarly, including the practice of damaging or destroying other files. Imagine the resources wasted in unintentional global wars between various strains of good viruses! We can only hope that all creators of such good viruses carefully write their code to recognize every other species of good virus -- a task made difficult or impossible by the fact that the good viruses would be cleverly polymorphic.

    Note that, for liability reasons, good viruses would have to be very nearly perfect. To have them mistakenly delete a recently patched copy of Microsoft Word could be very inconvenient.

    And, of course, let's not overlook the possibility of mutant evil strains of the so-called good virus -- strains created by shady programmers who would not otherwise be capable of writing such sophisticated code. The new evil -- and polymorphic -- strains would likely be mis-identified as good by unmodified good viruses yet carry a very destructive payload. A payload which could include the killing of all the unsuspecting good viruses that it can so easily identify.

    Then, some time in the future, we will pause for a moment of silence while we remember the deceased good viruses that first invaded our computers, escalated the virus wars, then gave their very essence to improving the breed of their sworn enemies before being themselves ruthlessly destroyed by derivatives of their own code.
    mail this link | permapage | score:9468 | -Ray, June 26, 2000 (Updated: January 1, 2003)

    Space Tyrant: Multithreading lessons learned on SMP hardware

    Up
    vote
    Down

    There is much to report in this update of Space Tyrant. Before getting into the new features and functions, I’ll dispense with the crisis of The Bug.

    For a couple of weeks, we had been noticing odd anomalies with Space Tyrant (ST) running on the virtual server at Ioresort.com (now offline -Ed.). We never saw the problem on any other box -- and it was tested on at least four other Linux boxes and a Mac OS X system. We did all manner of stress testing, locally and over the Internet, script based and even feeding the game the output of /dev/random. Nothing caused the anomaly on any other box.

    At first, I suspected that it might just be an obscure problem with the virtual server itself; after all, I had been forced to modify the TLR code to get it to run properly there. That problem turned out to be merely a limitation of NFS, not a bug with the virtual server software. However, the environment was clearly different from any other system I had used which raised my suspicions -- and reduced my urgency about looking for the bug.

    While the bug wasn’t frequent, it was persistent. The bug appeared to be related to corrupted buffers or corrupted buffer indexes. Out of idle curiosity, I lowered the number of buffers used by ST to see if that affected the bug. Somewhat counter-intuitively, it substantially raised the frequency of the problem.

    Brian Estabrooks (the hero of this release) and I spent more and more of our efforts hunting this incredibly elusive bug until that was all we were doing. I implemented various diagnostic routines hunting for clues. The all seemed to point to buffer indexes being changed incorrectly. Both Brian and I audited the code. It seemed impossible for the indexes to be changed improperly. Brian even went so far as to replace the ring buffer scheme with a high watermark approach but to no avail.

    While I continued to suspect it to be a simple logic error in the code, Brian turned his efforts elsewhere. What he came up with was quite interesting. It seems that on many hardware architectures (most? all?), modifying a bit field can temporarily modify other bit fields in the same word! Now, this isn’t a problem on a single-CPU system; it repairs the damage in the same operation, making it, effectively, atomic. On an SMP machine, however, two different CPU’s working on different bit fields of the same word simultaneously create havoc. The operation isn’t really atomic and it doesn’t work.

    Did I mention that the virtual server is a 4-way Xeon system?

    The ring buffer indexing in ST relies on unsigned integer bit fields to automate wrapping back around to the first buffer after using the last one. My parsimonious programming, of course, packed all the bit fields together, several to a word. Brian’s test version of ST added a pad after each buffer index to round it out so that each bit field lived alone in its own complete word. We abused the new version for nearly an hour before either of us would dare say it. The bug was gone.

    Yay!

    So, the moral of this story is: Operations on sub-word fields affect other bits in that word (at least on many hardware architectures). Tread very carefully if multiple threads are accessing different bits in shared words. It may appear to work perfectly, only to crumble into a pile of smoldering rubble the first time it's loaded on a multiple CPU system!

    Other than the primary lesson, some other good things came out of (the search for) the bug. Several other latent bugs were found and fixed and Brian and I are both much more intimate with the code.

    And, on to the enhancements. ST is starting to look like an actual playable game. The following functions implement the new major features.

    players(): We now have player rankings. It works by adding all the players’ ship resources to an integer array. Then it scans the universe looking for deployed fighters and adds those to the array as well. Currently, those two items comprise the total strength of a player.

    It then sorts the array with a recursive bit-plane sort that I wrote for Starship Traders in 1998. The qsort() function in the C library was plenty fast, but took too much memory for my taste. Memory was a bit scarcer in those days and, worse, the SST software model gave each player his own copy of the server.

    The sort reorders the array in place as follows. It scans the high-order bit in each element of the array. It then moves all elements starting with ‘1’ bits to the top and all starting with ‘0’ bits to the bottom. Next, it calls itself twice to reorder the first and second chunks of the array on the second bit. Each of those two instances of the sort then call the sort twice again, now giving 4 new sorts for the third bit, and so on. When all 32 bits are accounted for, the array is in the correct order with the top player on top, etc.

    Scanning the entire universe can be expensive with a large map. Therefore, the player rankings function keeps the result and time stamps it. If another player asks for a player ranking within five seconds, the system just gives them the old one. After five seconds, however, any new request triggers a fresh listing.

    autopilot(): We’ve added an autopilot to let a player find a specific sector -- or to locate the nearest planet. If you type a ‘0’ (zero), you’ll be prompted for a sector number within 1000 of the sector you’re currently in. You then will have the option of pressing ‘/’ to automatically warp to the destination sector.

    If you’re looking for a planet, type the ‘L’ command that you would normally use to land on a planet in your sector. In the absence of a planet, the L key will engage the autopilot which will search for the nearest planet and give you a ‘/’ command to autowarp to it.

    The new autopilot function consists of two other functions in addition to autopilot(), which is merely a control function. I had intended to use the old shortest path algorithm function from TLR but it was big and complicated. I decided to try to write a simpler, recursive shortest path algorithm instead. The new recursive function is much simpler but not quite as efficient as the giant for loop in TLR.

    The actual algorithm is implemented in two functions called pathdepth() and pathcalc(). The pathdepth() function repeatedly calls pathcalc() with an increasing ‘depth’ parameter. ‘Depth’ tells pathcalc() how many levels deep to search before giving up.

    The pathcalc() function simply looks to see if the sector it is looking at is the target sector. If not, it calls itself for each new sector that the current sector connects to. If the current sector is the target sector, it starts filling in an array for the autowarp() function to follow to reach the target sector. As the previous recursive calls to the pathcalc() function exit, they fill in the remainder of the path array.

    And, yes, I seem to like reinventing the wheel. ;-)

    The other interesting addition to the code is the backup thread. It is implemented by a function called backupdata() and works as follows: It scans the player data, the map data, and the history data looking for ‘dirty’ flags. (Whenever any persistent data is changed anywhere in the game, a dirty flag is set to tell the backup thread to write it out to disk.) This process is quite fast for a small game, but for a game with millions of sectors, it’s a significant waste of resources to scan the dirty flag array frequently.

    Therefore, for the map and history data, I’ve implemented a ‘dirty block’ scheme as well. When a dirty flag is set, its corresponding dirty block flag is set too. Then, the backup thread need only scan the dirty block arrays, typically only about one percent the size of the arrays it represents. When a dirty block is found, only the hundred or so records it points to are scanned to find the actual dirty records for backup.

    The backup file, named ‘st.9999.dat’ -- where ‘9999’ varies with the port number you run the game on -- goes into the current working directory from where you start the daemon. If the file doesn’t exist, a new game is started. Also, if you’ve modified the game in a way that changes the size of the data -- by increasing the map size, for example -- it will start a new game upon startup.

    The game can be shut down from the command line by sending a signal 15 (kill -15 pid) or by the admin with the ^ command. Note that the first player to create an account in a new game automatically becomes the admin of the game!

    makehistory(): The storing of historical data is new as well. Whenever another player attacks your ship while you’re logged off, you’ll get a report of the action and any losses when you next log on. Also, for remote deployed fighters, you never get immediate notification, so that information is stored in the history log even if you're logged on when it happens. You can view any accumulated event information since your login time by pressing the ‘e’ key.

    deploy(): This simple function allows a player to deploy, or retrieve, guard fighters in a sector. Those fighters will not let another player pass through or view any of the contents of that sector. Any ships parked under the fighters are automatically protected against all attacks except for an attack by the fighters’ owner. Once the fighters are destroyed, of course, all ships there are visible and can be attacked.

    There is also a newly implemented time limit in the game to limit the total online time of a day’s sessions to 4 hours. Like most other parameters, it can be changed by modifying a #define statement near the top of the code.

    command(): The help page, a menu of available commands that a player can perform, has been redesigned and rewritten. This menu is attached to the '?' key.

    The old debugger thread is gone, replaced by an in-game command function called showdata(). Press the ‘z’ key to see information on buffers, buffer indexes, and the backup thread’s state and history. Only if you’re serious about modifying the code will this information be useful.

    The section of the gameloop thread that broadcasts radio and news messages has been modified to show only one of each type of message per pass. That way, replaying a long radio history won’t flood the output buffers and longer radio and news histories can therefore be retained.

    The old jumprtn() movement function has been consolidated into the warprtn() function. It’s only slightly more complicated than having them separate.

    The current source code can be downloaded from http://librenix.com/st/st.158.c. and the original article in this series is here. As usual, the compile script is embedded in the comments at the top of the source file. You’ll have to rename the source st.c for the script to work unchanged.

    [A Space Tyrant home page has been created as a central index to the various ST articles, links, and files.]
    mail this link | permapage | score:9459 | -Ray, June 26, 2005 (Updated: July 26, 2008)

    Closed Source Linux Distribution Launched

    Up
    vote
    Down

    In a press release dated 00:01, 2003Apr01, Random Hackers, Ink., announced the release of Randlix 0.01, a new Linux distribution. The distro, according to the announcement, includes proprietary code and doesn't exactly look like open source. Our reporter managed to contact one of the principals of this new company earlier today.

    "We are in compliance with the terms of the GPL", said Rupert Goldberg, president and cofounder of Random Hackers, Ink. "Technically, our product is open source. However, we intend to charge $25,000 for a single license of the binary distribution. A coupon for a free copy of the source CD will be included at no charge with each binary distribution and license. The catch, heh heh, is that we bill in two stages. We initially charge only a nominal media fee -- fifty dollars -- for the binary distribution. Included along with the advance binary media kit is the bill for the balance due, $24,950. Upon receipt of the balance due and the coupon, we ship the source CD. No one has yet completed the licensing terms to the point of receiving a source CD -- and we suspect that no one ever will. This method of closing the source of a GPL product does have the disadvantage of rendering all of our binary users uncompliant with our license, but we don't care."

    "Technically, we have no official users. There is no one out there who can legally request the source to our Linux distribution. And that's the way we like it."


    'Rube', as the Random Hackers, Ink. president prefers to be called, went on to explain at some length how his method of distribution allows the buyer to look over the distribution and, in contrast with many other software products, lets the buyer complete the transaction only after they agree to the terms of the included license.

    "We don't believe in the legality of the shrink-wrap license", said Mr. Goldberg. "With our system, there are no surprises. Uh, except one.", he added with a sly laugh.

    For more information on Randlix Linux, click the [read more] link below.
    mail this link | permapage | score:9425 | -Ray, April 1, 2003

    Tutorial: Installing Desktop FreeBSD

    Up
    vote
    Down

    If you would like to use your personal, dial-up system as a FreeBSD desktop computer, read on...
    What follows is a tutorial aimed specifically at the ordinary desktop user interested in getting started with FreeBSD. Ed provides an easy to understand guide through FreeBSD's Sysinstall installer in part one of this series.
    read more...
    permapage | score:9419 | -Ray, December 4, 2003

    Beginner Ubuntu Tips

    Up
    vote
    Down

    Twenty-five tips for Ubuntu beginners...
    Adding another source for software is easy; once you've installed addrepo, you can do it in the terminal by typing something along the lines of addrepo deb http://mirror2.ubuntulinux.nl/ lucid-seveas all.

    If you find new repositories online, they will usually give you the right details to enter. You could also do it graphically by going to 'System | Administration | Software Sources | Third Party Software | Add'. read more...
    permapage | score:9416 | -Ray, November 9, 2010

    How to customize your Linux system

    Up
    vote
    Down

    Ubuntu, Arch, Suse, and Linux from Scratch are covered in this multi-page article...
    You can install and remove packages as and when you want to, and you can choose whether to install free or non-free software on your system. Why should you have to live with community decisions when you can make Linux work your way?

    However, the distribution you install will likely contain drivers and components that you don't really want or need on your system.
    read more...
    permapage | score:9405 | -Ray, December 4, 2009

    Linux vs Mac OS X

    Up
    vote
    Down

    Five ways Linux beats Mac OS X...
    First is the question of permissions: Linux users are not automatically given administrator privileges on their computers, meaning that viruses and malware don't automatically have access to everything in the proverbial "castle." So, when a computer is compromised, the most the malware can typically do is trash the user's local files and programs.

    With Apple, on the other hand--as with Windows--social engineering is painfully easy. Just convince the user to click on something, and away you go, with the castle keys in hand.
    read more...
    mail this link | permapage | score:9385 | -Ray, October 4, 2010
    More articles...
    Abstract Art Prints by Ray Yeargin

    Recent headlines

    Pre-release Ubuntu 12.10 has partial support for manual LVM and disk encryption

    Setup Nginx + php-FPM + apc + MariaDB on Debian: The perfect LEMP server

    StartOS 6 GNOME 3 and KDE preview

    PC-BSD 9.1 preview

    Run Unity desktop on Linux Mint 12

    Linux Iptables Examples

    OpenShift Online: a non-developer guide

    Plasma Media Center 1.1 and digiKam 3.3

    Tutorial: Install Debian 7 (testing) with debootstrap from a Grml live Linux

    Nuvola Player: Enjoy all your Cloud music services from one interface

    How to install Orange in Ubuntu 13.04

    Increase file descriptors for Transmission on Linux

    LinSSID – Graphical wireless scanning for Linux

    Tutorial: Debian 7 Samba Server with tdbsam

    Installing FreeIPA with Replication

    Detailed Error Handling In Bash

    Apple DIY Repair

    High-resolution text console with uvesafb in Debian/Ubuntu

    Using OpenVZ on Debian 7 (AMD64)

    Triple-boot Windows 7, Ubuntu 12.10 and Fedora 18 on one HDD

    Tutorial: Fedora 19 Samba server with tdbsam

    Tutorial: Ubuntu Jeos 12.04 LTS Spam Filter Gateway Server

    E-book: Perl One-Liners Explained

    Mixxx – The most advanced free DJ software

    Alternative operating systems

    Tutorial: Linux game programming with Ogre 3D

    Take Android Screenshots

    GhostBSD 2.5 review

    Better Grails apps with CSS

    The future of PC-BSD

    Apache2, mod_rewrite tutorial: Redirect requests by device

    VPS: Xen vs. OpenVZ

    Set up Oneiric PVHVM at Xen 4.1.2 Ubuntu 11.10 Dom0

    Install Cinnamon on Fedora 19

    Active Directory Integration with Centrify DirectControl Express on Ubuntu 11.10 (Oneiric)

    This is not just about your personal privacy

    Sagemath in the Cloud and Sagemath 5.11

    OpenBSD Tutorial: Configure Ralink USB Wireless Adapter

    Linux mail command examples: send mails from command line

    PC-BSD 9.1 installer preview

     

    Firefox sidebar

    Site map

    Site info

    News feed

    Features

    Login
    (to post)

    Search

     
    Articles are owned by their authors.   © 2000-2012 Ray Yeargin