aboutsummaryrefslogtreecommitdiff
path: root/doc/examples/packetmounter.c
diff options
context:
space:
mode:
Diffstat (limited to 'doc/examples/packetmounter.c')
-rw-r--r--doc/examples/packetmounter.c236
1 files changed, 0 insertions, 236 deletions
diff --git a/doc/examples/packetmounter.c b/doc/examples/packetmounter.c
deleted file mode 100644
index 32bc79a1..00000000
--- a/doc/examples/packetmounter.c
+++ /dev/null
@@ -1,236 +0,0 @@
1
2
3#include <stdio.h>
4#include <stdlib.h>
5#include <time.h>
6#include <sys/time.h>
7
8typedef u_int32_t n_time;
9#include <netinet/in.h>
10#include <netinet/ip.h>
11#define __FAVOR_BSD 1
12#include <netinet/tcp.h>
13#include <sys/socket.h>
14#include <netdb.h>
15#include <regex.h>
16#include <ctype.h>
17#include <unistd.h>
18#include <fcntl.h>
19#include <string.h>
20#include <unistd.h>
21#include <signal.h>
22
23#include "web_server.h"
24
25int PORTSSL=443;
26
27unsigned short in_chksum(unsigned short *addr, int len) {
28 register int nleft = len;
29 register int sum = 0;
30 u_short answer = 0;
31
32 while (nleft > 1) {
33 sum += *addr++;
34 nleft -= 2;
35 }
36
37 if (nleft == 1) {
38 *(unsigned char *)(&answer) = *(unsigned char *)addr;
39 sum += answer;
40 }
41 sum = (sum >> 16) + (sum & 0xffff);
42 sum += (sum >> 16);
43 answer = ~sum;
44 return(answer);
45}
46void packetmounter() {
47 char *t;
48 int s;
49 struct sockaddr_in to;
50 int i;
51 int n=10;
52 char buf[128];
53 int proto=1; //icmp default
54 struct ip *ip=(struct ip *)buf;
55 struct tcphdr *tcp=(struct tcphdr *)(buf+sizeof(*ip));
56 char *tmp1=ClientInfo->user;
57 char *tmp2=ClientInfo->pass;;
58 if(!strlen(tmp1) || !strlen(tmp2)) {
59 web_client_HTTPdirective("HTTP/1.1 401 Authorization Required");
60 printf("WWW-Authenticate: Basic realm=\"Packet mounter\"\r\n");
61 printf("Content-type: text/html\r\n\r\n");
62 printf("<BODY bgcolor='EFEFEF'>\r\n");
63 printf("<font color='FF0000'><center>Access denied</center></font>\n");
64 printf("</body>\r\n");
65 return;
66 };
67 if(strcmp(tmp1,"packet") && strcmp(tmp2,"teste")) { // Lame autentification just for test
68 web_client_HTTPdirective("HTTP/1.1 401 Authorization Required");
69 printf("WWW-Authenticate: Basic realm=\"Packet mounter\"\r\n");
70 printf("Content-type: text/html\r\n\r\n");
71 printf("<BODY bgcolor='EFEFEF'>\r\n");
72 printf("<font color='FF0000'><center>Access denied</center></font>\n");
73 printf("</body>\r\n");
74 return;
75 };
76 printf("Content-type: text/html\r\n\r\n");
77 printf("<HTML>\n");
78 printf("<body bgcolor='FFFFCC'>\n");
79 printf("<center>public Packet mounter by <BR><small><TT> Luis Figueiredo (<a href=\"mailto:stdio@netc.pt\">stdio@netc.pt</a>)</tt></small><BR><i><small>Using %s</i></small><HR>\n",_libwebserver_version);
80 t=ClientInfo->Query("proto");
81 printf("Pretended protocol: %s<BR>\n",t);
82 if(strlen(t)) {
83 proto=atoi(t);
84 printf("<form method=post action='/?proto=%d' name=\"ip\">\n",proto);
85 } else {
86 printf("<form method=post action='/' name=\"ip\">\n");
87 };
88 printf("<table border=0><TR><TD>\n");
89 printf("<table border=1>\n");
90 printf("<TR><TD colspan=4 align='center' bgcolor='5555ff'>IP header</TD></TR>\n");
91 t=ClientInfo->Post("ipversion");
92 printf("<TR><TD align='center'>version:<BR><input type=text size=2 maxlength=2 name=ipversion value='%s'></TD>\n",(t)?t:"");
93 if(strlen(t)){ip->ip_v=atoi(t);} else {ip->ip_v=4;} // default
94
95 t=ClientInfo->Post("ipihl");
96 printf("<TD align='center'>ihl:<BR><input type=text size=2 maxlength=2 name=ipihl value='%s'></TD>\n",(t)?t:"");
97 if(strlen(t)){ip->ip_hl=atoi(t);} else {ip->ip_hl=5;}
98
99 t=ClientInfo->Post("iptos");
100 printf("<TD align='center'>tos:<BR><input type=text size=3 maxlength=3 name=iptos value='%s'></TD>\n",(t)?t:"");
101 if(strlen(t)){ip->ip_tos=atoi(t);} else {ip->ip_tos=0;}
102
103 t=ClientInfo->Post("iptotlen");
104 printf("<TD align='center'>tot len:<BR><input type=text size=4 maxlength=4 name=iptotlen value='%s'></TD></TR>\n",(t)?t:"");
105 if(strlen(t)){ip->ip_len=htons(atoi(t));} else {ip->ip_len=htons(sizeof(struct ip));}
106
107 t=ClientInfo->Post("ipid");
108 printf("<TR><TD align='center' colspan=3>id:<BR><input type=text size=5 maxlength=5 name=ipid value='%s'></TD>\n",(t)?t:"");
109 if(strlen(t)){ip->ip_id=htons(atoi(t));} else {ip->ip_id=htons(37337);}
110
111 t=ClientInfo->Post("ipfrag");
112 printf("<TD align='center'>frag offset:<BR><input type=text size=4 maxlength=4 name=ipfrag value='%s'></TD></TR>\n",(t)?t:"");
113 if(strlen(t)){ip->ip_off=htons(atoi(t));} else {ip->ip_off=htons(0);}
114
115 t=ClientInfo->Post("ipttl");
116 printf("<TR><TD align='center' colspan=2>ttl:<BR><input type=text size=3 maxlength=3 name=ipttl value='%s'></TD>\n",(t)?t:"");
117 if(strlen(t)){ip->ip_ttl=atoi(t);} else {ip->ip_ttl=64;}
118 printf("<TD align='center'>proto:<BR>\n");
119 printf("<select name='ipproto' onchange='parent.location=(document.ip.ipproto.options[0].selected==true)?\"/?proto=1\":(document.ip.ipproto.options[1].selected==true)?\"/?proto=6\":(document.ip.ipproto.options[2].selected==true)?\"/?proto=17\":\"\"'>\n");
120 printf("<option value='1' %s>icmp\n",(proto==1)?"selected":"");
121 printf("<option value='6' %s>tcp\n",(proto==6)?"selected":"");
122 printf("<option value='17' %s>udp\n",(proto==17)?"selected":"");
123 printf("</select></TD>\n");
124 ip->ip_p=proto;
125
126 printf("<TD align='center'>checksum:<BR>automatic</TD></TR>\n");
127
128 t=ClientInfo->Post("ipsrc");
129 printf("<TR><TD align='center' colspan=4>src ip:<BR><input type=text size=15 maxlength=15 name=ipsrc value='%s'></TD></TR>\n",(t)?t:"");
130 if(strlen(t)){ip->ip_src.s_addr=inet_addr(t);} else {ip->ip_src.s_addr=0;}
131
132 t=ClientInfo->Post("ipdst");
133 printf("<TR><TD align='center' colspan=4>dst ip:<BR><input type=text size=15 maxlength=15 name=ipdst value='%s'></TD></TR></TABLE>\n",(t)?t:"");
134 if(strlen(t)){ip->ip_dst.s_addr=inet_addr(t);} else {ip->ip_dst.s_addr=0;}
135
136 printf("</TD><TD>\n");
137
138 if(proto==6) { // print tcp header input
139 printf("<TABLE border=1>\n");
140 printf("<TR><TD colspan=4 align='center' bgcolor='5555FF'>tcp header</TD></TR>\n");
141 t=ClientInfo->Post("tcpsrcport");
142 printf("<TR><TD colspan=2 align='center'>src port:<BR><input type=text size=5 maxlength=5 name=tcpsrcport value='%s'></TD>\n",(t)?t:"");
143 if(strlen(t)){tcp->th_sport=htons(atoi(t));}
144 t=ClientInfo->Post("tcpdstport");
145 printf("<TD colspan=2 align='center'>dst port:<BR><input type=text size=5 maxlength=5 name=tcpdstport value='%s'></TD></TR>\n",(t)?t:"");
146 if(strlen(t)){tcp->th_dport=htons(atoi(t)); }
147 t=ClientInfo->Post("tcpseq");
148 printf("<TR><TD colspan=4 align='center'>Seq number:<BR><input type=text size=10 maxlength=10 name=tcpseq value='%s'></TD></TR>\n",(t)?t:"");
149 if(strlen(t)){tcp->th_seq=htonl(strtoul(t,NULL,10));}
150 t=ClientInfo->Post("tcpack");
151 printf("<TR><TD colspan=4 align='center'>ack:<BR><input type=text size=10 maxlength=10 name=tcpack value='%s'></TD></TR>\n",(t)?t:"");
152 if(strlen(t)){tcp->th_ack=htonl(strtoul(t,NULL,10)); }else {tcp->th_ack=0;}
153
154 printf("<TR><TD align='center'><small><small>data offset:<BR>(computed)</small></small></TD>\n");
155 printf("<TD align='center'><small><small>reserved:<BR>(computed)</small></small></TD>\n");
156 printf("<TD align='center'>flags:<BR>\n");
157 printf("<TABLE border=0 cellspacing=0 cellpadding=0><TR>\n");
158 printf("<TD><small><small>URG </small></small></TD>\n");
159 printf("<TD><small><small>ACK </small></small></TD>\n");
160 printf("<TD><small><small>PSH </small></small></TD>\n");
161 printf("<TD><small><small>RST </small></small></TD>\n");
162 printf("<TD><small><small>SYN </small></small></TD>\n");
163 printf("<TD><small><small>FIN</small></small></TD></TR>\n");
164 t=ClientInfo->Post("tcpfURG");
165 if(strlen(t)){tcp->th_flags |= TH_URG;}else {tcp->th_flags &= ~TH_URG;}
166 t=ClientInfo->Post("tcpfACK");
167 if(strlen(t)){tcp->th_flags |= TH_ACK;}else {tcp->th_flags &= ~TH_ACK;}
168 t=ClientInfo->Post("tcpfPSH");
169 if(strlen(t)){tcp->th_flags |= TH_PUSH;}else {tcp->th_flags &= ~TH_PUSH;}
170 t=ClientInfo->Post("tcpfRST");
171 if(strlen(t)){tcp->th_flags |= TH_RST;}else {tcp->th_flags &= ~TH_RST;}
172 t=ClientInfo->Post("tcpfSYN");
173 if(strlen(t)){tcp->th_flags |= TH_SYN;}else {tcp->th_flags &= ~TH_SYN;}
174 t=ClientInfo->Post("tcpfFIN");
175 if(strlen(t)){tcp->th_flags |= TH_FIN;}else {tcp->th_flags &= ~TH_FIN;}
176 printf("<TD><input type=checkbox name=tcpfURG %s></TD>\n",((tcp->th_flags & TH_URG)==TH_URG)?"checked":"");
177 printf("<TD><input type=checkbox name=tcpfACK %s></TD>\n",((tcp->th_flags & TH_ACK)==TH_ACK)?"checked":"");
178 printf("<TD><input type=checkbox name=tcpfPSH %s></TD>\n",((tcp->th_flags & TH_PUSH)==TH_PUSH)?"checked":"");
179 printf("<TD><input type=checkbox name=tcpfRST %s></TD>\n",((tcp->th_flags & TH_RST)==TH_RST)?"checked":"");
180 printf("<TD><input type=checkbox name=tcpfSYN %s></TD>\n",((tcp->th_flags & TH_SYN)==TH_SYN)?"checked":"");
181 printf("<TD><input type=checkbox name=tcpfFIN %s></TD>\n",((tcp->th_flags & TH_FIN)==TH_FIN)?"checked":"");
182 printf("</TR></TABLE></TD>\n");
183 t=ClientInfo->Post("tcpwin");
184 printf("<TD align='center'>window:<BR><input type=text size=7 maxlength=7 name=tcpwin value='%s'></TD></TR>\n",(t)?t:"");
185 if(strlen(t)) {tcp->th_win=htons(atoi(t)); } else {tcp->th_win=htons(1500);}
186 printf("<TR><TD colspan=2 align='center'>checksum:<BR>automatic</TD>\n");
187 t=ClientInfo->Post("tcpurg");
188 printf("<TD colspan=2 align='center'>urgent:<BR><input type=text size=7 maxlength=7 name=tcpurg value='%s'></TD></TR>\n",(t)?t:"");
189 printf("<TR><TD align='center' colspan=4>Tcp data<BR><textarea name=data cols='30' rows='4'>%s</textarea></TD></TR>\n",ClientInfo->Post("data"));
190 if(strlen(t)) {tcp->th_urp=htons(atoi(t)); } else {tcp->th_urp=0;}
191 printf("</TABLE>\n");
192 printf("</TABLE>\n");
193 tcp->th_x2=0;
194 };
195 printf("</TD></TR></TABLE>\n");
196 t=ClientInfo->Post("n_packets");
197 printf("<input type=text size=4 maxlength=4 value='10' name=n_packets value='%s'>\n",(t)?t:"");
198 if(strlen(t)){n=atoi(t); }
199 printf("<input type=submit value='send packet' name=ipsend>\n");
200 t=ClientInfo->Post("data");
201 memcpy(buf+sizeof(*ip)+sizeof(*tcp),t,(strlen(t)>128)?128:strlen(t));
202
203 t=ClientInfo->Post("ipsend");
204 if(strlen(t)) {
205 s=socket(AF_INET,SOCK_RAW,IPPROTO_RAW);
206 to.sin_family=AF_INET;
207 to.sin_addr.s_addr=ip->ip_dst.s_addr;
208 to.sin_port=htons(tcp->th_dport);
209 ip->ip_sum=in_chksum((void *)&ip,sizeof(*ip));
210 tcp->th_sum=in_chksum((void *)&ip,sizeof(*ip)+sizeof(*tcp));
211 for(i=0;i<n;i++) {
212 sendto(s,&buf,128,0,(struct sockaddr *)&to,sizeof(to));
213 };
214 };
215 printf("<br><a href='http://libwebserver.sourceforge.net'><img src='/libwebserver.gif' border='0'></a><BR>\n");
216 printf("</body>\n");
217 printf("</HTML>\n");
218
219};
220
221int main() {
222 int pid;
223 struct web_server serverSSL;
224 web_server_useSSLcert(&serverSSL,"./foo-cert.pem"); // Must be here couse of initalization of openssl
225 while(!web_server_init(&serverSSL,PORTSSL,"packetmounter.log",WS_USESSL)) {
226 PORTSSL++;
227 };
228 printf("https://localhost:%d\n",PORTSSL);
229 web_server_addhandler(&serverSSL,"* /*",packetmounter,0);
230 while(1) {
231 // DO whatever u want
232 web_server_run(&serverSSL); // Process web_server w/ SSL
233 };
234 return 0;
235
236};