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. */
13 #include <sys/types.h>
18 #include "gras_private.h"
19 #include "transport_private.h"
21 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_file,transport,
22 "Pseudo-transport to write to/read from a file");
27 void gras_trp_file_close(gras_socket_t *sd);
29 gras_error_t gras_trp_file_chunk_send(gras_socket_t *sd,
33 gras_error_t gras_trp_file_chunk_recv(gras_socket_t *sd,
39 *** Specific plugin part
43 fd_set incoming_socks;
44 } gras_trp_file_plug_data_t;
47 *** Specific socket part
56 gras_trp_file_setup(gras_trp_plugin_t *plug) {
58 gras_trp_file_plug_data_t *file = gras_new(gras_trp_file_plug_data_t,1);
60 FD_ZERO(&(file->incoming_socks));
62 plug->socket_close = gras_trp_file_close;
63 plug->chunk_send = gras_trp_file_chunk_send;
64 plug->chunk_recv = gras_trp_file_chunk_recv;
65 plug->data = (void*)file;
71 * gras_socket_client_from_file:
73 * Create a client socket from a file path.
75 * This only possible in RL, and is mainly for debugging.
78 gras_socket_client_from_file(const char*path,
79 /* OUT */ gras_socket_t **dst) {
81 gras_trp_plugin_t *trp;
83 gras_assert0(gras_if_RL(),
84 "Cannot use file as socket in the simulator");
86 gras_trp_socket_new(0,dst);
88 TRY(gras_trp_plugin_get_by_name("file",&trp));
91 if (strcmp("-", path)) {
92 (*dst)->sd = open(path, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP );
94 if ( (*dst)->sd < 0) {
96 "Cannot create a client socket from file %s: %s",
97 path, strerror(errno));
100 (*dst)->sd = 1; /* stdout */
103 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
106 (*dst)->incoming?'y':'n',
107 (*dst)->outgoing?'y':'n',
108 (*dst)->accepting?'y':'n');
114 * gras_socket_server_from_file:
116 * Create a server socket from a file path.
118 * This only possible in RL, and is mainly for debugging.
121 gras_socket_server_from_file(const char*path,
122 /* OUT */ gras_socket_t **dst) {
123 gras_error_t errcode;
124 gras_trp_plugin_t *trp;
126 gras_assert0(gras_if_RL(),
127 "Cannot use file as socket in the simulator");
129 gras_trp_socket_new(1,dst);
131 TRY(gras_trp_plugin_get_by_name("file",&trp));
135 if (strcmp("-", path)) {
136 (*dst)->sd = open(path, O_RDONLY );
138 if ( (*dst)->sd < 0) {
140 "Cannot create a server socket from file %s: %s",
141 path, strerror(errno));
144 (*dst)->sd = 0; /* stdin */
147 DEBUG4("sd=%d in=%c out=%c accept=%c",
149 (*dst)->incoming?'y':'n',
150 (*dst)->outgoing?'y':'n',
151 (*dst)->accepting?'y':'n');
156 void gras_trp_file_close(gras_socket_t *sock){
157 gras_trp_file_plug_data_t *data;
159 if (!sock) return; /* close only once */
160 data=sock->plugin->data;
163 DEBUG0("Do not close stdin");
164 } else if (sock->sd == 1) {
165 DEBUG0("Do not close stdout");
167 DEBUG1("close file connection %d", sock->sd);
169 /* forget about the socket */
170 FD_CLR(sock->sd, &(data->incoming_socks));
172 /* close the socket */
173 if(close(sock->sd) < 0) {
174 WARN2("error while closing file %d: %s",
175 sock->sd, strerror(errno));
181 * gras_trp_file_chunk_send:
183 * Send data on a file pseudo-socket
186 gras_trp_file_chunk_send(gras_socket_t *sock,
190 gras_assert0(sock->outgoing, "Cannot write on client file socket");
191 gras_assert0(size >= 0, "Cannot send a negative amount of data");
196 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
197 status = write(sock->sd, data, (long int)size);
200 RAISE4(system_error,"write(%d,%p,%d) failed: %s",
201 sock->sd, data, (int)size,
209 RAISE0(system_error,"file descriptor closed");
216 * gras_trp_file_chunk_recv:
218 * Receive data on a file pseudo-socket.
221 gras_trp_file_chunk_recv(gras_socket_t *sock,
225 gras_assert0(sock, "Cannot recv on an NULL socket");
226 gras_assert0(sock->incoming, "Cannot recv on client file socket");
227 gras_assert0(size >= 0, "Cannot receive a negative amount of data");
232 status = read(sock->sd, data, (long int)size);
233 DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
236 RAISE4(system_error,"read(%d,%p,%d) failed: %s",
237 sock->sd, data, (int)size,
245 RAISE0(system_error,"file descriptor closed");