Commit | Line | Data |
---|---|---|
78d45895 IJ |
1 | /* |
2 | test-example/bogus-setup-request 127.0.0.1 19098 test-example/inside/inside 127.0.0.1 16096 test-example/outside/outside | |
3 | */ | |
4 | ||
5 | #include <sys/types.h> | |
6 | #include <sys/socket.h> | |
7 | #include <netinet/in.h> | |
8 | #include <arpa/inet.h> | |
9 | #include <netdb.h> | |
10 | ||
11 | #include <assert.h> | |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <stdio.h> | |
15 | ||
16 | /* | |
17 | | 00000 00 00 00 00 00 00 00 01 01 01 01 01 00 1a 74 65 ........ ......te | | |
18 | ~~~~~~~~~~~ ~~~~~~~~~~~ ~~~~~~~~~~~ ~~~~~|~~~~~ | |
19 | sessionid sender's type sender's | |
20 | zero in index fixed for name | |
21 | msg1 msg1 | |
22 | ||
23 | | 00010 73 74 2d 65 78 61 6d 70 6c 65 2f 69 6e 73 69 64 st-examp le/insid | | |
24 | | 00020 65 2f 69 6e 73 69 64 65 00 1c 74 65 73 74 2d 65 e/inside ..test-e | | |
25 | ~~~~~|~~~~~~~~~~~~~~~~~ | |
26 | recipient's name | |
27 | ||
28 | | 00030 78 61 6d 70 6c 65 2f 6f 75 74 73 69 64 65 2f 6f xample/o utside/o | | |
29 | | 00040 75 74 73 69 64 65 8d f0 3f 35 d6 c8 1f c0 utside.. ?5.... | | |
30 | ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ | |
31 | sender's nonce | |
32 | */ | |
33 | ||
34 | typedef struct { | |
35 | const char *name; | |
36 | union { | |
37 | struct sockaddr sa; | |
38 | struct sockaddr_in sin; | |
39 | }; | |
40 | } Ep; | |
41 | ||
42 | static void endaddr(Ep *ep, char **argv, int base) { | |
43 | int r; | |
44 | ep->sin.sin_family=AF_INET; | |
45 | r=inet_aton(argv[base],&ep->sin.sin_addr); assert(r); | |
46 | ep->sin.sin_port=htons(atoi(argv[base+1])); | |
47 | ep->name=argv[base+2]; | |
48 | } | |
49 | ||
50 | static void endname(uint8_t **msgp, const Ep *ep) { | |
51 | int l=strlen(ep->name); assert(l<=65535); | |
52 | *(*msgp)++ = l>>8; | |
53 | *(*msgp)++ = l; | |
54 | memcpy(*msgp, ep->name, l); | |
55 | *msgp += l; | |
56 | } | |
57 | ||
58 | static Ep us, them; | |
59 | ||
60 | int main(int argc, char **argv) { | |
61 | int r; | |
62 | ||
63 | assert(argc==7); | |
64 | ||
65 | endaddr(&us,argv,1); | |
66 | endaddr(&them,argv,4); | |
67 | ||
68 | static const uint8_t mprefix[]={ | |
69 | 0x00, 0x00, 0x00, 0x00, | |
70 | 0x00, 0x00, 0x00, 0x01, | |
71 | 0x01, 0x01, 0x01, 0x01, | |
72 | }; | |
73 | static const uint8_t msuffix[]={ | |
74 | /* our nonce, fixed he he */ | |
75 | 0x8d, 0xf0, 0x3f, 0x35, 0xd6, 0xc8, 0x1f, 0xc0 | |
76 | }; | |
77 | int msglen= (sizeof(mprefix) + | |
78 | 2+strlen(us.name) + | |
79 | 2+strlen(them.name) + | |
80 | sizeof(msuffix)); | |
81 | uint8_t msg[msglen]; | |
82 | uint8_t *msgp=msg; | |
83 | ||
84 | #define PREFIXSUFFIX(prefixsuffix) do { \ | |
85 | memcpy(msgp,prefixsuffix,sizeof(prefixsuffix)); \ | |
86 | msgp += sizeof(prefixsuffix); \ | |
87 | }while(0) | |
88 | ||
89 | PREFIXSUFFIX(mprefix); | |
90 | ||
91 | endname(&msgp,&us); | |
92 | endname(&msgp,&them); | |
93 | ||
94 | PREFIXSUFFIX(msuffix); | |
95 | ||
96 | assert(msgp == msg+msglen); | |
97 | ||
98 | struct protoent *proto=getprotobyname("udp"); | |
99 | int fd=socket(AF_INET, SOCK_DGRAM, proto->p_proto); | |
100 | r=bind(fd,&us.sa,sizeof(us.sin)); if (r) { perror("bind us2"); exit(1); } | |
101 | ||
102 | for (;;) { | |
103 | r=sendto(fd,msg,msglen,0,&them.sa,sizeof(them.sin)); | |
104 | if (r < 0) perror("sendto"); | |
105 | ||
106 | r=getchar(); | |
107 | if (r==EOF) { | |
108 | if (ferror(stdin)) { perror("getchar"); exit(1); } | |
109 | break; | |
110 | } | |
111 | if (r!='\n') | |
112 | break; | |
113 | } | |
114 | exit(0); | |
115 | } |