ga_serv.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "gai_hdr.h"
  2. /*
  3. * This function handles the service string.
  4. */
  5. /* include ga_serv */
  6. int
  7. ga_serv(struct addrinfo *aihead, const struct addrinfo *hintsp,
  8. const char *serv)
  9. {
  10. int port, rc, nfound;
  11. struct servent *sptr;
  12. nfound = 0;
  13. if (isdigit(serv[0])) { /* check for port number string first */
  14. port = htons(atoi(serv));
  15. if (hintsp->ai_socktype) {
  16. /* 4caller specifies socket type */
  17. if ( (rc = ga_port(aihead, port, hintsp->ai_socktype)) < 0)
  18. return(EAI_MEMORY);
  19. nfound += rc;
  20. } else {
  21. /* 4caller does not specify socket type */
  22. if ( (rc = ga_port(aihead, port, SOCK_STREAM)) < 0)
  23. return(EAI_MEMORY);
  24. nfound += rc;
  25. if ( (rc = ga_port(aihead, port, SOCK_DGRAM)) < 0)
  26. return(EAI_MEMORY);
  27. nfound += rc;
  28. }
  29. } else {
  30. /* 4try service name, TCP then UDP */
  31. if (hintsp->ai_socktype == 0 || hintsp->ai_socktype == SOCK_STREAM) {
  32. if ( (sptr = getservbyname(serv, "tcp")) != NULL) {
  33. if ( (rc = ga_port(aihead, sptr->s_port, SOCK_STREAM)) < 0)
  34. return(EAI_MEMORY);
  35. nfound += rc;
  36. }
  37. }
  38. if (hintsp->ai_socktype == 0 || hintsp->ai_socktype == SOCK_DGRAM) {
  39. if ( (sptr = getservbyname(serv, "udp")) != NULL) {
  40. if ( (rc = ga_port(aihead, sptr->s_port, SOCK_DGRAM)) < 0)
  41. return(EAI_MEMORY);
  42. nfound += rc;
  43. }
  44. }
  45. }
  46. if (nfound == 0) {
  47. if (hintsp->ai_socktype == 0)
  48. return(EAI_NONAME); /* all calls to getservbyname() failed */
  49. else
  50. return(EAI_SERVICE);/* service not supported for socket type */
  51. }
  52. return(0);
  53. }
  54. /* end ga_serv */