LoginSignup
0
0
新規開発や新技術の検証、導入にまつわる記事を投稿しよう!

Macintosh対応「基礎からわかるTCP/IP アナライザ作成とパケット解析 Linux/FreeBSD対応」小高知宏 オーム社

Last updated at Posted at 2018-02-14

kiso.png

<この項は書きかけです。順次追記します。>
This article is not completed. I will add some words in order.

Macintoshで動かす「基礎からわかるTCP/IP アナライザ作成とパケット解析 Linux/FreeBSD対応」小高知宏 オーム社, 2001.1, ISBN 978-4274064043

「基礎からわかるTCP/IP」シリーズ。
「基礎からわかるTCP/IP Javaネットワークプログラミング」, オーム社, 小高 知宏, 1999.8, ISBN 9784274063213
の著者の第二弾。

基礎.png
https://qiita.com/kaizen_nagoya/items/14519536b827fdadb32e

どちらの本にもCDが付いています。

目次

CDの中身
準備
第二章 make
付録1 参考文献
付録2 libpcapの導入
付録3 エラー集
付録4 不必要だった準備
付録5 BSD版を解凍しても一緒だった

CDの中身

「アナライザ作成とパケット解析」CDの中身を順にコンパイルしてみます。
CDの中身をpacketというフォルダに全て入れていることを前提にします。
Makefileは、makeコマンドが処理する手順を記載した実行可能な仕様(script)です。
まず、Makefileがあるかどうか、find命令で探します。

$ cd packet
$ find ./ -name Makefile -print
.//CH2/21/Makefile
.//ch2B/2.1/Makefile
.//ch2B/2.2/Makefile
.//ch2B/2.3.1/Makefile
.//ch2B/2.3.2/Makefile
.//ch2B/2.3.3/Makefile
.//ch2B/2.4/Makefile

findの次の「./」は、現在位置(current directory)から下を検索しろという指示です。
ファイルシステム全体を検索するときは「/」を指示します。
「-name Makefile」は「Makefile」という名前で始まるファイルがあるかどうか。
「-print」はあったら出力という指示です。出力先は標準出力(stdout: standard out)で、OS起動時の既定値では画面になっています。

$ cat .//CH2/21/Makefile
ether : ether.o
	cc ether.o -o ether -lpcap
tcp : tcp.o
	cc tcp.o -o tcp -lpcap
clean :
	rm -f *.o core a.out

最初に処理する可能性がある第二章の最初のMakefileの中身を出力します。
catコマンドは複数のファイルを一つにするコマンドです。一つしかファイルを指定しないと、ファイルを標準出力へ表示する意味になります。

ccはCコンパイラを呼び出す別名。「-o ether」は生成ファイル名の指定。「-lpcap」はリンクするライブラリとしてpcap
「ether: ether.o」
は、ether.oファイルが、etherファイルよりも新しければ、次の命令を実行するという条件分岐記述です。
「clean:」のように右側がない区画は、「make clean」のようにmakeコマンドで直接指定する使い方をします。
rmは削除コマンド。
「make clean」で、ファイルを編集していないがコンパイルしたいとき。例えば、gccとllvm/clangで比較したいときなどに使う。
使い勝手から少しMakefileを手直し。

ether : ether.c
	cc ether.c -o ether -lpcap
tcp : tcp.c
	cc tcp.c -o tcp -lpcap
clean :
	rm -f *.o core a.out ether tcp

実行はGO211を呼び出すらしい。p.56

$cat GO211
./ether $1 | ./graph.tcl

ファイルの拡張子tclはtcl/tkのスクリプト。tcl/tkを使っているらしい。
実行コマンドを最後まで調べるとperlも利用していた。
手探りで実行しています。不必要なことも色々やっています。

準備

cコンパイラ、tcl/tk, perlが要る。
Cコンパイラはxcodeを導入。<付録1参照>
pcap(libpcap)の導入<付録2参照>

perl, tcl/tk現在のMacintoshは入っている。Macintoshはtcl/tkはOS 10.9からは導入済みらしい。
現在入っているそれぞれの版を確認(mac osX 10.12.6, 20180219現在)

$ perl --version
This is perl 5, version 26, subversion 1 (v5.26.1) built for darwin-thread-multi-2level
$ cc --version
cc  Apple LLVM version 9.0.0 (clang-900.0.39.2)Target: x86_64-apple-darwin16.7.0

wish(tcl/tk)の版の確認方法

Get the version of TCL from the command-line?
https://stackoverflow.com/questions/9200108/get-the-version-of-tcl-from-the-command-line

tcl/tkとシェル

tclのシェルはtclsh
tcl/tkのシェルはwish:window shell

ちょっとwishにしてみた。

echo 'puts $tcl_version;exit 0' | wish
8.5

版のまとめ

c 9.0.0(darwin16.7.0)
tcl-tk 8.5
perl 5.26.1

tcl-tkはmacOS 10.9以降は入っているらしい。

第二章 make

21 make

なにはなくてもmake

$ cd CH2
$ make
make: *** No targets specified and no makefile found.  Stop.

CH2にはMakefileがない。
2章一気に全部makeしてくれる設定はないらしい。
21フォルダのMakefileの中身を見る。

$ cd 21
$ cat Makefile
ether : ether.o
	cc ether.o -o ether -lpcap
tcp : tcp.o
	cc tcp.o -o tcp -lpcap
clean :
	rm -f *.o core a.out

ひとまず、一つづつコンパイルすることに。

ether.c コンパイル

$cc ether.c -o ether -lpcap
ether.c:78:3: warning: implicit declaration of function 'alarm' is invalid in
      C99 [-Wimplicit-function-declaration]
  alarm(1);// <82>P<95>b<82>ɃZ<83>b<83>g
  ^
ether.c:104:5: warning: implicitly declaring library function 'exit' with type
      'void (int) __attribute__((noreturn))' [-Wimplicit-function-declaration]
    exit(0);
    ^
ether.c:104:5: note: include the header <stdlib.h> or explicitly provide a
      declaration for 'exit'
2 warnings generated.

ether.cに次の行を追加。

#include <stdlib.h>

コンパイルオプションに-Wimplicit-function-declarationを追加。

cc ether.c -Wimplicit-function-declaration -o ether -lpcap
ether.c:79:3: warning: implicit declaration of function 'alarm' is invalid in C99
      [-Wimplicit-function-declaration]
  alarm(1);// <82>P<95>b<82>ɃZ<83>b<83>g
  ^
1 warning generated.

なぜ、警告が消えないか未調査。

go211実行

$./go211 eth0
eth0: No such device exists (BIOCSETIF failed: Device not configured)

何が駄目なんだろう。ネットワークの設定を確認するifconfigコマンドを打つ。(windowsだとipconfig)

$ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
	options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
	inet 127.0.0.1 netmask 0xff000000 
	inet6 ::1 prefixlen 128 
	inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
	nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
	inet 192.168.2.38 netmask 0xffffff00 broadcast 192.168.2.255
	nd6 options=201<PERFORMNUD,DAD>
	media: autoselect (1000baseT <full-duplex,energy-efficient-ethernet>)
	status: active
...

en0という名前。

