diff options
author | Michael McTernan <mike.mcternan@wavemobile.com> | 2015-05-02 07:52:23 +0200 |
---|---|---|
committer | Holger Hans Peter Freyther <holger@moiji-mobile.com> | 2015-05-02 07:53:34 +0200 |
commit | b07d07072e70ac4f920be9dfdf45615193b4ec2d (patch) | |
tree | 1200445bb560a1a1c34a90b7a201214b0c91c33b | |
parent | 633cc0d7cbc224b7e5ba811e2161553df30a6dfe (diff) |
openggsn: Check return codes and take error paths on failure.
Return early when socket() returns -1, and check return codes
where indicated by some TODOs. This removes 2 TODOs and fixes
a compiler warning about assignment to a variable which then
isn't used.
Signed-off-by: Michael McTernan <mike.mcternan@wavemobile.com>
-rw-r--r-- | lib/tun.c | 19 |
1 files changed, 16 insertions, 3 deletions
@@ -117,6 +117,7 @@ int tun_sifflags(struct tun_t *this, int flags) ifr.ifr_name[IFNAMSIZ - 1] = 0; /* Make sure to terminate */ if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { SYS_ERR(DTUN, LOGL_ERROR, errno, "socket() failed"); + return -1; } if (ioctl(fd, SIOCSIFFLAGS, &ifr)) { SYS_ERR(DTUN, LOGL_ERROR, errno, @@ -304,7 +305,8 @@ int tun_addaddr(struct tun_t *this, iov.iov_len = req.n.nlmsg_len; msg.msg_name = (void *)&nladdr; - msg.msg_namelen = sizeof(nladdr), msg.msg_iov = &iov; + msg.msg_namelen = sizeof(nladdr); + msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_control = NULL; msg.msg_controllen = 0; @@ -318,9 +320,20 @@ int tun_addaddr(struct tun_t *this, req.n.nlmsg_seq = 0; req.n.nlmsg_flags |= NLM_F_ACK; - status = sendmsg(fd, &msg, 0); /* TODO Error check */ + status = sendmsg(fd, &msg, 0); + if (status != req.n.nlmsg_len) { + SYS_ERR(DTUN, LOGL_ERROR, errno, "sendmsg() failed, returned %d", status); + close(fd); + return -1; + } + + status = tun_sifflags(this, IFF_UP | IFF_RUNNING); + if (status == -1) { + close(fd); + return -1; + } + - tun_sifflags(this, IFF_UP | IFF_RUNNING); /* TODO */ close(fd); this->addrs++; return 0; |