Ady Wicaksono Daily Activities

Archive for October 5th, 2007

Learning C/C++ part 1

with 6 comments


#include <iostream>
#include <cstdlib> // Define system()
#include <string>  // Define string
#include <fstream> // File Operation
#include <vector>  // The same with java Vector implementation
using namespace std;

// Global Variable
int globalIntOK = 1000;

string hello(){
    return "Hello TAHU\n";
}

void swap(int *a, int *b){
    int c;
    c  = *a; // C now contains memory address of a
    *a = *b; // data pointed by address memory a is now contain data pointed by memory b
    *b = c;  // data pointed by address memory b is now = c
}

void func_withnostatic(){
    int y = 10;
    cout << "y: " << y++ << endl;
}

void func_withstatic(){
    static int y = 10;
    cout << "y: " << y++ << endl;
}

void swapCPP(int& a, int& b){
    cout << "C++ SWAP -> a = " << a << endl;
    cout << "C++ SWAP -> b = " << b << endl;
    cout << "C++ SWAP address -> a = " << &a << endl;
    cout << "C++ SWAP address -> b = " << &b << endl;

    int c;
    c = a; // karena &a adalah alamat memory,
    // maka a adalah isi dari alamat memori itu
    a = b;
    b = c;
}

int main_learn1(int argc, char **argv){
    int number;
    cout << "Hello World" << endl
    << "Hello too !" << endl;
    cout << "Number 15 in decimal: " << dec << 15 << endl;
    cout << "Number 15 in octal : " << oct << 15 << endl;
    cout << "Number 15 in hex : " << hex << 15 << endl;
    cout << "Floating Point : " << 15.12 << endl;
    cout << "Char(27) is : " << char(27) << endl;
    cout << "Char concat "
    " continue 1 "
    " continue 2 " << endl;
    cout << "Enter Number: " ;
    cin >> number;
    cout << "Number: " << dec << number << endl;

    // Calling SYSTEM Program
    system("echo 'Hallo ADY'");

    // String on C++
    string s1, s2; // Empty String
    string s3 = "hello World String"; // init string
    string s4("Hello World Another string"); // init string too

    cout << "String 3: " << s3 << endl;
    cout << "String 4: " << s4 << endl;

    cout << "Just like java...concat is simple-> " << s3 + " " + s4 << endl;

    s1 = "testaja.yt";
    ifstream inFile(s1.c_str()); // ifstream -> open for reading
    if(inFile.is_open()){
        cout << "File " << s1 << " is opened" << endl;
        string buff1;
        while(getline(inFile,buff1)){ // ReadFile .. but '\n' discarded
            cout << buff1 << endl;
        }
        inFile.close();
    }else{
        cout << "File " << s1 << " is NOT opened" << endl;
    }

    ofstream outFile(s1.c_str()); // ofstream -> open for writing
    if(outFile.is_open()){
        cout << "File " << s1 << " is opened to be written" << endl;
        outFile.write(s3.c_str(),s3.length());
        outFile.close(); // Close File
    }else{
        cout << "File " << s1 << " is NOT opened to be written" << endl;
    }

    cout << "Vector" << endl;
    string x1,x2,x3;
    vector<string> vStr;

    x1 = "Data 1";
    x2 = "Data 2";
    x3 = "Data 3";
    vStr.push_back(x1); // Masukin Elemen
    vStr.push_back(x2); // Masukin Elemen
    vStr.push_back(x3); // Masukin Elemen

    // Tampilkan Elemen
    int i;
    for(i=0; i < vStr.size(); i++){
        cout << "Elemen ke-" << i+i << ": " << vStr[i] << endl;
    }

    vector<int> vInt;
    vInt.push_back(1);
    vInt.push_back(2);
    vInt.push_back(3);
    for(i=0; i < vInt.size(); i++){
        cout << "Elemen ke-" << i+i << ": " << vInt[i] << endl;
    }

    // Fungsi juga bisa dipakai lho untuk cout
    cout << "Hasil fungsi hello(): " << hello() << endl;

    // Ada boolean di sana
    bool x;
    x = true;

    cout << "Boolean x = " << x << endl;

    // Pointer tetap ada
    int y = 1231;
    cout << "Alamat memory dari y yang bernilai " << y << ", adalah " << (long)&y << endl;
    cout << "Alamat memory dari fungsi hello(), adalah " << (long)&hello << endl;

    int abc = 1000;
    int *p_abc = &abc; // p_abc adalah pointer ke integer -> diacu ke alamat abc
    cout << "ABC = " << abc << endl;
    *p_abc = 2000;
    cout << "Skrg ABC = " << abc << endl;

    // Fungsi Pointer dengan C

    char ans;
    while(true){
        cout << "Ketik y untuk melanjutkan: ";
        cin >> ans;
        if(ans == 'y'){
            break;
        }
    }
    int tahu = 100;
    int tempe = 200;
    cout << "Tahu = " << tahu << endl;
    cout << "Tempe = " << tempe << endl;
    swap(&tahu,&tempe); // Alamat memory tahu & tempe dipass ke fungsi
    cout << "SWAP(), now...\nTahu = " << tahu << endl;
    cout << "Tempe = " << tempe << endl;

    // C++ reference
    // C++ punya model baru untuk pass by reference,
    // Yakni dengan menggunakan tanda & pada parameter fungsi, bukan *
    swapCPP(tahu,tempe);
    cout << "SWAP back with C++ Reference(), now...\nTahu = " << tahu << endl;
    cout << "Tempe = " << tempe << endl;

    cout << "Now about scoping" << endl;
    { int tahutempe; // variabel ini hanya dikenali di sini
    };

    // Disini jika diuncomment akan tampil
    // : error C2065: 'tahutempe' : undeclared identifier (vc++6)
    /* cout << "Tahu Tempe variabel? " << tahutempe; */

    // Global Variable
    cout << "Global Variable globalIntOK = " << globalIntOK << endl;

    // Register Variable, process it as fast as you can :D
    // Only valid in block, you may not have global register :D
    register int test01 = 1001;
    cout << "Test01 Var as Register = " << test01 << endl;

    // Static Variables
    // Variabel yang tidak didefinisikan static dalam sebuah fungsi
    // nilainya akan diinisialisasi ulang, tp jika static maka
    // saat "return" dari fungsi nilainya akan tetap disimpan
    // Static mirip dengan global, tapi variabel static hanya berlaku
    // dalam blok fungsi dimana dia diterapkan :)
    // dikenal juga file scope, dimana static variabel yang ditaruh
    // spt penempatan variabel global -> berarti variabel ini hanya dikenali
    // di file ini saja
    for(i = 0; i < 10; i++){
        func_withnostatic();
    }
    for(i = 0; i < 10; i++){
        func_withstatic();
    }

    // Variabel extern ... waktu dilakukan linking (compile -> link)
    // diasumsikan ada diantara file2 yang linking
    /*
    When the compiler encounters the declaration ‘extern int i’, it
    knows that the definition for i must exist somewhere as a global
    variable.
    */
    // Constanta di C++
    // di C biasanya digunakan macro
    #define MY_CONST 1000
    cout << "My Const = " << MY_CONST << endl;
    // di C++ bisa digunakan const
    const int h = 121;
    cout << "Const h = " << h << endl;

    return 0;
}

