Blog Archives

Cisco iOS Emulator Hardware and Software Simulations

Good article with introductory discussion:
http://www.techrepublic.com/blog/networking/practice-cisco-router-configuration-using-a-free-emulator/419

Supported Hardware

Dynagen/Dynamips:
Cisco 7200, Cisco 3600 series (3620, 3640 and 3660), 3700 series (3725, 3745) and 2600 series (2610 to 2650XM, 2691).

Emulators/Simulators

Dynagen/Dynamips (excellent choice and good for windows and *nix):
http://www.gns3.net/download
http://www.ipflow.utc.fr/index.php/Cisco_7200_Simulator
http://dynagen.org/tutorial.htm

Boson (proprietary at least 100USD$):
http://www.boson.com/AboutNetSim.html

pyios (not as much following as the other two):
http://sourceforge.net/projects/pyios/

Get Current Directory in Objective-C

Since Mac OS X is a unix based system, you can always use “getcwd” from the standard C library, but in general if you want to stick within the context of Objective-C/Cocoa, see the examples below.

Here’s one snippet you will probably Google quickly, but ultimately not the solution I chose.

(original code from stackoverflow see reference below)

NSFileManager *filemgr;
NSString *currentpath;

filemgr = [[NSFileManager alloc] init];

currentpath = [filemgr currentDirectoryPath];

Solution

Using the “bundle path” rather than “executable path” turned out to work much better in my instance:

NSString *currentpath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingPathExtension] stringByDeletingLastPathcomponent];
NSString *fileName = [NSString stringWithFormat:@"%@/%@/",currentpath,@"filename.pdf"];

References
stackoverflow.com, http://stackoverflow.com/questions/3692489/getting-current-directory-in-objective-c
friendlydeveloper.com, “NSFileManager”, http://www.friendlydeveloper.com/tag/nsfilemanager/
techtopia.com, “Working with Directories in Objective c”, http://www.techotopia.com/index.php/Working_with_Directories_in_Objective-C
Macrumors Forums, http://forums.macrumors.com/showthread.php?t=524754
stackoverflow, “Find Parent Directory of a path”, http://stackoverflow.com/questions/1309524/cocoa-objc-find-parent-directory-of-a-path

Concatenate string in Objective-C

Unfortunately, this is not as straight forward as it seems it should be. This really comes down to roughly two approaches in my opinion:

stringByAppendingString approach:

NSString *robot= @"Ronnie";
NSString *robotname = [robot stringByAppendingString:@" is the name of a robot."];

strigWithFormat approach:

NSString *robot= @"Ronnie";
NSString *robotname= @" is the name of the a robot.";
NSString *robotknowledge = @" knows eight languages.";
[NSString stringWithFormat:@"%@/%@/%@", robot, robotname, robot, robotknowledge];

References
StackOverflow, http://stackoverflow.com/questions/510269/how-do-i-concatenate-strings-in-objective-c
cocoadevcentral.com, “Learn Objective-C”, http://cocoadevcentral.com/d/learn_objectivec/

View PDF in Objective-C Cocoa on Mac OS X using PDFKit

The code below has been tested and works clean on the latest version of Mac OS X 10.5 Leopard.

Since it applies to native Mac OS X, and not iOS, porting this to work on iPhone/iPad is a bit different, however, it is overall very easy once you have the base concept and the main functionality below is the same.

Note: If you create this as a blank new project and are new to PDFKit, first make sure to add the Quartz framework to your project. (ref)

First, create a class (and corresponding header) to utilize the PDF. (CODE IS CASE SENSITIVE)

PDFImageView.h

#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>

@interface PDFImageView : NSImageView

- (void) loadFromPath: (NSString *) path;

@end

PDFImageView.m

#import "PDFImageView.h"
#import "DraggableScrollView.h"

@implementation PDFImageView

- (NSPDFImageRep *) pdfRep
{
return [[[self image] representations] lastObject];
}

- (void) loadFromPath: (NSString *)path
{
NSPDFImageRep *pdfRep;
NSImage *pdfImage;
NSRect frame;

pdfRep = [NSPDFImageRep imageRepWithContentsOfFile:path];
pdfImage = [[[NSImage alloc] init]autorelease];
[pdfImage addRepresentation: pdfRep];

frame = [pdfRep bounds];
frame.size.height *= [pdfRep pageCount];

[self setImage: pdfImage];

[super setFrame: frame];

if ([self isFlipped])
    [self scrollPoint: NSMakePoint(0,0)];
else {
    [self scrollPoint: NSMakePoint(0,frame.size.height)];
}
}

