ipAddress stuff
authorClaus Gittinger <cg@exept.de>
Mon, 08 May 2000 15:57:42 +0200
changeset 888 a868b0e54a2b
parent 887 2b2dc6b59b5a
child 889 b3bfa0862f55
ipAddress stuff
Socket.st
--- a/Socket.st	Fri Mar 24 13:50:17 2000 +0100
+++ b/Socket.st	Mon May 08 15:57:42 2000 +0200
@@ -10,6 +10,8 @@
  hereby transferred.
 "
 
+"{ Package: 'stx:libbasic2' }"
+
 NonPositionableExternalStream subclass:#Socket
 	instanceVariableNames:'domain socketType protocol port serviceName peerName'
 	classVariableNames:''
@@ -2245,195 +2247,195 @@
     int flags = 0;
 
     if (fp != nil) {
-	sock = fileno(__FILEVal(fp));
-
-	oClass = __Class(aDataBuffer);
-	switch (_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
-	    case BYTEARRAY:
-	    case WORDARRAY:
-	    case SWORDARRAY:
-	    case LONGARRAY:
-	    case SLONGARRAY:
-	    case FLOATARRAY:
-	    case DOUBLEARRAY:
-		break;
-	    default:
-		goto bad;
-	}
-
-	nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars);
-	nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ);
-	objSize = _Size(aDataBuffer) - nInstBytes;
-	cp = (char *)__InstPtr(aDataBuffer) + nInstBytes;
-	if (__isSmallInteger(startIndex)) {
-	    cp += __intVal(startIndex);
-	    objSize -= __intVal(startIndex);
-	}
-	if (__isSmallInteger(nBytes)) {
-	    if (__intVal(nBytes) < objSize) {
-		objSize = __intVal(nBytes);
-	    }
-	}
-
-	__BEGIN_INTERRUPTABLE__
-	do {
-	    if (addrBytes == nil) {
-		n = recvfrom(sock, cp, objSize, flags, (struct sockaddr *) 0, 0);
-	    } else {
-		n = recvfrom(sock, cp, objSize, flags, (struct sockaddr *) &sa, &alen);
-	    }
-	} while ((n < 0) && (errno == EINTR));
-	__END_INTERRUPTABLE__
-
-	if (n >= 0) {
-	    if (addrBytes != nil) {
-		oClass = __Class(addrBytes);
-		if ((_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) != BYTEARRAY) 
-		    goto bad;
-		nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars);
-		nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ);
-		objSize = _Size(addrBytes) - nInstBytes;
-		cp = (char *)__InstPtr(addrBytes) + nInstBytes;
-		if (objSize < alen) 
-		    goto bad;
-
-		myDomain = __INST(domain);
-
-		/*
-		 * extract the datagrams address
-		 */
+        sock = fileno(__FILEVal(fp));
+
+        oClass = __Class(aDataBuffer);
+        switch (_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
+            case BYTEARRAY:
+            case WORDARRAY:
+            case SWORDARRAY:
+            case LONGARRAY:
+            case SLONGARRAY:
+            case FLOATARRAY:
+            case DOUBLEARRAY:
+                break;
+            default:
+                goto bad;
+        }
+
+        nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars);
+        nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ);
+        objSize = _Size(aDataBuffer) - nInstBytes;
+        cp = (char *)__InstPtr(aDataBuffer) + nInstBytes;
+        if (__isSmallInteger(startIndex)) {
+            cp += __intVal(startIndex);
+            objSize -= __intVal(startIndex);
+        }
+        if (__isSmallInteger(nBytes)) {
+            if (__intVal(nBytes) < objSize) {
+                objSize = __intVal(nBytes);
+            }
+        }
+
+        __BEGIN_INTERRUPTABLE__
+        do {
+            if (addrBytes == nil) {
+                n = recvfrom(sock, cp, objSize, flags, (struct sockaddr *) 0, 0);
+            } else {
+                n = recvfrom(sock, cp, objSize, flags, (struct sockaddr *) &sa, &alen);
+            }
+        } while ((n < 0) && (errno == EINTR));
+        __END_INTERRUPTABLE__
+
+        if (n >= 0) {
+            if (addrBytes != nil) {
+                oClass = __Class(addrBytes);
+                if ((_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) != BYTEARRAY) 
+                    goto bad;
+                nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars);
+                nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ);
+                objSize = _Size(addrBytes) - nInstBytes;
+                cp = (char *)__InstPtr(addrBytes) + nInstBytes;
+                if (objSize < alen) 
+                    goto bad;
+
+                myDomain = __INST(domain);
+
+                /*
+                 * extract the datagrams address
+                 */
 # ifdef AF_INET
