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 "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);
59 FD_ZERO(&(file->incoming_socks));
61 plug->socket_close = gras_trp_file_close;
62 plug->chunk_send = gras_trp_file_chunk_send;
63 plug->chunk_recv = gras_trp_file_chunk_recv;
64 plug->data = (void*)file;
70 * gras_socket_client_from_file:
72 * Create a client socket from a file path.
74 * This only possible in RL, and is mainly for debugging.
77 gras_socket_client_from_file(const char*path,
78 /* OUT */ gras_socket_t *dst) {
80 gras_trp_plugin_t *trp;
82 gras_assert0(gras_if_RL(),
83 "Cannot use file as socket in the simulator");
85 gras_trp_socket_new(0,dst);
87 TRY(gras_trp_plugin_get_by_name("file",&trp));
90 if (strcmp("-", path)) {
91 (*dst)->sd = open(path, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP );
93 if ( (*dst)->sd < 0) {
95 "Cannot create a client socket from file %s: %s",
96 path, strerror(errno));
99 (*dst)->sd = 1; /* stdout */
102 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
105 (*dst)->incoming?'y':'n',
106 (*dst)->outgoing?'y':'n',
107 (*dst)->accepting?'y':'n');
113 * gras_socket_server_from_file:
115 * Create a server socket from a file path.
117 * This only possible in RL, and is mainly for debugging.
120 gras_socket_server_from_file(const char*path,
121 /* OUT */ gras_socket_t *dst) {
122 gras_error_t errcode;
123 gras_trp_plugin_t *trp;
125 gras_assert0(gras_if_RL(),
126 "Cannot use file as socket in the simulator");
128 gras_trp_socket_new(1,dst);
130 TRY(gras_trp_plugin_get_by_name("file",&trp));
134 if (strcmp("-", path)) {
135 (*dst)->sd = open(path, O_RDONLY );
137 if ( (*dst)->sd < 0) {
139 "Cannot create a server socket from file %s: %s",
140 path, strerror(errno));
143 (*dst)->sd = 0; /* stdin */
146 DEBUG4("sd=%d in=%c out=%c accept=%c",
148 (*dst)->incoming?'y':'n',
149 (*dst)->outgoing?'y':'n',
150 (*dst)->accepting?'y':'n');
155 void gras_trp_file_close(gras_socket_t sock){
156 gras_trp_file_plug_data_t *data;
158 if (!sock) return; /* close only once */
159 data=sock->plugin->data;
162 DEBUG0("Do not close stdin");
163 } else if (sock->sd == 1) {
164 DEBUG0("Do not close stdout");
166 DEBUG1("close file connection %d", sock->sd);
168 /* forget about the socket */
169 FD_CLR(sock->sd, &(data->incoming_socks));
171 /* close the socket */
172 if(close(sock->sd) < 0) {
173 WARN2("error while closing file %d: %s",
174 sock->sd, strerror(errno));
180 * gras_trp_file_chunk_send:
182 * Send data on a file pseudo-socket
185 gras_trp_file_chunk_send(gras_socket_t sock,
189 gras_assert0(sock->outgoing, "Cannot write on client file socket");
190 gras_assert0(size >= 0, "Cannot send a negative amount of data");
195 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
196 status = write(sock->sd, data, (long int)size);
199 RAISE4(system_error,"write(%d,%p,%d) failed: %s",
200 sock->sd, data, (int)size,
208 RAISE0(system_error,"file descriptor closed");
215 * gras_trp_file_chunk_recv:
217 * Receive data on a file pseudo-socket.
220 gras_trp_file_chunk_recv(gras_socket_t sock,
224 gras_assert0(sock, "Cannot recv on an NULL socket");
225 gras_assert0(sock->incoming, "Cannot recv on client file socket");
226 gras_assert0(size >= 0, "Cannot receive a negative amount of data");
231 status = read(sock->sd, data, (long int)size);
232 DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
235 RAISE4(system_error,"read(%d,%p,%d) failed: %s",
236 sock->sd, data, (int)size,
244 RAISE0(system_error,"file descriptor closed");