1 #define _POSIX_C_SOURCE 200112L
5 #define __USE_XOPEN_EXTENDED
13 #include <netinet/in.h>
16 #include <sys/socket.h>
18 #include <linux/udp.h>
26 #define PPPCHAP 0xc223
27 #define PPPIPCP 0x8021
43 #define PACKET_LENGTH 1000
44 #define TARGET_PPS 5000
45 #define TARGET "211.29.131.33"
46 #define GWADDR "211.29.131.30"
47 #define NUM_SESSIONS 1
51 typedef unsigned short u16
;
52 typedef unsigned int u32
;
53 typedef unsigned char u8
;
90 char *attributes
[] = {
93 "Protocol Version", // 2
94 "Framing Capabilities", // 3
95 "Bearer Capabilities", // 4
97 "Firmware Revision", // 6
100 "Assigned Tunnel ID", // 9
101 "Receive Window Size", // 10
103 "Q.931 Cause Code", // 12
104 "Challenge Response", // 13
105 "Assigned Session ID", // 14
106 "Call Serial Number", // 15
109 "Bearer Type", // 18 (2 = Analog, 1 = Digital)
110 "Framing Type", // 19 (2 = Async, 1 = Sync)
112 "Called Number", // 21
113 "Calling Number", // 22
115 "Tx Connect Speed", // 24
116 "Physical Channel ID", // 25
117 "Initial Received LCP CONFREQ", // 26
118 "Last Sent LCP CONFREQ", // 27
119 "Last Received LCP CONFREQ", // 28
120 "Proxy Authen Type", // 29
121 "Proxy Authen Name", // 30
122 "Proxy Authen Challenge", // 31
123 "Proxy Authen ID", // 32
124 "Proxy Authen Response", // 33
127 "Random Vector", // 36
128 "Private Group ID", // 37
129 "Rx Connect Speed", // 38
130 "Sequencing Required", // 39
133 char *result_codes
[] = {
135 "General request to clear control connection",
136 "General error--Error Code indicates the problem",
137 "Control channel already exists",
138 "Requester is not authorized to establish a control channel",
139 "The protocol version of the requester is not supported",
140 "Requester is being shut down",
141 "Finite State Machine error",
144 char *error_codes
[] = {
146 "No control connection exists yet for this LAC-LNS pair",
148 "One of the field values was out of range or reserved field was non-zero",
149 "Insufficient resources to handle this operation now",
150 "The Session ID is invalid in this context",
151 "A generic vendor-specific error occurred in the LAC",
153 "Session or tunnel was shutdown due to receipt of an unknown AVP with the M-bit set",
185 long long send_count
, recv_count
;
186 long long spkt
, rpkt
;
188 long sbytes
, rbytes
;
192 short remote_session
;
195 unsigned char ppp_identifier
;
199 int active_sessions
;
204 void controlsend(controlt
* c
, short t
, short s
);
205 void controlnull(short t
);
206 controlt
*controlnew(u16 mtype
);
207 void controls(controlt
* c
, u16 avp
, char *val
, u8 m
);
208 void control16(controlt
* c
, u16 avp
, u16 val
, u8 m
);
209 void control32(controlt
* c
, u16 avp
, u32 val
, u8 m
);
210 void controlfree(controlt
*c
);
211 control_message
*parsecontrol(char *buf
, int length
);
212 void dump_control_message(control_message
*c
);
213 u32
avp_get_32(control_message
*c
, int id
);
214 u16
avp_get_16(control_message
*c
, int id
);
215 char *avp_get_s(control_message
*c
, int id
);
216 void reader_thread(void);
218 void cm_free(control_message
*m
);
219 controlt
*ppp_new(u16 session
, int protocol
);
220 void ppp_free(controlt
*packet
);
221 controlt
*ppp_lcp(u16 s
, unsigned char type
, char identifier
);
222 controlt
*ppp_ipcp(u16 s
, unsigned char type
, char identifier
);
223 void ppp_send(controlt
*c
);
224 void ppp_add_16(controlt
* c
, u16 val
);
225 void ppp_add_32(controlt
* c
, u32 val
);
226 void ppp_add_s(controlt
* c
, char *val
);
227 void ppp_lcp_add_option(controlt
*c
, unsigned char option
, unsigned char length
, int data
);
228 void dump_ppp_packet(char *packet
, int l
);
229 controlt
*ppp_pap(u16 s
, unsigned char type
, char identifier
, char *username
, char *password
);
230 char *inet_toa(unsigned long addr
);
231 __u16
checksum(unsigned char *addr
, int count
);
232 void sigalarm(int junk
);
233 void sigint(int signal
);
234 void clean_shutdown();
240 struct sockaddr_in gatewayaddr
;
241 int numsessions
= NUM_SESSIONS
;
242 int packet_length
= PACKET_LENGTH
;
243 int target_pps
= TARGET_PPS
;
244 char *target
= TARGET
;
245 char *gwaddr
= GWADDR
;
246 int max_packets
= MAX_PACKETS
;
249 char **session_usernames
;
250 char *base_username
= "dslloadtest";
251 char *base_password
= "testing";
252 char *suffix
= "@optusnet.com.au";
254 int main(int argc
, char *argv
[])
257 unsigned char *packet
;
259 ss
= (sharedt
*) mmap(NULL
, sizeof(*ss
), PROT_READ
| PROT_WRITE
, MAP_SHARED
| MAP_ANONYMOUS
, 0, 0);
261 // Process Arguments {{{
262 while ((s
= getopt(argc
, argv
, "?hs:g:l:p:m:t:nU:P:")) > 0)
267 numsessions
= atoi(optarg
);
268 if (numsessions
<= 0)
270 printf("You must have at least 1 session\n");
275 packet_length
= atoi(optarg
);
276 if (packet_length
< 64)
278 printf("You must have at least 64 byte packets\n");
286 target_pps
= atoi(optarg
);
289 max_packets
= atoi(optarg
);
290 if (max_packets
< 50)
292 printf("You must send at least 50 packets.\n");
297 target
= strdup(optarg
);
300 gwaddr
= strdup(optarg
);
303 base_username
= strdup(optarg
);
306 base_password
= strdup(optarg
);
310 printf("Options:\n");
311 printf("\t-s number of ss->sessions\n");
312 printf("\t-l packet length\n");
313 printf("\t-p target pps\n");
314 printf("\t-m maximum number of packets\n");
315 printf("\t-t target IP address\n");
316 printf("\t-g gateway IP address\n");
317 printf("\t-U username (or base if multiple)\n");
318 printf("\t-P password\n");
324 ppsend
= target_pps
/ 50;
328 packet
= calloc(4096, 1);
330 memset(ss
->sessions
, 0, sizeof(ss
->sessions
));
333 printf("Creating %d ss->sessions to %s\n", numsessions
, gwaddr
);
334 printf("Targeting %d packets per second\n", target_pps
);
335 if (max_packets
) printf("Sending a maximum of %d packets\n", max_packets
);
336 printf("Sending packets to %s\n", target
);
337 printf("Sending %d byte packets\n", packet_length
);
339 session_usernames
= (char **)calloc(sizeof(char *), numsessions
);
342 int sul
= strlen(base_username
) + 10;
345 for (i
= 0; i
< numsessions
; i
++)
347 session_usernames
[i
] = (char *)calloc(sul
, 1);
348 snprintf(session_usernames
[i
], sul
, "%s%d", base_username
, i
+1);
353 session_usernames
[0] = strdup(base_username
);
357 // Create socket/*{{{*/
360 struct sockaddr_in addr
;
362 memset(&addr
, 0, sizeof(addr
));
363 addr
.sin_family
= AF_INET
;
364 addr
.sin_port
= htons(38001);
366 udpfd
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
373 setsockopt(udpfd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
));
374 if (bind(udpfd
, (void *) &addr
, sizeof(addr
)) < 0)
380 printf("Bound to port %d\n", htons(addr
.sin_port
));
383 memset(&gatewayaddr
, 0, sizeof(gatewayaddr
));
384 gatewayaddr
.sin_family
= AF_INET
;
385 gatewayaddr
.sin_port
= htons(1701);
386 inet_aton(gwaddr
, &gatewayaddr
.sin_addr
);
388 // Create tunnel/*{{{*/
393 c
= controlnew(1); // SCCRQ
394 controls(c
, 7, "loadtest", 0); // Tunnel Hostname
395 controls(c
, 8, "OIE", 0); // Vendor Name
396 control16(c
, 9, 1, 0); // Assigned Tunnel ID
397 control16(c
, 2, 256, 0); // Version 1.0
398 control16(c
, 3, 1, 0); // Framing (Async)
399 control16(c
, 4, 1, 0); // Bearer (Digital)
400 control16(c
, 10, 20, 0); // Receive Window Size
401 controlsend(c
, 0, 0);
404 // Receive reply/*{{{*/
406 struct sockaddr_in addr
;
408 socklen_t alen
= sizeof(addr
);
410 l
= recvfrom(udpfd
, packet
, 4096, 0, (void *) &addr
, &alen
);
413 printf("Error creating tunnel: %s\n", strerror(errno
));
417 r
= parsecontrol((char *) packet
, l
);
420 printf("Invalid packet.. no first avp\n");
424 printf("Assigned tunnel: %d\n", t
= avp_get_16(r
, 9));
427 c
= controlnew(3); // SCCCN
428 controlsend(c
, t
, 0);
435 // Create ss->sessions/*{{{*/
438 for (s
= 1; s
<= numsessions
; s
++)
442 c
= controlnew(10); // ICRQ
443 controls(c
, 21, "12356", 0); // Called Number
444 controls(c
, 22, "000", 0); // Calling Number
445 control16(c
, 14, s
, 0); // Assigned Session ID
446 controlsend(c
, t
, 0);
448 usleep(15000); // 15 ms
451 printf("All session create requests sent...\n");/*}}}*/
460 fprintf(stderr
, "Press enter to begin sending traffic\n");
461 fgets(tmp
, 512, stdin
);
464 fprintf(stderr
, "Beginning sending traffic through %d ss->sessions\n", ss
->active_sessions
);
465 printf(" TS: Total Packets Sent\n");
466 printf(" TL: Total Packets Lost\n");
467 printf(" PL: Packet Loss\n");
468 printf(" SS: Send Speed\n");
469 printf(" RS: Receive Speed\n");
470 printf(" SP: Packets/Second Sent\n");
471 printf(" RP: Packets/Second Received\n");
472 printf(" NS: Number of active ss->sessions\n");
474 signal(SIGALRM
, sigalarm
);
475 signal(SIGINT
, sigint
);
478 // Traffic generation loop {{{
480 struct sockaddr_in to
;
485 unsigned int seq
= 0;
489 memset(&to
, 0, sizeof(struct sockaddr_in
));
490 to
.sin_family
= AF_INET
;
491 inet_aton(target
, &to
.sin_addr
);
493 c
= ppp_new(1, PPPIP
);
495 iph
= (struct iphdr
*)(c
->buf
+ c
->length
);
496 udph
= (struct udphdr
*)(c
->buf
+ c
->length
+ sizeof(struct iphdr
));
497 data
= (char *)(c
->buf
+ c
->length
+ sizeof(struct iphdr
) + sizeof(struct udphdr
));
498 len
= sizeof(struct iphdr
) + sizeof(struct udphdr
);
502 c
->length
+= sizeof(struct iphdr
);
505 iph
->frag_off
= ntohs(1 << 14);
511 memcpy(&iph
->daddr
, &to
.sin_addr
, sizeof(iph
->daddr
));
514 udph
->source
= ntohs(39999);
515 udph
->dest
= ntohs(39000);
519 memset(data
, 64, 1500);
521 udph
->len
= ntohs(sizeof(struct udphdr
) + packet_length
);
522 iph
->tot_len
= ntohs(len
+ packet_length
);
523 c
->length
+= packet_length
;
525 while (!ss
->quitit
&& ss
->active_sessions
)
528 for (i
= 1; i
<= numsessions
&& !ss
->quitit
; i
++)
530 // Skip ss->sessions that aren't active yet
531 if (!ss
->sessions
[i
].open
|| ss
->sessions
[i
].ppp_state
!= 2)
534 *(u16
*)(c
->buf
+ 4) = htons(ss
->sessions
[i
].remote_session
); // Session ID
535 iph
->saddr
= ss
->sessions
[i
].addr
;
537 iph
->check
= ntohs(checksum((unsigned char *)iph
, sizeof(struct iphdr
)));
539 *((unsigned int *) data
) = seq
++;
544 ss
->sbytes
+= c
->length
;
546 if (ppsend
&& ss
->send_count
% ppsend
== 0)
550 req
.tv_nsec
= 5 * 1000 * 1000;
551 nanosleep(&req
, NULL
);
554 if (max_packets
&& ss
->send_count
>= max_packets
)
559 c
->length
-= packet_length
;
574 loss
= 100 - (((ss
->recv_count
* 1.0) / (ss
->send_count
* 1.0)) * 100.0);
577 printf("Total Packets Sent: %llu\n", ss
->send_count
);
578 printf("Total Packets Received: %llu\n", ss
->recv_count
);
579 printf("Overall Packet Loss: %0.2f%%", loss
);
583 void clean_shutdown()/*{{{*/
586 for (i
= 0; i
< numsessions
; i
++)
591 if (!ss
->sessions
[i
].open
) continue;
592 c
= controlnew(14); // CDN
593 control16(c
, 14, i
, 0); // Assigned Session ID
594 control16(c
, 1, 1, 0); // Result Code
595 controlsend(c
, t
, ss
->sessions
[i
].remote_session
);
603 c
= controlnew(4); // StopCCN
604 control16(c
, 9, 1, 0); // Assigned Tunnel ID
605 control16(c
, 1, 1, 0); // Result Code
606 controlsend(c
, t
, 0);
611 void sigint(int unused
__attribute__ ((unused
)))
616 void sigalarm(int unused
__attribute__ ((unused
)))
618 static unsigned long long last_rpkts
[AVG_SIZE
], last_spkts
[AVG_SIZE
];
619 static int last
= 0, avg_count
= 0;
620 unsigned int avg_s
= 0, avg_r
= 0;
624 last_rpkts
[last
] = ss
->rpkt
;
625 last_spkts
[last
] = ss
->spkt
;
626 last
= (last
+ 1) % AVG_SIZE
;
627 if (avg_count
< AVG_SIZE
)
630 for (i
= 0; i
< avg_count
; i
++)
632 avg_s
+= last_spkts
[i
];
633 avg_r
+= last_rpkts
[i
];
638 loss
= 100 - (((avg_r
* 1.0) / (avg_s
* 1.0)) * 100.0);
639 fprintf(stderr
, "TS:%llu TL:%lld DR:%4d PL:%-3.2f%% SS:%0.1fMbits/s RS:%0.1fMbits/s NS:%u SP:%u RP:%u\n",
640 ss
->send_count
, ss
->send_count
-ss
->recv_count
, ss
->dropped
, loss
,
641 (ss
->sbytes
/1024.0/1024.0*8), (ss
->rbytes
/1024.0/1024.0*8),
645 ss
->spkt
= ss
->rpkt
= 0;
646 ss
->sbytes
= ss
->rbytes
= 0;
650 __u16
checksum(unsigned char *addr
, int count
)
652 register long sum
= 0;
654 for (; count
> 1; count
-= 2)
656 sum
+= ntohs(*(u16
*)addr
);
660 if (count
> 0) sum
+= *(unsigned char *)addr
;
662 // take only 16 bits out of the 32 bit sum and add up the carries
664 sum
= (sum
& 0xFFFF) + (sum
>> 16);
666 // one's complement the result
673 void control16(controlt
* c
, u16 avp
, u16 val
, u8 m
)
675 u16 l
= (m
? 0x8008 : 0x0008);
676 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
677 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
678 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
679 *(u16
*) (c
->buf
+ c
->length
+ 6) = htons(val
);
683 // add an AVP (32 bit)
684 void control32(controlt
* c
, u16 avp
, u32 val
, u8 m
)
686 u16 l
= (m
? 0x800A : 0x000A);
687 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
688 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
689 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
690 *(u32
*) (c
->buf
+ c
->length
+ 6) = htonl(val
);
694 // add an AVP (32 bit)
695 void controls(controlt
* c
, u16 avp
, char *val
, u8 m
)
697 u16 l
= ((m
? 0x8000 : 0) + strlen(val
) + 6);
698 *(u16
*) (c
->buf
+ c
->length
+ 0) = htons(l
);
699 *(u16
*) (c
->buf
+ c
->length
+ 2) = htons(0);
700 *(u16
*) (c
->buf
+ c
->length
+ 4) = htons(avp
);
701 memcpy(c
->buf
+ c
->length
+ 6, val
, strlen(val
));
702 c
->length
+= 6 + strlen(val
);
705 // new control connection
706 controlt
*controlnew(u16 mtype
)
709 c
= calloc(sizeof(controlt
), 1);
711 control16(c
, 0, mtype
, 1);
715 void controlnull(short t
)
718 c
= calloc(sizeof(controlt
), 1);
720 controlsend(c
, t
, 0);
725 // add a control message to a tunnel, and send if within window
726 void controlsend(controlt
* c
, short t
, short s
)
728 *(u16
*) (c
->buf
+ 0) = htons(0xC802); // flags/ver
729 *(u16
*) (c
->buf
+ 2) = htons(c
->length
); // length
730 *(u16
*) (c
->buf
+ 4) = htons(t
); // tunnel
731 *(u16
*) (c
->buf
+ 6) = htons(s
); // session
732 *(u16
*) (c
->buf
+ 8) = htons(ns
++); // sequence
733 *(u16
*) (c
->buf
+ 10) = htons(nr
); // sequence
734 // printf("Sending ");
735 // cm_free(parsecontrol(c->buf, c->length));
736 sendto(udpfd
, c
->buf
, c
->length
, 0, (struct sockaddr
*)&gatewayaddr
, sizeof(gatewayaddr
));
739 void controlfree(controlt
*c
)
745 control_message
*parsecontrol(char *buf
, int length
)
750 c
= calloc(sizeof(control_message
), 1);
754 c
->tunnel
= ntohs(*(u16
*)(buf
+ 4));
755 c
->session
= ntohs(*(u16
*)(buf
+ 6));
756 c
->ns
= ntohs(*(u16
*)(buf
+ 8));
757 c
->nr
= nr
= ntohs(*(u16
*)(buf
+ 10));
759 while ((p
- buf
) < length
)
761 avp
*a
= calloc(sizeof(avp
), 1);
762 a
->length
= ntohs(*(short *)(p
)) & 0x3FF;
763 a
->type
= ntohs(*(short *)(p
+ 4));
764 memcpy(a
->value
, p
+ 6, a
->length
- 6);
765 if (a
->type
== 0) c
->mtype
= ntohs(*(short *)a
->value
);
774 dump_control_message(c
);
778 void dump_control_message(control_message
*c
)
781 printf("Control Message (type=%u s=%u t=%d ns=%d nr=%d)\n", c
->mtype
, c
->session
, c
->tunnel
, c
->ns
, c
->nr
);
782 for (a
= c
->first
; a
; a
= a
->next
)
784 printf(" avp: %s, len: %d", attributes
[a
->type
], a
->length
- 6);
792 case 14 : printf(", value: %u\n", ntohs(*(short *)a
->value
));
801 case 15 : printf(", value: %u\n", ntohl(*(u32
*)a
->value
));
810 case 8 : printf(", value: \"%s\"\n", a
->value
);
813 case 2 : printf(", value: %d.%d\n", *(char *)a
->value
, *(char *)a
->value
+ 1);
815 case 0 : printf(", value: %s\n", mtypes
[ntohs(*(short *)a
->value
)]);
818 case 3 : printf(", value: (%d) %s %s\n", ntohl(*(u32
*)a
->value
),
819 (ntohl(*(u32
*)a
->value
) & 0x01) ? "synchronous" : "",
820 (ntohl(*(u32
*)a
->value
) & 0x02) ? "asynchronous" : "");
823 case 4 : printf(", value: (%d) %s %s\n", ntohl(*(u32
*)a
->value
),
824 (ntohl(*(u32
*)a
->value
) & 0x01) ? "digital" : "",
825 (ntohl(*(u32
*)a
->value
) & 0x02) ? "analog" : "");
828 default : printf("\n");
835 u16
avp_get_16(control_message
*c
, int id
)
839 for (a
= c
->first
; a
; a
= a
->next
)
840 if (a
->type
== id
) return ntohs(*(short *)a
->value
);
844 u32
avp_get_32(control_message
*c
, int id
)
848 for (a
= c
->first
; a
; a
= a
->next
)
849 if (a
->type
== id
) return ntohl(*(u32
*)a
->value
);
853 char *avp_get_s(control_message
*c
, int id
)
857 for (a
= c
->first
; a
; a
= a
->next
)
858 if (a
->type
== id
) return (char *)a
->value
;
862 void cm_free(control_message
*m
)
866 for (a
= m
->first
; a
; )
878 void reader_thread()/*{{{*/
880 unsigned char *packet
;
881 unsigned int seq
= 0;
883 printf("Starting reader thread\n");
884 packet
= malloc(4096);
887 struct sockaddr_in addr
;
888 socklen_t alen
= sizeof(addr
);
894 // memset(packet, 0, 4096);
895 if ((l
= recvfrom(udpfd
, packet
, 4096, 0, (void *) &addr
, &alen
)) < 0) break;
905 printf("Short packet received: %d bytes\n", l
);
907 s
= ntohs(*(u16
*)(packet
+ 4));
910 printf("Invalid session ID\n");
913 if (packet
[0] == 0xc8)
916 printf("Reader Received ");
917 m
= parsecontrol((char *) packet
, l
);
923 case 4 : printf("StopCCN\n");
924 printf("Killing tunnel %d\n", avp_get_16(m
, 9));
927 case 6 : printf("HELLO, sending ZLB ACK\n");
934 printf("Received ICRP. Responding with CONFREQ\n");
936 ss
->sessions
[s
].remote_session
= avp_get_16(m
, 14);
937 ss
->sessions
[s
].open
= 1;
938 ss
->sessions
[s
].ppp_state
= 1;
940 c
= controlnew(12); // ICCN
941 controlsend(c
, t
, ss
->sessions
[s
].remote_session
);
944 c
= ppp_lcp(s
, CONFREQ
, 0);
945 ppp_lcp_add_option(c
, 1, 2, htons(1500)); // MRU = 1400
946 ppp_lcp_add_option(c
, 3, 2, htons(0xC023)); // Authentication Protocol - PAP
954 s
= avp_get_16(m
, 14);
955 printf("Killing session %d\n", s
);
956 ss
->sessions
[s
].open
= 0;
957 ss
->sessions
[s
].ppp_state
= 0;
958 ss
->active_sessions
--;
966 printf("StopCCN Received.. Dieing\n");
975 unsigned short protocol
= ntohs(*(u16
*)(packet
+ 6));
977 if (protocol
== 0xff03)
981 protocol
= ntohs(*(u16
*)(packet
+ 6));
983 if (protocol
!= PPPIP
)
986 dump_ppp_packet((char *) (packet
+ 6), l
- 6);
989 if (protocol
== PPPLCP
)
992 unsigned char ppp_id
= *(char *)(packet
+ 9);
994 switch (*(char *)(packet
+ 8))
997 r
= ppp_lcp(s
, CONFACK
, ppp_id
);
1001 r
= ppp_pap(s
, CONFREQ
, 0, session_usernames
[s
-1], base_password
);
1005 r
= ppp_lcp(s
, TERMACK
, ppp_id
);
1009 r
= ppp_lcp(s
, ECHOREP
, ppp_id
);
1015 else if (protocol
== PPPIPCP
)
1019 u32 address
= *(u32
*)(packet
+ 14);
1021 switch (*(char *)(packet
+ 8))
1024 r
= ppp_ipcp(s
, CONFREQ
, time(NULL
) % 255);
1025 ppp_lcp_add_option(r
, 3, 4, htonl(taddr
)); // Request 0.0.0.0
1028 r
= ppp_ipcp(s
, CONFACK
, time(NULL
) % 255);
1029 ppp_lcp_add_option(r
, 3, 4, address
); // ACK gateway IP
1034 // Request whatever address we are given - it's ours
1035 r
= ppp_ipcp(s
, CONFREQ
, time(NULL
) % 255);
1036 ppp_lcp_add_option(r
, 3, 4, address
);
1039 printf("Session %d: %s\n", s
, inet_toa(address
));
1040 ss
->sessions
[s
].ppp_state
= 2;
1041 ss
->sessions
[s
].addr
= address
;
1042 ss
->active_sessions
++;
1045 printf("Conf-Ack Received\n");
1048 printf("Term-Req Received\n");
1051 printf("Echo-Req Received\n");
1054 printf("Echo-Rep Received\n");
1058 else if (protocol
== PPPPAP
)
1060 if (*(u16
*)(packet
+ 8) == 3)
1063 printf("Closing Connection\n");
1065 c
= controlnew(14); // CDN
1066 control16(c
, 14, ss
->sessions
[s
].remote_session
, 0); // Assigned Session ID
1067 controlsend(c
, t
, 0);
1069 ss
->sessions
[s
].open
= 0;
1072 else if (protocol
== PPPIP
)
1074 struct iphdr
*iph
= (struct iphdr
*)(packet
+ 8);
1075 char * data
= (char*) (packet
+ 8 + sizeof(struct iphdr
) + sizeof(struct udphdr
));
1076 if (!ss
->sessions
[s
].open
)
1078 printf("Packet for closed session %d\n", s
);
1082 if (iph
->protocol
== 17)
1087 iseq
= *((unsigned int *) data
);
1089 ss
->dropped
+= (iseq
- seq
) ;
1091 seq
= iseq
+ 1; // Next sequence number to expect.
1099 printf("Closing reader thread\n");
1103 void skip_zlb() /*{{{*/
1105 struct sockaddr_in addr
;
1106 socklen_t alen
= sizeof(addr
);
1109 l
= recvfrom(udpfd
, buf
, 1024, MSG_PEEK
, (void *) &addr
, &alen
);
1112 printf("recvfrom: %s\n", strerror(errno
));
1117 printf("Skipping ZLB (l=%d)\n", l
);
1118 recvfrom(udpfd
, buf
, 1024, 0, (void *) &addr
, &alen
);
1124 controlt
*ppp_new(u16 session
, int protocol
)
1126 controlt
*c
= calloc(sizeof(controlt
), 1);
1127 *(u16
*)(c
->buf
+ 4) = htons(ss
->sessions
[session
].remote_session
); // Tunnel
1128 *(u16
*)(c
->buf
+ 6) = htons(protocol
);
1134 void ppp_free(controlt
*c
)
1139 controlt
*ppp_lcp(u16 s
, unsigned char type
, char identifier
)
1143 if (!identifier
) identifier
= ss
->sessions
[s
].ppp_identifier
++;
1144 c
= ppp_new(s
, PPPLCP
);
1145 *(char *)(c
->buf
+ c
->length
+ 0) = type
;
1146 *(char *)(c
->buf
+ c
->length
+ 1) = identifier
;
1147 *(u16
*)(c
->buf
+ c
->length
+ 2) = ntohs(4);
1153 controlt
*ppp_ipcp(u16 s
, unsigned char type
, char identifier
)
1157 if (!identifier
) identifier
= ss
->sessions
[s
].ppp_identifier
++;
1158 c
= ppp_new(s
, PPPIPCP
);
1159 *(char *)(c
->buf
+ c
->length
+ 0) = type
;
1160 *(char *)(c
->buf
+ c
->length
+ 1) = identifier
;
1161 *(u16
*)(c
->buf
+ c
->length
+ 2) = ntohs(4);
1167 controlt
*ppp_pap(u16 s
, unsigned char type
, char identifier
, char *username
, char *password
)
1171 if (!identifier
) identifier
= ss
->sessions
[s
].ppp_identifier
++;
1172 c
= ppp_new(s
, PPPPAP
);
1173 *(char *)(c
->buf
+ c
->length
+ 0) = type
;
1174 *(char *)(c
->buf
+ c
->length
+ 1) = identifier
;
1175 *(u16
*)(c
->buf
+ c
->length
+ 2) = ntohs(4);
1178 *(char *)(c
->buf
+ c
->length
) = strlen(username
) + strlen(suffix
);
1179 memcpy((c
->buf
+ c
->length
+ 1), username
, strlen(username
));
1180 memcpy((c
->buf
+ c
->length
+ 1 + strlen(username
)), suffix
, strlen(suffix
));
1181 c
->length
+= strlen(username
) + 1 + strlen(suffix
);
1183 *(char *)(c
->buf
+ c
->length
) = strlen(password
);
1184 memcpy((c
->buf
+ c
->length
+ 1), password
, strlen(password
));
1185 c
->length
+= strlen(password
) + 1;
1190 void ppp_send(controlt
*c
)
1192 *(u16
*)(c
->buf
+ 0) = htons(0x0002); // flags/ver
1193 *(u16
*)(c
->buf
+ 2) = htons(t
); // tunnel
1194 *(u16
*)(c
->buf
+ 10) = ntohs(c
->length
- 8);
1195 if (sendto(udpfd
, c
->buf
, c
->length
, 0, (struct sockaddr
*)&gatewayaddr
, sizeof(gatewayaddr
)) < 0)
1197 if (htons(*(u16
*)(c
->buf
+ 6)) != PPPIP
)
1199 printf("PPP Sending ");
1200 dump_ppp_packet(c
->buf
+ 6, c
->length
- 6);
1204 void ppp_add_16(controlt
*c
, u16 val
)
1206 *(u16
*) (c
->buf
+ c
->length
) = htons(val
);
1210 void ppp_add_32(controlt
*c
, u32 val
)
1212 *(u32
*) (c
->buf
+ c
->length
) = htons(val
);
1216 void ppp_add_s(controlt
*c
, char *val
)
1218 memcpy(c
->buf
+ c
->length
, val
, strlen(val
));
1219 c
->length
+= strlen(val
);
1222 void ppp_lcp_add_option(controlt
*c
, unsigned char option
, unsigned char length
, int data
)
1224 *(char *)(c
->buf
+ c
->length
+ 0) = option
;
1225 *(char *)(c
->buf
+ c
->length
+ 1) = length
+ 2;
1226 memcpy(c
->buf
+ c
->length
+ 2, &data
, length
);
1227 c
->length
+= 2 + length
;
1230 void dump_ppp_packet(char *packet
, int l
)
1234 if (*(unsigned char *)p
== 0xff) p
+= 2;
1235 protocol
= ntohs(*(u16
*)(p
));
1236 printf("PPP Packet\n");
1239 case PPPCCP
: printf(" Protocol: PPPCCP\n"); break;
1241 if (protocol
== PPPLCP
)
1243 printf(" Protocol: PPPLCP\n");
1244 printf(" LCP Code: %s\n", lcp_codes
[*(u8
*)(p
+ 2)]);
1246 else if (protocol
== PPPPAP
)
1248 printf(" Protocol: PPPPAP\n");
1249 if (*(char *)(p
+ 2) == 2)
1251 printf(" Authentication accepted\n");
1253 else if (*(char *)(p
+ 2) == 3)
1255 printf(" Authentication denied\n");
1258 else if (protocol
== PPPIPCP
)
1260 printf(" Protocol: PPPIPCP\n");
1261 printf(" IPCP Code: %s\n", lcp_codes
[*(u8
*)(p
+ 2)]);
1262 printf(" Address: %s\n", inet_toa(*(u32
*)(p
+ 8)));
1264 else if (protocol
== PPPIP
)
1267 struct protoent
*pr
;
1269 iph
= (struct iphdr
*)(p
+ 2);
1271 printf(" Protocol: PPPIP\n");
1272 printf(" Length: %d\n", l
);
1273 printf(" IP Version: %d\n", iph
->version
);
1274 if (iph
->version
!= 4) return;
1275 pr
= getprotobynumber(iph
->protocol
);
1276 printf(" IP Header Length: %d\n", iph
->ihl
);
1277 printf(" IP TTL: %d\n", iph
->ttl
);
1278 printf(" IP Protocol: %s (%d)\n", (pr
? pr
->p_name
: "unknown"), iph
->protocol
);
1279 printf(" IP Checksum: %x\n", ntohs(iph
->check
));
1283 printf(" Protocol: unknown 0x%x\n", protocol
);
1288 char *inet_toa(unsigned long addr
)
1291 memcpy(&in
, &addr
, sizeof(unsigned long));
1292 return inet_ntoa(in
);