Top > C/C++あれこれ > PINGします(IPヘッダから自作編)。Windows版

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
-
|
-
|
|
!
|
|
-
|
|
!
!
!
 
 
-
|
|
|
|
|
|
|
|
|
|
|
!
 
 
 
 
-
|
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
 
 
-
|
|
|
|
|
!
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
|
!
 
-
|
|
!
 
-
|
!
 
-
|
-
|
|
!
|
!
 
-
|
|
|
|
|
-
|
|
!
|
|
-
|
!
|
!
 
-
|
|
|
|
|
|
|
|
|
|
|
-
|
|
!
|
|
|
|
!
 
-
|
|
-
|
|
!
|
!
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
!
|
|
|
-
|
|
!
|
!
 
-
|
-
|
|
!
-
|
!
|
|
|
!
 
-
|
|
|
!
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
#include <winsock2.h>  
#include <ws2tcpip.h>
#include <stdio.h>
#include <errno.h>
#include <io.h>
#include <fcntl.h>
 
#define CLOSE_SOCKET(s)    closesocket(s)
typedef unsigned short    ushort;
typedef unsigned char    u_int8_t;
typedef unsigned short  u_int16_t;
typedef unsigned int    u_int32_t;
 
// ICMPヘッダ
struct icmphdr {
  u_int8_t type;        /* message type */
  u_int8_t code;        /* type sub-code */
  u_int16_t checksum;
  union
  {
    struct
    {
      u_int16_t id;
      u_int16_t sequence;
    } echo;         /* echo datagram */
    u_int32_t   gateway;    /* gateway address */
    struct
    {
      u_int16_t __unused;
      u_int16_t mtu;
    } frag;         /* path mtu discovery */
  } un;
};
 
// IPヘッダ
struct iphdr {
    BYTE ihl:4;
    BYTE version:4;
    BYTE tos;
    USHORT tot_len;
    USHORT id;
    USHORT frag_off;
    BYTE ttl;
    BYTE protocol;
    USHORT check;
    ULONG saddr;
    ULONG daddr;
};
 
#define ICMP_ECHO_REPLY 0
#define ICMP_ECHO_REQUEST 8
 
struct IP_ICMP_Headers {
    struct iphdr    stIpHdr;
    struct icmphdr    stIcmpHdr;
};
 
void SetIpHeader(const char* srcAddr, const char* dstAddr, struct iphdr* pIpHdr);
void SetIcmpHeader(struct icmphdr*    pIcmpHdr);
void StartUp();
void CleanUp();
SOCKET Create();
int Read(void* pBuf, int lBufSize, SOCKET dest, struct sockaddr_in* lpSockAddr);
int Write(void* buf, int bufsize, SOCKET dest, struct sockaddr_in* lpSockAddr);
int SetSockAddr(const char* lpszHostName, const char* port, struct sockaddr_in*    lpSockAddr);
int Close(SOCKET socket);
ushort GetIpCheckSum(ushort* data, int size);
const char* N2A(unsigned int addr);
void PrintHeader(const char* title, struct IP_ICMP_Headers* pHeader);
 
 
int main(int argc, char* argv[]) {
 
    struct sockaddr_in        sockaddr;
    struct IP_ICMP_Headers    stRequest;
    struct IP_ICMP_Headers    stReply;
    SOCKET                    dest;
    const char*                srcAddr = "192.168.100.99";
    const char*                dstAddr = "118.67.64.98";
 
    StartUp();
 
    //接続情報の作成
    memset( &sockaddr, 0, sizeof(struct sockaddr_in) );
    SetSockAddr(dstAddr, NULL, &sockaddr);
 
    //ソケットの作成
    dest = Create();
 
    //IP+ICMPパケット作成(Ethernetヘッダは自動的に付加されます)
    memset( &stRequest, 0, sizeof(struct IP_ICMP_Headers) );
    SetIpHeader(srcAddr, dstAddr, &stRequest.stIpHdr);
    SetIcmpHeader(&stRequest.stIcmpHdr);
 
    //送信
    Write(&stRequest, sizeof(struct IP_ICMP_Headers), dest, &sockaddr);
    PrintHeader("Send Data :", &stRequest);
 
    //りぷらい受信(IPヘッダ+ICMPヘッダを受信します)
    memset( &stReply, 0, sizeof(struct IP_ICMP_Headers) );
    Read(&stReply, sizeof(struct IP_ICMP_Headers), dest, &sockaddr);
    PrintHeader("Receive Data :", &stReply);
 
    Close(dest);
    CleanUp();
    return 0;
}
 
//ICMPヘッダを作成します。
void SetIcmpHeader(struct icmphdr*    pIcmpHdr) {
    pIcmpHdr->type = ICMP_ECHO_REQUEST;
    pIcmpHdr->code = 0;
    pIcmpHdr->un.echo.id = 999;
    pIcmpHdr->un.echo.sequence = 0;
    pIcmpHdr->checksum = GetIpCheckSum((ushort*)pIcmpHdr, sizeof(struct icmphdr));
}
 
//IPヘッダを作成します。
//ただし、tot_len(全長)とcheck(チェックサム)は自動的に設定されます。
//saddr(送信元アドレス)を偽装すると、送信エラーになります。
void SetIpHeader(const char* srcAddr, const char* dstAddr, struct iphdr* pIpHdr) {
    pIpHdr->ihl = 0x5;
    pIpHdr->version = 0x4;
    pIpHdr->tos = 0x0;
    //pIpHdr->tot_len = htons(sizeof(struct IP_ICMP_Headers));
    pIpHdr->id = 0x8f8f;
    pIpHdr->frag_off = 0x0;
    pIpHdr->ttl = 0x33;
    pIpHdr->protocol = 0x1;
    pIpHdr->saddr = inet_addr(srcAddr);
    pIpHdr->daddr = inet_addr(dstAddr);
    //pIpHdr->check = GetIpCheckSum((ushort*)pIpHdr, sizeof(struct iphdr));
}
 
