#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
+#include <sys/un.h>
#include <unistd.h>
extern char**environ;
"\r\n" \
"CGI Bridge not working correctly.\r\n"
+#ifdef DEBUG
+#define dprint(args...) fprintf(stderr,args)
+#else
+#define dprint(args...)
+#endif
+
void encodeEnviron()
{
char *evc,**evn;
char*scgihost=0;
char*scgiport=0;
+#ifndef UNIX_PATH_MAX
+#define UNIX_PATH_MAX 108
+#endif
+
void openFile()
{
+ struct sockaddr_un sa;
if(scgifile==0)return;
- scfd=open(scgifile,O_RDWR);
+ dprint("Attempting to connect to file socket %s\n",scgifile);
+ if(strlen(scgifile)>=UNIX_PATH_MAX)return;
+ scfd=socket(AF_UNIX,SOCK_STREAM,0);
+ memset(&sa,0,sizeof(sa));
+ sa.sun_family=AF_UNIX;
+ strncpy(sa.sun_path,scgifile,UNIX_PATH_MAX);
+ if(connect(scfd,(struct sockaddr*)&sa,sizeof(sa))!=0){
+ dprint("Unable to connect to %s: %s\n",scgifile,strerror(errno));
+ scfd=-1;
+ }else{
+ dprint("Connected to file socket %s\n",scgifile);
+ }
}
void openNet()
struct addrinfo *ai,*rai;
//sanity checks and get port
if(scgihost==0 || scgiport==0)return;
+ dprint("Attempting to connect to host %s port %s\n",scgihost,scgiport);
port=strtol(scgiport,&ep,10);
if(*ep!=0)return;
if(port<=0 || port > 0xffff)return;
memcpy(&sa.sin_addr, &((struct sockaddr_in*)(ai->ai_addr))->sin_addr, sizeof(sa.sin_addr));
if(connect(sfd,(struct sockaddr*)&sa,sizeof(sa))==0){
scfd=sfd;
+ dprint("Successfully connected.\n");
break;
}else
close(sfd);
memcpy(&sa.sin6_addr, &((struct sockaddr_in6*)(ai->ai_addr))->sin6_addr, sizeof(sa.sin6_addr));
if(connect(sfd,(struct sockaddr*)&sa,sizeof(sa))==0){
scfd=sfd;
+ dprint("Successfully connected.\n");
break;
}else
close(sfd);
return 1;
}
//send request
+ dprint("Encoding environment...\n");
encodeEnviron();
+ dprint("Relaying request body...\n");
relayBody();
//relay response
+ dprint("Relaying response...\n");
relayResponse();
return 0;