]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/autotest/v2_0/host/awaitorder.c
Initial revision
[karo-tx-redboot.git] / packages / net / autotest / v2_0 / host / awaitorder.c
1 //==========================================================================
2 //
3 //      awaitorder.c
4 //
5 //      Await an order from the target, and print it out...
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // -------------------------------------------
37 //####ECOSGPLCOPYRIGHTEND####
38 //####BSDCOPYRIGHTBEGIN####
39 //
40 // -------------------------------------------
41 //
42 // Portions of this software may have been derived from OpenBSD or other sources,
43 // and are covered by the appropriate copyright disclaimers included herein.
44 //
45 // -------------------------------------------
46 //
47 //####BSDCOPYRIGHTEND####
48 //==========================================================================
49 //#####DESCRIPTIONBEGIN####
50 //
51 // Author(s):    hmt,gthomas
52 // Contributors: hmt,gthomas
53 // Date:         2000-10-10
54 //
55 //####DESCRIPTIONEND####
56 //
57 //==========================================================================
58
59 #include <stdio.h>
60
61 #include <net/if.h>
62 #include <netinet/in.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_icmp.h>
65 #include <net/route.h>
66
67 void
68 pexit(char *s)
69 {
70     perror(s);
71     exit(1);
72 }
73
74 #define SOURCE_PORT 9980
75
76 int main( int argc, char **argv )
77 {
78     int s_source, e_source;
79     struct sockaddr_in e_source_addr, local;
80     int one = 1;
81     int len;
82     char orders[256];
83     char ack[] = "Acknowledged\n";
84     int loop = 1;
85
86     if ( 2 < argc ) {
87     usage:
88         fprintf( stderr, "usage: %s [once|many]\n", argv[0] );
89         exit(1);
90     } 
91
92     if ( 2 == argc ) {
93         if ( !strcmp( "once", argv[1] ) )
94             loop = 0;
95         else if ( !strcmp( "many", argv[1] ) )
96             loop = 1;
97         else 
98             goto usage;
99     }
100
101     s_source = socket(AF_INET, SOCK_STREAM, 6 /* TCP */ );
102     if (s_source < 0) {
103         pexit("stream socket");
104     }
105
106
107     memset(&local, 0, sizeof(local));
108     local.sin_family = AF_INET;
109     local.sin_len = sizeof(local);
110     local.sin_port = ntohs(SOURCE_PORT);
111     local.sin_addr.s_addr = INADDR_ANY;
112     if(bind(s_source, (struct sockaddr *) &local, sizeof(local)) < 0) {
113         pexit("bind /source/ error");
114     }
115     if (setsockopt(s_source, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
116         pexit("setsockopt /source/ SO_REUSEADDR");
117     }
118 #ifdef SO_REUSEPORT
119     if (setsockopt(s_source, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) {
120         pexit("setsockopt /source/ SO_REUSEPORT");
121     }
122 #endif
123
124     listen(s_source, SOMAXCONN);
125
126     do {
127         len = sizeof(e_source_addr);
128         if ((e_source = accept(s_source,
129                  (struct sockaddr *)&e_source_addr, &len)) < 0) {
130             pexit("accept /source/");
131         }
132         fprintf( stderr, "awaitorder: connection from %s:%d\n", 
133                  inet_ntoa(e_source_addr.sin_addr), ntohs(e_source_addr.sin_port));
134         
135         // Wait for "source" to tell us the orders
136         if ( 0 >= (len = read(e_source, orders, sizeof(orders)))) {
137             pexit("Can't read orders");
138         }
139         if ( sizeof(ack) != write(e_source, ack, sizeof( ack ) ) ) {
140             pexit("Can't write ACK");
141         }
142
143         orders[len] = 0;
144
145         printf( "ORDERS: %s\n", orders );
146         fflush( stdout );
147
148         close( e_source );
149     } while ( loop );
150
151     close( s_source );
152 }
153 // EOF awaitorder.c