fix local socket path
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Tue, 21 Dec 2010 14:53:21 +0000 (14:53 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Tue, 21 Dec 2010 14:53:21 +0000 (14:53 +0000)
git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@666 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

cgi2scgi/Makefile
cgi2scgi/main.c

index c437455..bd02cf5 100644 (file)
@@ -8,7 +8,7 @@ CC      = gcc
 #compiler flags
 
 # version for development
-CFLAGS = -Wall -g
+CFLAGS = -Wall -g -DDEBUG
 #version for production
 #CFLAGS        = -O2 -s
 
index aa34711..37b8fe4 100644 (file)
@@ -28,6 +28,7 @@
 #include <string.h>
 #include <sys/socket.h>
 #include <sys/types.h>
+#include <sys/un.h>
 #include <unistd.h>
 
 extern char**environ;
@@ -45,6 +46,12 @@ int scfd=-1;
  "\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;
@@ -148,10 +155,26 @@ char*scgifile=0;
 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()
@@ -161,6 +184,7 @@ 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;
@@ -183,6 +207,7 @@ void openNet()
                        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);
@@ -195,6 +220,7 @@ void openNet()
                        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);
@@ -237,9 +263,12 @@ int main(int argc,char**argv)
                return 1;
        }
        //send request
+       dprint("Encoding environment...\n");
        encodeEnviron();
+       dprint("Relaying request body...\n");
        relayBody();
        //relay response
+       dprint("Relaying response...\n");
        relayResponse();
        
        return 0;