2010년/5월
5월17일 C++(PCAP),API()
뽀얀햄스터
2010. 5. 17. 10:55
Protocol 규약/약속
TCP/IP 4Layer
Ethernet
기본적으로 리눅스에서 제공하는 함수외에 다른 pcap 같은 라이브러리를 사용하여 할경우
뒤에 -lpcap을 붙여줘야 컴파일이된다. 빼주게 되면.. 링킹 에러가 뜬다.
리눅스에서 기본적은 네트워크 정보를 보기위해서 명령어가 ifconfig이다. 그리고 보다시피 물리주소와 논리주소 브로드캐스트주소라든지.. MTU(Maxium Transmission Unit)등 여러가지를 알아 볼수있다.
다음 도스창에서의 ipconfig이다.
pcap_openlive() ..
Linux같은경우. man통해서 거의 모든 정보를 알수 있다.
단.. 영어메뉴얼이다 ㅠ
#include<stdio.h>
#include <pcap/pcap.h>
char errbuf[PCAP_ERRBUF_SIZE];
void HexaView(void *vp,int iSize);
int main()
{
char *NICNAME;
pcap_t *Stphandle;
const u_char *ucData;
struct pcap_pkthdr stInfo;
NICNAME=pcap_lookupdev(errbuf);
if(NULL==NICNAME)
{
printf("%s\n",errbuf);
return 0;
}
printf("%s\n",NICNAME);
Stphandle=pcap_open_live(NICNAME,1500,
1,0,errbuf);
if(NULL==Stphandle)
{
printf("%s\n",errbuf);
return 0;
}
ucData=pcap_next(Stphandle,&stInfo);
HexaView((void*)ucData,stInfo.len);
pcap_close(Stphandle);
return 0;
}
=============================================
============================================================
#include<stdio.h>
#define MAX_FILE_SIZE 1500
void HexaView(void *vp,int iSize)
{
int iaddr=16;
int iCnt;
if(0 == vp)
{
return;
}
if(MAX_FILE_SIZE<iSize)
{
iSize=MAX_FILE_SIZE;
}
printf(" _ADDRESS_ ");
for(iCnt=0;16>iCnt;++iCnt)
{
printf("%02X ",iCnt);
}
for(iCnt=0;16>iCnt;++iCnt)
{
printf("%X",iCnt);
}
putchar('\n');
for(iCnt=0;((16*4)+12)>iCnt;++iCnt)
{
putchar('=');
}
putchar('\n');
for(iCnt=0;iSize>iaddr;iaddr=iaddr+16)
{
printf("0x%08X ",iCnt);
while(iaddr>iCnt)
{
printf("%02X ",*(((unsigned char*)vp)+iCnt));
++iCnt;
}
for(iCnt=iCnt-16;iaddr>iCnt;++iCnt)
{
if( (32> (*(((unsigned char*)vp)+iCnt)) )||
(127<(*(((unsigned char*)vp)+iCnt)) ))
{
printf(".");
}
else
{
printf("%c",*(((unsigned char*)vp)+iCnt));
}
}
printf("\n");
}
}
============================================================
==================================================================================
main.cpp
#include <windows.h>//API함수들의 원형과 사용하는 상수들이 정의 되어 있다.
#include "MsgProc.h"
//전역변수 선언
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LPCTSTR lpszClass = TEXT("First"); //const char * //lpszClass : 윈도우 클래스를 정의하는데 사용
HINSTANCE g_hInst;
typedef struct MESSAGEMAP
{
UINT iMessage;
LRESULT (*lpfnMsgProc)(HWND,WPARAM,LPARAM);
}MESSAGEMAP;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lPSZCmdParam, int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass; //구도체 선언
WndClass.cbClsExtra=0;//특수한 목적에 사용되는 여분의 공간, 사용하지
WndClass.cbWndExtra=0;//않을 때는 0으로 지정한다.
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);//배경색상 설정
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //커서지정
WndClass.hIcon=LoadIcon(NULL, IDI_APPLICATION); //마우스지정
WndClass.hInstance=hInstance; //클래스를 등록하는 프로그램 번호
WndClass.lpfnWndProc=WndProc; //윈도우의 메시지 처리함수를 지정(여기는 WndProc)
WndClass.lpszClassName=lpszClass;//이름지정, 전역변수를 대입하므로 "First"가 들어감
WndClass.lpszMenuName=NULL;//메뉴지정, NULL이므로 생략
WndClass.style=CS_HREDRAW|CS_VREDRAW | CS_DBLCLKS; //윈도우의 스타일 정의
RegisterClass(&WndClass);//이런 이런 특성을 가진 윈도우를 앞으로 사용하겠다고 등록함.
//윈도우를 만들려면 윈도우 클래스를 등록한 후 CreateWindow함수를 호출한다.
//CreateWindow : 메모리에 윈도우 생성
//윈도우 클래스 지정하는 문자열,
hWnd=CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,NULL, (HMENU)NULL, hInstance, NULL);
// 4.화면에 윈도우 표시
ShowWindow(hWnd,nCmdShow);
// 5.사용자로부터의 메시지 처리 루프
while(GetMessage(&Message, NULL, 0, 0)) //메시지 큐에서 메시지 읽어옴
{
TranslateMessage(&Message); //키보드 입력 처리
DispatchMessage(&Message); // 큐에서 꺼낸 메시지를 WndProc함수의 iMessage
}
return (int)Message.wParam; //메시지 루프 종료후 이프로그램을
}
//WndProc : 사용자와 시스템이 보내오는 메시지를 처리
//WinMain 은 초기화 하고 시작시키기만 하므로 모양이 일정
//WndProc은 프로그램의 실질적이고도 고유한 처리를 하는 곳이므로
//CALLBACK : APIENTRY와 마찬가지로 _stdcall로 정의되어 있다
//운영제제가 호출하는 함수는 CALLBACK, LRESULT = 4바이트 long
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM IParam)
{
int i;
static MESSAGEMAP MessageMaps[] =
{
{WM_CREATE,OnCreate},
{WM_COMMAND,OnCommand},
{WM_LBUTTONDOWN,OnLButtonDown},
{WM_PAINT,OnPaint},
{WM_DESTROY,OnDestroy}
};
for(i=0;i<sizeof(MessageMaps)/sizeof(MessageMaps[0]);i++)
{
if(MessageMaps[i].iMessage == iMessage)
{
return (*MessageMaps[i].lpfnMsgProc )(hWnd,wParam, IParam);
}
}
return(DefWindowProc(hWnd, iMessage, wParam, IParam));
}
===================================================================
=========================================================================================
#include "MsgProc.h"
static HBITMAP MyBitmap;
void DrawBitmap(HDC hdc,int x, int y,HBITMAP hBit)
{
int bx,by;
BITMAP bit;
HDC MemDC;
HBITMAP OldBitmap;
MemDC=CreateCompatibleDC(hdc);
OldBitmap=(HBITMAP)SelectObject(MemDC,hBit);
GetObject(hBit,sizeof(BITMAP),&bit);
bx=bit.bmWidth;
by=bit.bmHeight;
BitBlt(hdc,x,y,bx,by,MemDC,0,0,SRCCOPY);
SelectObject(MemDC,OldBitmap);
DeleteDC(MemDC);
}
LRESULT OnCreate(HWND hWnd, WPARAM wParam, LPARAM IParam)
{
MyBitmap=LoadBitmap(g_hInst,MAKEINTRESOURCE(IDB_BITMAP1));
return 0;
}
LRESULT OnCommand(HWND hWnd, WPARAM wParam, LPARAM IParam)
{
return 0;
}
LRESULT OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM IParam)
{
MessageBox(hWnd,TEXT("왼쪽버튼클릭."),TEXT("왼쪽버튼"),MB_OK);
return 0;
}
LRESULT OnPaint(HWND hWnd, WPARAM wParam, LPARAM IParam)
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd,&ps);
DrawBitmap(hdc,10,10,MyBitmap);
EndPaint(hWnd,&ps);
return 0;
}
LRESULT OnDestroy(HWND hWnd, WPARAM wParam, LPARAM IParam)
{
DeleteObject(MyBitmap);
PostQuitMessage(0);
return 0;
}
============================================================
================================================================================
#ifndef __MSGPROC_H_
#define __MSGPROC_H_
#include <windows.h>
#include "resource.h"
void DrawBitmap(HDC hdc,int x, int y,BITMAP hBit);
LRESULT OnCreate(HWND, WPARAM, LPARAM);
LRESULT OnCommand(HWND, WPARAM, LPARAM);
LRESULT OnLButtonDown(HWND, WPARAM, LPARAM);
LRESULT OnPaint(HWND, WPARAM, LPARAM);
LRESULT OnDestroy(HWND, WPARAM, LPARAM);
extern HINSTANCE g_hInst;
#endif
TCP/IP 4Layer
Ethernet
기본적으로 리눅스에서 제공하는 함수외에 다른 pcap 같은 라이브러리를 사용하여 할경우
뒤에 -lpcap을 붙여줘야 컴파일이된다. 빼주게 되면.. 링킹 에러가 뜬다.
리눅스에서 기본적은 네트워크 정보를 보기위해서 명령어가 ifconfig이다. 그리고 보다시피 물리주소와 논리주소 브로드캐스트주소라든지.. MTU(Maxium Transmission Unit)등 여러가지를 알아 볼수있다.
다음 도스창에서의 ipconfig이다.
pcap_openlive() ..
Linux같은경우. man통해서 거의 모든 정보를 알수 있다.
단.. 영어메뉴얼이다 ㅠ
#include<stdio.h>
#include <pcap/pcap.h>
char errbuf[PCAP_ERRBUF_SIZE];
void HexaView(void *vp,int iSize);
int main()
{
char *NICNAME;
pcap_t *Stphandle;
const u_char *ucData;
struct pcap_pkthdr stInfo;
NICNAME=pcap_lookupdev(errbuf);
if(NULL==NICNAME)
{
printf("%s\n",errbuf);
return 0;
}
printf("%s\n",NICNAME);
Stphandle=pcap_open_live(NICNAME,1500,
1,0,errbuf);
if(NULL==Stphandle)
{
printf("%s\n",errbuf);
return 0;
}
ucData=pcap_next(Stphandle,&stInfo);
HexaView((void*)ucData,stInfo.len);
pcap_close(Stphandle);
return 0;
}
=============================================
============================================================
#include<stdio.h>
#define MAX_FILE_SIZE 1500
void HexaView(void *vp,int iSize)
{
int iaddr=16;
int iCnt;
if(0 == vp)
{
return;
}
if(MAX_FILE_SIZE<iSize)
{
iSize=MAX_FILE_SIZE;
}
printf(" _ADDRESS_ ");
for(iCnt=0;16>iCnt;++iCnt)
{
printf("%02X ",iCnt);
}
for(iCnt=0;16>iCnt;++iCnt)
{
printf("%X",iCnt);
}
putchar('\n');
for(iCnt=0;((16*4)+12)>iCnt;++iCnt)
{
putchar('=');
}
putchar('\n');
for(iCnt=0;iSize>iaddr;iaddr=iaddr+16)
{
printf("0x%08X ",iCnt);
while(iaddr>iCnt)
{
printf("%02X ",*(((unsigned char*)vp)+iCnt));
++iCnt;
}
for(iCnt=iCnt-16;iaddr>iCnt;++iCnt)
{
if( (32> (*(((unsigned char*)vp)+iCnt)) )||
(127<(*(((unsigned char*)vp)+iCnt)) ))
{
printf(".");
}
else
{
printf("%c",*(((unsigned char*)vp)+iCnt));
}
}
printf("\n");
}
}
============================================================
==================================================================================
main.cpp
#include <windows.h>//API함수들의 원형과 사용하는 상수들이 정의 되어 있다.
#include "MsgProc.h"
//전역변수 선언
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LPCTSTR lpszClass = TEXT("First"); //const char * //lpszClass : 윈도우 클래스를 정의하는데 사용
HINSTANCE g_hInst;
typedef struct MESSAGEMAP
{
UINT iMessage;
LRESULT (*lpfnMsgProc)(HWND,WPARAM,LPARAM);
}MESSAGEMAP;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lPSZCmdParam, int nCmdShow)
{
HWND hWnd;
MSG Message;
WNDCLASS WndClass; //구도체 선언
WndClass.cbClsExtra=0;//특수한 목적에 사용되는 여분의 공간, 사용하지
WndClass.cbWndExtra=0;//않을 때는 0으로 지정한다.
WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);//배경색상 설정
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW); //커서지정
WndClass.hIcon=LoadIcon(NULL, IDI_APPLICATION); //마우스지정
WndClass.hInstance=hInstance; //클래스를 등록하는 프로그램 번호
WndClass.lpfnWndProc=WndProc; //윈도우의 메시지 처리함수를 지정(여기는 WndProc)
WndClass.lpszClassName=lpszClass;//이름지정, 전역변수를 대입하므로 "First"가 들어감
WndClass.lpszMenuName=NULL;//메뉴지정, NULL이므로 생략
WndClass.style=CS_HREDRAW|CS_VREDRAW | CS_DBLCLKS; //윈도우의 스타일 정의
RegisterClass(&WndClass);//이런 이런 특성을 가진 윈도우를 앞으로 사용하겠다고 등록함.
//윈도우를 만들려면 윈도우 클래스를 등록한 후 CreateWindow함수를 호출한다.
//CreateWindow : 메모리에 윈도우 생성
//윈도우 클래스 지정하는 문자열,
hWnd=CreateWindow(lpszClass, lpszClass, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,NULL, (HMENU)NULL, hInstance, NULL);
// 4.화면에 윈도우 표시
ShowWindow(hWnd,nCmdShow);
// 5.사용자로부터의 메시지 처리 루프
while(GetMessage(&Message, NULL, 0, 0)) //메시지 큐에서 메시지 읽어옴
{
TranslateMessage(&Message); //키보드 입력 처리
DispatchMessage(&Message); // 큐에서 꺼낸 메시지를 WndProc함수의 iMessage
}
return (int)Message.wParam; //메시지 루프 종료후 이프로그램을
}
//WndProc : 사용자와 시스템이 보내오는 메시지를 처리
//WinMain 은 초기화 하고 시작시키기만 하므로 모양이 일정
//WndProc은 프로그램의 실질적이고도 고유한 처리를 하는 곳이므로
//CALLBACK : APIENTRY와 마찬가지로 _stdcall로 정의되어 있다
//운영제제가 호출하는 함수는 CALLBACK, LRESULT = 4바이트 long
LRESULT CALLBACK WndProc(HWND hWnd, UINT iMessage, WPARAM wParam, LPARAM IParam)
{
int i;
static MESSAGEMAP MessageMaps[] =
{
{WM_CREATE,OnCreate},
{WM_COMMAND,OnCommand},
{WM_LBUTTONDOWN,OnLButtonDown},
{WM_PAINT,OnPaint},
{WM_DESTROY,OnDestroy}
};
for(i=0;i<sizeof(MessageMaps)/sizeof(MessageMaps[0]);i++)
{
if(MessageMaps[i].iMessage == iMessage)
{
return (*MessageMaps[i].lpfnMsgProc )(hWnd,wParam, IParam);
}
}
return(DefWindowProc(hWnd, iMessage, wParam, IParam));
}
===================================================================
=========================================================================================
#include "MsgProc.h"
static HBITMAP MyBitmap;
void DrawBitmap(HDC hdc,int x, int y,HBITMAP hBit)
{
int bx,by;
BITMAP bit;
HDC MemDC;
HBITMAP OldBitmap;
MemDC=CreateCompatibleDC(hdc);
OldBitmap=(HBITMAP)SelectObject(MemDC,hBit);
GetObject(hBit,sizeof(BITMAP),&bit);
bx=bit.bmWidth;
by=bit.bmHeight;
BitBlt(hdc,x,y,bx,by,MemDC,0,0,SRCCOPY);
SelectObject(MemDC,OldBitmap);
DeleteDC(MemDC);
}
LRESULT OnCreate(HWND hWnd, WPARAM wParam, LPARAM IParam)
{
MyBitmap=LoadBitmap(g_hInst,MAKEINTRESOURCE(IDB_BITMAP1));
return 0;
}
LRESULT OnCommand(HWND hWnd, WPARAM wParam, LPARAM IParam)
{
return 0;
}
LRESULT OnLButtonDown(HWND hWnd, WPARAM wParam, LPARAM IParam)
{
MessageBox(hWnd,TEXT("왼쪽버튼클릭."),TEXT("왼쪽버튼"),MB_OK);
return 0;
}
LRESULT OnPaint(HWND hWnd, WPARAM wParam, LPARAM IParam)
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd,&ps);
DrawBitmap(hdc,10,10,MyBitmap);
EndPaint(hWnd,&ps);
return 0;
}
LRESULT OnDestroy(HWND hWnd, WPARAM wParam, LPARAM IParam)
{
DeleteObject(MyBitmap);
PostQuitMessage(0);
return 0;
}
============================================================
================================================================================
#ifndef __MSGPROC_H_
#define __MSGPROC_H_
#include <windows.h>
#include "resource.h"
void DrawBitmap(HDC hdc,int x, int y,BITMAP hBit);
LRESULT OnCreate(HWND, WPARAM, LPARAM);
LRESULT OnCommand(HWND, WPARAM, LPARAM);
LRESULT OnLButtonDown(HWND, WPARAM, LPARAM);
LRESULT OnPaint(HWND, WPARAM, LPARAM);
LRESULT OnDestroy(HWND, WPARAM, LPARAM);
extern HINSTANCE g_hInst;
#endif