⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ip.c

📁 嵌入式操作系统ECOS的网络开发包
💻 C
📖 第 1 页 / 共 2 页
字号:
        cp = (u_char *)&(low_ia->ia_subnetmask);
        string[0] = *cp++;
        string[1] = *cp++;
        string[2] = *cp++;
        string[3] = *cp++;
        *var_len = 4;
        return (unsigned char *) string;

    case IPADENTBCASTADDR:
        long_ret = 1;
        return (unsigned char *) &long_ret;

    case IPADENTREASMMAXSIZE:
        long_ret = IP_MAXPACKET;
        return (unsigned char *) &long_ret;

    default:
      ERROR_MSG("");
    }
    return NULL;
}




/*
 * var_ipNetToMediaTable():
 *   Handle this table separately from the scalar value case.
 *   The workings of this are basically the same as for var_ip above.
 */

// According to sections 6.1.5 (ip) and 6.1.4 (at) pp.130-138 of the book
// by William Stallings, this lists *our* interfaces only, not the ARP
// table.  The MIBs are rather ambiguous, as is Mark A. Miller's book also.
// 
// Specifically, the indexing by interface Id suggests there should only be
// one entry per interface.

unsigned char *
var_ipNetToMediaTable(struct variable *vp,
    	    oid     *name,
    	    size_t  *length,
    	    int     exact,
    	    size_t  *var_len,
    	    WriteMethod **write_method)
{
    static long long_ret;
    static unsigned char string[SPRINT_MAX_LEN];
    /*
     * IP Net to Media table object identifier is of form:
     * 1.3.6.1.2.1.4.22.1.?.interface.A.B.C.D,  where A.B.C.D is IP address.
     * Interface is at offset 10,
     * IPADDR starts at offset 11.
     */
    u_char		    *cp;
    oid			    *op;
    oid			    lowest[16];
    oid			    current[16];

    register struct in_ifaddr *ia;
    register struct in_ifaddr *low_ia = NULL;

    /* fill in object part of name for current (less sizeof instance part) */
    memcpy((char *)current, (char *)vp->name, (int)vp->namelen * sizeof(oid));

    for (ia = in_ifaddr.tqh_first; ia; ia = ia->ia_list.tqe_next) {
        // interface number
        current[10] = ia->ia_ifa.ifa_ifp->if_index;
        // IP address
	cp = (u_char *)&(ia->ia_addr.sin_addr.s_addr);
	op = current + 11;
	*op++ = *cp++;
	*op++ = *cp++;
	*op++ = *cp++;
	*op++ = *cp++;
        
	if (exact){
	    if (snmp_oid_compare(current, 15, name, *length) == 0){
		memcpy( (char *)lowest,(char *)current, 15 * sizeof(oid));
		low_ia = ia;
		break;	/* no need to search further */
	    }
	} else {
	    if ((snmp_oid_compare(current, 15, name, *length) > 0) &&
                ((!low_ia) || (snmp_oid_compare(current, 15, lowest, 15) < 0))) {
		/*
		 * if new one is greater than input and closer to input than
		 * previous lowest, save this one as the "next" one.
		 */
		memcpy( (char *)lowest,(char *)current, 15 * sizeof(oid));
		low_ia = ia;
	    }
	}
    }
    if ( ! low_ia )
	return(NULL);

    memcpy( (char *)name,(char *)lowest, 15 * sizeof(oid));
    *length = 15;
    *write_method = 0;
    *var_len = sizeof(long_return);

    /* 
     * this is where we do the value assignments for the mib results.
     */
    switch(vp->magic) {
    case IPNETTOMEDIAIFINDEX:
        //NOTSUPPORTED: *write_method = write_ipNetToMediaIfIndex;
        long_ret = low_ia->ia_ifa.ifa_ifp->if_index;
        return (unsigned char *) &long_ret;

    case IPNETTOMEDIAPHYSADDRESS: {
        struct eth_drv_sc *sc = low_ia->ia_ifa.ifa_ifp->if_softc;
        if (!sc) {
            // No hardware associated with this device.
            return(NULL);
        }
        bcopy(&sc->sc_arpcom.ac_enaddr, string, ETHER_ADDR_LEN);
        *var_len = ETHER_ADDR_LEN;
        //NOTSUPPORTED: *write_method = write_ipNetToMediaPhysAddress;
        return (unsigned char *) string;
    }
    case IPNETTOMEDIANETADDRESS:
        //NOTSUPPORTED: *write_method = write_ipNetToMediaNetAddress;
	cp = (u_char *)&(low_ia->ia_addr.sin_addr.s_addr);
        string[0] = *cp++;
        string[1] = *cp++;
        string[2] = *cp++;
        string[3] = *cp++;
        *var_len = 4;
        return (unsigned char *) string;

    case IPNETTOMEDIATYPE:
        //NOTSUPPORTED: *write_method = write_ipNetToMediaType;
        long_ret = 4; // Static mapping
        return (unsigned char *) &long_ret;

    default:
        ERROR_MSG("");
    }
    return NULL;
}




