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);
62 FD_ZERO(&(file->incoming_socks));
64 plug->socket_close = gras_trp_file_close;
66 plug->chunk_send = gras_trp_file_chunk_send;
67 plug->chunk_recv = gras_trp_file_chunk_recv;
69 plug->data = (void*)file;
75 * gras_socket_client_from_file:
77 * Create a client socket from a file path.
79 * This only possible in RL, and is mainly for debugging.
82 gras_socket_client_from_file(const char*path,
83 /* OUT */ gras_socket_t **dst) {
85 gras_trp_plugin_t *trp;
87 gras_assert0(gras_if_RL(),
88 "Cannot use file as socket in the simulator");
90 TRY(gras_trp_socket_new(0,dst));
92 TRY(gras_trp_plugin_get_by_name("file",&trp));
95 if (strcmp("-", path)) {
96 (*dst)->sd = open(path, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP );
98 if ( (*dst)->sd < 0) {
100 "Cannot create a client socket from file %s: %s",
101 path, strerror(errno));
104 (*dst)->sd = 1; /* stdout */
107 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
110 (*dst)->incoming?'y':'n',
111 (*dst)->outgoing?'y':'n',
112 (*dst)->accepting?'y':'n');
118 * gras_socket_server_from_file:
120 * Create a server socket from a file path.
122 * This only possible in RL, and is mainly for debugging.
125 gras_socket_server_from_file(const char*path,
126 /* OUT */ gras_socket_t **dst) {
127 gras_error_t errcode;
128 gras_trp_plugin_t *trp;
130 gras_assert0(gras_if_RL(),
131 "Cannot use file as socket in the simulator");
133 TRY(gras_trp_socket_new(1,dst));
135 TRY(gras_trp_plugin_get_by_name("file",&trp));
139 if (strcmp("-", path)) {
140 (*dst)->sd = open(path, O_RDONLY );
142 if ( (*dst)->sd < 0) {
144 "Cannot create a server socket from file %s: %s",
145 path, strerror(errno));
148 (*dst)->sd = 0; /* stdin */
151 DEBUG4("sd=%d in=%c out=%c accept=%c",
153 (*dst)->incoming?'y':'n',
154 (*dst)->outgoing?'y':'n',
155 (*dst)->accepting?'y':'n');
160 void gras_trp_file_close(gras_socket_t *sock){
161 gras_trp_file_plug_data_t *data;
163 if (!sock) return; /* close only once */
164 data=sock->plugin->data;
167 DEBUG0("Do not close stdin");
168 } else if (sock->sd == 1) {
169 DEBUG0("Do not close stdout");
171 DEBUG1("close file connection %d", sock->sd);
173 /* forget about the socket */
174 FD_CLR(sock->sd, &(data->incoming_socks));
176 /* close the socket */
177 if(close(sock->sd) < 0) {
178 WARN2("error while closing file %d: %s",
179 sock->sd, strerror(errno));
185 * gras_trp_file_chunk_send:
187 * Send data on a file pseudo-socket
190 gras_trp_file_chunk_send(gras_socket_t *sock,
194 gras_assert0(sock->outgoing, "Cannot write on client file socket");
195 gras_assert0(size >= 0, "Cannot send a negative amount of data");
200 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
201 status = write(sock->sd, data, (long int)size);
204 RAISE4(system_error,"write(%d,%p,%d) failed: %s",
205 sock->sd, data, (int)size,
213 RAISE0(system_error,"file descriptor closed");
220 * gras_trp_file_chunk_recv:
222 * Receive data on a file pseudo-socket.
225 gras_trp_file_chunk_recv(gras_socket_t *sock,
229 gras_assert0(sock, "Cannot recv on an NULL socket");
230 gras_assert0(sock->incoming, "Cannot recv on client file socket");
231 gras_assert0(size >= 0, "Cannot receive a negative amount of data");
236 status = read(sock->sd, data, (long int)size);
237 DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
240 RAISE4(system_error,"read(%d,%p,%d) failed: %s",
241 sock->sd, data, (int)size,
249 RAISE0(system_error,"file descriptor closed");