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