Tuesday, May 25, 2004

"Getting my feet wet with Sockets, BSD style."

Just came across this site with an interesting tutorial:
BSD Sockets: A Quick And Dirty Primer
by Jim Frost
February 13, 1990
URL: http://www.softlab.ece.ntua.gr/facilities/documentation/unix/sockets.html


BSD Sockets are thought to be hard to understand at first. I guess I'll see for my self as I read through this tutorial.

The BSD socket is one of the primary medium of communication between different processes much like the telephone.

There are many types of socket schemes which are not to be confused with socket types, but for this tutorial only one socket scheme will be discussed in detail and this is the AF_INET socket scheme. Sockets are created with the socket() command which basically instantiates a connection with the addressing scheme specified as a parameter. The two most used addressing schemes are AF_UNIX and AF_INET. The AF_UNIX socket scheme uses UNIX pathnames to identify sockets and is useful for IPC between processes on the same machine. The AF_INET scheme uses Internet addresses in addition to a machine address and a port number which can allow form more than one socket connection on each macnine.

Now on to socket types. In addition to specifing the socket schemes and port the socket type must also be specified to the socket() command. The two most commonly used socket types are SOCK_STREAM and SOCK_DGRAM. SOCK_STREAM looks like what I will need, to read in those characters from vortex.labs.pulltheplug.com in order to get started with the wargame. SOCK_STREAM allows for data to move across the network as a stream of characters. SOCK_DGRAM allows for chunks of characters to come through as one at a time and are call datagrams.

When a socket is created that socket must then be binded to an address to listen to. This is done with the bind() function when working with sockets. Also with SOCK_STREAM there is the ability to work with incoming connections in a queue like fashion. To handle connections in a queue like fashion does not mean there is an unlimited number of space to have connections queued up in. We are limited to a maximum of five connections. The listen() function is use to set the number of request to have in a queue. Once we reach this maximum connections will be denied.


Now that I have a semi-ok grep on how socket(), bind() and listen() are used lets put it to practice.

/* This is not my code
* Taken from the URL above
*/

FreeBSD4.9-Stable (4.10-PRERELEASE) Backing up

Today I just bought a whole new pack of blank CD-R from stables. I bought the 50pk Memorex CDs for $20.00.

I will be backing up the following directories on my system. I have made a lot of changes since I last installed FreeBSD 4.9. I would hate to start from scratch. This happened to me before and it was not nice. I spent about a week reinstalling, making world, installing several ports, recompiling the kernel and fine tuning the system to my liking.

Before a disaster happens I will back up the following directories:

# /usr/ports/distfiles
# Directory size: 898 Megabytes (919, 206 bytes)
#

# /usr/local/etc/
# Directory size: 2.4 Megabytes (2, 450 bytes)
#

# /etc
# Directory size: 1.9 Megabytes (1, 898 bytes)
#

# /root
# Directory size: 85 Megabytes (86, 912 bytes)
#

# /var
# Directory size: 46 Megabytes (46, 916 bytes)
#

# /home
# Directory size: 1.2 Gigabytes (1, 301, 796 bytes)
#

# /storage/icon.storage/
# Directory size: 2.6 Gigabytes ( 2, 736, 608 bytes)
# If had a DVD Recorder I could have put everything on one CD.
#

# /storage/Lucious
# Directory size: 3.6 Megabytes (3, 666 bytes)
#

Backup CD 1 will contain:
1. /usr/local/etc (2, 450 bytes)
2. /etc (1, 898 bytes)
3. /root (86, 912 bytes)
4. /var (46, 916 bytes)
5. /storage/Lucious (3, 666 bytes)
Total: 141, 842 bytes

The iso for CD 1 will be named eervs`date`.iso and was created as follows:
$> mkisofs -UR -b /boot/cdboot -o ./eervs.iso /usr/local/etc /etc /root /var /storage/Lucious

-U disables filename restrictions
-R RockRidge CD format. This produces an image identical to the FreeBSD system tree.
-b Makes the CD bootable

This produced the following error message:
Using motd000 for /motd (motd)
mkisofs: Error: '/root/motd' and '/etc/motd' have the same Rock Ridge name 'motd'.
mkisofs: Unable to sort directory

Looks like I will be using tar the mkisofs.

For /usr/local/etc:

$> tar --gzip --create --verbose --absolute-names --preserve --exclude '*~' --file usr-etc.tgz /usr/local/etc

For /etc:

$> tar --gzip --create --verbose --absolute-names --preserve --exclude '*~' --file etc.tgz /etc

For /root:

$> tar --gzip --create --verbose --absolute-names --preserve --exclude '*~' --exclude '*Cache*' --exclude '*cache*' --file root.tgz