int main_learn2(int argc, char **argv){
    return 0;
}

int main(int argc, char **argv){
    main_learn1(argc,argv);
    return 0;
}

Written by adywicaksono

October 5, 2007 at 10:19 pm

Posted in C/C++

Traceroute – how it works?

without comments

I was a network and system administrator, one of tools I used is traceroute or tracert (in Windows). See this example traceroute from my desktop:

Tracing route to www.yahoo-ht3.akadns.net [69.147.114.210]
over a maximum of 30 hops:
1     2 ms     1 ms     2 ms  192.168.1.1
2     9 ms    15 ms    12 ms  10.53.128.1
3    13 ms    11 ms    12 ms  172.20.53.65
4    12 ms    11 ms     8 ms  172.26.53.1
5    16 ms    12 ms    13 ms  172.20.8.245
6    12 ms    14 ms    13 ms  203.116.5.125
7    20 ms    16 ms    24 ms  203.118.3.229
8   232 ms   237 ms   230 ms  so-4-0-0.edge2.LosAngeles1.Level3.net [4.71.134.1]
9   233 ms   232 ms   230 ms  ae-2-54.bbr2.LosAngeles1.Level3.net [4.68.102.97]
10   237 ms   229 ms   232 ms  ae-0-0.bbr1.SanJose1.Level3.net [64.159.1.129]
11   316 ms   230 ms   426 ms  ae-13-69.car3.SanJose1.Level3.net [4.68.18.5]
12   386 ms   402 ms   400 ms  4.71.112.14
13   283 ms   294 ms   287 ms  so-0-0-0.pat2.dcp.yahoo.com [216.115.101.150]
14   292 ms   295 ms   289 ms  ge-3-1-0-p171.msr2.re1.yahoo.com [216.115.108.71]
15   288 ms   291 ms   293 ms  gi1-22.bas-a1.re3.yahoo.com [68.142.238.65]
16   285 ms   286 ms   286 ms  f1.www.vip.re3.yahoo.com [69.147.114.210]

Traceroute use TTL (time-to-live) feature of IP (Internet Protocol). TTL is an upper bound of the time that IP datagram packet can exist in an internet system. TTL field will be decrease by one value by every host on the route to its destination. If the TTL field reaches zero before the datagram arrives at its destination, then the datagram is discarded and an ICMP error datagram (11 – Time Exceeded) is sent back to the sender.

So for previous example, traceroute application will firstly set TTL to 1 to access www.yahoo.com, but because when pass 192.168.1.1 the value will reduced by one, become zero than, host 192.168.1.1 will send ICMP error datagram. The traceroute application will then know that next hop is 192.168.1.1. Again it’s now set TTL to 2, but after 10.53.128.1 TTL value become 0 but the IP is still not reach it’s final destination so 10.53.128.1 will send ICMP error datagram. Again and again traceroute application will increase TTL value and collect ICMP error datagram from each hop until the TTL value is big enough to reach final destination. The result is shown as traceroute log above.

Another issue, this traceroute application on UNIX platform need to access ICMP protocol which is only accessible by root (or any user with uid = 0). So, application traceroute mostly will run with setuid root active and can become a security hole. Don’t worry, normally current traceroute application is secure enough event with setuid flag root active.

For more information about ICMP please refer to RFC 792

Written by adywicaksono

October 5, 2007 at 4:10 pm

Posted in networking