int
write_ipForwarding(int      action,
            u_char   *var_val,
            u_char   var_val_type,
            size_t   var_val_len,
            u_char   *statP,
            oid      *name,
            size_t   name_len)
{
    static long setval;
    
    switch ( action ) {
    case RESERVE1:
        if (var_val_type != ASN_INTEGER){
            fprintf(stderr, "write to ipForwarding not ASN_INTEGER\n");
            return SNMP_ERR_WRONGTYPE;
        }
        if (var_val_len > sizeof(setval)){
            fprintf(stderr,"write to ipForwarding: bad length\n");
            return SNMP_ERR_WRONGLENGTH;
        }
        setval = *(long *)var_val;
        if ( 1 != setval && 2 != setval )
            return SNMP_ERR_WRONGVALUE;
        break;

    case RESERVE2:
    case FREE:
    case ACTION:
    case UNDO:
        break;

    case COMMIT:
        ipforwarding = (setval == 1);
        break;
    }
    return SNMP_ERR_NOERROR;
}




int
write_ipDefaultTTL(int      action,
            u_char   *var_val,
            u_char   var_val_type,
            size_t   var_val_len,
            u_char   *statP,
            oid      *name,
            size_t   name_len)
{
    static long setval;

    switch ( action ) {
    case RESERVE1:
        if (var_val_type != ASN_INTEGER){
            fprintf(stderr, "write to ipDefaultTTL not ASN_INTEGER\n");
            return SNMP_ERR_WRONGTYPE;
        }
        if (var_val_len > sizeof(setval)){
            fprintf(stderr,"write to ipDefaultTTL: bad length\n");
            return SNMP_ERR_WRONGLENGTH;
        }
        setval = *(long *)var_val;
        break;

    case RESERVE2:
    case FREE:
    case ACTION:
    case UNDO:
        break;

    case COMMIT:
        ip_defttl = setval;
        break;
    }
    return SNMP_ERR_NOERROR;
}


// ---------------------------------------------------------------------------
// writing these is not supported.  The templates from mib2c are retained.
//
//NOTSUPPORTED:
#if 0
int
write_ipNetToMediaIfIndex(int      action,
            u_char   *var_val,
            u_char   var_val_type,
            size_t   var_val_len,
            u_char   *statP,
            oid      *name,
            size_t   name_len)
{
  static long *long_ret;
  int size;


  switch ( action ) {
        case RESERVE1:
          if (var_val_type != ASN_INTEGER){
              fprintf(stderr, "write to ipNetToMediaIfIndex not ASN_INTEGER\n");
              return SNMP_ERR_WRONGTYPE;
          }
          if (var_val_len > sizeof(long_ret)){
              fprintf(stderr,"write to ipNetToMediaIfIndex: bad length\n");
              return SNMP_ERR_WRONGLENGTH;
          }
          break;


        case RESERVE2:
          size = var_val_len;
          long_ret = (long *) var_val;


          break;


        case FREE:
             /* Release any resources that have been allocated */
          break;


        case ACTION:
             /* The variable has been stored in long_ret for
             you to use, and you have just been asked to do something with
             it.  Note that anything done here must be reversable in the UNDO case */
          break;


        case UNDO:
             /* Back out any changes made in the ACTION case */
          break;


        case COMMIT:
             /* Things are working well, so it's now safe to make the change
             permanently.  Make sure that anything done here can't fail! */
          break;
  }
  return SNMP_ERR_NOERROR;
}