For /var:

$> tar --gzip --create --verbose --absolute-names --preserve --exclude '*~' --file var.tgz /var


I will now create my iso image with:

$> mkisofs -UR -V "EERVS" -o "eervs-4.9BSD`date +%Y%m%d%H%M%S`.iso" Lucious.tgz etc.tgz root.tgz usr-etc.tgz
Warning: creating filesystem that does not conform to ISO-9660.
20.58% done, estimate finish Tue May 25 23:16:09 2004
41.18% done, estimate finish Tue May 25 23:16:09 2004
61.72% done, estimate finish Tue May 25 23:16:11 2004
82.33% done, estimate finish Tue May 25 23:16:11 2004
Total translation table size: 0
Total rockridge attributes bytes: 494
Total directory bytes: 0
Path table size(bytes): 10
Max brk space used 10d24
24304 extents written (47 Mb)

The iso was written to CD with:
$> cdrecord dev=2,0,0 -v speed=42 eervs-3.9BSD20040525.iso

Track 01: 47 of 47 MB written (fifo 100%) [buf 98%] 20.7x.
Track 01: Total bytes read/written: 49774592/49774592 (24304 sectors).
Writing time: 27.526s
Average write speed 16.0x.
Min drive buffer fill was 89%
Fixating...
Fixating time: 13.667s
cdrecord: fifo had 784 puts and 784 gets.
cdrecord: fifo was 0 times empty and 385 times full, min fill was 93%.



Since the next set of directories all can not fit on one CD, I will create *.iso and split
the files in half before burning on CD. There is probably a better way to do this but I'll do it like this for now.



Backup CD 2
1. /usr/ports/distfiles (919, 206 bytes)
Instead of creating an iso image I can probably get away with deleting some files to make this
fit on a 700MB CD. After deleting the directories in /usr/ports/distfiles it only takes up 624M of storage space.

The iso was created and burned with the following commands respectively:
$> mkisofs -UR -o "distfiles-4.9BSD`date +%Y%m%d`.iso" -V "Distfiles" distfiles.tgz
$> cdrecord dev=2,0,0 -v speed=40 distfiles-4.9BSD20040525.iso

Backup CD 3 & 4
1. /home (1, 301, 796 bytes)
The iso for this directory will be named: home`date`.iso. Those are backquotes so the current date and time will replace the word 'date'. This iso will be split in half.

The iso was created and burned with the following commands respectively:
$> mkisofs -UR -o "home-4.9BSD`date +%Y%m%d`.iso" -V "HOME Directories" home.tgz

To split the iso:

$> split -b 699m home-4.9BSD20040526.iso home.iso

This produced two files:
1. xaa
2. xab

Which got names to home-4.9BSD_1_of_2_20040526.iso and home-4.9BSD_2_of_2_20040526.iso respectively.

To burn disk 1 and 2:

$> cdrecord dev=2,0,0 -v speed=40 home-4.9BSD_1_of_2_20040526.iso
$> cdrecord dev=2,0,0 -v speed=40 home-4.9BSD_2_of_2_20040526.iso


Backup CD 5, 6, 7 & 8
1. /storage/icon.storage ( 2, 736, 608 bytes)
The iso for this directory will be named: icon.storage`date`.iso. This image is going to be split 4 ways and placed onto four seperate CDs, each 700MB.


After createing the .tgz file for this backup then trying to create the iso from it I realized I was going this back up process backwards. Instead I just split the .tgz file into 4 chunks with split like so:

$> split -b 699m Storage.tgz

Then renamed each split file to:

$> mv xaa Storage_1_of_4_.tgz
$> mv xab Storage_2_of_4_.tgz
$> mv xac Storage_3_of_4_.tgz
$> mv xad Storage_4_of_4_.tgz

Time to create the .iso images and burn. Each .tgz was handled in this manner:

$> cdrecord dev=2,0,0 -v speed=52 -eject iconStorage-4.9BSD_X_of_420040526.iso
$> mkisofs -UR -o "iconStorage-4.9BSD_X_of_4`date +%Y%m%d`.iso" -V "Storage Directory X_4" Storage_X_of_4_.tgz > & /dev/null &


To mount the iso images:

$> vnconfig -v /dev/vn0c myimake.iso
$> mount -r -t cd9660 /dev/vn0c /mountpoint

After unmounting the vnode must be cleared so that another image may be mounted:

$> vnconfig -u -v /dev/vn0c

That was all. Lets hope my HD do not fail. "Knock on wood"

Monday, May 24, 2004

Just the ipf.rules I needed for my GATEWAY machine

I came across this really nice site when I was looking for example ipf.rules to use with IPFILTER.

This document was more than I needed.