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