00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #define _GNU_SOURCE
00028
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <pthread.h>
00032 #include <string.h>
00033 #include <stdarg.h>
00034 #include <sys/types.h>
00035 #include <sys/socket.h>
00036 #include <sys/un.h>
00037 #include <unistd.h>
00038 #include <syslog.h>
00039 #include <errno.h>
00040
00041 #include "wdctl.h"
00042
00043 s_config config;
00044
00045 static void usage(void);
00046 static void init_config(void);
00047 static void parse_commandline(int, char **);
00048 static int connect_to_server(char *);
00049 static int send_request(int, char *);
00050 static void wdctl_status(void);
00051 static void wdctl_stop(void);
00052 static void wdctl_reset(void);
00053
00059 static void
00060 usage(void)
00061 {
00062 printf("Usage: wdctl [options] command [arguments]\n");
00063 printf("\n");
00064 printf("options:\n");
00065 printf(" -s <path> Path to the socket\n");
00066 printf(" -h Print usage\n");
00067 printf("\n");
00068 printf("commands:\n");
00069 printf(" reset [mac|ip] Reset the specified mac or ip connection\n");
00070 printf(" status Obtain the status of wifidog\n");
00071 printf(" stop Stop the running wifidog\n");
00072 printf("\n");
00073 }
00074
00079 static void
00080 init_config(void)
00081 {
00082
00083 config.socket = strdup(DEFAULT_SOCK);
00084 config.command = WDCTL_UNDEF;
00085 }
00086
00091 void
00092 parse_commandline(int argc, char **argv)
00093 {
00094 extern int optind;
00095 int c;
00096
00097 while (-1 != (c = getopt(argc, argv, "s:h"))) {
00098 switch(c) {
00099 case 'h':
00100 usage();
00101 exit(1);
00102 break;
00103
00104 case 's':
00105 if (optarg) {
00106 free(config.socket);
00107 config.socket = strdup(optarg);
00108 }
00109 break;
00110
00111 default:
00112 usage();
00113 exit(1);
00114 break;
00115 }
00116 }
00117
00118 if ((argc - optind) <= 0) {
00119 usage();
00120 exit(1);
00121 }
00122
00123 if (strcmp(*(argv + optind), "status") == 0) {
00124 config.command = WDCTL_STATUS;
00125 } else if (strcmp(*(argv + optind), "stop") == 0) {
00126 config.command = WDCTL_STOP;
00127 } else if (strcmp(*(argv + optind), "reset") == 0) {
00128 config.command = WDCTL_KILL;
00129 if ((argc - (optind + 1)) <= 0) {
00130 fprintf(stderr, "wdctl: Error: You must specify an IP "
00131 "or a Mac address to reset\n");
00132 usage();
00133 exit(1);
00134 }
00135 config.param = strdup(*(argv + optind + 1));
00136 }
00137
00138 if (config.command == WDCTL_UNDEF) {
00139 fprintf(stderr, "wdctl: Error: Invalid command \"%s\"\n", *argv);
00140 usage();
00141 exit(1);
00142 }
00143 }
00144
00145 static int
00146 connect_to_server(char *sock_name)
00147 {
00148 int sock;
00149 struct sockaddr_un sa_un;
00150
00151
00152 sock = socket(AF_UNIX, SOCK_STREAM, 0);
00153 memset(&sa_un, 0, sizeof(sa_un));
00154 sa_un.sun_family = AF_UNIX;
00155 strncpy(sa_un.sun_path, sock_name, (sizeof(sa_un.sun_path) - 1));
00156
00157 if (connect(sock, (struct sockaddr *)&sa_un,
00158 strlen(sa_un.sun_path) + sizeof(sa_un.sun_family))) {
00159 fprintf(stderr, "wdctl: wifidog probably not started (Error: %s)\n", strerror(errno));
00160 exit(1);
00161 }
00162
00163 return sock;
00164 }
00165
00166 static int
00167 send_request(int sock, char *request)
00168 {
00169 ssize_t len,
00170 written;
00171
00172 len = 0;
00173 while (len != strlen(request)) {
00174 written = write(sock, (request + len), strlen(request) - len);
00175 if (written == -1) {
00176 fprintf(stderr, "Write to wifidog failed: %s\n",
00177 strerror(errno));
00178 exit(1);
00179 }
00180 len += written;
00181 }
00182
00183 return((int)len);
00184 }
00185
00186 static void
00187 wdctl_status(void)
00188 {
00189 int sock;
00190 char buffer[4096];
00191 char request[16];
00192 int len;
00193
00194 sock = connect_to_server(config.socket);
00195
00196 strncpy(request, "status\r\n\r\n", 15);
00197
00198 len = send_request(sock, request);
00199
00200 while ((len = read(sock, buffer, sizeof(buffer))) > 0) {
00201 buffer[len] = '\0';
00202 printf("%s", buffer);
00203 }
00204
00205 shutdown(sock, 2);
00206 close(sock);
00207 }
00208
00209 static void
00210 wdctl_stop(void)
00211 {
00212 int sock;
00213 char buffer[4096];
00214 char request[16];
00215 int len;
00216
00217 sock = connect_to_server(config.socket);
00218
00219 strncpy(request, "stop\r\n\r\n", 15);
00220
00221 len = send_request(sock, request);
00222
00223 while ((len = read(sock, buffer, sizeof(buffer))) > 0) {
00224 buffer[len] = '\0';
00225 printf("%s", buffer);
00226 }
00227
00228 shutdown(sock, 2);
00229 close(sock);
00230 }
00231
00232 void
00233 wdctl_reset(void)
00234 {
00235 int sock;
00236 char buffer[4096];
00237 char request[64];
00238 int len,
00239 rlen;
00240
00241 sock = connect_to_server(config.socket);
00242
00243 strncpy(request, "reset ", 64);
00244 strncat(request, config.param, (64 - strlen(request)));
00245 strncat(request, "\r\n\r\n", (64 - strlen(request)));
00246
00247 len = send_request(sock, request);
00248
00249 len = 0;
00250 memset(buffer, 0, sizeof(buffer));
00251 while ((len < sizeof(buffer)) && ((rlen = read(sock, (buffer + len),
00252 (sizeof(buffer) - len))) > 0)){
00253 len += rlen;
00254 }
00255
00256 if (strcmp(buffer, "Yes") == 0) {
00257 printf("Connection %s successfully reset.\n", config.param);
00258 } else if (strcmp(buffer, "No") == 0) {
00259 printf("Connection %s was not active.\n", config.param);
00260 } else {
00261 fprintf(stderr, "wdctl: Error: WiFiDog sent an abnormal "
00262 "reply.\n");
00263 }
00264
00265 shutdown(sock, 2);
00266 close(sock);
00267 }
00268
00269 int
00270 main(int argc, char **argv)
00271 {
00272
00273
00274 init_config();
00275 parse_commandline(argc, argv);
00276
00277 switch(config.command) {
00278 case WDCTL_STATUS:
00279 wdctl_status();
00280 break;
00281
00282 case WDCTL_STOP:
00283 wdctl_stop();
00284 break;
00285
00286 case WDCTL_KILL:
00287 wdctl_reset();
00288 break;
00289
00290 default:
00291
00292 fprintf(stderr, "Oops\n");
00293 exit(1);
00294 break;
00295 }
00296 exit(0);
00297 }