raws-sample/0040755000000000000000000000000007721357064012147 5ustar rootwheelraws-sample/server/0040755000000000000000000000000007721356460013454 5ustar rootwheelraws-sample/server/Makefile0100644000000000000000000000033607721356353015114 0ustar rootwheel CFLAGS=-g LDFLAGS=-lpthread -lnsl SOBJS=server.o COBJS=client.o s: $(SOBJS) $(CC) -o server $(CFLAGS) $(LDFLAGS) $(SOBJS) c: $(COBJS) $(CC) -o client $(CFLAGS) $(LDFLAGS) $(COBJS) clean: rm -f *.o server client raws-sample/server/client.c0100644000000000000000000000125007721356416015072 0ustar rootwheel/* Murat Balaban murat@enderunix.org */ #include #include #include #include #include #include #include #include #include int main(int argc, char **argv) { int sd; struct sockaddr_in sin; char r[100]; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = inet_addr("127.0.0.1"); sin.sin_port = htons(1342); if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(1); } if (connect(sd, (void *)&sin, sizeof(sin)) < 0) { perror("connect"); exit(1); } read(sd, r, 100); printf(r); close(sd); } raws-sample/server/server.c0100644000000000000000000000206307721356457015132 0ustar rootwheel/* Murat Balaban murat@enderunix.org */ #include #include #include #include #include #include #include #include #include void *thread_run (void *arg) { int sd = *(int *)arg; FILE *fp; printf("before sleep"); sleep(1000); close(sd); pthread_exit(0); } int main(int argc, char **argv) { int ld, *cd, len; pthread_t *tid; struct sockaddr_in sin, cin; memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(33334); if ((ld = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(1); } if (bind(ld, (void *)&sin, sizeof(sin)) < 0) { perror("bind"); exit(1); } if (listen(ld, 128) < 0) { perror("listen"); exit(1); } for ( ; ; ) { len = sizeof(cin); cd = (int *)malloc(sizeof(int)); tid = (pthread_t *)malloc(sizeof(pthread_t)); memset(&cin, 0, sizeof(cin)); *cd = accept(ld, &cin, &len); pthread_create(tid, NULL, thread_run, cd); } } raws-sample/tcp/0040755000000000000000000000000007721356777012747 5ustar rootwheelraws-sample/tcp/tcp.c0100644000000000000000000001411107721356770013665 0ustar rootwheel/* TCP RAW SOCKET EXAMPLE Murat Balaban [murat@enderunix.org] You should've received a copy of BSD-style license with the tarball. See COPYING for copyright info. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include int sd; struct psd_tcp { struct in_addr src; struct in_addr dst; unsigned char pad; unsigned char proto; unsigned short tcp_len; struct tcphdr tcp; }; struct tcphdr_opt { struct tcphdr tcp; int mss; /* unsigned char nop; unsigned char[3] ws; unsigned char[2] nop; unsignet char[10] tstamp; */ }; struct psd_tcp_opt { struct in_addr src; struct in_addr dst; unsigned char pad; unsigned char proto; unsigned short tcp_len; struct tcphdr_opt tcp; }; unsigned short in_cksum(unsigned short *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); } unsigned short in_cksum_tcp(int src, int dst, unsigned short *addr, int len) { struct psd_tcp buf; u_short ans; memset(&buf, 0, sizeof(buf)); buf.src.s_addr = src; buf.dst.s_addr = dst; buf.pad = 0; buf.proto = IPPROTO_TCP; buf.tcp_len = htons(len); memcpy(&(buf.tcp), addr, len); ans = in_cksum((unsigned short *)&buf, 12 + len); return (ans); } void send_syn_ack(int s_seq) { struct ip ip; struct tcphdr tcp; const int on = 1; struct sockaddr_in sin; u_char *packet; packet = (u_char *)malloc(60); ip.ip_hl = 0x5; ip.ip_v = 0x4; ip.ip_tos = 0x0; ip.ip_len = sizeof(struct ip) + sizeof(struct tcphdr); ip.ip_id = htons(12831); ip.ip_off = 0x0; ip.ip_ttl = 64; ip.ip_p = IPPROTO_TCP; ip.ip_sum = 0x0; ip.ip_src.s_addr = inet_addr("172.17.14.90"); ip.ip_dst.s_addr = inet_addr("172.16.1.204"); ip.ip_sum = in_cksum((unsigned short *)&ip, sizeof(ip)); memcpy(packet, &ip, sizeof(ip)); tcp.th_sport = htons(3333); tcp.th_dport = htons(33334); tcp.th_seq = htonl(0x131123 + 1); tcp.th_ack = htonl(s_seq + 1); tcp.th_off = sizeof(struct tcphdr) / 4; tcp.th_flags = TH_ACK; tcp.th_win = htons(32768); tcp.th_sum = 0; tcp.th_sum = in_cksum_tcp(ip.ip_src.s_addr, ip.ip_dst.s_addr, (unsigned short *)&tcp, sizeof(tcp)); memcpy((packet + sizeof(ip)), &tcp, sizeof(tcp)); memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = ip.ip_dst.s_addr; if (sendto(sd, packet, 60, 0, (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0) { perror("sendto"); exit(1); } } void send_syn() { struct ip ip; struct tcphdr tcp; const int on = 1; struct sockaddr_in sin; u_char *packet; packet = (u_char *)malloc(60); ip.ip_hl = 0x5; ip.ip_v = 0x4; ip.ip_tos = 0x0; ip.ip_len = sizeof(struct ip) + sizeof(struct tcphdr); ip.ip_id = htons(12830); ip.ip_off = 0x0; ip.ip_ttl = 64; ip.ip_p = IPPROTO_TCP; ip.ip_sum = 0x0; ip.ip_src.s_addr = inet_addr("172.17.14.90"); ip.ip_dst.s_addr = inet_addr("172.16.1.204"); ip.ip_sum = in_cksum((unsigned short *)&ip, sizeof(ip)); memcpy(packet, &ip, sizeof(ip)); tcp.th_sport = htons(3333); tcp.th_dport = htons(33334); tcp.th_seq = htonl(0x131123); tcp.th_off = sizeof(struct tcphdr) / 4; tcp.th_flags = TH_SYN; tcp.th_win = htons(32768); tcp.th_sum = 0; tcp.th_sum = in_cksum_tcp(ip.ip_src.s_addr, ip.ip_dst.s_addr, (unsigned short *)&tcp, sizeof(tcp)); memcpy((packet + sizeof(ip)), &tcp, sizeof(tcp)); memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = ip.ip_dst.s_addr; if (sendto(sd, packet, 60, 0, (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0) { perror("sendto"); exit(1); } } void *run(void *arg) { struct ip ip; struct tcphdr tcp; const int on = 1; struct sockaddr_in sin; if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) { perror("raw socket"); exit(1); } if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) { perror("setsockopt"); exit(1); } send_syn(sd); } void raw_packet_receiver(u_char *udata, const struct pcap_pkthdr *pkthdr, const u_char *packet) { struct ip *ip; struct tcphdr *tcp; u_char *ptr; int l1_len = (int)udata; int s_seq; ip = (struct ip *)(packet + l1_len); tcp = (struct tcphdr *)(packet + l1_len + sizeof(struct ip)); printf("%d\n", l1_len); printf("a packet came, ack is: %d\n", ntohl(tcp->th_ack)); printf("a packet came, seq is: %u\n", ntohl(tcp->th_seq)); s_seq = ntohl(tcp->th_seq); send_syn_ack(s_seq); sleep(100); } void *pth_capture_run(void *arg) { pcap_t *pd; char *filter = "dst host 172.17.14.90 and ip"; char *dev = "fxp0"; char errbuf[PCAP_ERRBUF_SIZE]; bpf_u_int32 netp; bpf_u_int32 maskp; struct bpf_program fprog; /* Filter Program */ int dl = 0, dl_len = 0; if ((pd = pcap_open_live(dev, 1514, 1, 500, errbuf)) == NULL) { fprintf(stderr, "cannot open device %s: %s\n", dev, errbuf); exit(1); } pcap_lookupnet(dev, &netp, &maskp, errbuf); pcap_compile(pd, &fprog, filter, 0, netp); if (pcap_setfilter(pd, &fprog) == -1) { fprintf(stderr, "cannot set pcap filter %s: %s\n", filter, errbuf); exit(1); } pcap_freecode(&fprog); dl = pcap_datalink(pd); switch(dl) { case 1: dl_len = 14; break; default: dl_len = 14; break; } if (pcap_loop(pd, -1, raw_packet_receiver, (u_char *)dl_len) < 0) { fprintf(stderr, "cannot get raw packet: %s\n", pcap_geterr(pd)); exit(1); } } int main(int argc, char **argv) { pthread_t tid_pr; if (pthread_create(&tid_pr, NULL, pth_capture_run, NULL) != 0) { fprintf(stderr, "cannot create raw packet reader: %s\n", strerror(errno)); exit(1); } printf("raw packet reader created, waiting 1 seconds for packet reader thread to settle down...\n"); sleep(1); run(NULL); pthread_join(tid_pr, NULL); return 0; } raws-sample/tcp/Makefile0100644000000000000000000000021507721347251014365 0ustar rootwheelOBJS=tcp.o LDFLAGS= LIBS=-pthread -lpcap CFLAGS=-g all: $(OBJS) $(CC) -o tcp $(CFLAGS) $(LDFLAGS) tcp.o $(LIBS) clean: rm -f tcp $(OBJS) raws-sample/icmp.c0100644000000000000000000000417007721357010013231 0ustar rootwheel/* ICMP RAW SOCKET EXAMPLE Murat Balaban [murat@enderunix.org] You should've received a copy of BSD-style license with the tarball. See COPYING for copyright info. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include unsigned short in_cksum(unsigned short *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); } void *run(void *arg) { struct ip ip; struct udphdr udp; struct icmp icmp; int sd; const int on = 1; struct sockaddr_in sin; u_char *packet; packet = (u_char *)malloc(60); ip.ip_hl = 0x5; ip.ip_v = 0x4; ip.ip_tos = 0x0; ip.ip_len = 60; ip.ip_id = htons(12830); ip.ip_off = 0x0; ip.ip_ttl = 64; ip.ip_p = IPPROTO_ICMP; ip.ip_sum = 0x0; ip.ip_src.s_addr = inet_addr("172.17.14.174"); ip.ip_dst.s_addr = inet_addr("172.17.14.169"); ip.ip_sum = in_cksum((unsigned short *)&ip, sizeof(ip)); memcpy(packet, &ip, sizeof(ip)); icmp.icmp_type = ICMP_ECHO; icmp.icmp_code = 0; icmp.icmp_id = 1000; icmp.icmp_seq = 0; icmp.icmp_cksum = 0; icmp.icmp_cksum = in_cksum((unsigned short *)&icmp, 8); memcpy(packet + 20, &icmp, 8); if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) { perror("raw socket"); exit(1); } if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) { perror("setsockopt"); exit(1); } memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = ip.ip_dst.s_addr; if (sendto(sd, packet, 60, 0, (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0) { perror("sendto"); exit(1); } } int main(int argc, char **argv) { run(NULL); return 0; } raws-sample/tcp_syn.c0100644000000000000000000000534107721357050013765 0ustar rootwheel/* TCP RAW SOCKET EXAMPLE Murat Balaban [murat@enderunix.org] You should've received a copy of BSD-style license with the tarball. See COPYING for copyright info. */ #include #include #include #include #include #include #include #include #include #include #include #include #include struct psd_tcp { struct in_addr src; struct in_addr dst; unsigned char pad; unsigned char proto; unsigned short tcp_len; struct tcphdr tcp; }; unsigned short in_cksum(unsigned short *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); } unsigned short in_cksum_tcp(int src, int dst, unsigned short *addr, int len) { struct psd_tcp buf; memset(&buf, 0, sizeof(buf)); buf.src.s_addr = src; buf.dst.s_addr = dst; buf.pad = 0; buf.proto = IPPROTO_TCP; buf.tcp_len = htons(len); memcpy(&(buf.tcp), addr, len); return in_cksum((unsigned short *)&buf, 12 + len); } void *run(void *arg) { struct ip ip; struct tcphdr tcp; int sd; const int on = 1; struct sockaddr_in sin; u_char *packet; packet = (u_char *)malloc(60); ip.ip_hl = 0x5; ip.ip_v = 0x4; ip.ip_tos = 0x0; ip.ip_len = sizeof(struct ip) + sizeof(struct tcphdr); ip.ip_id = htons(12830); ip.ip_off = 0x0; ip.ip_ttl = 64; ip.ip_p = IPPROTO_TCP; ip.ip_sum = 0x0; ip.ip_src.s_addr = inet_addr("172.17.14.169"); ip.ip_dst.s_addr = inet_addr("172.16.1.204"); ip.ip_sum = in_cksum((unsigned short *)&ip, sizeof(ip)); memcpy(packet, &ip, sizeof(ip)); tcp.th_sport = htons(3333); tcp.th_dport = htons(33333); tcp.th_seq = htonl(0x131123); tcp.th_off = sizeof(struct tcphdr) / 4; tcp.th_flags = TH_SYN; tcp.th_win = htons(2048); tcp.th_sum = 0; tcp.th_sum = in_cksum_tcp(ip.ip_src.s_addr, ip.ip_dst.s_addr, (unsigned short *)&tcp, sizeof(tcp)); memcpy((packet + sizeof(ip)), &tcp, sizeof(tcp)); if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) { perror("raw socket"); exit(1); } if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) { perror("setsockopt"); exit(1); } memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = ip.ip_dst.s_addr; if (sendto(sd, packet, 60, 0, (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0) { perror("sendto"); exit(1); } } int main(int argc, char **argv) { run(NULL); return 0; } raws-sample/COPYING0100644000000000000000000000346407721356551013206 0ustar rootwheel Copyright (c) 2002 , Murat Balaban All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of Murat Balaban nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Fri Aug 22 12:07:30 EEST 2003 raws-sample/Makefile0100644000000000000000000000075507721355064013611 0ustar rootwheel# # # RAW SOCKETS TUTORIAL SAMPLE CODE Makefile # # See COPYING for copying and copyright information # This source code is provided for only educational purposes; # Author cannot be held liable for the consequences. # OBJS=udp.o icmp.o tcp_syn.o LDFLAGS= LIBS=-pthread CFLAGS=-g all: $(OBJS) $(CC) -o udp $(CFLAGS) $(LDFLAGS) udp.o $(LIBS) $(CC) -o icmp $(CFLAGS) $(LDFLAGS) icmp.o $(LIBS) $(CC) -o tcp_syn $(CFLAGS) $(LDFLAGS) tcp_syn.o $(LIBS) clean: rm -f udp icmp tcp_syn $(OBJS) raws-sample/udp.c0100644000000000000000000000514607721357056013107 0ustar rootwheel/* UDP RAW SOCKET EXAMPLE Murat Balaban [murat@enderunix.org] You should've received a copy of BSD-style license with the tarball. See COPYING for copyright info. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct psd_udp { struct in_addr src; struct in_addr dst; unsigned char pad; unsigned char proto; unsigned short udp_len; struct udphdr udp; }; unsigned short in_cksum(unsigned short *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); } unsigned short in_cksum_udp(int src, int dst, unsigned short *addr, int len) { struct psd_udp buf; memset(&buf, 0, sizeof(buf)); buf.src.s_addr = src; buf.dst.s_addr = dst; buf.pad = 0; buf.proto = IPPROTO_UDP; buf.udp_len = htons(len); memcpy(&(buf.udp), addr, len); return in_cksum((unsigned short *)&buf, 12 + len); } void *run(void *arg) { struct ip ip; struct udphdr udp; int sd; const int on = 1; struct sockaddr_in sin; u_char *packet; packet = (u_char *)malloc(60); ip.ip_hl = 0x5; ip.ip_v = 0x4; ip.ip_tos = 0x0; ip.ip_len = 60; ip.ip_id = htons(12830); ip.ip_off = 0x0; ip.ip_ttl = 64; ip.ip_p = IPPROTO_UDP; ip.ip_sum = 0x0; ip.ip_src.s_addr = inet_addr("172.17.14.174"); ip.ip_dst.s_addr = inet_addr("172.17.14.169"); ip.ip_sum = in_cksum((unsigned short *)&ip, sizeof(ip)); memcpy(packet, &ip, sizeof(ip)); udp.uh_sport = htons(45512); udp.uh_dport = htons(53); udp.uh_ulen = htons(8); udp.uh_sum = 0; udp.uh_sum = in_cksum_udp(ip.ip_src.s_addr, ip.ip_dst.s_addr, (unsigned short *)&udp, sizeof(udp)); memcpy(packet + 20, &udp, sizeof(udp)); if ((sd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) { perror("raw socket"); exit(1); } if (setsockopt(sd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) < 0) { perror("setsockopt"); exit(1); } memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = ip.ip_dst.s_addr; if (sendto(sd, packet, 60, 0, (struct sockaddr *)&sin, sizeof(struct sockaddr)) < 0) { perror("sendto"); exit(1); } } int main(int argc, char **argv) { run(NULL); return 0; }