2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file net.c networking-related helper functions */
27 extern void (*crypt_function_recv
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
);
28 extern void (*crypt_function_send
)(unsigned long len
, const unsigned char *indata
, unsigned char *outdata
);
32 * initialize a struct sockaddr_in
33 * @param addr A pointer to the struct to be initialized
34 * @param port The port number to use
35 * @param he The address to use
37 * If \a he is null (server mode), \a addr->sin_addr is initialized with \p INADDR_ANY.
38 * Otherwise, the address given by \a he is copied to addr.
40 void init_sockaddr(struct sockaddr_in
*addr
, int port
, const struct hostent
*he
)
43 addr
->sin_family
= AF_INET
;
44 /* short, network byte order */
45 addr
->sin_port
= htons(port
);
47 addr
->sin_addr
= *((struct in_addr
*)he
->h_addr
);
49 addr
->sin_addr
.s_addr
= INADDR_ANY
;
50 /* zero the rest of the struct */
51 memset(&addr
->sin_zero
, '\0', 8);
55 * send out a buffer, resend on short writes
56 * @param fd the file descriptor
57 * @param buf The buffer to be sent
58 * @len The length of \a buf
60 * Due to circumstances beyond your control, the kernel might not send all the
61 * data out in one chunk, and now, my friend, it's up to us to get the data out
62 * there (Beej's Guide to Network Programming).
64 static int sendall(int fd
, const char *buf
, size_t *len
)
66 int total
= 0; /* how many bytes we've sent */
67 int bytesleft
= *len
; /* how many we have left to send */
70 while (total
< *len
) {
71 n
= send(fd
, buf
+ total
, bytesleft
, 0);
77 PARA_DEBUG_LOG("short write (%zd byte(s) left)",
80 *len
= total
; /* return number actually sent here */
81 return n
== -1? -E_SEND
: 1; /* return 1 on success */
85 * encrypt and send buffer
86 * @param fd: the file descriptor
87 * @param buf the buffer to be encrypted and sent
88 * @param len the length of \a buf
90 * Check if encrytion is available. If yes, encrypt the given buffer. Send out
91 * the buffer, encrypted or not, and try to resend the remaing part in case of
94 * @return Positive on success, \p -E_SEND on errors.
96 int send_bin_buffer(int fd
, const char *buf
, size_t len
)
101 PARA_CRIT_LOG("%s", "len == 0\n");
102 if (crypt_function_send
) {
103 unsigned char *outbuf
= para_malloc(len
);
104 crypt_function_send(len
, (unsigned char *)buf
, outbuf
);
105 ret
= sendall(fd
, (char *)outbuf
, &len
);
108 ret
= sendall(fd
, buf
, &len
);
113 * encrypt and send null terminated buffer.
114 * @param fd the file descriptor
115 * @param buf the null-terminated buffer to be send
117 * This is equivalent to send_bin_buffer(fd, buf, strlen(buf)).
119 * @return Positive on success, \p -E_SEND on errors.
121 int send_buffer(int fd
, const char *buf
)
123 return send_bin_buffer(fd
, buf
, strlen(buf
));
128 * send and encrypt a buffer given by a format string
129 * @param fd the file descriptor
130 * @param fmt a format string
132 * @return Positive on success, \p -E_SEND on errors.
134 __printf_2_3
int send_va_buffer(int fd
, const char *fmt
, ...)
139 PARA_VSPRINTF(fmt
, msg
);
140 ret
= send_buffer(fd
, msg
);
146 * receive and decrypt.
148 * @param fd the file descriptor
149 * @param buf the buffer to write the decrypted data to
150 * @param size the size of @param buf
152 * Receive at most \a size bytes from filedescriptor fd. If encrytion is
153 * available, decrypt the received buffer.
155 * @return the number of bytes received on success. On receive errors, -E_RECV
156 * is returned. On crypt errors, the corresponding crypt error number is
160 __must_check
int recv_bin_buffer(int fd
, char *buf
, ssize_t size
)
164 if (crypt_function_recv
) {
165 unsigned char *tmp
= para_malloc(size
);
166 n
= recv(fd
, tmp
, size
, 0);
168 crypt_function_recv(n
, tmp
, (unsigned char *)buf
);
171 n
= recv(fd
, buf
, size
, 0);
178 * receive, decrypt and write terminating NULL byte
180 * @param fd the file descriptor
181 * @param buf the buffer to write the decrypted data to
182 * @param size the size of \a buf
184 * Read and decrypt at most size - 1 bytes from file descriptor \a fd and write
185 * a NULL byte at the end of the received data.
188 int recv_buffer(int fd
, char *buf
, ssize_t size
)
192 n
= recv_bin_buffer(fd
, buf
, size
- 1);
201 * wrapper around gethostbyname
203 * @param host hostname or IPv4 address
204 * \return The hostent structure or a NULL pointer if an error occurs
205 * \sa gethostbyname(2)
207 struct hostent
*get_host_info(char *host
)
209 PARA_INFO_LOG("getting host info of %s\n", host
);
210 /* FIXME: gethostbyname() is obsolete */
211 return gethostbyname(host
);
215 * a wrapper around socket(2)
217 * Create an IPv4 socket for sequenced, reliable, two-way, connection-based
220 * @return The socket fd on success, -E_SOCKET on errors.
227 if ((socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
233 * a wrapper around connect(2)
235 * @param fd the file descriptor
236 * @param their_addr the address to connect
238 * @return \p -E_CONNECT on errors, 1 on success
241 int para_connect(int fd
, struct sockaddr_in
*their_addr
)
245 if ((ret
= connect(fd
, (struct sockaddr
*)their_addr
,
246 sizeof(struct sockaddr
))) == -1)
252 * paraslash's wrapper around the accept system call
254 * @param fd the listening socket
255 * @param addr structure which is filled in with the address of the peer socket
256 * @param size should contain the size of the structure pointed to by \a addr
260 int para_accept(int fd
, void *addr
, socklen_t size
)
264 new_fd
= accept(fd
, (struct sockaddr
*) addr
, &size
);
265 return new_fd
== -1? -E_ACCEPT
: new_fd
;
268 static int setserversockopts(int socket_fd
)
272 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &yes
,
274 return -E_SETSOCKOPT
;
278 static int do_bind(int socket_fd
, struct sockaddr_in
*my_addr
)
280 if (bind(socket_fd
, (struct sockaddr
*)my_addr
,
281 sizeof(struct sockaddr
)) == -1)
287 * prepare a structure for \p AF_UNIX socket addresses
289 * \param u pointer to the struct to be prepared
290 * \param name the socket pathname
292 * This just copies \a name to the sun_path component of \a u.
294 * \return Positive on success, \p -E_NAME_TOO_LONG if \a name is longer
295 * than \p UNIX_PATH_MAX.
297 int init_unix_addr(struct sockaddr_un
*u
, const char *name
)
299 if (strlen(name
) >= UNIX_PATH_MAX
)
300 return -E_NAME_TOO_LONG
;
301 memset(u
->sun_path
, 0, UNIX_PATH_MAX
);
302 u
->sun_family
= PF_UNIX
;
303 strcpy(u
->sun_path
, name
);
308 * prepare, create, and bind and socket for local communication
310 * \param name the socket pathname
311 * \param unix_addr pointer to the \p AF_UNIX socket structure
312 * \param mode the desired mode of the socket
314 * This functions creates a local socket for sequenced, reliable,
315 * two-way, connection-based byte streams.
320 int create_pf_socket(const char *name
, struct sockaddr_un
*unix_addr
, int mode
)
324 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
328 ret
= init_unix_addr(unix_addr
, name
);
331 if (bind(fd
, (struct sockaddr
*) unix_addr
, UNIX_PATH_MAX
) < 0)
333 if (chmod(name
, mode
) < 0)
342 ssize_t
send_cred_buffer(int sock
, char *buf
)
344 return send_buffer(sock
, buf
);
346 int recv_cred_buffer(int fd
, char *buf
, size_t size
)
348 return recv_buffer(fd
, buf
, size
) > 0? 1 : -E_RECVMSG
;
350 #else /* HAVE_UCRED */
352 * send NULL terminated buffer and Unix credentials of the current process
354 * \param sock the socket file descriptor
355 * \param buf the buffer to be sent
357 * \return On success, this call returns the number of characters sent. On
358 * error, \p -E_SENDMSG ist returned.
359 * \sa okir's Black Hats Manual
362 ssize_t
send_cred_buffer(int sock
, char *buf
)
364 char control
[sizeof(struct cmsghdr
) + 10];
366 struct cmsghdr
*cmsg
;
367 static struct iovec iov
;
373 iov
.iov_len
= strlen(buf
);
377 /* compose the message */
378 memset(&msg
, 0, sizeof(msg
));
381 msg
.msg_control
= control
;
382 msg
.msg_controllen
= sizeof(control
);
383 /* attach the ucred struct */
384 cmsg
= CMSG_FIRSTHDR(&msg
);
385 cmsg
->cmsg_level
= SOL_SOCKET
;
386 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
387 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
388 *(struct ucred
*)CMSG_DATA(cmsg
) = c
;
389 msg
.msg_controllen
= cmsg
->cmsg_len
;
390 ret
= sendmsg(sock
, &msg
, 0);
396 static void dispose_fds(int *fds
, int num
)
400 for (i
= 0; i
< num
; i
++)
405 * receive a buffer and the Unix credentials of the sending process
407 * \param fd the socket file descriptor
408 * \param buf the buffer to store the message
409 * \param size the size of \a buffer
411 * \return negative on errors, the user id on success.
413 * \sa okir's Black Hats Manual
416 int recv_cred_buffer(int fd
, char *buf
, size_t size
)
420 struct cmsghdr
*cmsg
;
426 setsockopt(fd
, SOL_SOCKET
, SO_PASSCRED
, &yes
, sizeof(int));
427 memset(&msg
, 0, sizeof(msg
));
428 memset(buf
, 0, size
);
433 msg
.msg_control
= control
;
434 msg
.msg_controllen
= sizeof(control
);
435 if (recvmsg(fd
, &msg
, 0) < 0)
437 result
= -E_SCM_CREDENTIALS
;
438 cmsg
= CMSG_FIRSTHDR(&msg
);
440 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
441 == SCM_CREDENTIALS
) {
442 memcpy(&cred
, CMSG_DATA(cmsg
), sizeof(struct ucred
));
445 if (cmsg
->cmsg_level
== SOL_SOCKET
446 && cmsg
->cmsg_type
== SCM_RIGHTS
) {
447 dispose_fds((int *) CMSG_DATA(cmsg
),
448 (cmsg
->cmsg_len
- CMSG_LEN(0))
451 cmsg
= CMSG_NXTHDR(&msg
, cmsg
);
455 #endif /* HAVE_UCRED */
457 /** how many pending connections queue will hold */
461 * create a socket, bind it and listen
462 * \param port the tcp port to listen on
464 * \return The file descriptor of the created socket, negative
472 int init_tcp_socket(int port
)
475 struct sockaddr_in my_addr
;
477 if ((sockfd
= get_socket()) < 0)
479 ret
= setserversockopts(sockfd
);
482 init_sockaddr(&my_addr
, port
, NULL
);
483 ret
= do_bind(sockfd
, &my_addr
);
486 if (listen(sockfd
, BACKLOG
) == -1)
488 PARA_INFO_LOG("listening on port %d, fd %d\n", port
, sockfd
);
493 * receive a buffer and check for a pattern
495 * \param fd the file descriptor to receive from
496 * \param pattern the expected pattern
497 * \param bufsize the size of the internal buffer
499 * \return Positive if \a pattern was received, negative otherwise.
501 * This function creates a buffer of size \a bufsize and tries
502 * to receive at most \a bufsize bytes from file descriptor \a fd.
503 * If at least \p strlen(\a pattern) bytes were received, the beginning of
504 * the received buffer is compared with \a pattern, ignoring case.
508 int recv_pattern(int fd
, const char *pattern
, size_t bufsize
)
510 size_t len
= strlen(pattern
);
511 char *buf
= para_malloc(bufsize
+ 1);
512 int ret
= -E_RECV_PATTERN
, n
= recv_buffer(fd
, buf
, bufsize
);
516 if (strncasecmp(buf
, pattern
, len
))
521 PARA_NOTICE_LOG("did not receive pattern '%s'\n", pattern
);