Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #include <sys/wait.h>
- #include <pthread.h>
- #define PPPD_PATH "/usr/sbin/pppd"
- void printbuf(unsigned char *buf, int cnt)
- {
- int i;
- for (i = 0; i < cnt; i++)
- {
- printf("0x%02X ", (unsigned)buf[i]);
- }
- }
- typedef void * pvoid;
- void * piperead(void *arg)
- {
- pvoid *inf = arg;
- char* buf = (char*)(inf[0]);
- int* p_conn_s = (int*)(inf[1]);
- int* writepipe = (int*)(inf[2]);
- int* readpipe = (int*)(inf[3]);
- int conn_s = *p_conn_s;
- printf("piperead\n");
- while (1)
- {
- int cnt;
- cnt = read(writepipe[0], buf, 1024);
- if (cnt > 0)
- {
- send(conn_s, buf, cnt, MSG_DONTWAIT);
- printf("send %i\n", cnt);
- }
- }
- return NULL;
- }
- void * pipewrite(void *arg)
- {
- pvoid *inf = arg;
- char* buf = (char*)(inf[0]);
- int* p_conn_s = (int*)(inf[1]);
- int* writepipe = (int*)(inf[2]);
- int* readpipe = (int*)(inf[3]);
- int conn_s = *p_conn_s;
- printf("pipewrite\n");
- while (1)
- {
- int cnt;
- cnt = recv(conn_s, buf, 1024, 0);
- if (cnt > 0)
- {
- printf("Receive %i: ", cnt);
- printbuf(buf, cnt);
- printf("\n");
- write(readpipe[1], buf, cnt);
- }
- }
- return NULL;
- }
- int main(int argc, char **argv)
- {
- char *server = NULL;
- char *mode;
- int srv;
- int port, conn_s;
- struct sockaddr_in serv_addr, client_addr;
- int cliaddr;
- int readpipe[2], writepipe[2];
- unsigned char buf[1024];
- int ret=0;
- if (argc == 1)
- {
- printf("no arguments!\n");
- return 0;
- }
- mode = argv[1];
- if (strcmp(mode, "server")==0)
- {
- srv = 1;
- }
- else if (strcmp(mode, "client") == 0)
- {
- srv = 0;
- }
- else
- {
- printf("wrong argument!\n");
- return 0;
- }
- pipe(readpipe);
- pipe(writepipe);
- port=socket(AF_INET, SOCK_STREAM, 0);
- if (srv == 0)
- {
- int portn;
- port=socket(AF_INET, SOCK_STREAM, 0);
- sscanf(argv[3], "%i", &portn);
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = inet_addr(argv[2]);
- serv_addr.sin_port = htons(portn);
- printf("connecting %s\n", argv[2]);
- ret = connect(port, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
- if (ret < 0)
- {
- printf("server not found\n");
- return (0);
- }
- conn_s = port;
- }
- else
- {
- int portn;
- sscanf(argv[2], "%i", &portn);
- serv_addr.sin_family = AF_INET;
- serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
- serv_addr.sin_port = htons(portn);
- bind(port, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
- listen(port, 1024);
- conn_s = accept(port, NULL, NULL);
- printf("Accepted!\n");
- }
- ret = fork();
- if (ret < 0)
- {
- printf("fork failed!\n");
- return 0;
- }
- else if (ret == 0)
- {
- printf("PPP\n");
- dup2(readpipe[0], 0);
- dup2(writepipe[1], 1);
- close(readpipe[0]);
- close(readpipe[1]);
- close(writepipe[0]);
- close(writepipe[1]);
- if (srv == 0)
- {
- execl(PPPD_PATH, "updetach", "notty", "noauth", NULL);
- }
- else
- {
- execl(PPPD_PATH, "nodetach", "notty", "noauth", "persist", "passive", NULL);
- }
- }
- else
- {
- int code;
- pvoid inf[4];
- pthread_t tr, tw;
- printf("PPP id = %i\n", ret);
- inf[0] = (pvoid)buf;
- inf[1] = (pvoid)&conn_s;
- inf[2] = (pvoid)writepipe;
- inf[3] = (pvoid)readpipe;
- pthread_create(&tr, NULL, piperead, (pvoid)inf);
- pthread_create(&tw, NULL, pipewrite, (pvoid)inf);
- wait(&code);
- printf("PARENT: Код возврата PPPD:%d\n", WEXITSTATUS(code));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement