模拟 Ping
//============================================================================
// Name : raw_myping.cpp
// Author : gaotong
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int s, i;
char buf[400];
struct ip *ip = (struct ip *) buf;
struct icmphdr *icmp = (struct icmphdr *) (ip + 1);
struct hostent *hp, *hp2;
struct sockaddr_in dst;
int offset;
int on;
int num = 100;
if (argc < 3)
{
printf("\nUsage: %s <saddress> <dstaddress> [number]\n", argv[0]);
printf("- saddress is the spoofed source address\n");
printf("- dstaddress is the target\n");
printf("- number is the number of packets to send, 100 is the default\n");
exit(1);
}
/* 如果输入的参数符合要求 */
if (argc == 4)
/* 给发送的包数量赋值 */
num = atoi(argv[3]);
/* Loop based on the packet number */
for (i = 1; i <= num; i++)
{
printf("test\n");
on = 1;
bzero(buf, sizeof(buf));
/* 创建 RAW socket */
if ((s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) {
perror("socket() error");
/* If something wrong, just exit */
exit(1);
}
/* 使用socket options, 告诉系统 我们提供 IP structure */
//Prototype: int setsockopt (int socket, int level, int optname, void *optval, socklen_t optlen)
if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0)
{
perror("setsockopt() for IP_HDRINCL error");
exit(1);
}
/* gethostbyname()返回对应于给定主机名的包含主机名字和地址信息的hostent结构指针。 */
if ((hp = gethostbyname(argv[2])) == NULL) {
if ((ip->ip_dst.s_addr = inet_addr(argv[2])) == -1) {
fprintf(stderr, "%s: Can't resolve, unknown host.\n", argv[2]);
exit(1);
}
} else
bcopy(hp->h_addr_list[0], &ip->ip_dst.s_addr, hp->h_length);
/* The following source address just redundant for target to collect */
if ((hp2 = gethostbyname(argv[1])) == NULL)
{
if ((ip->ip_src.s_addr = inet_addr(argv[1])) == -1)
{
fprintf(stderr, "%s: Can't resolve, unknown host\n", argv[1]);
exit(1);
}
}
else
bcopy(hp2->h_addr_list[0], &ip->ip_src.s_addr, hp->h_length);
printf("Sending to %s from spoofed %s\n", inet_ntoa(ip->ip_dst),argv[1]);
/* Ip structure, check the ip.h */
ip->ip_v = 4;
ip->ip_hl = sizeof *ip >> 2;
ip->ip_tos = 0;
ip->ip_len = htons(sizeof(buf));
ip->ip_id = htons(4321);
ip->ip_off = htons(0);
ip->ip_ttl = 255;
ip->ip_p = 1;
ip->ip_sum = 0; /* Let kernel fills in */
dst.sin_addr = ip->ip_dst;
dst.sin_family = AF_INET;
icmp->type = ICMP_ECHO;
icmp->code = 0;
/* Header checksum */
icmp->checksum = htons(~(ICMP_ECHO << 8));
//for (offset = 0; offset < 65536; offset += (sizeof(buf) - sizeof(*ip)))
//{
ip->ip_off = htons(offset >> 3);
if (offset < 65120)
ip->ip_off |= htons(0x2000);
else
ip->ip_len = htons(418); /* make total 65538 */
/* sending time */
if (sendto(s, buf, sizeof(buf), 0, (struct sockaddr *) &dst,
sizeof(dst)) < 0)
{
fprintf(stderr, "offset %d: ", offset);
perror("sendto() error");
}
else
printf("sendto() is OK.\n");
/* IF offset = 0, define our ICMP structure */
if (offset == 0)
{
icmp->type = 0;
icmp->code = 0;
icmp->checksum = 0;
}
//}
/* close socket */
close(s);
usleep(30000);
}
return 0;
}
分享到:
相关推荐
第六章 Socket编程 1、什么是套接字 套接字是一组用于编写网络程序的API 常见的套接字有: --Windows平台 WinSock --Unix/Linux socket 套接字类型: --stream socket 面向流的一种套接字。 --datagram socket...
利用RawSocket实现简单的Ping程序 ICMP协议 编程语言 C++ 操作系统 Linux(Ubuntu 10.0) 开发环境(可选)Qt4.7.0
在 LINUX 系统环境下通过Raw Socket 编程,设计实现局域网嗅探程序,环境:VMware Workstation 6.0,RED Hat 9.0,vi 编辑器或gedit 文本编辑器,gcc 编译环境,局 域网环境
在linux平台上,用C++ 封装 raw socket套节子编程,包括makefile文件的所有源代码,希望对你有帮助。
一、套接字套接字是为特定网络协议(例如TCP/IP,ICMP/IP,UDP/IP等)套件对上的网络应用程序提供者提供当前可移植标准的对象。它们允许程序接受并进行连接,如发送和接受数据。为了建立通信通道,网络通信的每个...
这是一个RAW套接口的抓包程序,在linux 平台下实现,已经测试通过,是一个原理性的测试程序
在 Linux 操作系统上自定义虚拟的网络接口,对于通信协议验证、网络设计实验有着重要作用。VNI 的发送部分基于 raw socket 实现,接收部分以注册内核 LKM 的形式实现,并且进行了软件运行测试、抓包对比和结果分析。
私有协议用SOCK_RAW实现自定义协议栈使用sock_ Raw实现私有协议栈注意只适用于Linux系统用管理员权限执行如何使用代码对于没有socket编程经验的好兄弟建议先看一下standardUDP /文件夹下如何使用UDP协议实现客户端和...
Unix网络编程卷1,第三版,英文版。大名顶顶的Richard Stevens所写 目录: Copyright Addison-Wesley Professional Computing Series Foreword Preface Introduction Changes from the Second Edition ...
" "名称 " " "实验目的:1、掌握Linux下的TCP客户端基本原理和基本编程方法 " " " "实验内容: " "1、写Linux下TCP服务器套接字程序,程序运行时服务器等待客户的连接,一旦连接 " "成功,则显示客户的IP地址、端口号...
/usr/bin/python# code for linuximport socket#s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)while True: ...
本文以Mandrake Linux 9.1(Kenrel 2.4.21)为例,介绍Linux下的安装和登录命令。 immortality按:请用ctrl+f在本页中查找某一部分的内容或某一命令的用法。 ----------------------------------------------------...
原始数据报套接字(Raw Socket) 提供对网络下层通讯协议(如IP协议)的直接访问,它一般不是提供给普通用户 的,主要用于开发新的协议或用于提取协议较隐蔽的功能。 图2 面向连接的SOCKET编程模型 图3 面向非连接的...
----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...