blob: 28ed07af9cf9172511ca867dddd3efee35f948c8 [file] [log] [blame]
Benny Prijonodd859a62005-11-01 16:42:51 +00001/* $Header: /pjproject-0.3/pjlib/src/pj/sock_linux_kernel.c 4 10/29/05 11:51a Bennylp $ */
2/*
3 * $Log: /pjproject-0.3/pjlib/src/pj/sock_linux_kernel.c $
4 *
5 * 4 10/29/05 11:51a Bennylp
6 * Version 0.3-pre2.
7 *
8 * 3 10/20/05 9:19a Bennylp
9 * Updated with new API convention (error code)
10 *
11 * 2 10/14/05 12:26a Bennylp
12 * Finished error code framework, some fixes in ioqueue, etc. Pretty
13 * major.
14 *
15 * 1 10/05/05 4:43p Bennylp
16 * Created.
17 *
18 */
19#include <pj/sock.h>
20#include <pj/assert.h>
21#include <pj/string.h> /* pj_memcpy() */
22#include <pj/os.h> /* PJ_CHECK_STACK() */
23#include <pj/addr_resolv.h> /* pj_gethostbyname() */
24#include <pj/ctype.h>
25#include <pj/compat/sprintf.h>
26#include <pj/log.h>
27#include <pj/errno.h>
28
29/* Linux kernel specific. */
30#include <linux/socket.h>
31#include <linux/net.h>
32//#include <net/sock.h>
33#include <linux/security.h>
34#include <linux/syscalls.h> /* sys_xxx() */
35#include <asm/ioctls.h> /* FIONBIO */
36#include <linux/utsname.h> /* for pj_gethostname() */
37
38/*
39 * Address families conversion.
40 * The values here are indexed based on pj_addr_family-0xFF00.
41 */
42const pj_uint16_t PJ_AF_UNIX = AF_UNIX;
43const pj_uint16_t PJ_AF_INET = AF_INET;
44const pj_uint16_t PJ_AF_INET6 = AF_INET6;
45#ifdef AF_PACKET
46const pj_uint16_t PJ_AF_PACKET = AF_PACKET;
47#else
48# error "AF_PACKET undeclared!"
49#endif
50#ifdef AF_IRDA
51const pj_uint16_t PJ_AF_IRDA = AF_IRDA;
52#else
53# error "AF_IRDA undeclared!"
54#endif
55
56/*
57 * Socket types conversion.
58 * The values here are indexed based on pj_sock_type-0xFF00
59 */
60const pj_uint16_t PJ_SOCK_STREAM= SOCK_STREAM;
61const pj_uint16_t PJ_SOCK_DGRAM = SOCK_DGRAM;
62const pj_uint16_t PJ_SOCK_RAW = SOCK_RAW;
63const pj_uint16_t PJ_SOCK_RDM = SOCK_RDM;
64
65/*
66 * Socket level values.
67 */
68const pj_uint16_t PJ_SOL_SOCKET = SOL_SOCKET;
69#ifdef SOL_IP
70const pj_uint16_t PJ_SOL_IP = SOL_IP;
71#else
72# error "SOL_IP undeclared!"
73#endif /* SOL_IP */
74#if defined(SOL_TCP)
75const pj_uint16_t PJ_SOL_TCP = SOL_TCP;
76#else
77# error "SOL_TCP undeclared!"
78#endif /* SOL_TCP */
79#ifdef SOL_UDP
80const pj_uint16_t PJ_SOL_UDP = SOL_UDP;
81#else
82# error "SOL_UDP undeclared!"
83#endif
84#ifdef SOL_IPV6
85const pj_uint16_t PJ_SOL_IPV6 = SOL_IPV6;
86#else
87# error "SOL_IPV6 undeclared!"
88#endif
89
90/*
91 * Convert 16-bit value from network byte order to host byte order.
92 */
93PJ_DEF(pj_uint16_t) pj_ntohs(pj_uint16_t netshort)
94{
95 return ntohs(netshort);
96}
97
98/*
99 * Convert 16-bit value from host byte order to network byte order.
100 */
101PJ_DEF(pj_uint16_t) pj_htons(pj_uint16_t hostshort)
102{
103 return htons(hostshort);
104}
105
106/*
107 * Convert 32-bit value from network byte order to host byte order.
108 */
109PJ_DEF(pj_uint32_t) pj_ntohl(pj_uint32_t netlong)
110{
111 return ntohl(netlong);
112}
113
114/*
115 * Convert 32-bit value from host byte order to network byte order.
116 */
117PJ_DEF(pj_uint32_t) pj_htonl(pj_uint32_t hostlong)
118{
119 return htonl(hostlong);
120}
121
122/*
123 * Convert an Internet host address given in network byte order
124 * to string in standard numbers and dots notation.
125 */
126PJ_DEF(char*) pj_inet_ntoa(pj_in_addr in)
127{
128#define UC(b) (((int)b)&0xff)
129 static char b[18];
130 char *p;
131
132 p = (char *)&in;
133 pj_snprintf(b, sizeof(b), "%d.%d.%d.%d",
134 UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
135
136 return b;
137}
138
139/*
140 * This function converts the Internet host address ccp from the standard
141 * numbers-and-dots notation into binary data and stores it in the structure
142 * that inp points to.
143 */
144PJ_DEF(int) pj_inet_aton(const pj_str_t *ccp, struct pj_in_addr *addr)
145{
146 pj_uint32_t val;
147 int base, n;
148 char c;
149 unsigned parts[4];
150 unsigned *pp = parts;
151 char cp_copy[18];
152 char *cp = cp_copy;
153
154 addr->s_addr = PJ_INADDR_NONE;
155
156 if (ccp->slen > 15) return 0;
157
158 pj_memcpy(cp, ccp->ptr, ccp->slen);
159 cp[ccp->slen] = '\0';
160
161 c = *cp;
162 for (;;) {
163 /*
164 * Collect number up to ``.''.
165 * Values are specified as for C:
166 * 0x=hex, 0=octal, isdigit=decimal.
167 */
168 if (!pj_isdigit((int)c))
169 return (0);
170 val = 0; base = 10;
171 if (c == '0') {
172 c = *++cp;
173 if (c == 'x' || c == 'X')
174 base = 16, c = *++cp;
175 else
176 base = 8;
177 }
178
179 for (;;) {
180 if (pj_isascii((int)c) && pj_isdigit((int)c)) {
181 val = (val * base) + (c - '0');
182 c = *++cp;
183 } else if (base==16 && pj_isascii((int)c) && pj_isxdigit((int)c)) {
184 val = (val << 4) |
185 (c + 10 - (pj_islower((int)c) ? 'a' : 'A'));
186 c = *++cp;
187 } else
188 break;
189 }
190
191 if (c == '.') {
192 /*
193 * Internet format:
194 * a.b.c.d
195 * a.b.c (with c treated as 16 bits)
196 * a.b (with b treated as 24 bits)
197 */
198 if (pp >= parts + 3)
199 return (0);
200 *pp++ = val;
201 c = *++cp;
202 } else
203 break;
204 }
205
206 /*
207 * Check for trailing characters.
208 */
209 if (c != '\0' && (!pj_isascii((int)c) || !pj_isspace((int)c)))
210 return (0);
211 /*
212 * Concoct the address according to
213 * the number of parts specified.
214 */
215 n = pp - parts + 1;
216 switch (n) {
217 case 0:
218 return (0); /* initial nondigit */
219 case 1: /* a -- 32 bits */
220 break;
221 case 2: /* a.b -- 8.24 bits */
222 if (val > 0xffffff)
223 return (0);
224 val |= parts[0] << 24;
225 break;
226 case 3: /* a.b.c -- 8.8.16 bits */
227 if (val > 0xffff)
228 return (0);
229 val |= (parts[0] << 24) | (parts[1] << 16);
230 break;
231 case 4: /* a.b.c.d -- 8.8.8.8 bits */
232 if (val > 0xff)
233 return (0);
234 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
235 break;
236 }
237
238 if (addr)
239 addr->s_addr = pj_htonl(val);
240 return (1);
241}
242
243/*
244 * Convert address string with numbers and dots to binary IP address.
245 */
246PJ_DEF(pj_in_addr) pj_inet_addr(const pj_str_t *cp)
247{
248 pj_in_addr addr;
249 pj_inet_aton(cp, &addr);
250 return addr;
251}
252
253/*
254 * Set the IP address of an IP socket address from string address,
255 * with resolving the host if necessary. The string address may be in a
256 * standard numbers and dots notation or may be a hostname. If hostname
257 * is specified, then the function will resolve the host into the IP
258 * address.
259 */
260PJ_DEF(pj_status_t) pj_sockaddr_in_set_str_addr( pj_sockaddr_in *addr,
261 const pj_str_t *str_addr)
262{
263 PJ_CHECK_STACK();
264
265 pj_assert(str_addr && str_addr->slen < PJ_MAX_HOSTNAME);
266
267 addr->sin_family = AF_INET;
268
269 if (str_addr && str_addr->slen) {
270 addr->sin_addr = pj_inet_addr(str_addr);
271 if (addr->sin_addr.s_addr == PJ_INADDR_NONE) {
272 pj_hostent he;
273 if (pj_gethostbyname(str_addr, &he) == 0) {
274 addr->sin_addr.s_addr = *(pj_uint32_t*)he.h_addr;
275 } else {
276 addr->sin_addr.s_addr = PJ_INADDR_NONE;
277 return -1;
278 }
279 }
280
281 } else {
282 addr->sin_addr.s_addr = 0;
283 }
284
285 return PJ_SUCCESS;
286}
287
288/*
289 * Set the IP address and port of an IP socket address.
290 * The string address may be in a standard numbers and dots notation or
291 * may be a hostname. If hostname is specified, then the function will
292 * resolve the host into the IP address.
293 */
294PJ_DEF(pj_status_t) pj_sockaddr_in_init( pj_sockaddr_in *addr,
295 const pj_str_t *str_addr,
296 pj_uint16_t port)
297{
298 pj_assert(addr && str_addr);
299
300 addr->sin_family = PJ_AF_INET;
301 pj_sockaddr_in_set_port(addr, port);
302 return pj_sockaddr_in_set_str_addr(addr, str_addr);
303}
304
305
306/*
307 * Get hostname.
308 */
309PJ_DEF(const pj_str_t*) pj_gethostname(void)
310{
311 static char buf[PJ_MAX_HOSTNAME];
312 static pj_str_t hostname;
313
314 PJ_CHECK_STACK();
315
316 if (hostname.ptr == NULL) {
317 hostname.ptr = buf;
318 down_read(&uts_sem);
319 hostname.slen = strlen(system_utsname.nodename);
320 if (hostname.slen > PJ_MAX_HOSTNAME) {
321 hostname.ptr[0] = '\0';
322 hostname.slen = 0;
323 } else {
324 pj_memcpy(hostname.ptr, system_utsname.nodename, hostname.slen);
325 }
326 up_read(&uts_sem);
327 }
328 return &hostname;
329}
330
331/*
332 * Get first IP address associated with the hostname.
333 */
334PJ_DEF(pj_in_addr) pj_gethostaddr(void)
335{
336 pj_sockaddr_in addr;
337 const pj_str_t *hostname = pj_gethostname();
338
339 pj_sockaddr_in_set_str_addr(&addr, hostname);
340 return addr.sin_addr;
341}
342
343
344/*
345 * Create new socket/endpoint for communication and returns a descriptor.
346 */
347PJ_DEF(pj_status_t) pj_sock_socket(int af, int type, int proto,
348 pj_sock_t *sock_fd)
349{
350 long result;
351
352 PJ_CHECK_STACK();
353
354 /* Sanity checks. */
355 PJ_ASSERT_RETURN(PJ_INVALID_SOCKET == -1 && sock_fd != NULL, PJ_EINVAL);
356
357 /* Initialize returned socket */
358 *sock_fd = PJ_INVALID_SOCKET;
359
360 /* Create socket. */
361 result = sys_socket(af, type, proto);
362 if (result < 0) {
363 return PJ_RETURN_OS_ERROR((-result));
364 }
365
366 *sock_fd = result;
367
368 return PJ_SUCCESS;
369}
370
371/*
372 * Bind socket.
373 */
374PJ_DEF(pj_status_t) pj_sock_bind( pj_sock_t sockfd,
375 const pj_sockaddr_t *addr,
376 int len)
377{
378 long err;
379 mm_segment_t oldfs;
380
381 PJ_CHECK_STACK();
382
383 PJ_ASSERT_RETURN(addr!=NULL && len >= sizeof(struct pj_sockaddr),
384 PJ_EINVAL);
385
386 oldfs = get_fs();
387 set_fs(KERNEL_DS);
388
389 err = sys_bind(sockfd, (struct sockaddr*)addr, len);
390
391 set_fs(oldfs);
392
393 if (err)
394 return PJ_RETURN_OS_ERROR(-err);
395 else
396 return PJ_SUCCESS;
397}
398
399
400/*
401 * Bind socket.
402 */
403PJ_DEF(pj_status_t) pj_sock_bind_in( pj_sock_t sockfd,
404 pj_uint32_t addr32,
405 pj_uint16_t port)
406{
407 pj_sockaddr_in addr;
408
409 PJ_CHECK_STACK();
410
411 addr.sin_family = PJ_AF_INET;
412 addr.sin_addr.s_addr = pj_htonl(addr32);
413 addr.sin_port = pj_htons(port);
414
415 return pj_sock_bind(sockfd, &addr, sizeof(pj_sockaddr_in));
416}
417
418/*
419 * Close socket.
420 */
421PJ_DEF(pj_status_t) pj_sock_close(pj_sock_t sockfd)
422{
423 long err;
424
425 err = sys_close(sockfd);
426
427 if (err != 0)
428 return PJ_RETURN_OS_ERROR(-err);
429 else
430 return PJ_SUCCESS;
431}
432
433/*
434 * Get remote's name.
435 */
436PJ_DEF(pj_status_t) pj_sock_getpeername( pj_sock_t sockfd,
437 pj_sockaddr_t *addr,
438 int *namelen)
439{
440 mm_segment_t oldfs;
441 long err;
442
443 PJ_CHECK_STACK();
444
445 oldfs = get_fs();
446 set_fs(KERNEL_DS);
447
448 err = sys_getpeername( sockfd, addr, namelen);
449
450 set_fs(oldfs);
451
452 if (err)
453 return PJ_RETURN_OS_ERROR(-err);
454 else
455 return PJ_SUCCESS;
456}
457
458/*
459 * Get socket name.
460 */
461PJ_DEF(pj_status_t) pj_sock_getsockname( pj_sock_t sockfd,
462 pj_sockaddr_t *addr,
463 int *namelen)
464{
465 mm_segment_t oldfs;
466 int err;
467
468 PJ_CHECK_STACK();
469
470 oldfs = get_fs();
471 set_fs(KERNEL_DS);
472
473 err = sys_getsockname( sockfd, addr, namelen );
474
475 set_fs(oldfs);
476
477 if (err)
478 return PJ_RETURN_OS_ERROR(-err);
479 else
480 return PJ_SUCCESS;
481}
482
483/*
484 * Send data
485 */
486PJ_DEF(pj_status_t) pj_sock_send( pj_sock_t sockfd,
487 const void *buf,
488 pj_ssize_t *len,
489 unsigned flags)
490{
491 return pj_sock_sendto(sockfd, buf, len, flags, NULL, 0);
492}
493
494
495/*
496 * Send data.
497 */
498PJ_DEF(pj_status_t) pj_sock_sendto( pj_sock_t sockfd,
499 const void *buff,
500 pj_ssize_t *len,
501 unsigned flags,
502 const pj_sockaddr_t *addr,
503 int addr_len)
504{
505 long err;
506 mm_segment_t oldfs;
507
508 PJ_CHECK_STACK();
509
510 oldfs = get_fs();
511 set_fs(KERNEL_DS);
512
513 err = *len = sys_sendto( sockfd, (void*)buff, *len, flags,
514 (void*)addr, addr_len );
515
516 set_fs(oldfs);
517
518 if (err >= 0) {
519 return PJ_SUCCESS;
520 }
521 else {
522 return PJ_RETURN_OS_ERROR(-err);
523 }
524}
525
526/*
527 * Receive data.
528 */
529PJ_DEF(pj_status_t) pj_sock_recv( pj_sock_t sockfd,
530 void *buf,
531 pj_ssize_t *len,
532 unsigned flags)
533{
534 return pj_sock_recvfrom(sockfd, buf, len, flags, NULL, NULL);
535}
536
537/*
538 * Receive data.
539 */
540PJ_DEF(pj_status_t) pj_sock_recvfrom( pj_sock_t sockfd,
541 void *buff,
542 pj_ssize_t *size,
543 unsigned flags,
544 pj_sockaddr_t *from,
545 int *fromlen)
546{
547 mm_segment_t oldfs;
548 long err;
549
550 PJ_CHECK_STACK();
551
552 oldfs = get_fs();
553 set_fs(KERNEL_DS);
554
555 err = *size = sys_recvfrom( sockfd, buff, *size, flags, from, fromlen);
556
557 set_fs(oldfs);
558
559 if (err >= 0) {
560 return PJ_SUCCESS;
561 }
562 else {
563 return PJ_RETURN_OS_ERROR(-err);
564 }
565}
566
567/*
568 * Get socket option.
569 */
570PJ_DEF(pj_status_t) pj_sock_getsockopt( pj_sock_t sockfd,
571 int level,
572 int optname,
573 void *optval,
574 int *optlen)
575{
576 mm_segment_t oldfs;
577 long err;
578
579 PJ_CHECK_STACK();
580
581 oldfs = get_fs();
582 set_fs(KERNEL_DS);
583
584 err = sys_getsockopt( sockfd, level, optname, optval, optlen);
585
586 set_fs(oldfs);
587
588 if (err)
589 return PJ_RETURN_OS_ERROR(-err);
590 else
591 return PJ_SUCCESS;
592}
593
594/*
595 * Set socket option.
596 */
597PJ_DEF(pj_status_t) pj_sock_setsockopt( pj_sock_t sockfd,
598 int level,
599 int optname,
600 const void *optval,
601 int optlen)
602{
603 long err;
604 mm_segment_t oldfs;
605
606 PJ_CHECK_STACK();
607
608
609 oldfs = get_fs();
610 set_fs(KERNEL_DS);
611
612 err = sys_setsockopt( sockfd, level, optname, (void*)optval, optlen);
613
614 set_fs(oldfs);
615
616 if (err)
617 return PJ_RETURN_OS_ERROR(-err);
618 else
619 return PJ_SUCCESS;
620}
621
622/*
623 * Shutdown socket.
624 */
625#if PJ_HAS_TCP
626PJ_DEF(pj_status_t) pj_sock_shutdown( pj_sock_t sockfd,
627 int how)
628{
629 long err;
630
631 PJ_CHECK_STACK();
632
633 err = sys_shutdown(sockfd, how);
634
635 if (err)
636 return PJ_RETURN_OS_ERROR(-err);
637 else
638 return PJ_SUCCESS;
639}
640
641/*
642 * Start listening to incoming connections.
643 */
644PJ_DEF(pj_status_t) pj_sock_listen( pj_sock_t sockfd,
645 int backlog)
646{
647 long err;
648
649 PJ_CHECK_STACK();
650
651 err = sys_listen( sockfd, backlog );
652
653 if (err)
654 return PJ_RETURN_OS_ERROR(-err);
655 else
656 return PJ_SUCCESS;
657}
658
659/*
660 * Connect socket.
661 */
662PJ_DEF(pj_status_t) pj_sock_connect( pj_sock_t sockfd,
663 const pj_sockaddr_t *addr,
664 int namelen)
665{
666 long err;
667 mm_segment_t oldfs;
668
669 PJ_CHECK_STACK();
670
671 oldfs = get_fs();
672 set_fs(KERNEL_DS);
673
674 err = sys_connect( sockfd, (void*)addr, namelen );
675
676 set_fs(oldfs);
677
678 if (err)
679 return PJ_RETURN_OS_ERROR(-err);
680 else
681 return PJ_SUCCESS;
682}
683
684/*
685 * Accept incoming connections
686 */
687PJ_DEF(pj_status_t) pj_sock_accept( pj_sock_t sockfd,
688 pj_sock_t *newsockfd,
689 pj_sockaddr_t *addr,
690 int *addrlen)
691{
692 long err;
693
694 PJ_CHECK_STACK();
695
696 PJ_ASSERT_RETURN(newsockfd != NULL, PJ_EINVAL);
697
698 err = sys_accept( sockfd, addr, addrlen);
699
700 if (err < 0) {
701 *newsockfd = PJ_INVALID_SOCKET;
702 return PJ_RETURN_OS_ERROR(-err);
703 }
704 else {
705 *newsockfd = err;
706 return PJ_SUCCESS;
707 }
708}
709#endif /* PJ_HAS_TCP */
710
711
712
713/*
714 * Permission to steal inet_ntoa() and inet_aton() as long as this notice below
715 * is included:
716 */
717/*
718 * Copyright (c) 1983, 1993
719 * The Regents of the University of California. All rights reserved.
720 *
721 * Redistribution and use in source and binary forms, with or without
722 * modification, are permitted provided that the following conditions
723 * are met:
724 * 1. Redistributions of source code must retain the above copyright
725 * notice, this list of conditions and the following disclaimer.
726 * 2. Redistributions in binary form must reproduce the above copyright
727 * notice, this list of conditions and the following disclaimer in the
728 * documentation and/or other materials provided with the distribution.
729 * 3. All advertising materials mentioning features or use of this software
730 * must display the following acknowledgement:
731 * This product includes software developed by the University of
732 * California, Berkeley and its contributors.
733 * 4. Neither the name of the University nor the names of its contributors
734 * may be used to endorse or promote products derived from this software
735 * without specific prior written permission.
736 *
737 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
738 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
739 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
740 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
741 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
742 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
743 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
744 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
745 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
746 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
747 * SUCH DAMAGE.
748 */
749