📄 l2tpd.c
字号:
memset (tmp->chal_us.reply, 0, MD_SIG_SIZE); tmp->chal_them.state = 0; tmp->chal_them.secret[0] = 0; memset (tmp->chal_them.reply, 0, MD_SIG_SIZE); tmp->chal_them.vector = (unsigned char *) malloc (VECTOR_SIZE); tmp->chal_us.vector = NULL; tmp->hbit = 0; return tmp;}void do_control (){ char buf[1024]; char *host; char *tunstr; char *callstr; char *sub_str; /* jz: use by the strtok function */ char *tmp_ptr; /* jz: use by the strtok function */ struct lac *lac; int call; int tunl; int cnt = -1; while (cnt) { cnt = read (control_fd, buf, sizeof (buf)); if (cnt > 0) { if (buf[cnt - 1] == '\n') buf[--cnt] = 0;#ifdef DEBUG_CONTROL log (LOG_DEBUG, "%s: Got message %s (%d bytes long)\n", __FUNCTION__, buf, cnt);#endif switch (buf[0]) { case 't': host = strchr (buf, ' ') + 1;#ifdef DEBUG_CONTROL log (LOG_DEBUG, "%s: Attempting to tunnel to %s\n", __FUNCTION__, host);#endif l2tp_call (host, UDP_LISTEN_PORT, NULL, NULL); break; case 'c': switch_io = 1; /* jz: Switch for Incoming - Outgoing Calls */ tunstr = strchr (buf, ' ') + 1; lac = laclist; while (lac) { if (!strcasecmp (lac->entname, tunstr)) { lac->active = -1; lac->rtries = 0; if (!lac->c) magic_lac_dial (lac); else log (LOG_DEBUG, "%s: Session '%s' already active!\n", __FUNCTION__, lac->entname); break; } lac = lac->next; } if (lac) break; tunl = atoi (tunstr); if (!tunl) { log (LOG_DEBUG, "%s: No such tunnel '%s'\n", __FUNCTION__, tunstr); break; }#ifdef DEBUG_CONTROL log (LOG_DEBUG, "%s: Attempting to call on tunnel %d\n", __FUNCTION__, tunl);#endif lac_call (tunl, NULL, NULL); break; case 'o': /* jz: option 'o' for doing a outgoing call */ switch_io = 0; /* jz: Switch for incoming - outgoing Calls */ sub_str = strchr (buf, ' ') + 1; tunstr = strtok (sub_str, " "); /* jz: using strtok function to get */ tmp_ptr = strtok (NULL, " "); /* params out of the pipe */ strcpy (dial_no_tmp, tmp_ptr); lac = laclist; while (lac) { if (!strcasecmp (lac->entname, tunstr)) { lac->active = -1; lac->rtries = 0; if (!lac->c) magic_lac_dial (lac); else log (LOG_DEBUG, "%s: Session '%s' already active!\n", __FUNCTION__, lac->entname); break; } lac = lac->next; } if (lac) break; tunl = atoi (tunstr); if (!tunl) { log (LOG_DEBUG, "%s: No such tunnel '%s'\n", __FUNCTION__, tunstr); break; }#ifdef DEBUG_CONTROL log (LOG_DEBUG, "%s: Attempting to call on tunnel %d\n", __FUNCTION__, tunl);#endif lac_call (tunl, NULL, NULL); break; case 'h': callstr = strchr (buf, ' ') + 1; call = atoi (callstr);#ifdef DEBUG_CONTROL log (LOG_DEBUG, "%s: Attempting to call %d\n", __FUNCTION__, call);#endif lac_hangup (call); break; case 'd': tunstr = strchr (buf, ' ') + 1; lac = laclist; while (lac) { if (!strcasecmp (lac->entname, tunstr)) { lac->active = 0; lac->rtries = 0; if (lac->t) lac_disconnect (lac->t->ourtid); else log (LOG_DEBUG, "%s: Session '%s' not up\n", __FUNCTION__, lac->entname); break; } lac = lac->next; } if (lac) break; tunl = atoi (tunstr); if (!tunl) { log (LOG_DEBUG, "%s: No such tunnel '%s'\n", __FUNCTION__, tunstr); break; }#ifdef DEBUG_CONTROL log (LOG_DEBUG, "%s: Attempting to disconnect tunnel %d\n", __FUNCTION__, tunl);#endif lac_disconnect (tunl); break; case 's': show_status (1); break; default: log (LOG_DEBUG, "%s: Unknown command %c\n", __FUNCTION__, buf[0]); } } } /* Otherwise select goes nuts */ close (control_fd); control_fd = open (CONTROL_PIPE, O_RDONLY | O_NONBLOCK, 0600);}void usage(void) { printf("Usage: l2tpd -D -c [config file] -s [secret file] -p [pid file]\n"); printf("\n"); exit(1);}void init_args(int argc, char *argv[]) { int i=0; gconfig.daemon=1; memset(gconfig.altauthfile,0,STRLEN); memset(gconfig.altconfigfile,0,STRLEN); memset(gconfig.authfile,0,STRLEN); memset(gconfig.configfile,0,STRLEN); memset(gconfig.pidfile,0,STRLEN); strncpy(gconfig.altauthfile,ALT_DEFAULT_AUTH_FILE, sizeof(gconfig.altauthfile) - 1); strncpy(gconfig.altconfigfile,ALT_DEFAULT_CONFIG_FILE, sizeof(gconfig.altconfigfile) - 1); strncpy(gconfig.authfile,DEFAULT_AUTH_FILE, sizeof(gconfig.authfile) - 1); strncpy(gconfig.configfile,DEFAULT_CONFIG_FILE, sizeof(gconfig.configfile) - 1); strncpy(gconfig.pidfile,DEFAULT_PID_FILE, sizeof(gconfig.pidfile) - 1); for (i = 1; i < argc; i++) { if(! strncmp(argv[i],"-c",2)) { if(++i == argc) usage(); else strncpy(gconfig.configfile,argv[i], sizeof(gconfig.configfile) - 1); } else if (! strncmp(argv[i],"-D",2)) { gconfig.daemon=0; } else if (! strncmp(argv[i],"-s",2)) { if(++i == argc) usage(); else strncpy(gconfig.authfile,argv[i], sizeof(gconfig.authfile) - 1); } else if (! strncmp(argv[i],"-p",2)) { if(++i == argc) usage(); else strncpy(gconfig.pidfile,argv[i], sizeof(gconfig.pidfile) - 1); } else { usage(); } }}void daemonize() { int pid=0; int i,l; char buf[STRLEN]; int pidfilewritten=0; if((pid = fork()) < 0) { log(LOG_LOG, "%s: Unable to fork ()\n",__FUNCTION__); close(server_socket); exit(1); } else if (pid) exit(0); /* close(0); */ /* This is a hack to "fix" problems with the daemonization code...more work will be forthcoming to do a proper fix for this */ close(1); close(2); /* Read previous pid file. */ if ((i = open(gconfig.pidfile,O_RDONLY)) > 0) { l=read(i,buf,sizeof(buf)-1); if (i < 0) { log(LOG_LOG, "%s: Unable to read pid file [%s]\n", __FUNCTION__, gconfig.pidfile); } buf[i] = '\0'; pid = atoi(buf); /* If the previous server process is not still running, write a new pid file immediately. */ if (pid && (pid == getpid () || kill (pid, 0) < 0)) { unlink (gconfig.pidfile); if ((i = open (gconfig.pidfile, O_WRONLY | O_CREAT, 0640)) >= 0) { snprintf (buf, sizeof(buf), "%d\n", (int)getpid()); write (i, buf, strlen(buf)); close (i); pidfilewritten = 1; } } else { log(LOG_LOG, "%s: There's already a l2tpd server running.\n", __FUNCTION__); close(server_socket); exit(1); } } pid = setsid(); if(! pidfilewritten) { unlink(gconfig.pidfile); if ((i = open (gconfig.pidfile, O_WRONLY | O_CREAT, 0640)) >= 0) { snprintf (buf, strlen(buf), "%d\n", (int)getpid()); write (i, buf, strlen(buf)); close (i); pidfilewritten = 1; } }}void init (int argc,char *argv[]){ struct lac *lac; init_args (argc,argv); rand_source = 0; init_addr (); if (init_config ()) { log (LOG_CRIT, "%s: Unable to load config file\n", __FUNCTION__); exit (1); } if (uname (&uts)) { log (LOG_CRIT, "%s : Unable to determine host system\n", __FUNCTION__); exit (1); } init_tunnel_list (&tunnels); if (init_network ()) exit (1); if (gconfig.daemon) daemonize (); signal (SIGTERM, &death_handler); signal (SIGINT, &death_handler); signal (SIGCHLD, &child_handler); signal (SIGUSR1, &status_handler); signal (SIGHUP, &null_handler); init_scheduler (); mkfifo (CONTROL_PIPE, 0600); control_fd = open (CONTROL_PIPE, O_RDONLY | O_NONBLOCK, 0600); if (control_fd < 0) { log (LOG_CRIT, "%s: Unable to open " CONTROL_PIPE " for reading.", __FUNCTION__); exit (1); } log (LOG_LOG, "l2tpd version " SERVER_VERSION " started on %s PID:%d\n", hostname, getpid ()); log (LOG_LOG, "Written by Mark Spencer, Copyright (C) 1998, Adtran, Inc.\n"); log (LOG_LOG, "Forked by Scott Balmos and David Stipp, (C) 2001\n"); log (LOG_LOG, "Inhereted by Jeff McAdams, (C) 2002\n"); log (LOG_LOG, "%s version %s on a %s, port %d\n", uts.sysname, uts.release, uts.machine, gconfig.port); lac = laclist; while (lac) { if (lac->autodial) {#ifdef DEBUG_MAGIC log (LOG_DEBUG, "%s: Autodialing '%s'\n", __FUNCTION__, lac->entname[0] ? lac->entname : "(unnamed)");#endif lac->active = -1; switch_io = 1; /* If we're a LAC, autodials will be ICRQ's */ magic_lac_dial (lac); } lac = lac->next; }}int main (int argc, char *argv[]){ init(argc,argv); dial_no_tmp = calloc (128, sizeof (char)); network_thread (); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -