inet_addr.c 770 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* posix */
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. /* bsd extensions */
  7. #include <sys/uio.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #define CLASS(x) (x[0]>>6)
  11. unsigned long
  12. inet_addr(char *from)
  13. {
  14. int i;
  15. char *p;
  16. unsigned char to[4];
  17. unsigned long x;
  18. p = from;
  19. memset(to, 0, 4);
  20. for(i = 0; i < 4 && *p; i++){
  21. to[i] = strtoul(p, &p, 0);
  22. if(*p == '.')
  23. p++;
  24. }
  25. switch(CLASS(to)){
  26. case 0: /* class A - 1 byte net */
  27. case 1:
  28. if(i == 3){
  29. to[3] = to[2];
  30. to[2] = to[1];
  31. to[1] = 0;
  32. } else if (i == 2){
  33. to[3] = to[1];
  34. to[1] = 0;
  35. }
  36. break;
  37. case 2: /* class B - 2 byte net */
  38. if(i == 3){
  39. to[3] = to[2];
  40. to[2] = 0;
  41. }
  42. break;
  43. }
  44. x = nptohl(to);
  45. x = htonl(x);
  46. return x;
  47. }