File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / mpd / src / mp.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Mon Jul 22 08:44:29 2013 UTC (10 years, 11 months ago) by misho
Branches: mpd, MAIN
CVS tags: v5_8p7, v5_8p1_cross, v5_8p1, v5_8, v5_7p0, v5_7, v5_6, HEAD
5.7

    1: 
    2: /*
    3:  * mp.c
    4:  *
    5:  * Written by Archie Cobbs <archie@freebsd.org>
    6:  * Copyright (c) 1995-1999 Whistle Communications, Inc. All rights reserved.
    7:  * See ``COPYRIGHT.whistle''
    8:  */
    9: 
   10: #include "ppp.h"
   11: #include "mp.h"
   12: #include "iface.h"
   13: #include "link.h"
   14: #include "fsm.h"
   15: #include "input.h"
   16: #include "util.h"
   17: 
   18: /*
   19:  * MpSetDiscrim()
   20:  *
   21:  * Figure out and set my discriminator
   22:  */
   23: 
   24: void
   25: MpSetDiscrim(void)
   26: {
   27:   u_int32_t		magic[2];
   28:   struct u_addr		ipaddr;
   29:   struct sockaddr_dl	hwa;
   30: 
   31:   /* Try Ethernet address first */
   32:   if (GetEther(NULL, &hwa) >= 0) {
   33:     self_discrim.class = DISCRIM_CLASS_802_1;
   34:     memcpy(self_discrim.bytes, LLADDR(&hwa), self_discrim.len = hwa.sdl_alen);
   35:     return;
   36:   }
   37: 
   38:   /* Then try an IP address */
   39:   if (GetAnyIpAddress(&ipaddr, NULL) >= 0) {
   40:     self_discrim.class = DISCRIM_CLASS_IPADDR;
   41:     memcpy(self_discrim.bytes, &ipaddr.u.ip4, self_discrim.len = sizeof(ipaddr.u.ip4));
   42:     return;
   43:   }
   44: 
   45:   /* Then just use a coupla magic numbers */
   46:   magic[0] = GenerateMagic();
   47:   magic[1] = GenerateMagic();
   48:   self_discrim.class = DISCRIM_CLASS_MAGIC;
   49:   memcpy(self_discrim.bytes, magic, self_discrim.len = sizeof(magic));
   50: }
   51: 
   52: /*
   53:  * MpDiscrimEqual()
   54:  *
   55:  * Returns TRUE if the two discriminators are equal.
   56:  */
   57: 
   58: int
   59: MpDiscrimEqual(Discrim d1, Discrim d2)
   60: {
   61:   if (d1->class != d2->class && d1->len != d2->len)
   62:     return(FALSE);
   63:   return(!memcmp(&d1->bytes, &d2->bytes, d1->len));
   64: }
   65: 
   66: /*
   67:  * MpDiscrimName()
   68:  */
   69: 
   70: static const char *
   71: MpDiscrimName(int class)
   72: {
   73:   switch (class)
   74:   {
   75:     case DISCRIM_CLASS_NULL:
   76:       return("NULL");
   77:     case DISCRIM_CLASS_LOCAL:
   78:       return("LOCAL");
   79:     case DISCRIM_CLASS_IPADDR:
   80:       return("IP Address");
   81:     case DISCRIM_CLASS_802_1:
   82:       return("802.1");
   83:     case DISCRIM_CLASS_MAGIC:
   84:       return("Magic");
   85:     case DISCRIM_CLASS_PSN:
   86:       return("PSN");
   87:     default:
   88:       return("???");
   89:   }
   90: }
   91: 
   92: /*
   93:  * MpDiscrimText()
   94:  */
   95: 
   96: char *
   97: MpDiscrimText(Discrim dis, char *buf, size_t len)
   98: {
   99:   int		k;
  100: 
  101:   snprintf(buf, len, "[%s]", MpDiscrimName(dis->class));
  102:   for (k = 0; k < dis->len && k < sizeof(dis->bytes); k++)
  103:     snprintf(buf + strlen(buf),
  104:       len - strlen(buf), " %02x", dis->bytes[k]);
  105:   return(buf);
  106: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>