-		if (myDomain == @symbol(inet)) {
-		    if (objSize < 4) 
-			goto bad;
-		    cp[0] = (sa.in.sin_addr.s_addr >> 24) & 0xFF;
-		    cp[2] = (sa.in.sin_addr.s_addr >> 16) & 0xFF;
-		    cp[3] = (sa.in.sin_addr.s_addr >> 8) & 0xFF;
-		    cp[4] = (sa.in.sin_addr.s_addr >> 0) & 0xFF;
-		    alen = 4;
-		}
+                if (myDomain == @symbol(inet)) {
+                    if (objSize < 4) 
+                        goto bad;
+                    cp[0] = (sa.in.sin_addr.s_addr >> 24) & 0xFF;
+                    cp[2] = (sa.in.sin_addr.s_addr >> 16) & 0xFF;
+                    cp[3] = (sa.in.sin_addr.s_addr >> 8) & 0xFF;
+                    cp[4] = (sa.in.sin_addr.s_addr >> 0) & 0xFF;
+                    alen = 4;
+                }
 # endif /* AF_INET */
 
 # ifdef AF_INET6
-		if (myDomain == @symbol(inet6)) {
-		    if (objSize < sizeof(sa.in6.sin6_addr.s6_addr)) 
-			goto bad;
-		    bcopy(sa.in6.sin6_addr.s6_addr, cp, sizeof(sa.in6.sin6_addr.s6_addr));
-		    alen = sizeof(sa.in6.sin6_addr.s6_addr);
-		}
+                if (myDomain == @symbol(inet6)) {
+                    if (objSize < sizeof(sa.in6.sin6_addr.s6_addr)) 
+                        goto bad;
+                    bcopy(sa.in6.sin6_addr.s6_addr, cp, sizeof(sa.in6.sin6_addr.s6_addr));
+                    alen = sizeof(sa.in6.sin6_addr.s6_addr);
+                }
 # endif /* AF_INET6 */
 
 # ifdef AF_APPLETALK
-		if (myDomain == @symbol(appletalk)) {
-		    if (objSize < 3) 
-			goto bad;
-		    cp[0] = (sa.at.sat_addr.s_net >> 8) & 0xFF;
-		    cp[2] = (sa.at.sat_addr.s_net) & 0xFF;
-		    cp[3] = (sa.at.sat_addr.s_node) & 0xFF;
-		    alen = 3;
-		}
+                if (myDomain == @symbol(appletalk)) {
+                    if (objSize < 3) 
+                        goto bad;
+                    cp[0] = (sa.at.sat_addr.s_net >> 8) & 0xFF;
+                    cp[2] = (sa.at.sat_addr.s_net) & 0xFF;
+                    cp[3] = (sa.at.sat_addr.s_node) & 0xFF;
+                    alen = 3;
+                }
 # endif /* AF_APPLETALK */
 
-		/*
-		 * XXXX add addressing stuff for other domains here ...
-		 */
+                /*
+                 * XXXX add addressing stuff for other domains here ...
+                 */
 # ifdef AF_X25
-		if (myDomain == @symbol(x25)) {
-		}
+                if (myDomain == @symbol(x25)) {
+                }
 # endif
 
 # ifdef AF_AX25
-		if (myDomain == @symbol(ax25)) {
-		}
+                if (myDomain == @symbol(ax25)) {
+                }
 # endif
 
 # ifdef AF_NS
-		if ((myDomain == @symbol(ns)) 
-		 || (myDomain == @symbol(xns))) {
-		}
+                if ((myDomain == @symbol(ns)) 
+                 || (myDomain == @symbol(xns))) {
+                }
 # endif
 
 # ifdef AF_SNA
-		if (myDomain == @symbol(sna)) {
-		}
+                if (myDomain == @symbol(sna)) {
+                }
 # endif
 
 # ifdef AF_RAW
-		if (myDomain == @symbol(raw)) {
-		}
+                if (myDomain == @symbol(raw)) {
+                }
 # endif
 
 # ifdef AF_ISO
