Browse Source

Add in support for inet_netof, inet_lnaof, inet_makeaddr and hstrerror.

David McCullough 23 years ago
parent
commit
9557844c48
3 changed files with 88 additions and 3 deletions
  1. 11 3
      libc/inet/Makefile
  2. 66 0
      libc/inet/addr.c
  3. 11 0
      libc/inet/herror.c

+ 11 - 3
libc/inet/Makefile

@@ -32,7 +32,8 @@ endif
 ALL_SUBDIRS = rpc
 
 MSRC=addr.c
-MOBJ=inet_aton.o inet_addr.o inet_ntoa.o
+MOBJ=inet_aton.o inet_addr.o inet_ntoa.o inet_makeaddr.o inet_lnaof.o \
+	inet_netof.o
 
 MSRC2=resolv.c
 MOBJ2=encodeh.o decodeh.o encoded.o decoded.o lengthd.o encodeq.o \
@@ -48,12 +49,15 @@ MOBJ3= accept.o bind.o connect.o getpeername.o getsockname.o getsockopt.o \
 	listen.o recv.o recvfrom.o recvmsg.o send.o sendmsg.o sendto.o \
 	setsockopt.o shutdown.o socket.o socketpair.o 
 
+MSRC4=herror.c
+MOBJ4=herror.o hstrerror.o
+
 CSRC =getservice.c getproto.c hostid.c getnetent.c getnetbynm.c getnetbyad.c \
-	herror.c inet_net.c ntop.c
+	inet_net.c ntop.c
 
 COBJS=$(patsubst %.c,%.o, $(CSRC))
 
-OBJS=$(MOBJ) $(MOBJ2) $(MOBJ3) $(COBJS)
+OBJS=$(MOBJ) $(MOBJ2) $(MOBJ3) $(MOBJ4) $(COBJS)
 
 
 all: $(OBJS) $(LIBC)
@@ -75,6 +79,10 @@ $(MOBJ3): $(MSRC3)
 	$(CC) $(CFLAGS) -DL_$* $< -c -o $*.o
 	$(STRIPTOOL) -x -R .note -R .comment $*.o
 
+$(MOBJ4): $(MSRC4)
+	$(CC) $(CFLAGS) -DL_$* $< -c -o $*.o
+	$(STRIPTOOL) -x -R .note -R .comment $*.o
+
 $(COBJS): %.o : %.c
 	$(CC) $(CFLAGS) -c $< -o $@
 	$(STRIPTOOL) -x -R .note -R .comment $*.o

+ 66 - 0
libc/inet/addr.c

@@ -112,3 +112,69 @@ struct in_addr in;
 	return p+1;
 }
 #endif
+
+#ifdef L_inet_makeaddr
+/*
+ * Formulate an Internet address from network + host.  Used in
+ * building addresses stored in the ifnet structure.
+ */
+struct in_addr inet_makeaddr(net, host)
+unsigned long net, host;
+{
+        unsigned long addr;
+
+        if (net < 128)
+                addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
+        else if (net < 65536)
+                addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
+        else if (net < 16777216L)
+                addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
+        else
+                addr = net | host;
+        addr = htonl(addr);
+        return (*(struct in_addr *)&addr);
+}
+
+#endif
+
+#ifdef L_inet_lnaof
+/*
+ * Return the local network address portion of an
+ * internet address; handles class a/b/c network
+ * number formats.
+ */
+unsigned long inet_lnaof(in)
+struct in_addr in;
+{
+	unsigned long i = ntohl(in.s_addr);
+
+	if (IN_CLASSA(i))
+		return ((i)&IN_CLASSA_HOST);
+	else if (IN_CLASSB(i))
+		return ((i)&IN_CLASSB_HOST);
+	else
+		return ((i)&IN_CLASSC_HOST);
+}
+#endif
+
+#ifdef L_inet_netof
+
+/*
+ * Return the network number from an internet
+ * address; handles class a/b/c network #'s.
+ */
+u_int32_t
+inet_netof(in)
+        struct in_addr in;
+{
+        u_int32_t i = ntohl(in.s_addr);
+
+        if (IN_CLASSA(i))
+                return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT);
+        else if (IN_CLASSB(i))
+                return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT);
+        else
+                return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT);
+}
+
+#endif

+ 11 - 0
libc/inet/herror.c

@@ -23,6 +23,8 @@
 #include <string.h>
 #include <netdb.h>
 
+#ifdef L_herror
+
 static const char *const h_errlist[] = {
 	"Error 0",
 	"Unknown host",				/* 1 HOST_NOT_FOUND */
@@ -51,3 +53,12 @@ void herror(const char *s)
 	}
 	fprintf(stderr, "%s%s%s\n", s, c, p);
 }
+
+#endif
+
+#ifdef L_hstrerror
+const char *hstrerror(int err)
+{
+	return(strerror(err));
+}
+#endif