| | 1452 | register int i; |
| | 1453 | unsigned h1=0; |
| | 1454 | unsigned h2=0; |
| | 1455 | unsigned h3=0; |
| | 1456 | unsigned h4=0; |
| | 1457 | unsigned h5=0; |
| | 1458 | unsigned h6=0; |
| | 1459 | unsigned h7=0; |
| | 1460 | unsigned h8=0; |
| | 1461 | char *cp; |
| | 1462 | unsigned uColonCount=0; |
| | 1463 | unsigned uRead=0; |
| | 1464 | |
| | 1465 | //Insure these are empty |
| | 1466 | cParam2[0]=0; |
| | 1467 | |
| | 1468 | if(strlen(cParam1)<4) |
| | 1469 | { |
| | 1470 | cParam1Style="type_fields_req"; |
| | 1471 | gcMessage="<blink>Error: </blink>IPv6 number must be at least 4 chars long (e.g. 1::a)"; |
| | 1472 | return(18); |
| | 1473 | } |
| | 1474 | |
| | 1475 | //if cParam1 has no consecutive colons we can simply: |
| | 1476 | if((cp=strstr(cParam1,"::"))) |
| | 1477 | { |
| | 1478 | if(strstr(cp+2,"::")) |
| | 1479 | { |
| | 1480 | cParam1Style="type_fields_req"; |
| | 1481 | gcMessage="<blink>Error: </blink>IPv6 number can not have more than one double colon!"; |
| | 1482 | return(18); |
| | 1483 | } |
| | 1484 | } |
| | 1485 | |
| | 1486 | //Now for the hard work |
| | 1487 | for(i=0;cParam1[i];i++) |
| | 1488 | { |
| | 1489 | if(cParam1[i]==':') |
| | 1490 | uColonCount++; |
| | 1491 | if(cParam1[i]!=':' && !isxdigit(cParam1[i])) |
| | 1492 | { |
| | 1493 | cParam1Style="type_fields_req"; |
| | 1494 | gcMessage="<blink>Error: </blink>IPv6 number can only have hexadecimal digits and colons!"; |
| | 1495 | return(18); |
| | 1496 | } |
| | 1497 | } |
| | 1498 | |
| | 1499 | switch(uColonCount) |
| | 1500 | { |
| | 1501 | case 0: |
| | 1502 | case 1: |
| | 1503 | cParam1Style="type_fields_req"; |
| | 1504 | gcMessage="<blink>Error: </blink>IPv6 too few colons: Min is 2!"; |
| | 1505 | return(18); |
| | 1506 | break; |
| | 1507 | |
| | 1508 | case 2: |
| | 1509 | uRead=sscanf(cParam1,"%x::%x",&h1,&h8); |
| | 1510 | if(uRead!=2) |
| | 1511 | { |
| | 1512 | cParam1Style="type_fields_req"; |
| | 1513 | gcMessage="<blink>Error: </blink>IPv6 format-2 error!"; |
| | 1514 | return(18); |
| | 1515 | } |
| | 1516 | break; |
| | 1517 | |
| | 1518 | case 3: |
| | 1519 | uRead=sscanf(cParam1,"%x::%x:%x",&h1,&h7,&h8); |
| | 1520 | if(uRead!=3) |
| | 1521 | { |
| | 1522 | uRead=sscanf(cParam1,"%x:%x::%x",&h1,&h2,&h8); |
| | 1523 | if(uRead!=3) |
| | 1524 | { |
| | 1525 | cParam1Style="type_fields_req"; |
| | 1526 | gcMessage="<blink>Error: </blink>IPv6 format-3 error!"; |
| | 1527 | return(18); |
| | 1528 | } |
| | 1529 | } |
| | 1530 | break; |
| | 1531 | |
| | 1532 | case 4: |
| | 1533 | uRead=sscanf(cParam1,"%x::%x:%x:%x",&h1,&h6,&h7,&h8); |
| | 1534 | if(uRead!=4) |
| | 1535 | { |
| | 1536 | uRead=sscanf(cParam1,"%x:%x::%x:%x",&h1,&h2,&h7,&h8); |
| | 1537 | if(uRead!=4) |
| | 1538 | { |
| | 1539 | uRead=sscanf(cParam1,"%x:%x:%x::%x",&h1,&h2,&h3,&h8); |
| | 1540 | if(uRead!=4) |
| | 1541 | { |
| | 1542 | cParam1Style="type_fields_req"; |
| | 1543 | gcMessage="<blink>Error: </blink>IPv6 format-4 error!"; |
| | 1544 | return(18); |
| | 1545 | } |
| | 1546 | } |
| | 1547 | } |
| | 1548 | break; |
| | 1549 | |
| | 1550 | case 5: |
| | 1551 | uRead=sscanf(cParam1,"%x::%x:%x:%x:%x",&h1,&h5,&h6,&h7,&h8); |
| | 1552 | if(uRead!=5) |
| | 1553 | { |
| | 1554 | uRead=sscanf(cParam1,"%x:%x::%x:%x:%x",&h1,&h2,&h6,&h7,&h8); |
| | 1555 | if(uRead!=5) |
| | 1556 | { |
| | 1557 | uRead=sscanf(cParam1,"%x:%x:%x::%x:%x",&h1,&h2,&h3,&h7,&h8); |
| | 1558 | if(uRead!=5) |
| | 1559 | { |
| | 1560 | uRead=sscanf(cParam1,"%x:%x:%x:%x::%x",&h1,&h2,&h3,&h4,&h8); |
| | 1561 | if(uRead!=5) |
| | 1562 | { |
| | 1563 | cParam1Style="type_fields_req"; |
| | 1564 | gcMessage="<blink>Error: </blink>IPv6 format-5 error!"; |
| | 1565 | return(18); |
| | 1566 | } |
| | 1567 | } |
| | 1568 | } |
| | 1569 | } |
| | 1570 | break; |
| | 1571 | |
| | 1572 | case 6: |
| | 1573 | uRead=sscanf(cParam1,"%x::%x:%x:%x:%x:%x",&h1,&h4,&h5,&h6,&h7,&h8); |
| | 1574 | if(uRead!=6) |
| | 1575 | { |
| | 1576 | uRead=sscanf(cParam1,"%x:%x::%x:%x:%x:%x",&h1,&h2,&h5,&h6,&h7,&h8); |
| | 1577 | if(uRead!=6) |
| | 1578 | { |
| | 1579 | uRead=sscanf(cParam1,"%x:%x:%x::%x:%x:%x",&h1,&h2,&h3,&h6,&h7,&h8); |
| | 1580 | if(uRead!=6) |
| | 1581 | { |
| | 1582 | uRead=sscanf(cParam1,"%x:%x:%x:%x::%x:%x",&h1,&h2,&h3,&h4,&h7,&h8); |
| | 1583 | if(uRead!=6) |
| | 1584 | { |
| | 1585 | uRead=sscanf(cParam1,"%x:%x:%x:%x:%x::%x", |
| | 1586 | &h1,&h2,&h3,&h4,&h5,&h8); |
| | 1587 | if(uRead!=6) |
| | 1588 | { |
| | 1589 | cParam1Style="type_fields_req"; |
| | 1590 | gcMessage="<blink>Error: </blink>IPv6 format-6 error!"; |
| | 1591 | return(18); |
| | 1592 | } |
| | 1593 | } |
| | 1594 | } |
| | 1595 | } |
| | 1596 | } |
| | 1597 | break; |
| | 1598 | |
| | 1599 | case 7: |
| | 1600 | uRead=sscanf(cParam1,"%x:%x:%x:%x:%x:%x:%x:%x",&h1,&h2,&h3,&h4,&h5,&h6,&h7,&h8); |
| | 1601 | if(uRead!=8) |
| | 1602 | { |
| | 1603 | cParam1Style="type_fields_req"; |
| | 1604 | gcMessage="<blink>Error: </blink>IPv6 format-7 error!"; |
| | 1605 | return(18); |
| | 1606 | } |
| | 1607 | break; |
| | 1608 | |
| | 1609 | default: |
| | 1610 | cParam1Style="type_fields_req"; |
| | 1611 | gcMessage="<blink>Error: </blink>IPv6 too many colons: Max is 7!"; |
| | 1612 | return(18); |
| | 1613 | |
| | 1614 | } |
| | 1615 | |
| | 1616 | //First basic checks for AAAA hosts |
| | 1617 | if(!h1) |
| | 1618 | { |
| | 1619 | cParam1Style="type_fields_req"; |
| | 1620 | gcMessage="<blink>Error: </blink>IPv6 number can not have a 0 in first 16 bit hex word."; |
| | 1621 | return(18); |
| | 1622 | } |
| | 1623 | |
| | 1624 | if(!h8) |
| | 1625 | { |
| | 1626 | cParam1Style="type_fields_req"; |
| | 1627 | gcMessage=malloc(256); |
| | 1628 | sprintf(gcMessage,"<blink>Error: </blink>IPv6 number can not have a 0 in last 16 bit hex word:" |
| | 1629 | " %x:%x:%x:%x:%x:%x:%x:%x",h1,h2,h3,h4,h5,h6,h7,h8); |
| | 1630 | return(18); |
| | 1631 | } |
| | 1632 | |
| | 1633 | //Mandatory rewrite in shortest possible IPv6 format. |
| | 1634 | //This is needed to speed up DNSSEC and reduce BIND zone file size. |
| | 1635 | //This may not be a good idea. Need to research further: If someone wants to |
| | 1636 | //write a bunch of 0's why not? |
| | 1637 | //Compress empty words: Double colon. Can only be used once. |
| | 1638 | //Trying KISS method here. sprintf does the leading 0 removal for us. |
| | 1639 | //6 consecutive 0 case |
| | 1640 | if(!h2 && !h3 && !h4 && !h5 && !h6 && !h7) |
| | 1641 | sprintf(cParam1,"%x::%x",h1,h8); |
| | 1642 | //5 consecutive 0 cases |
| | 1643 | else if(!h3 && !h4 && !h5 && !h6 && !h7) |
| | 1644 | sprintf(cParam1,"%x:%x::%x",h1,h2,h8); |
| | 1645 | else if(!h2 && !h3 && !h4 && !h5 && !h6) |
| | 1646 | sprintf(cParam1,"%x::%x:%x",h1,h7,h8); |
| | 1647 | //4 consecutive 0 cases |
| | 1648 | else if(!h4 && !h5 && !h6 && !h7) |
| | 1649 | sprintf(cParam1,"%x:%x:%x::%x",h1,h2,h3, h8); |
| | 1650 | else if(!h3 && !h4 && !h5 && !h6) |
| | 1651 | sprintf(cParam1,"%x:%x::%x:%x",h1,h2, h7,h8); |
| | 1652 | else if(!h2 && !h3 && !h4 && !h5) |
| | 1653 | sprintf(cParam1,"%x::%x:%x:%x",h1, h6,h7,h8); |
| | 1654 | //3 consecutive 0 cases |
| | 1655 | else if(!h5 && !h6 && !h7) |
| | 1656 | sprintf(cParam1,"%x:%x:%x:%x::%x",h1,h2,h3,h4, h8); |
| | 1657 | else if(!h4 && !h5 && !h6) |
| | 1658 | sprintf(cParam1,"%x:%x:%x::%x:%x",h1,h2,h3, h7,h8); |
| | 1659 | else if(!h3 && !h4 && !h5) |
| | 1660 | sprintf(cParam1,"%x:%x::%x:%x:%x",h1,h2, h6,h7,h8); |
| | 1661 | else if(!h2 && !h3 && !h4) |
| | 1662 | sprintf(cParam1,"%x::%x:%x:%x:%x",h1, h5,h6,h7,h8); |
| | 1663 | //2 consecutive 0 cases |
| | 1664 | else if(!h6 && !h7) |
| | 1665 | sprintf(cParam1,"%x:%x:%x:%x:%x::%x",h1,h2,h3,h4,h5, h8); |
| | 1666 | else if(!h5 && !h6) |
| | 1667 | sprintf(cParam1,"%x:%x:%x:%x::%x:%x",h1,h2,h3,h4, h7,h8); |
| | 1668 | else if(!h4 && !h5) |
| | 1669 | sprintf(cParam1,"%x:%x:%x::%x:%x:%x",h1,h2,h3, h6,h7,h8); |
| | 1670 | else if(!h3 && !h4) |
| | 1671 | sprintf(cParam1,"%x:%x::%x:%x:%x:%x",h1,h2, h5,h6,h7,h8); |
| | 1672 | else if(!h2 && !h3) |
| | 1673 | sprintf(cParam1,"%x::%x:%x:%x:%x:%x",h1, h4,h5,h6,h7,h8); |
| | 1674 | //0 consecutive 0 case, i.e. no double colon case |
| | 1675 | else if(1) |
| | 1676 | sprintf(cParam1,"%x:%x:%x:%x:%x:%x:%x:%x",h1,h2,h3,h4,h5,h6,h7,h8); |
| | 1677 | |