Staging: comedi: Correct use of ! and &
[linux-2.6/linux-2.6-openrd.git] / net / phonet / datagram.c
blobe087862ed7e4342f5e5a0c28feb50d724150a881
1 /*
2 * File: datagram.c
4 * Datagram (ISI) Phonet sockets
6 * Copyright (C) 2008 Nokia Corporation.
8 * Contact: Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * Original author: Sakari Ailus <sakari.ailus@nokia.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * version 2 as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * 02110-1301 USA
26 #include <linux/kernel.h>
27 #include <linux/socket.h>
28 #include <asm/ioctls.h>
29 #include <net/sock.h>
31 #include <linux/phonet.h>
32 #include <net/phonet/phonet.h>
34 static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb);
36 /* associated socket ceases to exist */
37 static void pn_sock_close(struct sock *sk, long timeout)
39 sk_common_release(sk);
42 static int pn_ioctl(struct sock *sk, int cmd, unsigned long arg)
44 struct sk_buff *skb;
45 int answ;
47 switch (cmd) {
48 case SIOCINQ:
49 lock_sock(sk);
50 skb = skb_peek(&sk->sk_receive_queue);
51 answ = skb ? skb->len : 0;
52 release_sock(sk);
53 return put_user(answ, (int __user *)arg);
56 return -ENOIOCTLCMD;
59 /* Destroy socket. All references are gone. */
60 static void pn_destruct(struct sock *sk)
62 skb_queue_purge(&sk->sk_receive_queue);
65 static int pn_init(struct sock *sk)
67 sk->sk_destruct = pn_destruct;
68 return 0;
71 static int pn_sendmsg(struct kiocb *iocb, struct sock *sk,
72 struct msghdr *msg, size_t len)
74 struct sockaddr_pn *target;
75 struct sk_buff *skb;
76 int err;
78 if (msg->msg_flags & MSG_OOB)
79 return -EOPNOTSUPP;
81 if (msg->msg_name == NULL)
82 return -EDESTADDRREQ;
84 if (msg->msg_namelen < sizeof(struct sockaddr_pn))
85 return -EINVAL;
87 target = (struct sockaddr_pn *)msg->msg_name;
88 if (target->spn_family != AF_PHONET)
89 return -EAFNOSUPPORT;
91 skb = sock_alloc_send_skb(sk, MAX_PHONET_HEADER + len,
92 msg->msg_flags & MSG_DONTWAIT, &err);
93 if (skb == NULL)
94 return err;
95 skb_reserve(skb, MAX_PHONET_HEADER);
97 err = memcpy_fromiovec((void *)skb_put(skb, len), msg->msg_iov, len);
98 if (err < 0) {
99 kfree_skb(skb);
100 return err;
104 * Fill in the Phonet header and
105 * finally pass the packet forwards.
107 err = pn_skb_send(sk, skb, target);
109 /* If ok, return len. */
110 return (err >= 0) ? len : err;
113 static int pn_recvmsg(struct kiocb *iocb, struct sock *sk,
114 struct msghdr *msg, size_t len, int noblock,
115 int flags, int *addr_len)
117 struct sk_buff *skb = NULL;
118 struct sockaddr_pn sa;
119 int rval = -EOPNOTSUPP;
120 int copylen;
122 if (flags & MSG_OOB)
123 goto out_nofree;
125 if (addr_len)
126 *addr_len = sizeof(sa);
128 skb = skb_recv_datagram(sk, flags, noblock, &rval);
129 if (skb == NULL)
130 goto out_nofree;
132 pn_skb_get_src_sockaddr(skb, &sa);
134 copylen = skb->len;
135 if (len < copylen) {
136 msg->msg_flags |= MSG_TRUNC;
137 copylen = len;
140 rval = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copylen);
141 if (rval) {
142 rval = -EFAULT;
143 goto out;
146 rval = (flags & MSG_TRUNC) ? skb->len : copylen;
148 if (msg->msg_name != NULL)
149 memcpy(msg->msg_name, &sa, sizeof(struct sockaddr_pn));
151 out:
152 skb_free_datagram(sk, skb);
154 out_nofree:
155 return rval;
158 /* Queue an skb for a sock. */
159 static int pn_backlog_rcv(struct sock *sk, struct sk_buff *skb)
161 int err = sock_queue_rcv_skb(sk, skb);
162 if (err < 0)
163 kfree_skb(skb);
164 return err ? NET_RX_DROP : NET_RX_SUCCESS;
167 /* Module registration */
168 static struct proto pn_proto = {
169 .close = pn_sock_close,
170 .ioctl = pn_ioctl,
171 .init = pn_init,
172 .sendmsg = pn_sendmsg,
173 .recvmsg = pn_recvmsg,
174 .backlog_rcv = pn_backlog_rcv,
175 .hash = pn_sock_hash,
176 .unhash = pn_sock_unhash,
177 .get_port = pn_sock_get_port,
178 .obj_size = sizeof(struct pn_sock),
179 .owner = THIS_MODULE,
180 .name = "PHONET",
183 static struct phonet_protocol pn_dgram_proto = {
184 .ops = &phonet_dgram_ops,
185 .prot = &pn_proto,
186 .sock_type = SOCK_DGRAM,
189 int __init isi_register(void)
191 return phonet_proto_register(PN_PROTO_PHONET, &pn_dgram_proto);
194 void __exit isi_unregister(void)
196 phonet_proto_unregister(PN_PROTO_PHONET, &pn_dgram_proto);