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>
16 #include "gras_private.h"
17 #include "transport_private.h"
19 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_file,transport);
24 void gras_trp_file_close(gras_socket_t *sd);
26 gras_error_t gras_trp_file_chunk_send(gras_socket_t *sd,
30 gras_error_t gras_trp_file_chunk_recv(gras_socket_t *sd,
36 *** Specific plugin part
40 fd_set incoming_socks;
41 } gras_trp_file_plug_data_t;
44 *** Specific socket part
53 gras_trp_file_setup(gras_trp_plugin_t *plug) {
55 gras_trp_file_plug_data_t *file = malloc(sizeof(gras_trp_file_plug_data_t));
59 FD_ZERO(&(file->incoming_socks));
61 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;
66 plug->data = (void*)file;
72 * gras_socket_client_from_file:
74 * Create a client socket from a file path.
76 * This only possible in RL, and is mainly for debugging.
79 gras_socket_client_from_file(const char*path,
80 /* OUT */ gras_socket_t **dst) {
82 gras_trp_plugin_t *trp;
84 gras_assert0(gras_if_RL(),
85 "Cannot use file as socket in the simulator");
87 TRY(gras_trp_socket_new(0,dst));
89 TRY(gras_trp_plugin_get_by_name("file",&trp));
92 if (strcmp("-", path)) {
93 (*dst)->sd = open(path, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP );
95 if ( (*dst)->sd < 0) {
97 "Cannot create a client socket from file %s: %s",
98 path, strerror(errno));
101 (*dst)->sd = 1; /* stdout */
104 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
107 (*dst)->incoming?'y':'n',
108 (*dst)->outgoing?'y':'n',
109 (*dst)->accepting?'y':'n');
110 /* register socket */
111 errcode = gras_dynar_push(_gras_trp_sockets,dst);
112 if (errcode != no_error) {
122 * gras_socket_server_from_file:
124 * Create a server socket from a file path.
126 * This only possible in RL, and is mainly for debugging.
129 gras_socket_server_from_file(const char*path,
130 /* OUT */ gras_socket_t **dst) {
131 gras_error_t errcode;
132 gras_trp_plugin_t *trp;
134 gras_assert0(gras_if_RL(),
135 "Cannot use file as socket in the simulator");
137 TRY(gras_trp_socket_new(1,dst));
139 TRY(gras_trp_plugin_get_by_name("file",&trp));
143 if (strcmp("-", path)) {
144 (*dst)->sd = open(path, O_RDONLY );
146 if ( (*dst)->sd < 0) {
148 "Cannot create a server socket from file %s: %s",
149 path, strerror(errno));
152 (*dst)->sd = 0; /* stdin */
155 DEBUG4("sd=%d in=%c out=%c accept=%c",
157 (*dst)->incoming?'y':'n',
158 (*dst)->outgoing?'y':'n',
159 (*dst)->accepting?'y':'n');
161 /* register socket */
162 errcode = gras_dynar_push(_gras_trp_sockets,dst);
163 if (errcode != no_error) {
172 void gras_trp_file_close(gras_socket_t *sock){
173 gras_trp_file_plug_data_t *data;
175 if (!sock) return; /* close only once */
176 data=sock->plugin->data;
179 DEBUG0("Do not close stdin");
180 } else if (sock->sd == 1) {
181 DEBUG0("Do not close stdout");
183 DEBUG1("close file connection %d", sock->sd);
185 /* forget about the socket */
186 FD_CLR(sock->sd, &(data->incoming_socks));
188 /* close the socket */
189 if(close(sock->sd) < 0) {
190 WARN2("error while closing file %d: %s",
191 sock->sd, strerror(errno));
197 * gras_trp_file_chunk_send:
199 * Send data on a file pseudo-socket
202 gras_trp_file_chunk_send(gras_socket_t *sock,
206 gras_assert0(sock->outgoing, "Cannot write on client file socket");
207 gras_assert0(size >= 0, "Cannot send a negative amount of data");
212 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (size_t)size);
213 status = write(sock->sd, data, (size_t)size);
216 RAISE4(system_error,"write(%d,%p,%d) failed: %s",
217 sock->sd, data, (int)size,
225 RAISE0(system_error,"file descriptor closed");
232 * gras_trp_file_chunk_recv:
234 * Receive data on a file pseudo-socket.
237 gras_trp_file_chunk_recv(gras_socket_t *sock,
241 gras_assert0(sock, "Cannot recv on an NULL socket");
242 gras_assert0(sock->incoming, "Cannot recv on client file socket");
243 gras_assert0(size >= 0, "Cannot receive a negative amount of data");
248 status = read(sock->sd, data, (size_t)size);
249 DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
252 RAISE4(system_error,"read(%d,%p,%d) failed: %s",
253 sock->sd, data, (int)size,
261 RAISE0(system_error,"file descriptor closed");