Advertisement
Guest User

Untitled

a guest
Apr 30th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <arpa/inet.h>
  8. #include <errno.h>
  9. #include <sys/wait.h>
  10. #include <pthread.h>
  11.  
  12.  
  13. #define PPPD_PATH "/usr/sbin/pppd"
  14.  
  15.  
  16. void printbuf(unsigned char *buf, int cnt)
  17. {
  18.     int i;
  19.     for (i = 0; i < cnt; i++)
  20.     {
  21.         printf("0x%02X ", (unsigned)buf[i]);
  22.     }
  23. }
  24.  
  25. typedef void * pvoid;
  26.  
  27.  
  28. void * piperead(void *arg)
  29. {
  30.     pvoid *inf = arg;
  31.     char* buf       = (char*)(inf[0]);
  32.     int*  p_conn_s      = (int*)(inf[1]);
  33.     int*  writepipe     = (int*)(inf[2]);
  34.     int*  readpipe      = (int*)(inf[3]);  
  35.     int conn_s = *p_conn_s;
  36.    
  37.     printf("piperead\n");
  38.  
  39.     while (1)
  40.     {
  41.         int cnt;
  42.         cnt = read(writepipe[0], buf, 1024);
  43.         if (cnt > 0)
  44.         {
  45.             send(conn_s, buf, cnt, MSG_DONTWAIT);
  46.             printf("send %i\n", cnt);
  47.         }              
  48.     }
  49.    
  50.    
  51.    
  52.     return NULL;
  53. }
  54.  
  55. void * pipewrite(void *arg)
  56. {
  57.     pvoid *inf = arg;
  58.     char* buf       = (char*)(inf[0]);
  59.     int*  p_conn_s      = (int*)(inf[1]);
  60.     int*  writepipe     = (int*)(inf[2]);
  61.     int*  readpipe      = (int*)(inf[3]);
  62.  
  63.     int conn_s = *p_conn_s;
  64.    
  65.     printf("pipewrite\n");
  66.     while (1)
  67.     {
  68.         int cnt;
  69.    
  70.         cnt = recv(conn_s, buf, 1024, 0);
  71.         if (cnt > 0)
  72.         {
  73.             printf("Receive %i: ", cnt);
  74.             printbuf(buf, cnt);
  75.             printf("\n");
  76.             write(readpipe[1], buf, cnt);
  77.         }
  78.     }
  79.    
  80.    
  81.     return NULL;
  82. }
  83.  
  84.  
  85.  
  86. int main(int argc, char **argv)
  87. {
  88.     char *server = NULL;
  89.     char *mode;
  90.     int srv;
  91.  
  92.  
  93.     int port, conn_s;
  94.     struct sockaddr_in serv_addr, client_addr; 
  95.     int cliaddr;
  96.  
  97.  
  98.  
  99.     int readpipe[2], writepipe[2];
  100.    
  101.     unsigned char buf[1024];
  102.  
  103.     int ret=0;
  104.    
  105.     if (argc == 1)
  106.     {
  107.         printf("no arguments!\n");
  108.         return 0;
  109.     }
  110.     mode = argv[1];
  111.    
  112.     if (strcmp(mode, "server")==0)
  113.     {
  114.         srv = 1;
  115.     }
  116.     else if (strcmp(mode, "client") == 0)
  117.     {
  118.         srv = 0;
  119.     }
  120.     else
  121.     {
  122.         printf("wrong argument!\n");
  123.         return 0;
  124.     }
  125.    
  126.     pipe(readpipe);
  127.     pipe(writepipe);
  128.  
  129.    
  130.    
  131.     port=socket(AF_INET, SOCK_STREAM, 0);  
  132.  
  133.  
  134.     if (srv == 0)
  135.     {
  136.         int portn;
  137.         port=socket(AF_INET, SOCK_STREAM, 0);
  138.         sscanf(argv[3], "%i", &portn);
  139.  
  140.  
  141.             serv_addr.sin_family = AF_INET;
  142.             serv_addr.sin_addr.s_addr = inet_addr(argv[2]);
  143.         serv_addr.sin_port = htons(portn);
  144.         printf("connecting %s\n", argv[2]);
  145.             ret = connect(port, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  146.             if (ret < 0)
  147.             {
  148.                     printf("server not found\n");
  149.                     return (0);
  150.             }
  151.         conn_s = port;
  152.     }
  153.     else
  154.     {
  155.         int portn;
  156.         sscanf(argv[2], "%i", &portn);
  157.         serv_addr.sin_family = AF_INET;
  158.             serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  159.             serv_addr.sin_port = htons(portn);
  160.  
  161.             bind(port, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
  162.             listen(port, 1024);
  163.         conn_s = accept(port, NULL, NULL);
  164.                 printf("Accepted!\n");
  165.     }
  166.     ret = fork();
  167.     if (ret < 0)
  168.     {
  169.         printf("fork failed!\n");
  170.         return 0;
  171.     }
  172.     else if (ret == 0)
  173.     {
  174.        
  175.         printf("PPP\n");
  176.         dup2(readpipe[0], 0);
  177.         dup2(writepipe[1], 1);
  178.  
  179.         close(readpipe[0]);
  180.         close(readpipe[1]);
  181.         close(writepipe[0]);
  182.         close(writepipe[1]);
  183.  
  184.         if (srv == 0)  
  185.         {
  186.             execl(PPPD_PATH, "updetach", "notty", "noauth", NULL); 
  187.         }
  188.         else
  189.         {
  190.             execl(PPPD_PATH, "nodetach", "notty", "noauth", "persist", "passive", NULL);   
  191.         }
  192.    
  193.        
  194.     }
  195.     else
  196.     {
  197.         int code;
  198.         pvoid inf[4];
  199.         pthread_t tr, tw;
  200.  
  201.  
  202.         printf("PPP id = %i\n", ret);
  203.  
  204.         inf[0] = (pvoid)buf;   
  205.         inf[1] = (pvoid)&conn_s;   
  206.         inf[2] = (pvoid)writepipe; 
  207.         inf[3] = (pvoid)readpipe;  
  208.        
  209.                
  210.         pthread_create(&tr, NULL, piperead, (pvoid)inf);
  211.         pthread_create(&tw, NULL, pipewrite, (pvoid)inf);
  212.        
  213.                
  214.         wait(&code);
  215.         printf("PARENT: Код возврата PPPD:%d\n", WEXITSTATUS(code));
  216.                        
  217.     }
  218.     return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement