3 /* File transport - send/receive a bunch of bytes from a file */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2004 Martin Quinson. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
12 #include <sys/types.h>
17 #include "gras_private.h"
18 #include "transport_private.h"
20 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_file,transport,
21 "Pseudo-transport to write to/read from a file");
26 void gras_trp_file_close(gras_socket_t *sd);
28 gras_error_t gras_trp_file_chunk_send(gras_socket_t *sd,
32 gras_error_t gras_trp_file_chunk_recv(gras_socket_t *sd,
38 *** Specific plugin part
42 fd_set incoming_socks;
43 } gras_trp_file_plug_data_t;
46 *** Specific socket part
55 gras_trp_file_setup(gras_trp_plugin_t *plug) {
57 gras_trp_file_plug_data_t *file = gras_new(gras_trp_file_plug_data_t,1);
61 FD_ZERO(&(file->incoming_socks));
63 plug->socket_close = gras_trp_file_close;
65 plug->chunk_send = gras_trp_file_chunk_send;
66 plug->chunk_recv = gras_trp_file_chunk_recv;
68 plug->data = (void*)file;
74 * gras_socket_client_from_file:
76 * Create a client socket from a file path.
78 * This only possible in RL, and is mainly for debugging.
81 gras_socket_client_from_file(const char*path,
82 /* OUT */ gras_socket_t **dst) {
84 gras_trp_plugin_t *trp;
86 gras_assert0(gras_if_RL(),
87 "Cannot use file as socket in the simulator");
89 TRY(gras_trp_socket_new(0,dst));
91 TRY(gras_trp_plugin_get_by_name("file",&trp));
94 if (strcmp("-", path)) {
95 (*dst)->sd = open(path, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP );
97 if ( (*dst)->sd < 0) {
99 "Cannot create a client socket from file %s: %s",
100 path, strerror(errno));
103 (*dst)->sd = 1; /* stdout */
106 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
109 (*dst)->incoming?'y':'n',
110 (*dst)->outgoing?'y':'n',
111 (*dst)->accepting?'y':'n');
117 * gras_socket_server_from_file:
119 * Create a server socket from a file path.
121 * This only possible in RL, and is mainly for debugging.
124 gras_socket_server_from_file(const char*path,
125 /* OUT */ gras_socket_t **dst) {
126 gras_error_t errcode;
127 gras_trp_plugin_t *trp;
129 gras_assert0(gras_if_RL(),
130 "Cannot use file as socket in the simulator");
132 TRY(gras_trp_socket_new(1,dst));
134 TRY(gras_trp_plugin_get_by_name("file",&trp));
138 if (strcmp("-", path)) {
139 (*dst)->sd = open(path, O_RDONLY );
141 if ( (*dst)->sd < 0) {
143 "Cannot create a server socket from file %s: %s",
144 path, strerror(errno));
147 (*dst)->sd = 0; /* stdin */
150 DEBUG4("sd=%d in=%c out=%c accept=%c",
152 (*dst)->incoming?'y':'n',
153 (*dst)->outgoing?'y':'n',
154 (*dst)->accepting?'y':'n');
159 void gras_trp_file_close(gras_socket_t *sock){
160 gras_trp_file_plug_data_t *data;
162 if (!sock) return; /* close only once */
163 data=sock->plugin->data;
166 DEBUG0("Do not close stdin");
167 } else if (sock->sd == 1) {
168 DEBUG0("Do not close stdout");
170 DEBUG1("close file connection %d", sock->sd);
172 /* forget about the socket */
173 FD_CLR(sock->sd, &(data->incoming_socks));
175 /* close the socket */
176 if(close(sock->sd) < 0) {
177 WARN2("error while closing file %d: %s",
178 sock->sd, strerror(errno));
184 * gras_trp_file_chunk_send:
186 * Send data on a file pseudo-socket
189 gras_trp_file_chunk_send(gras_socket_t *sock,
193 gras_assert0(sock->outgoing, "Cannot write on client file socket");
194 gras_assert0(size >= 0, "Cannot send a negative amount of data");
199 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
200 status = write(sock->sd, data, (long int)size);
203 RAISE4(system_error,"write(%d,%p,%d) failed: %s",
204 sock->sd, data, (int)size,
212 RAISE0(system_error,"file descriptor closed");
219 * gras_trp_file_chunk_recv:
221 * Receive data on a file pseudo-socket.
224 gras_trp_file_chunk_recv(gras_socket_t *sock,
228 gras_assert0(sock, "Cannot recv on an NULL socket");
229 gras_assert0(sock->incoming, "Cannot recv on client file socket");
230 gras_assert0(size >= 0, "Cannot receive a negative amount of data");
235 status = read(sock->sd, data, (long int)size);
236 DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
239 RAISE4(system_error,"read(%d,%p,%d) failed: %s",
240 sock->sd, data, (int)size,
248 RAISE0(system_error,"file descriptor closed");