void StartUp() {
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2,2), &wsaData);
}
 
void CleanUp() {
    WSACleanup();
}
 
int Close(SOCKET socket) {
    int iRtn = 0;
    if(socket != INVALID_SOCKET){
        shutdown(socket, 2);
        iRtn = CLOSE_SOCKET(socket);
    }
    return(iRtn);
}
 
SOCKET Create(){
 
    SOCKET handle;
    int on = 1;
 
    handle = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
    if(handle==INVALID_SOCKET)    {
        perror("socket error :");
        return 0;
    }
 
    //IPヘッダもアプリケーションで作成しますよ〜ということ。
    if (setsockopt(handle, IPPROTO_IP, IP_HDRINCL, (const char*)&on, sizeof(int)) < 0) {
        perror("setsockopt");
    }
    return handle;
}
 
int SetSockAddr(const char* lpszHostName, const char* lpszPort, struct sockaddr_in*    lpSockAddr) {
 
    struct addrinfo hints;
    struct addrinfo* work;
    int rtn;
    
    memset ((char *)&hints, 0, sizeof(hints));
    hints.ai_socktype = SOCK_RAW;
    hints.ai_family = AF_INET;
    hints.ai_protocol = IPPROTO_IP;
 
    rtn = getaddrinfo(lpszHostName,lpszPort,&hints,&work);
    if( rtn != 0 ){
        printf("getaddrinfo error[%s]\n", gai_strerror(rtn));
        return -1;
    }
    memset(lpSockAddr,0,sizeof(struct sockaddr_in));
    memcpy(lpSockAddr,work->ai_addr,sizeof(struct sockaddr));
    freeaddrinfo(work);
    return 0;
}
 
int Write(void* buf, int bufsize, SOCKET dest, struct sockaddr_in* lpSockAddr) {
    
    int len = sendto(dest, (const char*)buf, bufsize, 0, (struct sockaddr*)lpSockAddr, sizeof(struct sockaddr_in));
    if (len == -1) {
        printf("sendto error:%d", errno);
        return -1;
    }
    return 0;
}
 
int Read(void* pBuf, int lBufSize, SOCKET dest, struct sockaddr_in* lpSockAddr) {
 
    fd_set rd;
    int fromlen;
    int len;
    struct timeval* pTimeval = NULL;
    struct timeval timeout;
 
    timeout.tv_sec  = 30;
    timeout.tv_usec = 0;
    pTimeval = &timeout;
 
    FD_ZERO(&rd);
    FD_SET(dest, &rd);
 
    if(!select((int)dest + 1, &rd, NULL, NULL, pTimeval)) {
        printf("Timeout occured\n");
        return -1;
    }
          
    fromlen = sizeof(struct sockaddr_in);
    len = recvfrom(dest, (char*)pBuf, lBufSize, 0, (struct sockaddr*)lpSockAddr, (socklen_t*)&fromlen);
    if(len < 0) {
        perror("recv error");
        return -1;
    }
    return 0;
}
 
ushort GetIpCheckSum(ushort* data, int size) {
    unsigned long cksum = 0;
    while (size > 1) {
        cksum += *data++;
        size -= sizeof(ushort);
    }
    if (size) {
        cksum += *(unsigned char*)data;
    }
    cksum = (cksum >> 16) + (cksum & 0xffff);
    cksum += (cksum >> 16);
    return (ushort)(~cksum);
}
 
const char* N2A(unsigned int addr) {
    struct in_addr in;
    in.s_addr = addr;
    return inet_ntoa(in);
}
 
void PrintHeader(const char* title, struct IP_ICMP_Headers* pHeader) {
   printf(
            "TITLE %s\n"
            "ihl   %x\n"
            "ver   %x\n"
            "tos   %x\n"
            "len   %x\n"
            "id    %x\n"
            "frag  %x\n"
            "ttl   %x\n"
            "proto %x\n"
            "check %x\n"
            "src   %08x\n"
            "dst   %08x\n"
            "type  %x\n"
            "code  %x\n"
            "check %x\n"
            "id    %x\n"
            "seq   %x\n"
            ,title
            ,pHeader->stIpHdr.ihl
            ,pHeader->stIpHdr.version
            ,pHeader->stIpHdr.tos
            ,pHeader->stIpHdr.tot_len
            ,pHeader->stIpHdr.id
            ,pHeader->stIpHdr.frag_off
            ,pHeader->stIpHdr.ttl
            ,pHeader->stIpHdr.protocol
            ,pHeader->stIpHdr.check
            ,pHeader->stIpHdr.saddr
            ,pHeader->stIpHdr.daddr
            ,pHeader->stIcmpHdr.type
            ,pHeader->stIcmpHdr.code
            ,pHeader->stIcmpHdr.checksum
            ,pHeader->stIcmpHdr.un.echo.id
            ,pHeader->stIcmpHdr.un.echo.sequence
    );
    printf("src[%s]\n", N2A(pHeader->stIpHdr.saddr));
    printf("dst[%s]\n", N2A(pHeader->stIpHdr.daddr));
    printf("\n\n");
 
}

リロード   新規 編集 凍結解除 差分 添付 複製 名前変更   ホーム 一覧 単語検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: 2012-08-08 (水) 23:25:09 (2928d)