- (void) drawRect: (NSRect) rect
{
NSPDFImageRep *rep;
int pageCount, pageNumber;
NSRect onePageBounds;

[[NSColor whiteColor] set];
NSRectFill (rect);

rep = [self pdfRep];
pageCount = [rep pageCount];

for (pageNumber=0;pageNumber<pageCount;pageNumber++)
{
onePageBounds = [self rectForPage: (1+pageNumber)];
if (! NSIntersectsRect(rect, onePageBounds))
continue;

[rep setCurrentPage: pageNumber];
[rep drawInRect: onePageBounds];
}
}

- (void) mouseDown: (NSEvent *) theEvent
{
NSScrollView *scrollView;
scrollView=[self enclosingScrollView];
if ([scrollView respondsToSelector: @selector(dragDocumentWithMouseDown:)])
[(DraggableScrollView*)scrollView dragDocumentWithMouseDown: theEvent];
else {
[super mouseDown: theEvent];
}

- (void) setFrameSize: (NSSize) newSize
{
NSSize PDFsize;
float correctHeight;

PDFsize = [[self pdfRep] bounds].size;
correctHeight = [[self pdfRep] pageCount] * (PDfsize.height/PDFsize.width) * newSize.width;
correctHeight = ceil(correctHeight);

if (abs (correctHeight - newSize.height) > 3.0)
newSize.height = correctHeight;

[super setFrameSize: newSize];
}

- (BOOL) knowsPageRange: (NSRangePointer) range
{
range->location=1;
range->length=[[self pdfRep] pageCount];

return YES;
}

- (NSRect) rectForPage: (int) pageNumber
{
NSPDFImageRep *rep;
int pageCount;
NSRect result;

rep = [self pdfRep];
pageCount = [rep PageCount];

result = [rep bounds];
if (! [self isFlipped])
result = NSOffsetRect(result,0.0,(pageCount-1)*result.size.height);

if ([self isFlipped])
result = NSOffsetRect (result,0.0,(pageNumber-1)*result.size.height);
else {
result = NSOffsetRect (result,0.0,-(pageNumber-1)*result.size.height);

return result;
}
}
@end

This class is courtesy of Apple DC and used in the above PDFViewer class. You shouldn’t have to really do anything additional in this one, just plug and play. Allows user to scroll with mouse. I removed alot of comments for brevity to minimize code lines. See Apple reference at bottom for full source and additional info.

DraggableScrollView.h

#import <Cocoa/Cocoa.h>
 
@interface DraggableScrollView : NSScrollView
 
- (BOOL) dragDocumentWithMouseDown:
    (NSEvent *) theEvent;
 
@end

DraggableScrollView.m

#import "DraggableScrollView.h"
 
@implementation DraggableScrollView
 
#pragma mark PRIVATE CLASS METHODS
 
+ (NSCursor *) dragCursor
{
    static NSCursor *openHandCursor = nil;
 
    if (openHandCursor == nil)
    {
        NSImage     *image;
 
        image = [NSImage imageNamed: @"fingerCursor"];
        openHandCursor = [[NSCursor alloc] initWithImage: image
            hotSpot: NSMakePoint (8, 8)]; // guess that the center is good
    }
 
    return openHandCursor;
}
 
 
#pragma mark PRIVATE INSTANCE METHODS
 
- (BOOL) canScroll
{
    if ([[self documentView] frame].size.height > [self documentVisibleRect].size.height)
        return YES;
    if ([[self documentView] frame].size.width > [self documentVisibleRect].size.width)
        return YES;
 
    return NO;
}
 
 
#pragma mark PUBLIC INSTANCE METHODS -- OVERRIDES FROM NSScrolLView
 
- (void) tile
{
    [super tile];
 
    //  If the user can scroll right now, make our document cursor reflect that.
    if ([self canScroll])
        [self setDocumentCursor: [[self class] dragCursor]];
    else
        [self setDocumentCursor: [NSCursor arrowCursor]];
}
 
 
#pragma mark PUBLIC INSTANCE METHODS
 
//  dragDocumentWithMouseDown: -- Given a mousedown event, which should be in
//  our document view, track the mouse to let the user drag the document.
- (BOOL) dragDocumentWithMouseDown: (NSEvent *) theEvent // RETURN: YES => user dragged (not clicked)
{
    NSPoint         initialLocation;
    NSRect          visibleRect;
    BOOL            keepGoing;
    BOOL            result = NO;
 
    initialLocation = [theEvent locationInWindow];
    visibleRect = [[self documentView] visibleRect];
    keepGoing = YES;
 
    while (keepGoing)
    {
        theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask | NSLeftMouseDraggedMask];
        switch ([theEvent type])
        {
            case NSLeftMouseDragged:
            {
                NSPoint newLocation;
                NSRect  newVisibleRect;
                float   xDelta, yDelta;
 
                newLocation = [theEvent locationInWindow];
                xDelta = initialLocation.x - newLocation.x;
                yDelta = initialLocation.y - newLocation.y;
 
                //  This was an amusing bug: without checking for flipped,
                //  you could drag up, and the document would sometimes move down!
                if ([[self documentView] isFlipped])
                    yDelta = -yDelta;
 
                //  If they drag MORE than one pixel, consider it a drag
                if ( (abs (xDelta) > 1) || (abs (yDelta) > 1) )
                    result = YES;
 
                newVisibleRect = NSOffsetRect (visibleRect, xDelta, yDelta);
                [[self documentView] scrollRectToVisible: newVisibleRect];
            }
            break;
 
            case NSLeftMouseUp:
                keepGoing = NO;
                break;
 
            default:
                /* Ignore any other kind of event. */
                break;
        }                               // end of switch (event type)
    }                                   // end of mouse-tracking loop
 
    return result;
}
 