-		if (myDomain == @symbol(iso)) {
-		}
+                if (myDomain == @symbol(iso)) {
+                }
 # endif
 
 # ifdef AF_DECnet
-		if (myDomain == @symbol(decnet)) {
-		}
+                if (myDomain == @symbol(decnet)) {
+                }
 # endif
 
 # ifdef AF_NETBIOS
-		if (myDomain == @symbol(netbios)) {
-		}
+                if (myDomain == @symbol(netbios)) {
+                }
 # endif
 
 # ifdef AF_IPX
-		if (myDomain == @symbol(ipx)) {
-		}
+                if (myDomain == @symbol(ipx)) {
+                }
 # endif
 
 # ifdef AF_BRIDGE
-		if (myDomain == @symbol(bridge)) {
-		}
+                if (myDomain == @symbol(bridge)) {
+                }
 # endif
 
 # ifdef AF_BSC
-		if (myDomain == @symbol(bsc)) {
-		}
+                if (myDomain == @symbol(bsc)) {
+                }
 # endif
 
 # ifdef AF_ROSE
-		if (myDomain == @symbol(rose)) {
-		}
+                if (myDomain == @symbol(rose)) {
+                }
 # endif
 
 # ifdef AF_IRDA
-		if (myDomain == @symbol(irda)) {
-		}
+                if (myDomain == @symbol(irda)) {
+                }
 # endif
 
 # if defined(AF_CCITT) && (AF_CCITT != AF_X25)
-		if (myDomain == @symbol(ccitt)) {
-		}
+                if (myDomain == @symbol(ccitt)) {
+                }
 # endif
 
-		addrLen = __MKSMALLINT(alen);
-	    }
-	}
-	if (n < 0) {
-	    __INST(lastErrorNumber) = __MKSMALLINT(errno);
-	}
-	nReceived = __MKSMALLINT(n);
+                addrLen = __MKSMALLINT(alen);
+            }
+        }
+        if (n < 0) {
+            __INST(lastErrorNumber) = __MKSMALLINT(errno);
+        }
+        nReceived = __MKSMALLINT(n);
     }
 #endif
 bad: ;
 %}.
     nReceived notNil ifTrue:[
-	nReceived < 0 ifTrue:[
-	    'Socket [warning]: ' infoPrint.
-	    (OperatingSystem errorTextForNumber:lastErrorNumber) infoPrintCR.
-	].
-	addrLen notNil ifTrue:[
-	    anAddressBuffer class isBytes ifTrue:[
-		"/ can be a ByteArray for ST/X compatibility
-		anAddressBuffer replaceFrom:1 to:addrLen with:addrBytes
-	    ] ifFalse:[
-		"/ can be a SocketAddress for ST-80 compatibility
-		anAddressBuffer hostAddress:(addrBytes copyTo:addrLen)
-	    ].
-	].
-	^ nReceived
+        nReceived < 0 ifTrue:[
+            'Socket [warning]: ' infoPrint.
+            (OperatingSystem errorTextForNumber:lastErrorNumber) infoPrintCR.
+        ].
+        addrLen notNil ifTrue:[
+           (anAddressBuffer isKindOf:SocketAddress) ifFalse:[
+                "/ can be a ByteArray for ST/X compatibility
+                anAddressBuffer replaceFrom:1 to:addrLen with:addrBytes
+            ] ifTrue:[
+                "/ can be a SocketAddress for ST-80 compatibility
+                anAddressBuffer hostAddress:(addrBytes copyTo:addrLen)
+            ].
+        ].
+        ^ nReceived
     ].
     "
      arrive here if you try to receive into an invalid buffer
@@ -2485,21 +2487,21 @@
     "/ addressBuffer can be a byteArray (last 2 bytes are portNo, msb-first)
     "/ or (better) an instance of SocketAddress
     "/
