1
1
/* ------------------------------------------------------------------------
2
- //
3
- // client_tipc.c
4
- //
5
- // Short description: Client side of multicast demo.
6
- //
7
- // ------------------------------------------------------------------------
8
- //
9
- // Copyright (c) 2003, Ericsson Research Canada
10
- * Copyright (c) 2005, Wind River Systems
11
- // All rights reserved.
12
- // Redistribution and use in source and binary forms, with or without
13
- // modification, are permitted provided that the following conditions are met:
14
- //
15
- // Redistributions of source code must retain the above copyright notice, this
16
- // list of conditions and the following disclaimer.
17
- // Redistributions in binary form must reproduce the above copyright notice,
18
- // this list of conditions and the following disclaimer in the documentation
19
- // and/or other materials provided with the distribution.
20
- // Neither the name of the copyright holders nor the names of its
21
- // contributors may be used to endorse or promote products derived from this
22
- // software without specific prior written permission.
23
- //
24
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
- // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
- // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
- // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28
- // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
- // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
- // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
- // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
- // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
- // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
- // POSSIBILITY OF SUCH DAMAGE.
35
- //
36
- // ------------------------------------------------------------------------
37
- //
38
- // Created 2000-09-30 by Jon Maloy
39
- //
40
- // ------------------------------------------------------------------------
41
- //
42
- // $Id: client_tipc.c,v 1.6 2005/09/20 20:23:15 ajstephens Exp $
43
- //
44
- // Revision history:
45
- // ----------------
46
- // Rev Date Rev by Reason
47
- // --- ---- ------ ------
48
- //
49
- // PA1 2000-09-30 Jon Maloy Created
50
- // PA2 2004-03-25 M. Pourzandi Simplified to support a simple hello message
51
- //
52
- // ------------------------------------------------------------------------
53
- */
2
+ *
3
+ * client_tipc.c
4
+ *
5
+ * Short description: TIPC multicast demo (client side)
6
+ *
7
+ * ------------------------------------------------------------------------
8
+ *
9
+ * Copyright (c) 2003, Ericsson Research Canada
10
+ * Copyright (c) 2005,2010 Wind River Systems
11
+ * All rights reserved.
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions are met:
14
+ *
15
+ * Redistributions of source code must retain the above copyright notice, this
16
+ * list of conditions and the following disclaimer.
17
+ * Redistributions in binary form must reproduce the above copyright notice,
18
+ * this list of conditions and the following disclaimer in the documentation
19
+ * and/or other materials provided with the distribution.
20
+ * Neither the name of the copyright holders nor the names of its
21
+ * contributors may be used to endorse or promote products derived from this
22
+ * software without specific prior written permission.
23
+ *
24
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
+ * POSSIBILITY OF SUCH DAMAGE.
35
+ *
36
+ * ------------------------------------------------------------------------
37
+ */
38
+
54
39
#include <sys/types.h>
55
40
#include <sys/socket.h>
56
41
#include <stdlib.h>
57
42
#include <stdio.h>
58
- #include <assert.h>
59
43
#include <string.h>
60
44
#include <sys/param.h>
61
- #include <netdb.h>
62
45
#include <errno.h>
63
46
#include <unistd.h>
64
- #include <sys/time.h>
65
- #include <ctype.h>
47
+ #include <netinet/in.h>
66
48
#include <linux/tipc.h>
67
49
68
50
#define SERVER_TYPE 18888
69
51
70
- void wait_for_server (int name_type , int name_inst , int wait )
52
+ void wait_for_server (__u32 name_type , __u32 name_instance , int wait )
71
53
{
72
- struct sockaddr_tipc topsrv ;
73
- struct tipc_subscr subscr = {{name_type , name_inst , ~0 }, wait ,
74
- TIPC_SUB_SERVICE , {}};
75
- struct tipc_event event ;
54
+ struct sockaddr_tipc topsrv ;
55
+ struct tipc_subscr subscr ;
56
+ struct tipc_event event ;
76
57
77
- int sd = socket (AF_TIPC , SOCK_SEQPACKET , 0 );
78
- assert (sd > 0 );
58
+ int sd = socket (AF_TIPC , SOCK_SEQPACKET , 0 );
79
59
80
- memset (& topsrv , 0 , sizeof (topsrv ));
60
+ memset (& topsrv , 0 , sizeof (topsrv ));
81
61
topsrv .family = AF_TIPC ;
82
- topsrv .addrtype = TIPC_ADDR_NAME ;
83
- topsrv .addr .name .name .type = TIPC_TOP_SRV ;
84
- topsrv .addr .name .name .instance = TIPC_TOP_SRV ;
62
+ topsrv .addrtype = TIPC_ADDR_NAME ;
63
+ topsrv .addr .name .name .type = TIPC_TOP_SRV ;
64
+ topsrv .addr .name .name .instance = TIPC_TOP_SRV ;
85
65
86
- if (0 > connect (sd , (struct sockaddr * )& topsrv , sizeof (topsrv ))) {
87
- perror ("failed to connect to topology server" );
88
- exit (1 );
89
- }
90
- if (send (sd , & subscr , sizeof (subscr ), 0 ) != sizeof (subscr )) {
91
- perror ("failed to send subscription" );
92
- exit (1 );
93
- }
94
- if (recv (sd , & event , sizeof (event ), 0 ) != sizeof (event )) {
95
- perror ("Failed to receive event" );
96
- exit (1 );
97
- }
98
- if (event .event != TIPC_PUBLISHED ) {
99
- printf ("Server %u not published within %u [s]\n" ,
100
- name_type , wait /1000 );
101
- exit (1 );
102
- }
103
- close (sd );
66
+ /* Connect to topology server */
67
+
68
+ if (0 > connect (sd , (struct sockaddr * )& topsrv , sizeof (topsrv ))) {
69
+ perror ("Client: failed to connect to topology server" );
70
+ exit (1 );
71
+ }
72
+
73
+ subscr .seq .type = htonl (name_type );
74
+ subscr .seq .lower = htonl (name_instance );
75
+ subscr .seq .upper = htonl (name_instance );
76
+ subscr .timeout = htonl (wait );
77
+ subscr .filter = htonl (TIPC_SUB_SERVICE );
78
+
79
+ if (send (sd , & subscr , sizeof (subscr ), 0 ) != sizeof (subscr )) {
80
+ perror ("Client: failed to send subscription" );
81
+ exit (1 );
82
+ }
83
+ /* Now wait for the subscription to fire */
84
+
85
+ if (recv (sd , & event , sizeof (event ), 0 ) != sizeof (event )) {
86
+ perror ("Client: failed to receive event" );
87
+ exit (1 );
88
+ }
89
+ if (event .event != htonl (TIPC_PUBLISHED )) {
90
+ printf ("Client: server {%u,%u} not published within %u [s]\n" ,
91
+ name_type , name_instance , wait /1000 );
92
+ exit (1 );
93
+ }
94
+
95
+ close (sd );
104
96
}
105
97
98
+
106
99
void client_mcast (int sd , int lower , int upper )
107
100
{
108
101
struct sockaddr_tipc server_addr ;
@@ -119,19 +112,20 @@ void client_mcast(int sd, int lower, int upper)
119
112
server_addr .addr .nameseq .type ,
120
113
server_addr .addr .nameseq .lower ,
121
114
server_addr .addr .nameseq .upper );
122
- printf ("Sending: %s\n" , buf );
115
+ printf ("Client: sending %s\n" , buf );
123
116
} else {
124
117
sd = - sd ;
125
118
buf [0 ] = '\0' ;
126
- printf ("Sending: termination message to {%u,%u,%u}\n" ,
119
+ printf ("Client: sending termination message to {%u,%u,%u}\n" ,
127
120
server_addr .addr .nameseq .type ,
128
121
server_addr .addr .nameseq .lower ,
129
122
server_addr .addr .nameseq .upper );
130
123
}
131
124
132
125
if (0 > sendto (sd , buf , strlen (buf ) + 1 , 0 ,
133
126
(struct sockaddr * )& server_addr , sizeof (server_addr ))) {
134
- perror ("Client: Failed to send" );
127
+ perror ("Client: failed to send" );
128
+ exit (1 );
135
129
}
136
130
}
137
131
@@ -149,12 +143,12 @@ void client_mcast(int sd, int lower, int upper)
149
143
* message.
150
144
*/
151
145
152
- int main (int argc , char * argv [], char * envp [])
146
+ int main (int argc , char * argv [], char * envp [])
153
147
{
154
- int sd = socket (AF_TIPC , SOCK_RDM , 0 );
155
148
int lower ;
156
149
int upper ;
157
150
char dummy ;
151
+ int sd = socket (AF_TIPC , SOCK_RDM , 0 );
158
152
159
153
if (sd < 0 ) {
160
154
printf ("TIPC not active on this node\n" );
@@ -180,11 +174,15 @@ int main(int argc, char* argv[], char* envp[])
180
174
181
175
/* multicast to instance range specified by user */
182
176
183
- if (sscanf (argv [1 ], "%u%c" , & lower , & dummy ) != 1 )
184
- perror ("invalid lower bound" );
177
+ if (sscanf (argv [1 ], "%u%c" , & lower , & dummy ) != 1 ) {
178
+ perror ("Client: invalid lower bound" );
179
+ exit (1 );
180
+ }
185
181
if (argc > 2 ) {
186
- if (sscanf (argv [2 ], "%u%c" , & upper , & dummy ) != 1 )
187
- perror ("invalid upper bound" );
182
+ if (sscanf (argv [2 ], "%u%c" , & upper , & dummy ) != 1 ) {
183
+ perror ("Client: invalid upper bound" );
184
+ exit (1 );
185
+ }
188
186
}
189
187
else
190
188
upper = lower ;
0 commit comments