Debian Codegraph @

src/clients/spdsend/client.c

raw
1 /* client.c -- Client part of spdsend
2 Author: Milan Zamazal <[email protected]>
3 */
4
5 /* Copyright (C) 2004 Brailcom, o.p.s.
6
7 COPYRIGHT NOTICE
8
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "spdsend.h"
28
29 #include <stddef.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 static Success send_header(Stream s, Action action, Connection_Id id)
34 {
35 if (write_data(s, &action, sizeof(Action)) == OK &&
36 write_data(s, &id, sizeof(Connection_Id)) == OK)
37 return OK;
38 else
39 return ERROR;
40 }
41
42 #define SEND_HEADER(action) \
43 if (send_header (s, action, id) == ERROR) \
44 return ERROR
45
46 static Success send_open_header(Stream s, const char *host, int port)
47 {
48 int hostlen = strlen(host);
49 Action action = A_OPEN;
50
51 if (write_data(s, &action, sizeof(Action)) == OK &&
52 write_data(s, &port, sizeof(int)) == OK &&
53 write_data(s, &hostlen, sizeof(int)) == OK &&
54 write_data(s, host, hostlen) == OK)
55 return OK;
56 else
57 return ERROR;
58 }
59
60 static Connection_Id read_reply(Stream s)
61 {
62 Result result;
63 Connection_Id id;
64
65 if (read_data(s, &result, sizeof(Result)) != sizeof(Result))
66 return NONE;
67 if (result != OK_CODE)
68 return NONE;
69
70 if (read_data(s, &id, sizeof(Connection_Id)) != sizeof(Connection_Id))
71 return NONE;
72
73 return id;
74 }
75
76 /* External functions */
77
78 extern Success open_connection(Stream s, const char *host, int port)
79 {
80 if (send_open_header(s, host, port) == ERROR)
81 return ERROR;
82 {
83 Connection_Id id = read_reply(s);
84 if (id == NONE)
85 return ERROR;
86 printf("%d\n", id);
87 }
88 return OK;
89 }
90
91 extern Success close_connection(Stream s, Connection_Id id)
92 {
93 SEND_HEADER(A_CLOSE);
94 return (read_reply(s) == OK ? OK : ERROR);
95 }
96
97 extern Success send_command(Stream s, Connection_Id id)
98 {
99 SEND_HEADER(A_DATA);
100 if (read_reply(s) == NONE)
101 return ERROR;
102 if (forward_data(0, s, true) == OK && forward_data(s, 1, false) == OK)
103 return OK;
104 else
105 return ERROR;
106 }