-    anAddressBuffer class isBytes ifTrue:[
-	domain == #inet ifTrue:[
-	    addrBytes := anAddressBuffer copyFrom:1 to:4.
-	    portNo := ((anAddressBuffer at:5) bitShift:8)
-		      + (anAddressBuffer at:6).
-	] ifFalse:[
-	    domain == #appletalk ifTrue:[
-		addrBytes := anAddressBuffer copyFrom:1 to:3.
-		portNo := ((anAddressBuffer at:5) bitShift:8)
-			  + (anAddressBuffer at:6).
-	    ].
-	].
-    ] ifFalse:[
-	addrBytes := anAddressBuffer hostAddress.
-	portNo := anAddressBuffer port.
+    (anAddressBuffer isKindOf:SocketAddress) ifFalse:[
+        domain == #inet ifTrue:[
+            addrBytes := anAddressBuffer copyFrom:1 to:4.
+            portNo := ((anAddressBuffer at:5) bitShift:8)
+                      + (anAddressBuffer at:6).
+        ] ifFalse:[
+            domain == #appletalk ifTrue:[
+                addrBytes := anAddressBuffer copyFrom:1 to:3.
+                portNo := ((anAddressBuffer at:5) bitShift:8)
+                          + (anAddressBuffer at:6).
+            ].
+        ].
+    ] ifTrue:[
+        addrBytes := anAddressBuffer hostAddress.
+        portNo := anAddressBuffer port.
     ].
 %{
 #ifndef NO_SOCKET
@@ -2521,111 +2523,111 @@
     if ((fp != nil) 
      && __isSmallInteger(startIndex)
      && __isSmallInteger(count)) {
-	sock = fileno(__FILEVal(fp));
-
-	if (addrBytes == nil) {
-	    alen = 0;
-	    saPtr = (struct sockaddr *)0;
-	} else {
-	    if (! __isByteArray(addrBytes)) goto bad;
-
-	    cp = (char *)__ByteArrayInstPtr(addrBytes)->ba_element;
-	    n = __byteArraySize(addrBytes);
-	    if (alen < n) n = alen;
+        sock = fileno(__FILEVal(fp));
+
+        if (addrBytes == nil) {
+            alen = 0;
+            saPtr = (struct sockaddr *)0;
+        } else {
+            if (! __isByteArray(addrBytes)) goto bad;
+
+            cp = (char *)__ByteArrayInstPtr(addrBytes)->ba_element;
+            n = __byteArraySize(addrBytes);
+            if (alen < n) n = alen;
 # ifdef DGRAM_DEBUG
-	    printf("address is %d bytes ... %d.%d.%d.%d", n, cp[0], cp[1], cp[2], cp[3]);
+            printf("address is %d bytes ... %d.%d.%d.%d", n, cp[0], cp[1], cp[2], cp[3]);
 # endif
-	    bcopy(cp, &sa.in.sin_addr.s_addr, n);
+            bcopy(cp, &sa.in.sin_addr.s_addr, n);
 
 # ifdef AF_INET
-	    if (__INST(domain) == @symbol(inet)) {
-		sa.in.sin_family = AF_INET;
-		sa.in.sin_port = htons((u_short) __intVal(portNo)); 
-	    }
+            if (__INST(domain) == @symbol(inet)) {
+                sa.in.sin_family = AF_INET;
+                sa.in.sin_port = htons((u_short) __intVal(portNo)); 
+            }
 # endif
 # ifdef AF_INET6
-	    if (__INST(domain) == @symbol(inet6)) {
-		sa.in6.sin6_family = AF_INET6;
-		sa.in6.sin6_port = htons((u_short) __intVal(portNo)); 
-	    }
+            if (__INST(domain) == @symbol(inet6)) {
+                sa.in6.sin6_family = AF_INET6;
+                sa.in6.sin6_port = htons((u_short) __intVal(portNo)); 
+            }
 # endif
 # ifdef AF_APPLETALK
-	    if (__INST(domain) == @symbol(appletalk)) {
-		sa.at.sat_family = AF_APPLETALK;
-		sa.at.sat_port = __intVal(portNo);
-	    }
+            if (__INST(domain) == @symbol(appletalk)) {
+                sa.at.sat_family = AF_APPLETALK;
+                sa.at.sat_port = __intVal(portNo);
+            }
 # endif
 # ifdef AF_IRDA
-	    if (__INST(domain) == @symbol(irda)) {
-	    }
+            if (__INST(domain) == @symbol(irda)) {
+            }
 # endif
-	}
-
-	oClass = __Class(aDataBuffer);
-	switch (_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
-	    case BYTEARRAY:
-		offs = __intVal(startIndex) - 1;
-		break;
-	    case WORDARRAY:
-	    case SWORDARRAY:
-		offs = (__intVal(startIndex) - 1) * 2;
-		break;
-	    case LONGARRAY:
-	    case SLONGARRAY:
-		offs = (__intVal(startIndex) - 1) * 4;
-		break;
-	    case LONGLONGARRAY:
-	    case SLONGLONGARRAY:
-		offs = (__intVal(startIndex) - 1) * 8;
+        }
+
+        oClass = __Class(aDataBuffer);
+        switch (_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
+            case BYTEARRAY:
+                offs = __intVal(startIndex) - 1;
+                break;
+            case WORDARRAY:
+            case SWORDARRAY:
+                offs = (__intVal(startIndex) - 1) * 2;
+                break;
+            case LONGARRAY:
+            case SLONGARRAY:
+                offs = (__intVal(startIndex) - 1) * 4;
+                break;
+            case LONGLONGARRAY:
+            case SLONGLONGARRAY:
+                offs = (__intVal(startIndex) - 1) * 8;
 # ifdef __NEED_LONGLONG_ALIGN
-		offs += 4;
+                offs += 4;
 # endif
-	    case FLOATARRAY:
-		offs = (__intVal(startIndex) - 1) * sizeof(float);
-		break;
-	    case DOUBLEARRAY:
-		offs = (__intVal(startIndex) - 1) * sizeof(double);
+            case FLOATARRAY:
+                offs = (__intVal(startIndex) - 1) * sizeof(float);
+                break;
+            case DOUBLEARRAY:
+                offs = (__intVal(startIndex) - 1) * sizeof(double);
 # ifdef __NEED_DOUBLE_ALIGN
-		offs += 4;
+                offs += 4;
 # endif
-		break;
-	    default:
-		goto bad;
-	}
-	nBytes = __intVal(count);
-
-	nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars);
-	nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ);
-	objSize = __qSize(aDataBuffer) - nInstBytes;
-	cp = (char *)__InstPtr(aDataBuffer) + nInstBytes;
-	cp += offs;
-	if ((offs + nBytes) > objSize) {
+                break;
+            default:
+                goto bad;
+        }
+        nBytes = __intVal(count);
+
+        nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars);
+        nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ);
+        objSize = __qSize(aDataBuffer) - nInstBytes;
+        cp = (char *)__InstPtr(aDataBuffer) + nInstBytes;
+        cp += offs;
+        if ((offs + nBytes) > objSize) {
 # ifdef DGRAM_DEBUG
-	    printf("cut off ...\n");
+            printf("cut off ...\n");
 # endif
-	    nBytes = objSize - offs;
-	}
-
-	norder = htonl(sa.in.sin_addr.s_addr);
+            nBytes = objSize - offs;
+        }
+
+        norder = htonl(sa.in.sin_addr.s_addr);
 # ifdef DGRAM_DEBUG
