Top > C/C++あれこれ > PINGします(IPヘッダから自作編)。
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
294
295
296
297
298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
-
|
-
|
|
!
|
|
-
|
|
!
!
!
 
 
-
|
|
|
|
|
|
|
|
|
|
|
!
 
 
 
 
 
-
|
|
!
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
 
-
|
|
|
|
!
 
-
|
|
|
!
 
-
|
-
|
|
!
|
!
 
-
|
|
|
|
|
-
|
|
!
|
!
 
 
-
|
|
|
|
|
!
 
-
|
|
|
|
|
|
|
|
|
|
|
-
|
|
!
|
|
|
|
!
 
-
|
|
-
|
!
|
!
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
!
|
|
|
-
|
|
!
|
!
 
-
|
-
|
|
!
-
|
!
|
|
|
!
 
-
|
|
|
!
 
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!
 
 
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <errno.h>
#include <io.h>
#include <fcntl.h>
#else
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#endif
 
#ifdef _WIN32
#define CLOSE_SOCKET(s)    closesocket(s)
typedef unsigned short    ushort;
#else
#define CLOSE_SOCKET(s)    close(s)
typedef int SOCKET;
const    int    INVALID_SOCKET = -1;
#endif
 
#ifdef _WIN32
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;
};
#endif
 
#define ICMP_ECHO_REPLY 0
#define ICMP_ECHO_REQUEST 8
 
struct IP_ICMP_Headers {
    struct iphdr    stIpHdr;
    struct icmphdr    stIcmpHdr;
};
 
 
void StartUp();
void CleanUp();
SOCKET Create();
void SetIcmpHeader(struct icmphdr*    pIcmpHdr);
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 icmphdr                stIcmpHdr;
    struct IP_ICMP_Headers        stReply;
    SOCKET                        dest;
 
    StartUp();
 
    //接続情報の作成
    memset( &sockaddr, 0, sizeof(struct sockaddr_in) );
    SetSockAddr("code.nanigac.com", NULL, &sockaddr);
 
    //ソケットの作成
    dest = Create();
    
    //ICMPヘッダの作成
    memset( &stIcmpHdr, 0, sizeof(struct icmphdr) );
    SetIcmpHeader(&stIcmpHdr);
 
    //送信(EthernetヘッダとIPヘッダは自動的に付加されます)
    Write(&stIcmpHdr, sizeof(struct icmphdr), dest, &sockaddr);
 
    //りぷらい受信(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;
}
 
void StartUp() {
#ifdef _WIN32
    WSADATA wsaData;
    WSAStartup(MAKEWORD(2,2), &wsaData);
#endif
}
 
void CleanUp() {
#ifdef _WIN32
    WSACleanup();
#endif
}
 
int Close(SOCKET socket) {
    int iRtn = 0;
    if(socket != INVALID_SOCKET){
        shutdown(socket, 2);
        iRtn = CLOSE_SOCKET(socket);
    }
    return(iRtn);
}
 
SOCKET Create(){
 
    SOCKET handle;
 
    //生ソケットを作成します。プロトコルはICMPを使用します。
    handle = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
    if(handle==INVALID_SOCKET)    {
        perror("socket error :");
        return 0;
    }
    return handle;
}
 
 
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));
}
 
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_ICMP;
 
    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) {
        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 (2923d)