Skip to content

Commit

Permalink
add books
Browse files Browse the repository at this point in the history
  • Loading branch information
mymmsc committed Jan 2, 2016
1 parent 9137e5b commit b62f67a
Show file tree
Hide file tree
Showing 459 changed files with 60,671 additions and 0 deletions.
Binary file added Network/.DS_Store
Binary file not shown.
Binary file added Network/Socket 编程.pdf
Binary file not shown.
Binary file added Network/TCP-IP详解卷1 -- 协议.pdf
Binary file not shown.
Binary file added Network/TCP-IP详解卷2 -- 实现.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Network/UNIX网络编程 Stevens/unpv12e.tar.gz
Binary file not shown.
Binary file added Network/UNIX网络编程 Stevens/unpv22e.tar.gz
Binary file not shown.
242 changes: 242 additions & 0 deletions Network/Unix网络编程技术与分析/PRG12_1.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// File: prg12_1.c
#include <iostream.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <arpa/inet.h> /* inet_ntoa */
#include <unistd.h> /* close */
#include <stdio.h> /* Basic I/O routines */
#include <sys/types.h> /* standard system types */
#include <netinet/in.h> /* Internet address structures */
#include <sys/socket.h> /* socket interface functions */
#include <netdb.h> /* host to IP resolution */
#include <netinet/in_systm.h>
#include <netinet/ip.h> /* ICMP */
#include <netinet/ip_icmp.h> /* ICMP */

#define ICMPHEAD 8 // ICMP packet header's length
#define MAXICMPLEN 200

/****************
class: RawSock
******************/
class RawSock
{
public:
int sock;
int error;
RawSock(int protocol =0);
virtual ~RawSock();
int send(const void* msg, int msglen,sockaddr* addr,unsigned int len);
int send(const void* msg, int msglen,char* addr);
int receive(void* buf,int buflen,sockaddr* from,int* len);
int Error() {return error;}
};

class ICMP: public RawSock
{
public:
struct icmp *packet;
int max_len;
int length;

ushort_t checksum(ushort_t *addr,int len);
ICMP();
ICMP(int len);
~ICMP();
int send_icmp(char *to, void* buf,int len);
int recv_icmp(sockaddr* from);
void setCode(int c) { packet->icmp_code = c;}
void setId(int i) { packet->icmp_id = i; }
void setSeq(int s) { packet->icmp_seq = s;}
void setType(int t) { packet->icmp_type = t;}
};

RawSock::RawSock(int protocol = 0) {
sock = socket(AF_INET, SOCK_RAW,protocol);
setuid(getuid());
if (sock == -1) error= 1;
else error = 0;
}

RawSock::~RawSock(){
close(sock);
}

int RawSock::send(const void* msg,int msglen,sockaddr* to, unsigned int len) {
if (error) return -1;
int length = sendto(sock,msg,msglen,0,(const sockaddr*)to,len);
if (length == -1) {
error = 2;
return -1;
}
return length;
}

int RawSock::send(const void* msg,int msglen,char* hostname) {
sockaddr_in sin; // Sock Internet address
if (error) return -1;
if(hostname) {
hostent *hostnm = gethostbyname(hostname);
if(hostnm == (struct hostent *) 0) {
return -1;
}
sin.sin_addr.s_addr = *((unsigned long *)hostnm->h_addr);
}
else
return -1;
sin.sin_family = AF_INET;

return send(msg,msglen,(sockaddr *)&sin, sizeof(sin));
}

int RawSock::receive(void* buf,int buflen,sockaddr* from,int* len)
{
if (error) return -1;
while (1) {
int length = recvfrom(sock,buf,buflen,0,from,len);
if (length == -1)
if (errno == EINTR) continue;
else {
error = 3;
return -1;
}
return length;
}
}

/****************
class: ICMP
******************/
ICMP::ICMP(): RawSock(IPPROTO_ICMP)
{
max_len = MAXICMPLEN;
packet = (icmp*) new char[max_len];

packet->icmp_code = 0;
packet->icmp_id = 0;
packet->icmp_seq = 0;
packet->icmp_type = ICMP_ECHO;
}

ICMP::ICMP(int len): RawSock(IPPROTO_ICMP)
{
max_len = len;
packet = (icmp*) new char[max_len];

packet->icmp_code = 0;
packet->icmp_id = 0;
packet->icmp_seq = 0;
packet->icmp_type = ICMP_ECHO;
}

ICMP::~ICMP()
{
delete[] (char*) packet;
}

ushort_t ICMP::checksum(ushort_t *addr,int len)
{
int nleft = len;
int sum = 0;
unsigned short *w = addr;
unsigned short answer = 0;

while (nleft > 1) {
sum+=*w++;
nleft -= 2;
}

if (nleft == 1) {
*(unsigned char*) (&answer) = *(unsigned char*) w;
sum += answer;
}

sum = (sum >> 16) + (sum & 0xffff);
sum += (sum>>16);
answer = ~sum;
return (answer);
}

int ICMP::send_icmp(char *host, void* buf,int len)
{
memcpy(packet->icmp_data,buf,len);
packet->icmp_cksum = 0;
packet->icmp_cksum = checksum((unsigned short *)packet, ICMPHEAD + 6);

int err = send(packet,MAXICMPLEN,host);
return err;
}

int ICMP::recv_icmp(sockaddr* from)
{
char buf[MAXICMPLEN + 100];
int hlen1, icmplen;
struct ip *ip;
struct icmp *icmp;

if (Error()) return -1;
int addrlen = 0;
int len = receive(buf,MAXICMPLEN+100,from,&addrlen);

if (len == -1) {
cout << "Receiving Failed!\n" ;
return -1;
}

ip = (struct ip *) buf; /* start of IP header */
hlen1 = ip->ip_hl << 2; /* length of IP header */

icmp = (struct icmp *) (buf + hlen1); /* start of ICMP header */
if ( (icmplen = len - hlen1) < 8){
cout << "Receiving Failed!\n" ;
return -1;
}

length = len - hlen1;
memcpy(packet,icmp,length);
return 0;
}

main(int argc, char *argv[])
{
ICMP icmp;
sockaddr from;
char *host;
int count;

if (argc < 2) {
printf("Usage: %s <IP Address> <try_number>\n",argv[0]);
exit(1);
}
if (argc == 2) {
host = argv[1];
count = 5;
}
if (argc == 3) {
host = argv[1];
count = atoi(argv[2]);
}

for (int i = 0; i <= count; i++) {
icmp.setId(getpid());
icmp.setSeq(i);
char* test_data= "abcde";
icmp.send_icmp(host,test_data,strlen(test_data));
}
int num = 1;
while(1) {
if (icmp.recv_icmp(&from) < 0) continue;
if (icmp.packet->icmp_type == ICMP_ECHOREPLY) {
if (icmp.packet->icmp_id == getpid()) {
printf("%d bytes from %s: seq=%u, data=%s\n",
icmp.length, host,icmp.packet->icmp_seq, icmp.packet->icmp_data);
num ++;
if (num > count) break;
}
}
}
}


134 changes: 134 additions & 0 deletions Network/Unix网络编程技术与分析/PRG13_1.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// File: prg13_1.c
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define BUFSIZ 1000
#define PCAP_ERRBUF_SIZE 200
void Display( const u_char * packet, const size_t length );

/* list all packets be filtered */
void my_callback ( u_char * none, const struct pcap_pkthdr * pkthdr, const u_char * packet )
{
Display( ( u_char * )packet, ( size_t )( pkthdr->caplen ) );
return;
}

int main(int argc,char **argv)
{
int i;
char *dev;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
const u_char *packet;
struct pcap_pkthdr hdr;
struct ether_header *eptr;
struct bpf_program fp;
bpf_u_int32 maskp;
bpf_u_int32 netp;

if(argc != 2){
fprintf(stdout,"Usage: %s \"filter program\"\n" ,argv[0]);
return 0;
}

/* grab a device */
dev = pcap_lookupdev(errbuf);
if(dev == NULL) {
fprintf(stderr,"%s\n",errbuf);
exit(1);
}

/* ask pcap for the network address and mask of the device */
pcap_lookupnet(dev,&netp,&maskp,errbuf);

/* open device for reading this time lets set it in promiscuous
* mode so we can monitor traffic to another machine */
descr = pcap_open_live(dev,BUFSIZ,1,-1,errbuf);
if(descr == NULL) {
printf("pcap_open_live(): %s\n",errbuf);
exit(1);
}

/* Lets try and compile the program.. non-optimized */
if(pcap_compile(descr,&fp,argv[1],0,netp) == -1) {
fprintf(stderr,"Error calling pcap_compile\n");
exit(1);
}

/* set the compiled program as the filter */
if(pcap_setfilter(descr,&fp) == -1) {
fprintf(stderr,"Error setting filter\n");
exit(1);
}

/* capture packets */
pcap_loop(descr,-1,my_callback,NULL);

return 0;
}

void Display ( const u_char * packet, const size_t length )
{
u_long offset;
int i, j, k;

printf( "packet [ %lu bytes ]: \n", ( long unsigned int )length );
if ( length <= 0 ) {
return;
}
i = 0;
offset = 0;
for ( k = length / 16; k > 0; k--, offset += 16 ){
printf( "%08X ", ( unsigned int )offset );
for ( j = 0; j < 16; j++, i++ ) {
if ( j == 8 ){
printf( "-%02X", packet[i] );
}
else printf( " %02X", packet[i] );
}
printf( " " );
i -= 16;
for ( j = 0; j < 16; j++, i++ ) {
/* if ( isprint( (int)packet[i] ) ) */
if ( ( packet[i] >= ' ' ) && ( packet[i] <= 255 ) ) {
printf( "%c", packet[i] );
}
else printf( "." );
}
printf( "\n" );
}
k = length - i;
if ( k <= 0 ){
return;
}
printf( "%08X ", ( unsigned int )offset );
for ( j = 0 ; j < k; j++, i++ ){
if ( j == 8 ){
printf( "-%02X", packet[i] );
}
else printf( " %02X", packet[i] );
}
i -= k;
for ( j = 16 - k; j > 0; j-- ) {
printf( " " );
}
printf( " " );
for ( j = 0; j < k; j++, i++ ){
if ( ( packet[i] >= ' ' ) && ( packet[i] <= 255 ) ) {
printf( "%c", packet[i] );
}
else {
printf( "." );
}
}
printf( "\n" );
return;
}



Loading

0 comments on commit b62f67a

Please sign in to comment.