-	printf("sending %d bytes ... to ", nBytes);
-	printf("%d.%d.%d.%d\n",
-		    (norder >> 24) & 0xFF,
-		    (norder >> 16) & 0xFF,
-		    (norder >> 8) & 0xFF,
-		    norder & 0xFF);
+        printf("sending %d bytes ... to ", nBytes);
+        printf("%d.%d.%d.%d\n",
+                    (norder >> 24) & 0xFF,
+                    (norder >> 16) & 0xFF,
+                    (norder >> 8) & 0xFF,
+                    norder & 0xFF);
 # endif
 
-	__BEGIN_INTERRUPTABLE__
-	do {
-	    n = sendto(sock, cp, nBytes, _flags, saPtr, alen);
-	} while ((n < 0) && (errno == EINTR));
-	__END_INTERRUPTABLE__
-
-	if (n < 0) {
-	    __INST(lastErrorNumber) = __MKSMALLINT(errno);
-	}
-	RETURN (__MKSMALLINT(n));
+        __BEGIN_INTERRUPTABLE__
+        do {
+            n = sendto(sock, cp, nBytes, _flags, saPtr, alen);
+        } while ((n < 0) && (errno == EINTR));
+        __END_INTERRUPTABLE__
+
+        if (n < 0) {
+            __INST(lastErrorNumber) = __MKSMALLINT(errno);
+        }
+        RETURN (__MKSMALLINT(n));
     }
 #endif
 bad: ;
@@ -4763,5 +4765,5 @@
 !Socket class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.146 2000-03-24 12:50:17 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.147 2000-05-08 13:57:42 cg Exp $'
 ! !