repo: shell-daemons
action: blob
revision: 
path_from: ident-full.c
revision_from: refs/heads/master:
path_to: 
revision_to: 
git.thebackupbox.net
shell-daemons
git clone git://git.thebackupbox.net/shell-daemons

blob of:

shell-daemons

/ ident-full.c

blob_plain of this file

refs/heads/master:/ident-full.c

 #include 
 #include 
 #include 
 #include 
 #include 
 #include  //read and write
 #include  //atoi

 int main(int argc,char *argv[]) {
 // ident our-host their-host 113 their-port our-port
   int ret=0;
   char buf[1024];
   char *oh;
   char *th;
   unsigned short iport;
   socklen_t len;
   char *tp;
   char *op;

 /*
   char *rtype=0;
   char *ainfo=0;
   char *user=0;
 */

   int s;
   struct sockaddr sa;
   struct sockaddr_in *s4=(struct sockaddr_in *)&sa;
   struct sockaddr_in6 *s6=(struct sockaddr_in6 *)&sa;
   if(argc < 5) {
     printf("usage: ident our-IP their-IP their-ident-port their-port our-port\r\n");
     return 1;
   }
   oh=argv[1];//our IP
   th=argv[2];//their IP
   iport=atoi(argv[3]);//their ident port
   tp=argv[4];//their port
   op=argv[5];//our port
   if(strchr(oh,':') && !strchr(th,':')) {//mismatch. exit.
     printf("our IP is IPv6 but their IP is not. won't work.\n");
     return 2;
   }
   if(!strchr(oh,':') && strchr(th,':')) {//another mismatch. exit.
     printf("our IP is not IPv6 but their IP is. won't work.\n");
     return 3;
   }
   //if we're here, they're the same.
   memset(&sa,0,sizeof(sa));
   if(strchr(oh,':')) {
     sa.sa_family=AF_INET6;
     len=sizeof(struct sockaddr_in6);
     inet_pton(sa.sa_family,oh,&s6->sin6_addr);
   } else {
     sa.sa_family=AF_INET;
     len=sizeof(struct sockaddr_in);
     inet_pton(sa.sa_family,oh,&s4->sin_addr);
   }

   s=socket(sa.sa_family,SOCK_STREAM,IPPROTO_TCP);//6 is TCP and I cbf to find the header that has a #define for it.
   if(bind(s,&sa,len) == -1) {
     perror("bind");
     return 4;
   }
   //we /could/ reuse the sa since it was already bound to...
   switch(sa.sa_family) {
     case AF_INET6:
       inet_pton(sa.sa_family,th,&s6->sin6_addr);
       s6->sin6_port=htons(iport);
       break;
     case AF_INET:
       inet_pton(sa.sa_family,th,&s4->sin_addr);
       s4->sin_port=htons(iport);
       break;
     default://lol wtf?
       break;
   }
   if(connect(s,&sa,len) == -1) {
     perror("connect");
     return 5;
   }
   snprintf(buf,sizeof(buf)-1,"%s,%s\r\n",tp,op);
   write(s,buf,strlen(buf));
   int n=read(s,buf,sizeof(buf));
   buf[n]=0;
   //stripping line endings
   if(strchr(buf,'\n')) *strchr(buf,'\n')=0;
   if(strchr(buf,'\r')) *strchr(buf,'\r')=0;
   printf("%s\n",buf);
   /*
   if(!(rtype=strchr(buf,':'))) return 6;//response isn't in expected format.
   *rtype=0;
   rtype++;
   if(!(ainfo=strchr(rtype,':'))) return 7;
   *ainfo=0;
   ainfo++;
   if(!strcasecmp(rtype,"error")) {
     fprintf(stderr,"ident error: %s\n",rtype+6);
     ret=8;
   }
   //printf("rtype: %s\n",rtype);
   if(!strcasecmp(rtype,"userid")) {
     if(strchr(ainfo,':')) {
       user=strchr(ainfo,':');
       *user=0;
       user++;
     }
   }
   if(user) printf("%s\n",user);
   */
   //this is wrong. we need to use the 3rd or something
   //printf("%s\n",strrchr(buf,':')+1);
   if(shutdown(s,SHUT_RDWR) == -1) {
     perror("shutdown");
   }
   close(s);
   return ret;
 }