@end

And now for the main class of our application which will use the above PDFImageView class and bind/map to our interface.

MainView.h

#import <Cocoa/Cocoa.h>
#import <Quartz/Quartz.h>

@interface MainView : NSDocument {
IBOutlet PDFView *_pdfView;
}

@end

MainView.m

#import "MainView.h"

@implementation MainView

//will load PDF from local filesystem in current path as .app is launched from
-(void)awakeFromNib{
NSFileManager *filemgr;
filemgr = [[NsFileManager alloc]init];

NSString *currentpath = [[[[NSBundle mainBundle] bundlePath] stringByDeletingPathExtension] stringByDeletingLastPathcomponent];

//[Util MessageBox:@"currentpath":currentpath]; //SEE MY REFERENCE BELOW FOR DEBUG UTILITY CLASS

NSString *fileName = [NSString stringWithFormat:@"%@/%@/",currentpath,@"filename.pdf"];
PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:fileName]];
[_pdfView setDocument: pdfDoc];
}

@end

References
Apple DC, http://developer.apple.com/library/mac/#samplecode/PDFView/Introduction/Intro.html
“Show Dialog Message Box in Objective-C Cocoa”, https://ronniediaz.com/2011/06/13/show-dialog-message-box-in-objective-c-cocoa/
Google CodeSearch (“_pdfView”), http://www.google.com/codesearch#search&q=_pdfView+lang:objectivec

Quick Cisco IOS Reference

As a general word of caution, if you have no experience with Cisco or other CLI based router admin (such as linux and red hat derivatives), I would not recommend diving in unless you have a lot of spare time on your hands in the event that you accidentally delete all routing tables or cause other serious problems!

Even in a professional business environment, due to some differences between IOS and hardware versions and the need to reboot for some settings to take effect, it’s always a good idea to perform changes after hours when critical business operations will not be interrupted by internet or service downtime.

Official Cisco IOS Manual, http://www.cisco.com/en/US/docs/ios/12_3/featlist/sec_vcg.html

Manipulate NAT and other general help references and top hits on Google for common Cisco beginner questions:
http://www.nthelp.com/handy_cisco.htm
http://www.loeppenthien.dk/Network_IOS.asp
http://www.simulationexams.com/tutorials/ccna/cisco-ios.htm
http://www.networkclue.com/routing/Cisco/IOS/index.aspx
http://www.oreillynet.com/pub/a/network/2002/01/02/ciscotips.html
http://portforward.com/english/routers/port_forwarding/Cisco/Cisco800Series/default.htm

Why Geeks Should Love the iPad

After reading a similarly titled article on Forbes, I was inspired to write a counter-argument.

The author of the Forbes article implies that the stringent security of the iPad and this line of Apple products is limiting users freedom to use the product as they wish and will ultimately drive geeks away. On the contrast, it seems the tighter the system is locked, the more desire geeks have to understand and hack these products. A perfect example of this is the trend of iOS jailbreaking.

“The desire to hack things” was born out of an age old, genetically preserved and burning curiosity to understand and take apart things which were designed for the mainstream consumer.

By taking apart the first PCs, mainframes, early computer systems and legacy telephone and PBX systems – the first modern day computer-techno-geniuses were born.

Like Apple, these former technology corporation giants designed their products to be used within the confines and restraints of a system which was meant to preserve the integrity of their creations – much like the “icosystem” mentioned by McAffee (see reference below).

In legacy phone systems long distance phone calls cost more than short distance calls even though power usage and internal cost was approximately the same. On the iPad and iPhone we see the same type of inflated margins on certain products and services that could cost less.

All this means is the cycle has reset; in Steve Jobs generation “geeks” were black boxing ATT/Bellsouth and other major telephone carriers. Following in this legacy, it is now a task of the next generation to take apart and understand Apples’ creations.

The iphone, iPad and iPod aren’t prisons.. Maybe for general consumers.. But for the true geek they are a playground.. A mystery to unravel with potential and secrets waiting to be uncovered.

References:

“Why Geeks Hate the iPad”, Forbes, Andrew McAfee. http://www.forbes.com/2010/07/07/apple-geeks-google-technology-cio-network-ipad.html?feed=rss_technology

Wikipedia. jailbreaking, black box