int
write_ipNetToMediaPhysAddress(int      action,
            u_char   *var_val,
            u_char   var_val_type,
            size_t   var_val_len,
            u_char   *statP,
            oid      *name,
            size_t   name_len)
{
  static unsigned char string[SPRINT_MAX_LEN];
  int size;


  switch ( action ) {
        case RESERVE1:
          if (var_val_type != ASN_OCTET_STR){
              fprintf(stderr, "write to ipNetToMediaPhysAddress not ASN_OCTET_STR\n");
              return SNMP_ERR_WRONGTYPE;
          }
          if (var_val_len > sizeof(string)){
              fprintf(stderr,"write to ipNetToMediaPhysAddress: bad length\n");
              return SNMP_ERR_WRONGLENGTH;
          }
          break;


        case RESERVE2:
          size = var_val_len;
          //string = (char *) var_val;


          break;


        case FREE:
             /* Release any resources that have been allocated */
          break;


        case ACTION:
             /* The variable has been stored in string for
             you to use, and you have just been asked to do something with
             it.  Note that anything done here must be reversable in the UNDO case */
          break;


        case UNDO:
             /* Back out any changes made in the ACTION case */
          break;


        case COMMIT:
             /* Things are working well, so it's now safe to make the change
             permanently.  Make sure that anything done here can't fail! */
          break;
  }
  return SNMP_ERR_NOERROR;
}




int
write_ipNetToMediaNetAddress(int      action,
            u_char   *var_val,
            u_char   var_val_type,
            size_t   var_val_len,
            u_char   *statP,
            oid      *name,
            size_t   name_len)
{
  static unsigned char string[SPRINT_MAX_LEN];
  int size;


  switch ( action ) {
        case RESERVE1:
          if (var_val_type != ASN_IPADDRESS){
              fprintf(stderr, "write to ipNetToMediaNetAddress not ASN_IPADDRESS\n");
              return SNMP_ERR_WRONGTYPE;
          }
          if (var_val_len > sizeof(string)){
              fprintf(stderr,"write to ipNetToMediaNetAddress: bad length\n");
              return SNMP_ERR_WRONGLENGTH;
          }
          break;


        case RESERVE2:
          size = var_val_len;
          //string = (char *) var_val;


          break;


        case FREE:
             /* Release any resources that have been allocated */
          break;


        case ACTION:
             /* The variable has been stored in string for
             you to use, and you have just been asked to do something with
             it.  Note that anything done here must be reversable in the UNDO case */
          break;


        case UNDO:
             /* Back out any changes made in the ACTION case */
          break;


        case COMMIT:
             /* Things are working well, so it's now safe to make the change
             permanently.  Make sure that anything done here can't fail! */
          break;
  }
  return SNMP_ERR_NOERROR;
}




int
write_ipNetToMediaType(int      action,
            u_char   *var_val,
            u_char   var_val_type,
            size_t   var_val_len,
            u_char   *statP,
            oid      *name,
            size_t   name_len)
{
  static long *long_ret;
  int size;


  switch ( action ) {
        case RESERVE1:
          if (var_val_type != ASN_INTEGER){
              fprintf(stderr, "write to ipNetToMediaType not ASN_INTEGER\n");
              return SNMP_ERR_WRONGTYPE;
          }
          if (var_val_len > sizeof(long_ret)){
              fprintf(stderr,"write to ipNetToMediaType: bad length\n");
              return SNMP_ERR_WRONGLENGTH;
          }
          break;


        case RESERVE2:
          size = var_val_len;
          long_ret = (long *) var_val;


          break;


        case FREE:
             /* Release any resources that have been allocated */
          break;


        case ACTION:
             /* The variable has been stored in long_ret for
             you to use, and you have just been asked to do something with
             it.  Note that anything done here must be reversable in the UNDO case */
          break;


        case UNDO:
             /* Back out any changes made in the ACTION case */
          break;


        case COMMIT:
            /* Things are working well, so it's now safe to make the change
                permanently.  Make sure that anything done here can't fail! */
            break;
  }
  return SNMP_ERR_NOERROR;
}

#endif
// ---------------------------------------------------------------------------

// EOF ip.c

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -