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);
23 } gras_trp_tcp_sock_specific_t;
28 void gras_trp_file_close(gras_socket_t *sd);
30 gras_error_t gras_trp_file_chunk_send(gras_socket_t *sd,
34 gras_error_t gras_trp_file_chunk_recv(gras_socket_t *sd,
38 void gras_trp_file_free_specific(void *s);
41 *** Specific plugin part
45 fd_set incoming_socks;
46 } gras_trp_file_specific_t;
49 *** Specific socket part
57 gras_trp_file_init(gras_trp_plugin_t **dst) {
59 gras_trp_plugin_t *res=malloc(sizeof(gras_trp_plugin_t));
60 gras_trp_file_specific_t *specif = malloc(sizeof(gras_trp_file_specific_t));
64 FD_ZERO(&(specif->incoming_socks));
66 res->name = strdup("file");
67 res->socket_client = NULL;
68 res->socket_server = NULL;
69 res->socket_accept = NULL;
70 res->socket_close = gras_trp_file_close;
72 res->chunk_send = gras_trp_file_chunk_send;
73 res->chunk_recv = gras_trp_file_chunk_recv;
75 res->specific = (void*)specif;
76 res->free_specific = gras_trp_file_free_specific;
82 void gras_trp_file_free_specific(void *s) {
83 gras_trp_file_specific_t *specific = s;
88 * gras_socket_client_from_file:
90 * Create a client socket from a file path.
92 * This only possible in RL, and is mainly for debugging.
95 gras_socket_client_from_file(const char*path,
96 /* OUT */ gras_socket_t **dst) {
98 gras_trp_plugin_t *trp;
100 gras_assert0(gras_if_RL(),
101 "Cannot use file as socket in the simulator");
103 TRY(gras_trp_socket_new(0,dst));
105 TRY(gras_trp_plugin_get_by_name("file",&trp));
108 if (strcmp("-", path)) {
109 (*dst)->sd = open(path, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP );
111 if ( (*dst)->sd < 0) {
113 "Cannot create a client socket from file %s: %s",
114 path, strerror(errno));
117 (*dst)->sd = 1; /* stdout */
120 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
123 (*dst)->incoming?'y':'n',
124 (*dst)->outgoing?'y':'n',
125 (*dst)->accepting?'y':'n');
126 /* register socket */
127 errcode = gras_dynar_push(_gras_trp_sockets,dst);
128 if (errcode != no_error) {
138 * gras_socket_server_from_file:
140 * Create a server socket from a file path.
142 * This only possible in RL, and is mainly for debugging.
145 gras_socket_server_from_file(const char*path,
146 /* OUT */ gras_socket_t **dst) {
147 gras_error_t errcode;
148 gras_trp_plugin_t *trp;
150 gras_assert0(gras_if_RL(),
151 "Cannot use file as socket in the simulator");
153 TRY(gras_trp_socket_new(1,dst));
155 TRY(gras_trp_plugin_get_by_name("file",&trp));
159 if (strcmp("-", path)) {
160 (*dst)->sd = open(path, O_RDONLY );
162 if ( (*dst)->sd < 0) {
164 "Cannot create a server socket from file %s: %s",
165 path, strerror(errno));
168 (*dst)->sd = 0; /* stdin */
171 DEBUG4("sd=%d in=%c out=%c accept=%c",
173 (*dst)->incoming?'y':'n',
174 (*dst)->outgoing?'y':'n',
175 (*dst)->accepting?'y':'n');
177 /* register socket */
178 errcode = gras_dynar_push(_gras_trp_sockets,dst);
179 if (errcode != no_error) {
188 void gras_trp_file_close(gras_socket_t *sock){
189 gras_trp_file_specific_t *specific;
191 if (!sock) return; /* close only once */
192 specific=sock->plugin->specific;
195 DEBUG0("Do not close stdin");
196 } else if (sock->sd == 1) {
197 DEBUG0("Do not close stdout");
199 DEBUG1("close file connection %d", sock->sd);
201 /* forget about the socket */
202 FD_CLR(sock->sd, &(specific->incoming_socks));
204 /* close the socket */
205 if(close(sock->sd) < 0) {
206 WARN2("error while closing file %d: %s",
207 sock->sd, strerror(errno));
213 * gras_trp_file_chunk_send:
215 * Send data on a file pseudo-socket
218 gras_trp_file_chunk_send(gras_socket_t *sock,
222 gras_assert0(size >= 0, "Cannot send a negative amount of data");
227 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (size_t)size);
228 status = write(sock->sd, data, (size_t)size);
231 RAISE4(system_error,"write(%d,%p,%d) failed: %s",
232 sock->sd, data, (int)size,
240 RAISE0(system_error,"file descriptor closed");
247 * gras_trp_file_chunk_recv:
249 * Receive data on a file pseudo-socket.
252 gras_trp_file_chunk_recv(gras_socket_t *sock,
256 /* TCP sockets are in duplex mode, don't check direction */
257 gras_assert0(sock, "Cannot recv on an NULL socket");
258 gras_assert0(size >= 0, "Cannot receive a negative amount of data");
263 status = read(sock->sd, data, (size_t)size);
264 DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
267 RAISE4(system_error,"read(%d,%p,%d) failed: %s",
268 sock->sd, data, (int)size,
276 RAISE0(system_error,"file descriptor closed");