$ ./go211 en0
./graph.tcl: line 9: proc: command not found
./graph.tcl: line 10: global: command not found
./graph.tcl: line 11: global: command not found
./graph.tcl: line 12: global: command not found
./graph.tcl: line 15: foreach: command not found
./graph.tcl: line 16: syntax error near unexpected token `$i'
./graph.tcl: line 16: `        if {$show_flag($i)} {'

ネットのエラーは無くなった。tclがエラーらしい。go211の中身を確認。

$ cat ./go211
./ether $1 | ./graph.tcl

tclがうまく動作していない。

$ tclsh 
% package require Img
can't find package Img
% package require Expect
5.45
% package require Tktable
2.10
% exit

tclはそれなりに導入済みらしい。
etherの動作だけを確認。

$ ./ether en0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
60 0 0 0 0 0 0 60
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

tcl/tk関連の記事を約10 読み実行して見る。

コマンドで一括実行するとエラーになるスクリプトも、インタプリタで1行づつ入力すると動く。
じゃ、インタプリタへの渡し方がまずいんじゃないかと思ってwishを足して見る。

vi go211
./ether $1 | wish ./graph.tcl 

実行!

./go211 en0

図が出ました。
go211.png

tcp.c

cc tcp.c -o tcp -lpcap
tcp.c:29:10: fatal error: 'netinet/protocols.h' file not found
#include <netinet/protocols.h>//<83>v<83><8D><83>g<83>R<83><8B><94>ԍ<86>...
         ^~~~~~~~~~~~~~~~~~~~~
1 error generated.

本では、protocol.hを/usr/include/netinetにコピーしろという。
システムのヘッダを追加するかどうか中身を拝見。

$ cat ../../include/protocols.h
/* protocols.h */
#define IP_ICMP		  1
#define IP_TCP 		  6
#define IP_EGP 		  8
#define IP_IGP 		  9
#define IP_UDP		 17

定数を決めているだけ。

#include <netinet/protocols.h>

#include "../../include/protocols.h"

に変更。これは暫定措置(work around)。

protocols.hをネットで検索してみる。

/* protocols.h */
#ifndef _NETINET_PROTOCOLS_H
#define _NETINET_PROTOCOLS_H

#define IP_ICMP		  1
#define IP_IGMP		  2
#define IP_GGP 		  3
#define IP_ST  		  5
#define IP_TCP 		  6
#define IP_UCL 		  7
#define IP_EGP 		  8
#define IP_IGP 		  9
#define IP_BBN_RCC_MON	 10
#define IP_NVP_II	 11
#define IP_PUP		 12
#define IP_ARGUS	 13
#define IP_EMCON	 14
#define IP_XNET		 15
#define IP_CHAOS	 16
#define IP_UDP		 17
#define IP_MUX		 18
#define IP_DCN_MEAS	 19
#define IP_HMP		 20

値は同じ。こっちをコピーして、ソースを直さないという手もある。

再コンパイル

$ cc tcp.c -o tcp -lpcap
tcp.c:63:7: warning: implicitly declaring library function 'strcpy' with type
      'char *(char *, const char *)' [-Wimplicit-function-declaration]
      strcpy(protocol,"tcp") ;
      ^
tcp.c:63:7: note: include the header <string.h> or explicitly provide a
      declaration for 'strcpy'
tcp.c:66:31: error: no member named 'source' in 'struct tcphdr'
      srcport = ntohs(tcph -> source) ;
                      ~~~~    ^
/usr/include/sys/_endian.h:132:39: note: expanded from macro 'ntohs'
#define ntohs(x)        __DARWIN_OSSwapInt16(x)
                                             ^
/usr/include/libkern/_OSByteOrder.h:72:40: note: expanded from macro
      '__DARWIN_OSSwapInt16'
    ((__uint16_t)(__builtin_constant_p(x) ? __DARWIN_OSSwapConstInt16(x...
                                       ^

警告は一つでも減らしておきたい。

#include <string.h>

行を追加。tcphに関する行をすべて表示。

$ grep tcph tcp.c
  struct tcphdr *tcph ;//TCP
      tcph = (struct tcphdr *)
      srcport = ntohs(tcph -> source) ;
      dstport = ntohs(tcph -> dest) ;

tcphという変数はtcphdr構造体へのポインタとして定義しているらしい。tcphdr(多分、tcp header)の中身確認。

$ grep tcphdr /usr/include/netinet/*.h
rep tcphdr /usr/include/netinet/*.h
/usr/include/netinet/tcp.h:#define tcp6hdr		tcphdr	/* for KAME src sync over BSD*'s */
/usr/include/netinet/tcp.h:struct tcphdr {
/usr/include/netinet/tcp.h:#define TCP_MAXOLEN	(TCP_MAXHLEN - sizeof(struct tcphdr))
/usr/include/netinet/tcpip.h:	struct	tcphdr ti_t;		/* tcp header */
/usr/include/netinet/tcpip.h:	struct	tcphdr

tcphdrはtcp.hで定義しているらしい。

$ cat /usr/include/netinet/tcp.h
...
/*
 * TCP header.
 * Per RFC 793, September, 1981.
 */
struct tcphdr {
  unsigned short th_sport;/* source port */
  unsigned short th_dport;/* destination port */
  tcp_seq th_seq;/* sequence number */
  tcp_seq th_ack;/* acknowledgement number */
...

th_sport, th_dportという名前で定義している。
tcp.cの先頭の方に、次の行を追加。

#define source th_sport /// add for error
#define dest th_dport /// add for error 

再度コンパイル

$ cc tcp.c -o tcp -lpcap -Wimplicit-function-declaration
tcp.c:92:3: warning: implicit declaration of function 'alarm' is invalid in C99
      [-Wimplicit-function-declaration]
  alarm(1);// 
  ^
tcp.c:114:5: warning: implicitly declaring library function 'exit' with type 'void (int)
      __attribute__((noreturn))' [-Wimplicit-function-declaration]
    exit(0);
    ^
tcp.c:114:5: note: include the header <stdlib.h> or explicitly provide a declaration for 'exit'
tcp.c:118:6: warning: implicitly declaring library function 'isdigit' with type 'int (int)'
      [-Wimplicit-function-declaration]
  if(isdigit(argv[2][0])) sscanf(argv[2],"%d",&portno) ;
     ^
tcp.c:118:6: note: include the header <ctype.h> or explicitly provide a declaration for 'isdigit'
tcp.c:118:47: warning: format specifies type 'int *' but the argument has type 'u_short *'
      (aka 'unsigned short *') [-Wformat]
  if(isdigit(argv[2][0])) sscanf(argv[2],"%d",&portno) ;
                                          ~~  ^~~~~~~
                                          %hd
4 warnings generated.

警告は残っている.

動作確認。

$ ./go212 en0 http
./graph.tcl: line 9: proc: command not found
./graph.tcl: line 10: global: command not found
./graph.tcl: line 11: global: command not found
./graph.tcl: line 12: global: command not found
./graph.tcl: line 15: foreach: command not found
./graph.tcl: line 16: syntax error near unexpected token `$i'
./graph.tcl: line 16: `        if {$show_flag($i)} {'

tclがうまく動いてない。

$ cat go212
./tcp $1 $2 | ./graph.tcl

tco以外の部分の確認。

$ ./tcp en0 http
0 0
0 0
0 0
60 0
0 0
180 0

同様にwishを入れる。

vi go212
./tcp $1 $2 | wish ./graph.tcl

で、実行

./go212 en0 http

go212.png

22 make

cc ip.c -o ip -lpcap
ip.c:50:5: warning: implicitly declaring library function 'strcpy' with type
      'char *(char *, const char *)' [-Wimplicit-function-declaration]
    strcpy(hn,nametable[i].hostname) ; 
    ^
ip.c:50:5: note: include the header <string.h> or explicitly provide a declaration for 'strcpy'
ip.c:54:5: warning: implicitly declaring library function 'exit' with type 'void (int)
      __attribute__((noreturn))' [-Wimplicit-function-declaration]
    exit(1);   
    ^
ip.c:54:5: note: include the header <stdlib.h> or explicitly provide a declaration for 'exit'
ip.c:102:6: warning: implicitly declaring library function 'strlen' with type
      'unsigned long (const char *)' [-Wimplicit-function-declaration]
  if(strlen(hn) > 0) // <95>ϊ<B7><82>ł<AB><82><BD><8F>ꍇ
     ^
ip.c:102:6: note: include the header <string.h> or explicitly provide a declaration for 'strlen'
3 warnings generated.

スクリプトは

$ cat go22
./ip $1 | ./sum.pl | ./fpause.tcl | ./display.tcl

を変更。

$vi go22
./ip $1 | ./sum.pl |wish ./fpause.tcl |wish ./display.tcl

実行

$ ./go22 en0

go22desu.png

231 make

tcpとudpの両方がある。

#define source uh_sport /// add for error
#define dest uh_dport /// add for error 

この置き直しでは駄目。
sort, destを書き直す。4箇所。

cc digest.c -o digest -lpcap
digest.c:28:10: warning: non-portable path to file '"../../INCLUDE/PROTOCOLS.H"'; specified path
      differs in case from file name on disk [-Wnonportable-include-path]
#include "../../include/protocols.h"//<83>v<83><8D><83>g<83>R<83><8B><94>ԍ<86><82>̒<E8><8B>`
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         "../../INCLUDE/PROTOCOLS.H"
digest.c:57:5: warning: implicitly declaring library function 'strcpy' with type
      'char *(char *, const char *)' [-Wimplicit-function-declaration]
    strcpy(hn,nametable[i].hostname) ; 
    ^
digest.c:57:5: note: include the header <string.h> or explicitly provide a declaration for 'strcpy'
digest.c:61:5: warning: implicitly declaring library function 'exit' with type 'void (int)
      __attribute__((noreturn))' [-Wimplicit-function-declaration]
    exit(1);   
    ^
digest.c:61:5: note: include the header <stdlib.h> or explicitly provide a declaration for 'exit'
digest.c:109:6: warning: implicitly declaring library function 'strlen' with type
      'unsigned long (const char *)' [-Wimplicit-function-declaration]
  if(strlen(hn) > 0) // <95>ϊ<B7><82>ł<AB><82><BD><8F>ꍇ
     ^
digest.c:109:6: note: include the header <string.h> or explicitly provide a declaration for
      'strlen'
4 warnings generated.

232

修正箇所は上記と同じ。

go232.png

振り返り

わかったこと

1.使っている関数のヘッダファイルはこまめに読み込む

#include <string.h>
#include <stdlib.h>
  1. tcp.hでの構造定義の変数の変化
    対応関係を定義。
#define source th_sport /// add for error
#define dest th_dport /// add for error 
  1. tclの起動の方法
    wishをつけて呼ぶ。
./tcp $1 $2 | wish ./graph.tcl

わからなかったこと

  1. 警告を抑制する方法
    -Wimplicit-function-declaration

よかったこと

午前中にC、午後にtclがそれぞれ解決し、1日で対応が取れた。

あらためること

資料の整理方法。要素技術のどれかを知らない人にも入り口となるURLを記載する。

付録1 参考文献

Xcodeの導入

Xcodeインストール
https://qiita.com/tagosaku324/items/44e4cfa4a0480311c9b2

x-codeインストール
https://qiita.com/rabbit1013/items/edd6544653bb87858cde

MacにXcodeをインストールする
https://qiita.com/NolikaOhkawa/items/1bc2960f513d3d6a8882

tcl/tk

Macintoshでtcl/tk そのままでも動くのに罠5つに嵌った
https://qiita.com/kaizen_nagoya/items/0bebb8e5a757a7d1b9f2

tcl
https://researchmap.jp/jofbuvwg5-1826017/#_1826017

Wish Tcl/Tk 入門
http://flex.phys.tohoku.ac.jp/texi/wish-nyumon/how-to-run.html

今から始めるTcl/Tk
http://d.hatena.ne.jp/jwakaya/

ファイル tclは
http://www.interq.or.jp/japan/s-imai/tcltk/file.html

MacintoshにはTcl/Tkが標準でインストールさ れている
http://www.tkderm.org/doc/tkdman-1_4/node11.html

<この項は書きかけです。順次追記します。>

付録2 libpcapの導入

brew install pcap
Updating Homebrew...

Error: No available formula with the name "pcap" 
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching for similarly named formulae...
==> Searching local taps...
This similarly named formula was found:
libpcap ✔
To install it, run:
  brew install libpcap ✔
==> Searching taps...
==> Searching taps on GitHub...
Error: No formulae found in taps.

pcapではなくlibpcapを導入したらという助言。-lpcapという指定で必要なライブラリはlibpcapだとわかる。

$ brew install libpcap

付録3 エラー集

システムの設定によってでるエラーを集めました。

E 1.1 make/Makefile

make
make: *** No targets specified and no makefile found.  Stop.

Makefileの記述を見て、そこを一つずつ実施。Makefile中の.oの記述は.cに変更

cc ether.c -o ether -lpcap

E 1.2 device name: eth0, en0

$./go211 eth0
eth0: No such device exists (BIOCSETIF failed: Device not configured)

eth0という名前ではない。

$ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
	options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
	inet 127.0.0.1 netmask 0xff000000 
	inet6 ::1 prefixlen 128 
	inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
	nd6 options=201<PERFORMNUD,DAD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
	options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
	inet 192.168.2.38 netmask 0xffffff00 broadcast 192.168.2.255
	nd6 options=201<PERFORMNUD,DAD>

en0なんだ。

$./go211 en0

動く。

E 1.3 tcl/tk script

$ ./go211 en0
./graph.tcl: line 9: proc: command not found
./graph.tcl: line 10: global: command not found
./graph.tcl: line 11: global: command not found
./graph.tcl: line 12: global: command not found
./graph.tcl: line 15: foreach: command not found
./graph.tcl: line 16: syntax error near unexpected token `$i'
./graph.tcl: line 16: `        if {$show_flag($i)} {'

go211の中身確認

$ cat go211
./ether $1 | ./graph.tcl 

./etherと./graphが動作するシェルは別。tcl/tkのshellを足してみる。

$ vi go211
./ether $1 | wish ./graph.tcl 

動く。

E 1.4 device permission

 ./ether en0
en0: You don't have permission to capture on that device ((cannot open BPF device) /dev/bpf0: Permission denied)

え、システム管理者なのに駄目なの?何かの設定の都合らしい。

sudo ./ether en0
Password:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
351 0 0 0 0 0 0 351

なるほど。

E 1.5 brew管理者じゃない

brew install tcl-tk
Error: Can't create update lock in /usr/local/var/homebrew/locks!
Fix permissions by running:
  sudo chown -R $(whoami) /usr/local/var/homebrew

言われた通りsudoすればいいとは限らない。brew管理者を"brew_manager"と決めていれば

$ su brew_manager
passwor:

brew管理者になってから実行。

E 1.6 spell miss

$ brew install tkl-tk
Error: No available formula with the name "tkl-tk" 
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching for similarly named formulae...
==> Searching local taps...
Error: No similarly named formulae found.
==> Searching taps...
==> Searching taps on GitHub...
Error: No formulae found in taps.

泣)
うまくいかないときは、疲れる。
疲れると打ち間違い増える。
エラーが増える。
さらに疲れる。
悪循環。
記録をとったらやめる。
疲れているので大事なとこを記録忘れる。
翌日うまくいない。
泣)

付録4 不必要だった準備

$ brew install libpcap
$ brew cask install xquartz
$ brew install tcl-tk
...
 echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.bash_profile
...

PATHの設定が必要らしい。

$ cd /usr/bin
$ls -al wish*
lrwxr-xr-x  1 root  wheel    7 12 28  2016 wish -> wish8.5
-r-xr-xr-x  1 root  wheel  127  7 31  2016 wish8.4
-r-xr-xr-x  1 root  wheel  127  7 31  2016 wish8.5
$ ls -al /usr/local/opt/tcl-tk/bin/wish*
lrwxr-xr-x  1 brew_manager  admin      7 12 22 03:16 /usr/local/opt/tcl-tk/bin/wish -> wish8.6
-r-xr-xr-x  1 brew_manager  admin  21248  2 14 08:59 /usr/local/opt/tcl-tk/bin/wish8.6

wishはtcl/tkのshellです。brewで導入した結果3つのwishが入っていることになりました。最新のtcl/tk(wish)が起動するようにするためには。

$ echo 'export PATH="/usr/local/opt/tcl-tk/bin:$PATH"' >> ~/.bash_profile
$ source ~/.bash_profile

PATHの確認

$ printenv
...
PATH=/usr/local/opt/tcl-tk/bin:/usr/local/arm-unknown-linux-gnueabihf/bin:/usr/local/bin:/Users/Administrator/Downloads/gcc-arm-none-eabi-5_4-2016q3/bin:/usr/local/Cellar/gcc/7.1.0/bin:/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin:usr/local/opt/libpcap/bin:/Users/administrator/.pyenv/shims:/usr/local/opt/libpcap/bin:/usr/local/arm-unknown-linux-gnueabihf/bin:/usr/local/bin:/Users/Administrator/Downloads/gcc-arm-none-eabi-5_4-2016q3/bin:/usr/local/Cellar/gcc/7.1.0/bin:/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin:/Users/administrator/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/TeX/texbin:/Applications/Wireshark.app/Contents/MacOS
...

「/usr/local/opt/tcl-tk/bin」が「/usr/bin」より先にないと古いwishが起動します。

sourceコマンドを実行する場合には、もう一つコマンドの窓を開けておくと良い。PATHの設定を間違えると、以降ほとんどのコマンドを受け付けなくなり、~./bash_profileの修正が難しくなる場合がある。もう一つコマンドが開いていればそちらで修正が可能になる。

現在の版

tcl-tk 8.6.8
xquartz 2.7.11
libpcap 1.8.1
perl 5.26.1
cc  Apple LLVM version 9.0.0 (clang-900.0.39.2)Target: x86_64-apple-darwin16.7.0

wish(tcl/tk)の版の確認方法

Get the version of TCL from the command-line?
https://stackoverflow.com/questions/9200108/get-the-version-of-tcl-from-the-command-line
使うのはtclshではなくwishなので。

echo 'puts $tcl_version;exit 0' | wish
8.6

付録5 BSD版を解凍しても一緒だった

結果として、まったく同じ修正で、tclが動かなかったのも一緒。
スクリプトにwishを足せば良い。

$ cd ch2/2.1
$ make
cc    -c -o ether.o ether.c
ether.c:78:3: warning: implicit declaration of function 'alarm' is invalid in C99
      [-Wimplicit-function-declaration]
  alarm(1);// <82>P<95>b<82>ɃZ<83>b<83>g
  ^
ether.c:104:5: warning: implicitly declaring library function 'exit' with type 'void (int)
      __attribute__((noreturn))' [-Wimplicit-function-declaration]
    exit(0);
    ^
ether.c:104:5: note: include the header <stdlib.h> or explicitly provide a declaration for 'exit'
2 warnings generated.
cc ether.o -o ether -lpcap

$ cat makefile
ether : ether.o
	cc ether.o -o ether -lpcap
tcp : tcp.o
	cc tcp.o -o tcp -lpcap
clean :
	rm -f *.o core a.out

実行して見る

./go211 en0
./graph.tcl: line 9: proc: command not found
./graph.tcl: line 10: global: command not found
./graph.tcl: line 11: global: command not found
./graph.tcl: line 12: global: command not found
./graph.tcl: line 15: foreach: command not found
./graph.tcl: line 16: syntax error near unexpected token `$i'
./graph.tcl: line 16: `        if {$show_flag($i)} {'

tcl以外を動かす。

$ ./ether en0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
60 0 0 0 0 0 0 60
0 0 0 0 0 0 0 0

tcp.cの方

 make tcp
cc    -c -o tcp.o tcp.c
tcp.c:29:10: fatal error: 'netinet/protocols.h' file not found
#include <netinet/protocols.h>//<83>v<83><8D><83>g<83>R<83><8B><94>ԍ<86><82>̒<E8><8B>`
         ^~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [tcp.o] Error 1

さっきと同じ。

#include <netinet/protocols.h>

#include "../../include/protocols.h"

に変更。
tcp.cの先頭の方に、次の行を追加。

#define source th_sport /// add for error
#define dest th_dport /// add for error 

error対応

make tcp
cc    -c -o tcp.o tcp.c
tcp.c:29:10: warning: non-portable path to file '"../../INCLUDE/PROTOCOLS.H"'; specified path
      differs in case from file name on disk [-Wnonportable-include-path]
#include "../../include/protocols.h"//<83>v<83><8D><83>g<83>R<83><8B><94>ԍ<86><82>̒<E8><8B>`
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         "../../INCLUDE/PROTOCOLS.H"
tcp.c:65:7: warning: implicitly declaring library function 'strcpy' with type
      'char *(char *, const char *)' [-Wimplicit-function-declaration]
      strcpy(protocol,"tcp") ;
      ^
tcp.c:65:7: note: include the header <string.h> or explicitly provide a declaration for 'strcpy'
tcp.c:83:3: warning: implicit declaration of function 'alarm' is invalid in C99
      [-Wimplicit-function-declaration]
  alarm(1);// <82>P<95>b<82>ɃZ<83>b<83>g
  ^
tcp.c:105:5: warning: implicitly declaring library function 'exit' with type 'void (int)
      __attribute__((noreturn))' [-Wimplicit-function-declaration]
    exit(0);
    ^
tcp.c:105:5: note: include the header <stdlib.h> or explicitly provide a declaration for 'exit'
tcp.c:109:6: warning: implicitly declaring library function 'isdigit' with type 'int (int)'
      [-Wimplicit-function-declaration]
  if(isdigit(argv[2][0])) sscanf(argv[2],"%d",&portno) ;
     ^
tcp.c:109:6: note: include the header <ctype.h> or explicitly provide a declaration for 'isdigit'
tcp.c:109:47: warning: format specifies type 'int *' but the argument has type 'u_short *'
      (aka 'unsigned short *') [-Wformat]
  if(isdigit(argv[2][0])) sscanf(argv[2],"%d",&portno) ;
                                          ~~  ^~~~~~~
                                          %hd
6 warnings generated.
cc tcp.o -o tcp -lpcap

警告だけ。

tcl以外の部分の動作確認。

./tcp en0 http
0 0
530 0
0 0
1128 0
179 0
0 0

参考文献

情報処理技術者試験 ネットワークスペシャリストに合格 
https://qiita.com/kaizen_nagoya/items/407857392ca5c5677ee4

通信エミュレータの移植
https://qiita.com/kaizen_nagoya/items/ce505bbea4229b83e93b

第二章JAVAによるネットワークプログラミング phttpd 三箇所でException収集
https://qiita.com/kaizen_nagoya/items/d1db78d3db0e90c0ff9f

「基礎からわかるTCP/IP JAVAネットワークプログラミング」Eclipseでコンパイル。Wiresharkでデバッグ。
https://qiita.com/kaizen_nagoya/items/14519536b827fdadb32e

プログラマが知っていると良い「公序良俗」
https://qiita.com/kaizen_nagoya/items/9fe7c0dfac2fbd77a945

通信記事100
https://qiita.com/kaizen_nagoya/items/1d67de5e1cd207b05ef7

Ethernet 記事一覧 Ethernet(0)
https://qiita.com/kaizen_nagoya/items/88d35e99f74aefc98794

Wireshark 一覧 wireshark(0)、Ethernet(48)
https://qiita.com/kaizen_nagoya/items/fbed841f61875c4731d0

線網(Wi-Fi)空中線(antenna)(0) 記事一覧(118/300目標)
https://qiita.com/kaizen_nagoya/items/5e5464ac2b24bd4cd001

資料集 [あなたもdocker私もdocker一覧] docker(0) to 166+61=227
https://qiita.com/kaizen_nagoya/items/45699eefd62677f69c1d

Error一覧 error(0)
https://qiita.com/kaizen_nagoya/items/48b6cbc8d68eae2c42b8

<この記事は個人の過去の経験に基づく個人の感想です。現在所属する組織、業務とは関係がありません。>
This article is an individual impression based on the individual's experience. It has nothing to do with the organization or business to which I currently belong.

文書履歴(document history)

ver. 0.01 初稿  20180214
ver. 0.02 ありがとう追記 20230513

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0