Claudio Jeker <cje...@diehard.n-r-g.com> wrote:

> On Wed, Dec 11, 2019 at 04:52:53PM +0100, Denis Fondras wrote:
> > Use sendmsg() instead of sendto() like ospfd(8) does.
> > 
> > Index: database.c
> > ===================================================================
> > RCS file: /cvs/src/usr.sbin/ospf6d/database.c,v
> > retrieving revision 1.16
> > diff -u -p -r1.16 database.c
> > --- database.c      10 May 2019 13:50:34 -0000      1.16
> > +++ database.c      8 Dec 2019 14:55:57 -0000
> > @@ -147,7 +147,7 @@ send_db_description(struct nbr *nbr)
> >             goto fail;
> >  
> >     /* transmit packet */
> > -   ret = send_packet(nbr->iface, buf->buf, buf->wpos, &dst);
> > +   ret = send_packet(nbr->iface, buf, &dst);
> >  done:
> >     ibuf_free(buf);
> >     return (ret);
> > Index: hello.c
> > ===================================================================
> > RCS file: /cvs/src/usr.sbin/ospf6d/hello.c,v
> > retrieving revision 1.18
> > diff -u -p -r1.18 hello.c
> > --- hello.c 22 Feb 2018 07:43:29 -0000      1.18
> > +++ hello.c 8 Dec 2019 14:55:57 -0000
> > @@ -104,7 +104,7 @@ send_hello(struct iface *iface)
> >     if (upd_ospf_hdr(buf, iface))
> >             goto fail;
> >  
> > -   ret = send_packet(iface, buf->buf, buf->wpos, &dst);
> > +   ret = send_packet(iface, buf, &dst);
> >  
> >     ibuf_free(buf);
> >     return (ret);
> > Index: lsack.c
> > ===================================================================
> > RCS file: /cvs/src/usr.sbin/ospf6d/lsack.c,v
> > retrieving revision 1.6
> > diff -u -p -r1.6 lsack.c
> > --- lsack.c 25 Oct 2014 03:23:49 -0000      1.6
> > +++ lsack.c 8 Dec 2019 14:55:57 -0000
> > @@ -55,7 +55,7 @@ send_ls_ack(struct iface *iface, struct 
> >     if (upd_ospf_hdr(buf, iface))
> >             goto fail;
> >  
> > -   ret = send_packet(iface, buf->buf, buf->wpos, &addr);
> > +   ret = send_packet(iface, buf, &addr);
> >  
> >     ibuf_free(buf);
> >     return (ret);
> > Index: lsreq.c
> > ===================================================================
> > RCS file: /cvs/src/usr.sbin/ospf6d/lsreq.c,v
> > retrieving revision 1.9
> > diff -u -p -r1.9 lsreq.c
> > --- lsreq.c 10 May 2019 13:50:34 -0000      1.9
> > +++ lsreq.c 8 Dec 2019 14:55:58 -0000
> > @@ -77,7 +77,7 @@ send_ls_req(struct nbr *nbr)
> >     if (upd_ospf_hdr(buf, nbr->iface))
> >             goto fail;
> >  
> > -   ret = send_packet(nbr->iface, buf->buf, buf->wpos, &dst);
> > +   ret = send_packet(nbr->iface, buf, &dst);
> >  
> >     ibuf_free(buf);
> >     return (ret);
> > Index: lsupdate.c
> > ===================================================================
> > RCS file: /cvs/src/usr.sbin/ospf6d/lsupdate.c,v
> > retrieving revision 1.13
> > diff -u -p -r1.13 lsupdate.c
> > --- lsupdate.c      28 Jan 2015 22:03:17 -0000      1.13
> > +++ lsupdate.c      8 Dec 2019 14:55:58 -0000
> > @@ -227,7 +227,7 @@ send_ls_update(struct ibuf *buf, struct 
> >     if (upd_ospf_hdr(buf, iface))
> >             goto fail;
> >  
> > -   ret = send_packet(iface, buf->buf, buf->wpos, &addr);
> > +   ret = send_packet(iface, buf, &addr);
> >  
> >     ibuf_free(buf);
> >     return (ret);
> > Index: ospfe.h
> > ===================================================================
> > RCS file: /cvs/src/usr.sbin/ospf6d/ospfe.h,v
> > retrieving revision 1.19
> > diff -u -p -r1.19 ospfe.h
> > --- ospfe.h 25 Oct 2014 03:23:49 -0000      1.19
> > +++ ospfe.h 8 Dec 2019 14:55:58 -0000
> > @@ -228,7 +228,7 @@ struct lsa_hdr  *lsa_hdr_new(void);
> >  /* packet.c */
> >  int         gen_ospf_hdr(struct ibuf *, struct iface *, u_int8_t);
> >  int         upd_ospf_hdr(struct ibuf *, struct iface *);
> > -int         send_packet(struct iface *, void *, size_t, struct in6_addr *);
> > +int         send_packet(struct iface *, struct ibuf *, struct in6_addr *);
> >  void        recv_packet(int, short, void *);
> >  
> >  char       *pkt_ptr;       /* packet buffer */
> > Index: packet.c
> > ===================================================================
> > RCS file: /cvs/src/usr.sbin/ospf6d/packet.c,v
> > retrieving revision 1.15
> > diff -u -p -r1.15 packet.c
> > --- packet.c        10 May 2019 01:29:31 -0000      1.15
> > +++ packet.c        8 Dec 2019 14:55:58 -0000
> > @@ -78,10 +78,12 @@ upd_ospf_hdr(struct ibuf *buf, struct if
> >  
> >  /* send and receive packets */
> >  int
> > -send_packet(struct iface *iface, void *pkt, size_t len,
> > +send_packet(struct iface *iface, struct ibuf *buf,
> >      struct in6_addr *dst)
> >  {
> > -   struct sockaddr_in6      sa6;
> > +   struct sockaddr_in6     sa6;
> > +   struct msghdr           msg;
> > +   struct iovec            iov[2];
> >  
> >     /* setup buffer */
> >     bzero(&sa6, sizeof(sa6));
> > @@ -102,8 +104,15 @@ send_packet(struct iface *iface, void *p
> >                     return (-1);
> >             }
> >  
> > -   if (sendto(iface->fd, pkt, len, 0, (struct sockaddr *)&sa6,
> > -       sizeof(sa6)) == -1) {
> > +   bzero(&msg, sizeof(msg));
> > +   msg.msg_name = &sa6;
> > +   msg.msg_namelen = sizeof(sa6);
> > +   iov[0].iov_base = buf->buf;
> > +   iov[0].iov_len = ibuf_size(buf);
> > +   msg.msg_iov = iov;
> > +   msg.msg_iovlen = 1;
> > +
> > +   if (sendmsg(iface->fd, &msg, 0) == -1) {
> >             log_warn("send_packet: error sending packet on interface %s",
> >                 iface->name);
> >             return (-1);
> > 
> 
> While it makes sense to pass the ibuf like in ospfd I see no reason to
> switch to sendmsg(). In ospfd this is done to prepend the IP header but
> that seems not needed here. Is there another reason to switch to
> sendmsg()?

There is a damn good reason:  Make the code similar.

Reply via email to