File: if_demo.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (590 lines) | stat: -rw-r--r-- 19,636 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/* if_demo.c

 interfacedemo for "pyramid" LCD device.
 */

/*
 Copyright (C) 2005 Silvan Marco Fin <silvan@kernelconcepts.de>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301

 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <wait.h>
#include <net/if.h>
#include <sys/socket.h>

#include "lib-myconnect.h"
#include "lib-mylcd.h"

#include "interface.h"
#include "sockets.h"
#include "net-support.h"

#define DEBUG 0
#define SUCC_DUR 10 /* Duration in sec. how long a success-message is displayed */

/* External programs to be called. */
#define INTERFACE_SCRIPT "./scripts/MyIPSet"
#define REBOOT_SCRIPT "./scripts/MyReboot"
#define HALT_SCRIPT "./scripts/MyHalt"

/* Modes for action */
#define BLANK 0
#define UPDATE 1
#define IFSELECTED 2
#define IPCHANGED 3
#define NMCHANGED 4
#define GWCHANGED 5
#define MENULEFT 6
#define SELECT 7

struct iftype
{
    char name[32];
    char ip[16];
    char netmask[16];
    char gateway[16];
};

struct
{
    unsigned int sIF;
    unsigned int IFnum;
    struct iftype IFdata[32];
} status;

int skfd;

void print_help()
{
    printf("Usage:\n");
    printf("  [-p #] specify Portnumber (defaults to 13666).\n");
    printf("  -d ... specify comma separated list of devices to manage.\n");
    printf("\n");
    printf("Example: if_demo -d eth0,wlan0 -p 12345\n\n");
}


void init_screen(int SD)
{
    char *buffer;

    buffer=basic_send(SD, "hello");
    buffer=basic_send(SD, "client_set -name {Managment}");
    buffer=basic_send(SD, "screen_add main");
    buffer=basic_send(SD, "screen_set main -name {main}");
    buffer=basic_send(SD, "screen_set main -heartbeat off");
    buffer=basic_send(SD, "screen_set main -priority foreground");
    /* buffer=basic_send(SD, "screen_set main -duration 80"); */
    /* buffer=basic_send(SD, "screen_set main -cursor on"); */
}

void alert_screen(int SD, char *line1, char *line2, int duration)
{
    char *buffer;
    char mesg[255];

    /* buffer=basic_send(SD, "widget_del main mainscroller"); */
    buffer=basic_send(SD, "screen_add info");
    buffer=basic_send(SD, "screen_set info -priority alert");
            sprintf(mesg, "screen_set info -timeout %d", duration);
    buffer=basic_send(SD, mesg);
    buffer=basic_send(SD, "widget_add info infostring1 string");
    buffer=basic_send(SD, "widget_add info infostring2 string");
            sprintf(mesg, "widget_set info infostring1 1 1 {%s}", line1);
    buffer=basic_send(SD, mesg);
            sprintf(mesg, "widget_set info infostring2 1 2 {%s}", line2);
    buffer=basic_send(SD, mesg);
}


void interfaces(int SD, char *cs_iflist)
{
    char *buffer;
    char *retval;
    char *next=cs_iflist;
    char mesg[255];
    unsigned int orig_len=strlen(cs_iflist);
    unsigned int i=0;

    /* Now we fill the interfaces to the status variable. */
    while (1)
    {
        retval=strtok(next, ",");
        next=NULL;
        if (retval==NULL)
            break; /* or return? */
        strcpy(status.IFdata[i].name, retval);
        /* If interface is up, then get it's numbers and use them, else set to default-Values */
        /* Default values: */
        sprintf(status.IFdata[i].ip, "000.000.000.000");
        sprintf(status.IFdata[i].netmask, "255.255.255.255");
        sprintf(status.IFdata[i].gateway, "000.000.000.000");
        if (DEBUG)
            printf("IF %s: IP=%s, NM=%s, GW=%s\n", status.IFdata[i].name, status.IFdata[i].ip, status.IFdata[i].netmask, status.IFdata[i].gateway);
        sprintf(mesg, "menu_add_item SIF %d action {sel. %s} -next CHSET", i, status.IFdata[i].name);
        buffer=basic_send(SD, mesg);
        i++;
    }
    status.IFnum=i+1;
    /* now prepare cs_iflist so that we can generate a menu entry. */
    for (i=0; i<orig_len; i++)
        if (cs_iflist[i]=='\0') cs_iflist[i]='\t';
    /* sprintf(mesg, "menu_add_item SIF selection ring {IF:} -strings {%s}", cs_iflist); */ /* For use of a selection ring */
    /* buffer=basic_send(SD, mesg); */
    status.sIF=0;
}

void old_autointerfaces(int SD)
{
    struct if_nameindex *if_list;
    char buffer[1000]="";
    int i;

    if_list=if_nameindex();
    for (i=0; if_list[i].if_name!=NULL; i++)
    {
        if (strcmp(if_list[i].if_name, "lo")==0)
            continue;
        if (strlen(buffer)==0)
            sprintf(buffer, "%s", if_list[i].if_name);
        else
            sprintf(buffer, "%s,%s", buffer, if_list[i].if_name);
    }
    interfaces(SD, buffer);
}

int do_if_action(int SD, struct interface *ife, void *cookie)
{
    int *opt_a = (int *) cookie;
    int res;
    struct aftype *ap;
    static unsigned int i=0;
    char mesg[255];
    char *buffer;

    res = do_if_fetch(ife);
    if (strcmp(ife->name, "lo")==0)
        return 0;
    if (res >= 0) {
        if ((ife->flags & IFF_UP) || *opt_a)
        {
            ap = get_afntype(ife->addr.sa_family);
            strcpy(status.IFdata[i].name, ife->name);
            if (ife->has_ip)
            {
                sprintf(status.IFdata[i].ip, "%s", ap->sprint(&ife->addr, 1));
                sprintf(status.IFdata[i].netmask, "%s", ap->sprint(&ife->netmask, 1));
                sprintf(status.IFdata[i].gateway, "%s", ap->sprint(&ife->addr, 1));
            } else
            {
                sprintf(status.IFdata[i].ip, "000.000.000.000");
                sprintf(status.IFdata[i].netmask, "255.255.255.255");
                sprintf(status.IFdata[i].gateway, "000.000.000.000");
            }
            sprintf(mesg, "menu_add_item SIF %d action {sel. %s} -next CHSET", i, status.IFdata[i].name);
            buffer=basic_send(SD, mesg);
        }
    }
    i++;
    return res;
}

void autointerfaces(int SD)
{
    int res;
    int opt_a=0;

    /* Create a channel to the NET kernel. */
    if ((skfd = sockets_open(0)) < 0) {
	perror("socket");
	exit(1);
    }

    res=for_all_interfaces(SD, do_if_action, &opt_a);
    close(skfd);
}

void set_main_info(int SD, char *ifname, char *ip, char *netmask, char *gateway)
{
    char *buffer;
    char mesg[255];

    sprintf(mesg, "widget_set main mainscroller 1 1 16 2 v 10 {%*s%*s%*s%*s%*s%*s%*s%*s}",
            16, " Watching IF:   ",
            16, ifname,
            16, " IP-Address:    ",
            16, ip,
            16, " Netmask:       ",
            16, netmask,
            16, " Gateway:       ",
            16, gateway);
    buffer=basic_send(SD, mesg);
}


void CHSET_menus_add(int SD)
{
    char *buffer;
    char mesg[255];

    sprintf(mesg, "menu_add_item CHSET CHIP ip {%s IP} -v6 false -value {%s}",
            status.IFdata[status.sIF].name, status.IFdata[status.sIF].ip);
    buffer=basic_send(SD, mesg);
    sprintf(mesg, "menu_add_item CHSET CHNM ip {%s netmask} -v6 false -value {%s}",
            status.IFdata[status.sIF].name, status.IFdata[status.sIF].netmask);
    buffer=basic_send(SD, mesg);
    sprintf(mesg, "menu_add_item CHSET CHGW ip {%s gateway} -v6 false -value {%s}",
            status.IFdata[status.sIF].name, status.IFdata[status.sIF].gateway);
    buffer=basic_send(SD, mesg);
    buffer=basic_send(SD, "menu_set_item CHSET CHIP -next CHNM");
    buffer=basic_send(SD, "menu_set_item CHSET CHNM -next CHGW");
    set_main_info(SD, status.IFdata[status.sIF].name, status.IFdata[status.sIF].ip, status.IFdata[status.sIF].netmask, status.IFdata[status.sIF].gateway);
}

void CHSET_menus(int SD)
{
    char *buffer;
    char mesg[255];

    sprintf(mesg, "menu_set_item CHSET CHIP -text {%s IP} -v6 false -value {%s}",
            status.IFdata[status.sIF].name, status.IFdata[status.sIF].ip);
    buffer=basic_send(SD, mesg);
    sprintf(mesg, "menu_set_item CHSET CHNM -text {%s netmask} -v6 false -value {%s}",
            status.IFdata[status.sIF].name, status.IFdata[status.sIF].netmask);
    buffer=basic_send(SD, mesg);
    sprintf(mesg, "menu_set_item CHSET CHGW -text {%s gateway} -v6 false -value {%s}",
            status.IFdata[status.sIF].name, status.IFdata[status.sIF].gateway);
    buffer=basic_send(SD, mesg);
    set_main_info(SD, status.IFdata[status.sIF].name, status.IFdata[status.sIF].ip, status.IFdata[status.sIF].netmask, status.IFdata[status.sIF].gateway);
}


void action(int SD, char *input)
{
    char *next=input;
    char *retstr;
    int len=0;
    int mode=0;
    int myid;

    if(DEBUG)
        printf("Eingabe: %s\n", input);
    while (1)
    {
        retstr=strtok(next, " \n");
        next=NULL;
        if (retstr==NULL)
        {
            input[0]=0; /* wof�r war das? */
            break;
        }
        len+=strlen(retstr)+1;
        /* key detected? */
        switch (mode)
        {
        case BLANK:
            {
                if (strcmp(retstr, "update")==0) mode=UPDATE;
                if (strcmp(retstr, "leave")==0) mode=MENULEFT;
                if (strcmp(retstr, "select")==0) mode=SELECT;
                break;
            }
        case UPDATE:
            {
                /* if (strcmp(retstr, "sIF")==0) mode=IFSELECTED; */
                if (strcmp(retstr, "CHIP")==0) mode=IPCHANGED;
                if (strcmp(retstr, "CHNM")==0) mode=NMCHANGED;
                if (strcmp(retstr, "CHGW")==0) mode=GWCHANGED;
                break;
            }
        case IFSELECTED:
            {
                status.sIF=atoi(retstr);
                CHSET_menus(SD);
                mode=BLANK;
                break;
            }
        case IPCHANGED:
            {
                strcpy(status.IFdata[status.sIF].ip, retstr);
                mode=BLANK;
                break;
            }
        case NMCHANGED:
            {
                strcpy(status.IFdata[status.sIF].netmask, retstr);
                mode=BLANK;
                break;
            }
        case GWCHANGED:
            {
                strcpy(status.IFdata[status.sIF].gateway, retstr);
                mode=BLANK;
                break;
            }
        case MENULEFT:
            {
                CHSET_menus(SD);
                mode=BLANK;
                break;
            }
        case SELECT:
            /* Now it gets a bit difficult: If we want to call the
             external script, we have to do the fork twice of
             wait for the child process (which we don't want to).
             The approach ist repeated for every call to external
             programm. */
            {
                if (strcmp(retstr, "APPLY")==0)
                {
                    char mesg[17];
                    myid=fork();
                    switch (myid)
                    {
                        int mynewid;
                    case 0:
                        mynewid=fork();
                        switch (mynewid)
                        {
                        case 0: /* Child of the child */
                            close(SD);
                            execl(INTERFACE_SCRIPT,
                                  INTERFACE_SCRIPT,
                                  status.IFdata[status.sIF].name,
                                  status.IFdata[status.sIF].ip,
                                  status.IFdata[status.sIF].netmask,
                                  status.IFdata[status.sIF].gateway,
                                  NULL);
                            break;
                        case -1:
                            sprintf(mesg, "Error %d", errno);
                            alert_screen(SD, mesg, "during apply.", SUCC_DUR);
                            exit(0);
                            break;
                        default: /* At this point, the child exits and the grandchild's parent is set to "1". */
                            exit(0);
                            break;
                        }
                        break;
                    case -1:
                        sprintf(mesg, "Error %d", errno);
                        alert_screen(SD, mesg, "during apply.", SUCC_DUR);
                        break;
                    default:
                        sprintf(mesg, "for IF %s", status.IFdata[status.sIF].name);
                        alert_screen(SD, "Settings applied", mesg, 160);
                        waitpid(myid, NULL, 0); /* the child's exitstate is read, but we don't need it. */
                        break;
                    }
                } else if (strcmp(retstr, "HALT")==0)
                {
                    char mesg[17];
                    myid=fork();
                    switch (myid)
                    {
                        int mynewid;
                    case 0:
                        mynewid=fork();
                        switch(mynewid)
                        {
                        case 0:
                            close(SD);
                            execl(HALT_SCRIPT,
                                  HALT_SCRIPT,
                                  NULL);
                            break;
                        case -1:
                            sprintf(mesg, "Error %d", errno);
                            alert_screen(SD, mesg, "during halt.", SUCC_DUR);
                            exit(0);
                            break;
                        default:
                            exit(0);
                            break;
                        }
                        break;
                    case -1:
                        sprintf(mesg, "Error %d", errno);
                        alert_screen(SD, mesg, "during halt.", SUCC_DUR);
                        break;
                    default:
                        alert_screen(SD, "Going down for", "halt now.", 160);
                        waitpid(myid, NULL, 0);
                        break;
                    }
                } else if (strcmp(retstr, "REBOOT")==0)
                {
                    char mesg[17];
                    myid=fork();
                    switch (myid)
                    {
                        int mynewid;
                    case 0:
                        mynewid=fork();
                        switch (mynewid)
                        {
                        case 0:
                            close(SD);
                            execl(REBOOT_SCRIPT,
                                  REBOOT_SCRIPT,
                                  NULL);
                            break;
                        case -1:
                            sprintf(mesg, "Error %d", errno);
                            alert_screen(SD, mesg, "during reboot.", SUCC_DUR);
                            exit(0);
                            break;
                        default:
                            exit(0);
                            break;
                        }
                        break;
                    case -1:
                        sprintf(mesg, "Error %d", errno);
                        alert_screen(SD, mesg, "during reboot.", SUCC_DUR);
                        break;
                    default:
                        alert_screen(SD, "Going down for", "reboot now.", 160);
                        waitpid(myid, NULL, 0);
                        break;
                    }
                } else /* Assume the Selection is a number and refers to the Index of an interface */
                {
                    status.sIF=atoi(retstr);
                    CHSET_menus(SD);
                }
                mode=BLANK;
                break;
            }
        }
    }
    return;
}


int main(int argc, char *argv[])
{
    int SD;
    int conn_port=13666;
    int i;
    int j;
    char *buffer;
    int starttime;
    int nowtime;
    int devpos=-1;
    int manualif=0;

    for(i = 1, j = 0; i < argc; i++)
    {
        if(argv[i][0] == '-')
        {
            switch(argv[i][1])
            {
            /* Port number specified */
            case 'p':
                {
                    if(argc < i + 1)
                            print_help();
                    conn_port = atoi(argv[++i]);
                    if(conn_port < 1 && conn_port > 0xffff)
                            fprintf(stderr, "Warning:  Port %d outside of standard range\n", conn_port);
                    break;
                }
            /* List of devices */
            case 'd':
                {
                    devpos=i+1;
                    manualif=1;
                    break;
                }
            /* otherwise...  Get help!*/
            default:
                {
                    print_help();
                    exit(0);
                    break;
                }
            }
        }
    }

    if (manualif && devpos<1)
    {
        printf("No interfaces specified, exiting (use -h for usage information).\n");
        close(SD);
        exit(1);
    }

    SD=get_connection("localhost", conn_port);
    if (SD<0)
        exit(0);

    init_screen(SD);

    buffer=basic_send(SD, "menu_add_item {} SIF menu {Current IF}");
    buffer=basic_send(SD, "menu_add_item {} CHSET menu {IF setting}");
    if (manualif)
        interfaces(SD, argv[devpos]);
    else
        autointerfaces(SD);

    /* buffer=basic_send(SD, "menu_set_item {} SIF -next CHSET"); */ /* Doesn't work like that */

    buffer=basic_send(SD, "menu_set_main {}");
    /* buffer=basic_send(SD, "widget_add main maintitle title");
    buffer=basic_send(SD, "widget_set main maintitle {IF-Status}"); */
    buffer=basic_send(SD, "widget_add main mainscroller scroller");
    buffer=basic_send(SD, "widget_set main mainscroller 1 1 16 2 v 4 {Please stand by. }");

    CHSET_menus_add(SD); /* Remember to set the status.sIF to a valid value before call to CHSET_menus*() (e.g. done by interfaces()). */
    buffer=basic_send(SD, "menu_add_item CHSET APPLY action {Apply settings} -next _quit_");
    buffer=basic_send(SD, "menu_set_item CHSET CHGW -next APPLY");

    buffer=basic_send(SD, "menu_add_item {} ASKREBOOT menu {Confirm reboot?} -is_hidden true");
    buffer=basic_send(SD, "menu_add_item ASKREBOOT REBOOT action {      [Yes][No]} -next _quit_");

    buffer=basic_send(SD, "menu_add_item {} ASKHALT menu {Really halt?} -is_hidden true");
    buffer=basic_send(SD, "menu_add_item ASKHALT HALT action {      [Yes][No]} -next _quit_");

    buffer=basic_send(SD, "menu_add_item {} SHUTDOWN menu {Shutdown}");
    buffer=basic_send(SD, "menu_add_item SHUTDOWN DOHALT action {Halt system} -next ASKHALT");
    buffer=basic_send(SD, "menu_add_item SHUTDOWN DOREBOOT action {Reboot system} -next ASKREBOOT");

    starttime=time(NULL);
    while (1)
    {
        nowtime=time(NULL);
        /* sprintf(cmd, "widget_set main c0 1 2 \"%ld\"\n", nowtime-starttime); */
        /* if nothing else to do, send noop, so we know for sure, that basic_send gets something to read */
        /* buffer=basic_send(SD, "noop\n"); */
        buffer=basic_read(SD);
        action(SD, buffer);
        usleep(250000);
    } /*  while ((nowtime-starttime)<500); */

    close(SD);

    return 0;
}