APR偽装

データリンク層の1つであるイーサネットでは、送信元と送信先のアドレスの指定にMACアドレスを利用しています。インターネット層におけるIPアドレスとMACアドレスの対応付けを担っているのがAPR(address resolution protocol)です。

コンピュータはAPRテーブルに一定期間、IPとMACの組をキャッシュとして保持しています。このキャッシュは、偽造したARPパケットを送信することで書き換え可能です。これで、通信を妨害することもあり得ます。

対策としては、IPSecのAHを利用する方法や、アプリケーションならびに通信路の暗号化と認証強化を行う方法があります。

派生クラスと基本クラス

#include < iostream >
using namespace std;

class base {
    int i;
public:
    base(int n){
        cout << "call base class constructor\n";
        i = n;
    }
    ~base() { cout << "call base class destructor\n"; }
    void showi() { cout << i << '\n'; }
};

class derived : public base {
    int j;
public:
    derived(int n, int m): base(m){
        cout << "call derived class constructor \n";
        j = n;
    }
    ~derived() { cout << "call derived class destructor\n"; }
    void showj() {cout << j << '\n'; }
};

int main()
{
    derived o(10, 20);
    
    o.showi();
    o.showj();
    
    return 0;
}

コンストラクタ、デストラクタ、継承

#include < iostream >
using namespace std;

class base {
public:
    base() { cout << "call base class constructor\n"; }
    ~base() { cout << "base class destructor\n"; }
};

class derived : public base {
public:
    derived() { cout << "call derived class constructor\n"; }
    ~derived() { cout << "call derived class destructor\n";}
};

int main()
{
    derived o;
    return 0;
}

被保護メンバがpublicとして継承

#include < iostream >
using namespace std;

class base {
protected:
    int a, b;
public:
    void setab(int n, int m){ a = n; b = m; }
};

class derived : public base {
    int c;
public:
    void setc(int n) { c = n; }
    void showabc(){
        cout << a << ' ' << b << ' ' << c << '\n';
    }
};

int main()
{
    derived ob;
    
    ob.setab(1, 2);
    
    ob.setc(3);
    ob.showabc();
    return 0;
}

被保護メンバの使用

#include < iostream >
using namespace std;

class samp {
    int a;
protected:
    int b;
public:
    int c;
    samp(int n, int m){ a = n; b = m; }
    int geta() { return a; }
    int getb() { return b; }
};

int main()
{
    samp ob(10, 20);
    
    ob.c = 30;
    
    cout << ob.geta() << ' ';
    cout << ob.getb() << ' ' << ob.c << '\n';
    
    return 0;
}

基本クラスのアクセス制御

#include < iostream >
using namespace std;

class base {
    int x;
public:
    void setx(int n){ x = n; }
    void showx() { cout << x << '\n'; }
};

class derived : public base {
    int y;
public:
    void sety(int n){ y = n; }
    void showy() { cout << y << '\n'; }
};

int main()
{
    derived ob;
    
    ob.setx(10);
    ob.sety(20);
    ob.showx();
    ob.showy();
    
    return 0;
}

正規表現リファレンス


正規表現に関する本を貰ったので、移動時間で読み終えました。
「電話番号っぽい文字列」「◯◯から始まるキーワード」「〜でくくられた文字列」のような条件検索の際に活用。Unixではgrepがありますね。

例えば、a,b,cいずれかの文字にマッチするには
[a-c]と書きます。

郵便番号を表す正規表現ならば、
\b\d{3}(-\d{2}-\d{4})\b といった具合です。

本書では、Perl, java, php, ruby, javascriptを扱っています。
分量が少ないので、一度目を通しておくとよいかも。

正規表現ポケットリファレンス (POCKET REFERENCE)

+演算子のオーバーロード

#include < iostream >
using namespace std;

class coord {
    int x, y;
public:
    coord() { x=0; y=0; }
    coord(int i, int j){ x=i; y=j; }
    void get_xy(int &i, int &j){ i=x; j=y; }
    coord operator+(coord ob2);
    coord operator+(int i);
};

coord coord::operator+(coord ob2)
{
    coord temp;
    
    temp.x = x + ob2.x;
    temp.y = y + ob2.y;
    
    return temp;
}

coord coord::operator+(int i)
{
    coord temp;
    
    temp.x = x + i;
    temp.y = y + i;
    
    return temp;
}

int main()
{
    coord o1(10, 10), o2(5, 3), o3;
    int x, y;
    
    o3 = o1 + o2;
    o3.get_xy(x, y);
    cout << "(o1+o2) x: " << x << ", Y: " << y << "\n";
    
    o3 = o1 + 100;
    o3.get_xy(x, y);
    cout << "(o1+100) x: " << x << ", Y: " << y << "\n";
    
    return 0;
}

参照 swap

#include < iostream >
using namespace std;

void swapargs(int &x, int &y);

int main()
{
    int i, j;
    
    i = 10;
    j = 19;
    
    cout << "i: " << i << ", ";
    cout << "j: " << j << "\n";
    
    swapargs(i, j);
    
    cout << "after exchange: ";
    cout << "i: " << i << ", ";
    cout << "j: " << j << "\n";
    
    return 0;
}

void swapargs(int &x, int &y)
{
    int t;
    
    t = x;
    x = y;
    y = t;
}