Skip to content

Instantly share code, notes, and snippets.

@sfan5
Last active December 16, 2015 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfan5/5462759 to your computer and use it in GitHub Desktop.
Save sfan5/5462759 to your computer and use it in GitHub Desktop.
Creates two FIFO files that are piped to a network connection.
/* NetFIFO
* Creates two FIFO files that are piped to a network connection.
* Made by sfan5
* License: CC-BY-SA v3
*
* Example Usage:
* netfifo tcp google.com 80 ififo ofifo &
* echo -e "GET / HTTP/1.1 \r\n" > ififo
* cat ofifo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#define log(e) { fprintf(stderr, "[NetFIFO] LOG %s\n", e); }
#define warn(e) { fprintf(stderr, "[NetFIFO] WARN %s\n", e); }
#define error(e) { fprintf(stderr, "[NetFIFO] ERR %s\n", e); perror(""); return EXIT_FAILURE; }
#define BUFLEN 256
#define VERSION "0.1"
//#define DEBUG
unsigned short run;
void sigfunc(int sig)
{
if(sig == SIGINT || sig == SIGTERM)
{
run = 0;
}
}
int main(int argc, char *argv[])
{
printf("NetFIFO %s\n", VERSION);
signal(SIGINT, sigfunc);
signal(SIGTERM, sigfunc);
if(argc < 5)
{
printf("Usage: %s <protocol> <address> <port> <inpipe> <outpipe>\n", argv[0]);
return EXIT_SUCCESS;
}
int sockfd, portno, n, socktype, wfd, rfd;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[BUFLEN];
if(!strcmp(argv[1], "tcp"))
{
socktype = SOCK_STREAM;
}
else if(!strcmp(argv[1], "udp"))
{
socktype = SOCK_DGRAM;
}
else error("Unknown protocol");
portno = atoi(argv[3]);
sockfd = socket(AF_INET, socktype, 0);
if(sockfd < 0) error("Could not open socket");
server = gethostbyname(argv[2]);
if(!server)
{
fprintf(stderr, "%s\n", hstrerror(h_errno));
error("gethostbyname failed");
}
bzero((char*) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char*) server->h_addr, (char*) &serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
if(connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) error("Could not connect");
fcntl(sockfd, F_SETFL, O_NONBLOCK);
log("Connected to Server");
if(mkfifo(argv[4], S_IRWXU | S_IRWXG) != 0) error("Could not create FIFO");
if(mkfifo(argv[5], S_IRWXU | S_IRWXG) != 0) error("Could not create FIFO");
if((rfd = open(argv[4], O_RDONLY | O_NONBLOCK)) < 0) error("Could not open FIFO for reading");
if((wfd = open(argv[5], O_WRONLY)) < 0) error("Could not open FIFO for writing");
log("Ready!");
run = 1;
while(run)
{
bzero(buffer, BUFLEN);
n = read(rfd, buffer, BUFLEN);
if(n > 0)
{
#ifdef DEBUG
printf(" FIFO -> Socket %d bytes\n", n);
#endif
n = write(sockfd, buffer, n);
if(n < 0) warn("Could not write to Socket");
}
bzero(buffer, BUFLEN);
n = read(sockfd, buffer, BUFLEN);
if(n > 0)
{
#ifdef DEBUG
printf(" Socket -> FIFO %d bytes\n", n);
#endif
n = write(wfd, buffer, n);
if(n < 0) warn("Could not write to FIFO");
}
}
log("Closing!");
close(sockfd);
close(rfd);
close(wfd);
unlink(argv[4]);
unlink(argv[5]);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment