~ubuntu-branches/ubuntu/raring/net-tools/raring

« back to all changes in this revision

Viewing changes to arp.c

  • Committer: Bazaar Package Importer
  • Author(s): Muharem Hrnjadovic
  • Date: 2009-05-04 12:29:43 UTC
  • mfrom: (3.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090504122943-6zyafjosb8a1u21q
Tags: 1.60-23ubuntu1
* Merge from debian unstable, remaining changes:
  Ubuntu_unit_conversion.patch:
    - the diff-style patch was converted into quilt format
    - Ubuntu Policy: output using standard SI unit multiples:
      KB (10^3), MB (10^6), GB (10^9), TB (10^12) and PB (10^15).
      Includes manpage update to remove comment about IEC units.
      LP: #240073.
  Dropped patches, already in debian:
    - lib/ec_hw.c: #include <stddef.h to get definition of NULL.
    - lib/fddi.c: #include <linux/types.h> to get types.  LP: #66209.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 *              NET-3 Networking Distribution for the LINUX operating
9
9
 *              system.
10
10
 *
11
 
 * Version:     $Id: arp.c,v 1.25 2005/12/04 02:57:15 ecki Exp $
 
11
 * Version:     $Id: arp.c,v 1.20 2001/04/08 17:05:05 pb Exp $
12
12
 *
13
13
 * Maintainer:  Bernd 'eckes' Eckenfels, <net-tools@lina.inka.de>
14
14
 *
100
100
{
101
101
    char host[128];
102
102
    struct arpreq req;
103
 
    struct sockaddr_storage ss;
104
 
    struct sockaddr *sa;
 
103
    struct sockaddr sa;
105
104
    int flags = 0;
106
 
    int deleted = 0;
 
105
    int err;
107
106
 
108
107
    memset((char *) &req, 0, sizeof(req));
109
108
 
113
112
        return (-1);
114
113
    }
115
114
    safe_strncpy(host, *args, (sizeof host));
116
 
    sa = (struct sockaddr *)&ss;
117
 
    if (ap->input(0, host, sa) < 0) {
 
115
    if (ap->input(0, host, &sa) < 0) {
118
116
        ap->herror(host);
119
117
        return (-1);
120
118
    }
121
119
    /* If a host has more than one address, use the correct one! */
122
 
    memcpy((char *) &req.arp_pa, (char *) sa, sizeof(struct sockaddr));
 
120
    memcpy((char *) &req.arp_pa, (char *) &sa, sizeof(struct sockaddr));
123
121
 
124
122
    if (hw_set)
125
123
        req.arp_ha.sa_family = hw->type;
150
148
            continue;
151
149
        }
152
150
        if (!strcmp(*args, "dontpub")) {
153
 
#ifdef ATF_DONTPUB
 
151
#ifdef HAVE_ATF_DONTPUB
154
152
            req.arp_flags |= ATF_DONTPUB;
155
153
#else
156
154
            ENOSUPP("arp", "ATF_DONTPUB");
159
157
            continue;
160
158
        }
161
159
        if (!strcmp(*args, "auto")) {
162
 
#ifdef ATF_MAGIC
 
160
#ifdef HAVE_ATF_MAGIC
163
161
            req.arp_flags |= ATF_MAGIC;
164
162
#else
165
163
            ENOSUPP("arp", "ATF_MAGIC");
179
177
                usage();
180
178
            if (strcmp(*args, "255.255.255.255") != 0) {
181
179
                strcpy(host, *args);
182
 
                if (ap->input(0, host, sa) < 0) {
 
180
                if (ap->input(0, host, &sa) < 0) {
183
181
                    ap->herror(host);
184
182
                    return (-1);
185
183
                }
186
 
                memcpy((char *) &req.arp_netmask, (char *) sa,
 
184
                memcpy((char *) &req.arp_netmask, (char *) &sa,
187
185
                       sizeof(struct sockaddr));
188
186
                req.arp_flags |= ATF_NETMASK;
189
187
            }
192
190
        }
193
191
        usage();
194
192
    }
195
 
 
196
 
    // if neighter priv nor pub is given, work on both
197
193
    if (flags == 0)
198
194
        flags = 3;
199
195
 
200
196
    strcpy(req.arp_dev, device);
201
197
 
202
 
    /* unfortuatelly the kernel interface does not allow us to
203
 
       delete private entries anlone, so we need this hack
204
 
       to avoid "not found" errors if we try both. */
205
 
    deleted = 0;
 
198
    err = -1;
206
199
 
207
200
    /* Call the kernel. */
208
201
    if (flags & 2) {
209
202
        if (opt_v)
210
 
            fprintf(stderr, "arp: SIOCDARP(dontpub)\n");
211
 
        if (ioctl(sockfd, SIOCDARP, &req) < 0) {
212
 
            if ((errno == ENXIO) || (errno == ENOENT)) {
 
203
            fprintf(stderr, "arp: SIOCDARP(nopub)\n");
 
204
        if ((err = ioctl(sockfd, SIOCDARP, &req) < 0)) {
 
205
            if (errno == ENXIO) {
213
206
                if (flags & 1)
214
 
                    goto dontpub;
 
207
                    goto nopub;
215
208
                printf(_("No ARP entry for %s\n"), host);
216
209
                return (-1);
217
210
            }
218
 
            perror("SIOCDARP(dontpub)");
 
211
            perror("SIOCDARP(priv)");
219
212
            return (-1);
220
 
        } else
221
 
          deleted = 1;
 
213
        }
222
214
    }
223
 
    if (!deleted && (flags & 1)) {
224
 
      dontpub:
 
215
    if ((flags & 1) && (err)) {
 
216
      nopub:
225
217
        req.arp_flags |= ATF_PUBL;
226
218
        if (opt_v)
227
219
            fprintf(stderr, "arp: SIOCDARP(pub)\n");
228
220
        if (ioctl(sockfd, SIOCDARP, &req) < 0) {
229
 
            if ((errno == ENXIO) || (errno == ENOENT)) {
 
221
            if (errno == ENXIO) {
230
222
                printf(_("No ARP entry for %s\n"), host);
231
223
                return (-1);
232
224
            }
268
260
{
269
261
    char host[128];
270
262
    struct arpreq req;
271
 
    struct sockaddr_storage ss;
272
 
    struct sockaddr *sa;
 
263
    struct sockaddr sa;
273
264
    int flags;
274
265
 
275
266
    memset((char *) &req, 0, sizeof(req));
280
271
        return (-1);
281
272
    }
282
273
    safe_strncpy(host, *args++, (sizeof host));
283
 
    sa = (struct sockaddr *)&ss;
284
 
    if (ap->input(0, host, sa) < 0) {
 
274
    if (ap->input(0, host, &sa) < 0) {
285
275
        ap->herror(host);
286
276
        return (-1);
287
277
    }
288
278
    /* If a host has more than one address, use the correct one! */
289
 
    memcpy((char *) &req.arp_pa, (char *) sa, sizeof(struct sockaddr));
 
279
    memcpy((char *) &req.arp_pa, (char *) &sa, sizeof(struct sockaddr));
290
280
 
291
281
    /* Fetch the hardware address. */
292
282
    if (*args == NULL) {
327
317
            continue;
328
318
        }
329
319
        if (!strcmp(*args, "dontpub")) {
330
 
#ifdef ATF_DONTPUB
 
320
#ifdef HAVE_ATF_DONTPUB
331
321
            flags |= ATF_DONTPUB;
332
322
#else
333
323
            ENOSUPP("arp", "ATF_DONTPUB");
336
326
            continue;
337
327
        }
338
328
        if (!strcmp(*args, "auto")) {
339
 
#ifdef ATF_MAGIC
 
329
#ifdef HAVE_ATF_MAGIC
340
330
            flags |= ATF_MAGIC;
341
331
#else
342
332
            ENOSUPP("arp", "ATF_MAGIC");
356
346
                usage();
357
347
            if (strcmp(*args, "255.255.255.255") != 0) {
358
348
                strcpy(host, *args);
359
 
                if (ap->input(0, host, sa) < 0) {
 
349
                if (ap->input(0, host, &sa) < 0) {
360
350
                    ap->herror(host);
361
351
                    return (-1);
362
352
                }
363
 
                memcpy((char *) &req.arp_netmask, (char *) sa,
 
353
                memcpy((char *) &req.arp_netmask, (char *) &sa,
364
354
                       sizeof(struct sockaddr));
365
355
                flags |= ATF_NETMASK;
366
356
            }
455
445
        strcat(flags, "M");
456
446
    if (arp_flags & ATF_PUBL)
457
447
        strcat(flags, "P");
458
 
#ifdef ATF_MAGIC
 
448
#ifdef HAVE_ATF_MAGIC
459
449
    if (arp_flags & ATF_MAGIC)
460
450
        strcat(flags, "A");
461
451
#endif
462
 
#ifdef ATF_DONTPUB
 
452
#ifdef HAVE_ATF_DONTPUB
463
453
    if (arp_flags & ATF_DONTPUB)
464
454
        strcat(flags, "!");
465
455
#endif
473
463
 
474
464
    if (!(arp_flags & ATF_COM)) {
475
465
        if (arp_flags & ATF_PUBL)
476
 
            printf("%-8.8s%-20.20s", "*", _("<from_interface>"));
 
466
            printf("%-8.8s%-20.20s", "*", "*");
477
467
        else
478
468
            printf("%-8.8s%-20.20s", "", _("(incomplete)"));
479
469
    } else {
496
486
 
497
487
    if (!(arp_flags & ATF_COM)) {
498
488
        if (arp_flags & ATF_PUBL)
499
 
            printf("<from_interface> ");
 
489
            printf("* ");
500
490
        else
501
491
            printf(_("<incomplete> "));
502
492
    } else {
509
499
    if (arp_flags & ATF_PERM)
510
500
        printf("PERM ");
511
501
    if (arp_flags & ATF_PUBL)
512
 
        printf("PUB ");
513
 
#ifdef ATF_MAGIC
 
502
        printf("PUP ");
 
503
#ifdef HAVE_ATF_MAGIC
514
504
    if (arp_flags & ATF_MAGIC)
515
505
        printf("AUTO ");
516
506
#endif
517
 
#ifdef ATF_DONTPUB
 
507
#ifdef HAVE_ATF_DONTPUB
518
508
    if (arp_flags & ATF_DONTPUB)
519
509
        printf("DONTPUB ");
520
510
#endif
529
519
static int arp_show(char *name)
530
520
{
531
521
    char host[100];
532
 
    struct sockaddr_storage ss;
533
 
    struct sockaddr *sa;
 
522
    struct sockaddr sa;
534
523
    char ip[100];
535
524
    char hwa[100];
536
525
    char mask[100];
543
532
 
544
533
    host[0] = '\0';
545
534
 
546
 
    sa = (struct sockaddr *)&ss;
547
535
    if (name != NULL) {
548
536
        /* Resolve the host name. */
549
537
        safe_strncpy(host, name, (sizeof host));
550
 
        if (ap->input(0, host, sa) < 0) {
 
538
        if (ap->input(0, host, &sa) < 0) {
551
539
            ap->herror(host);
552
540
            return (-1);
553
541
        }
554
 
        safe_strncpy(host, ap->sprint(sa, 1), sizeof(host));
 
542
        safe_strncpy(host, ap->sprint(&sa, 1), sizeof(host));
555
543
    }
556
544
    /* Open the PROCps kernel table. */
557
545
    if ((fp = fopen(_PATH_PROCNET_ARP, "r")) == NULL) {
587
575
            if (opt_n)
588
576
                hostname = "?";
589
577
            else {
590
 
                if (ap->input(0, ip, sa) < 0)
 
578
                if (ap->input(0, ip, &sa) < 0)
591
579
                    hostname = ip;
592
580
                else
593
 
                    hostname = ap->sprint(sa, opt_n | 0x8000);
 
581
                    hostname = ap->sprint(&sa, opt_n | 0x8000);
594
582
                if (strcmp(hostname, ip) == 0)
595
583
                    hostname = "?";
596
584
            }
624
612
static void usage(void)
625
613
{
626
614
    fprintf(stderr, _("Usage:\n  arp [-vn]  [<HW>] [-i <if>] [-a] [<hostname>]             <-Display ARP cache\n"));
627
 
    fprintf(stderr, _("  arp [-v]          [-i <if>] -d  <host> [pub]               <-Delete ARP entry\n"));
628
 
    fprintf(stderr, _("  arp [-vnD] [<HW>] [-i <if>] -f  [<filename>]            <-Add entry from file\n"));
629
 
    fprintf(stderr, _("  arp [-v]   [<HW>] [-i <if>] -s  <host> <hwaddr> [temp]            <-Add entry\n"));
630
 
    fprintf(stderr, _("  arp [-v]   [<HW>] [-i <if>] -Ds <host> <if> [netmask <nm>] pub          <-''-\n\n"));
 
615
    fprintf(stderr, _("  arp [-v]          [-i <if>] -d  <hostname> [pub][nopub]    <-Delete ARP entry\n"));
 
616
    fprintf(stderr, _("  arp [-vnD] [<HW>] [-i <if>] -f  [<filename>]              <-Add entry from file\n"));
 
617
    fprintf(stderr, _("  arp [-v]   [<HW>] [-i <if>] -s  <hostname> <hwaddr> [temp][nopub] <-Add entry\n"));
 
618
    fprintf(stderr, _("  arp [-v]   [<HW>] [-i <if>] -s  <hostname> <hwaddr> [netmask <nm>] pub  <-''-\n"));
 
619
    fprintf(stderr, _("  arp [-v]   [<HW>] [-i <if>] -Ds <hostname> <if> [netmask <nm>] pub      <-''-\n\n"));
631
620
    
632
621
    fprintf(stderr, _("        -a                       display (all) hosts in alternative (BSD) style\n"));
633
622
    fprintf(stderr, _("        -s, --set                set a new ARP entry\n"));