• Subject: Re: TCP/IP sockets - Getservbyname
  • From: "Scott Klement" <infosys@xxxxxxxxxxxx>
  • Date: 22 Jul 1999 17:46:51 -0500

Hi Chris...

Chris Bipes <ChrisB@cross-check.com> wrote:
>
> I am trying to add the ability to look up a service name using the
> Getservbyname function.  I have defined the data structure servent
>  with-in
> my program but the pointer returned by the function is not to the da
> structure I defined.  How do you retrieve the entries from the memor
>  the
> pointer is pointing to?  I would imagine that the s_name start at th
> pointer and goes to the first null, followed by list of aliases, eac
>  null
> terminated.  Then there should be the port number as a short integer
>  and
> that is what I am looking for.  I suppose I could just open the file
> qusrsys/qatocls1 and chain to receive but that would be cheating....
>  I am
> carious as to see if anyone else has successfully used this function
>
> Christopher K. Bipes  mailto:ChrisB@Cross-Check.com
> Sr. Programmer/Analyst  mailto:Chris_Bipes@Yahoo.com
> CrossCheck, Inc.  http://www.cross-check.com
> 6119 State Farm Drive  Phone: 707 586-0551 x 1102
> Rohnert Park  CA  94928 Fax: 707 586-1884

Whats actually in the servent structure will be the pointer data,
not a null terminated string.   In other words, the pointer returned
by getservbyname does not start with a null-terminated character
string, but rather with the address of another area of memory.

What makes it even more fun is the s_aliases subfield in the
servent structure.   Its not just a pointer, but a pointer to
an array of pointers!  (variable-length, too)

The gethostbyname (which I use almost constantly) works the same
way, so if you're familiar with that, maybe that'll help you :)

Tell ya what, I'll include some example code (at the bottom of this
message) that shows the use of the getservbyname function.   (This
code has been tested, and it works on my system...)

Please keep in mind that these things ARE case-sensitive.  In other
words, if you use my example code, and do:

CALL PGM(WHATEVER) PARM('telnet' 'tcp')

This WILL work, but:

CALL PGM(WHATEVER) PARM('telnet' 'TCP')

this won't.   The service is 'tcp' not 'TCP'

Hope that helps...

Scott Klement
Information Systems Manager
Klement's Sausage Co, Inc.




Here's the sample code for the getservbyname function:
-------------------------cut here------------------------------------
D MAX_ALIAS       C                   CONST(50)

D p_servent       S               *
D servent         ds                  based(p_servent)
D   s_name                        *
D   s_aliases                     *
D   s_port                      10I 0
D   s_proto                       *

D alist           S               *   DIM(MAX_ALIAS) BASED(s_aliases)

D c_name          S             50A   BASED(s_name)
D c_proto         S             50A   BASED(s_proto)
D p_alias         S               *
D c_alias         S             50A   BASED(p_alias)

D ServByName      PR              *   ExtProc('getservbyname')
D   service                       *   value
D   proto                         *   value


C     *entry        plist
c                   parm                    Service          20
c                   parm                    Protocol         20

c                   eval      Service = %trim(service) + x'00'
c                   eval      Protocol = %trim(Protocol) + x'00'

************************************
**  Lookup the service
************************************
c                   eval      p_servent = ServByName( %addr(Service) :
c                                                     %addr(Protocol))

c                   if        p_servent = *NULL
c                   eval      Msg = 'Lookup failed!'
c                   dsply                   Msg              50
c                   eval      *INLR = *ON
c                   Return
c                   endif

************************************
**  Show the name of the service
************************************
c     x'00'         scan      c_name        pos               3 0
c                   Select
c                   when      pos < 1
c                   eval      Msg = 'Name=' + %Trim(c_name)
c                   when      pos = 0
c                   eval      msg = 'Name=*BLANKS'
c                   other
c                   eval      msg = 'Name=' +
c                               %trim( %subst(c_name:1:pos-1) )
c                   endsl

c     msg           dsply

************************************
**  Show the aliases of the service
**  (if there are any)
************************************
c                   do        MAX_ALIAS     X                 3 0

c                   if        alist(X) = *NULL
c                   Leave
c                   endif

c                   eval      p_alias = alist(X)

c     x'00'         scan      c_alias       pos               3 0
c                   Select
c                   when      pos < 1
c                   eval      Msg = 'Alias=' + %Trim(c_alias)
c                   when      pos = 0
c                   eval      msg = 'Alias=*BLANKS'
c                   other
c                   eval      msg = 'Alias=' +
c                               %trim( %subst(c_alias:1:pos-1) )
c                   endsl

c     msg           dsply
c                   enddo

************************************
**  Port Number (the easy one)
************************************
c                   move      s_port        wkPort           10
c     '0'           check     wkPort        pos
c                   eval      Msg = 'Port=' + %subst(wkPort:pos)

c     msg           dsply

************************************
**  Finally, protocol....
************************************
c     x'00'         scan      c_proto       pos               3 0
c                   Select
c                   when      pos < 1
c                   eval      Msg = 'Prot=' + %Trim(c_proto)
c                   when      pos = 0
c                   eval      msg = 'Prot=*BLANKS'
c                   other
c                   eval      msg = 'Prot=' +
c                               %trim( %subst(c_proto:1:pos-1) )
c                   endsl

c                   dsply                   msg

c                   eval      *INLR = *ON
c                   Return
-------------------------cut here------------------------------------
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This is the RPG/400 Discussion Mailing List!  To submit a new         *
* message, send your mail to "RPG400-L@midrange.com".  To unsubscribe   *
* from this list send email to MAJORDOMO@midrange.com and specify       *
* 'unsubscribe RPG400-L' in the body of your message.  Questions should *
* be directed to the list owner / operator: david@midrange.com          *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2019 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].