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 */
32 static struct crypt_data
*crypt_data_array
;
33 static unsigned cda_size
= 0;
35 void enable_crypt(int fd
, crypt_function
*recv
, crypt_function
*send
,
38 if (fd
+ 1 > cda_size
) {
39 crypt_data_array
= para_realloc(crypt_data_array
,
40 (fd
+ 1) * sizeof(struct crypt_data
));
41 memset(crypt_data_array
+ cda_size
* sizeof(struct crypt_data
), 0,
42 (fd
+ 1 - cda_size
) * sizeof(struct crypt_data
));
45 crypt_data_array
[fd
].recv
= recv
;
46 crypt_data_array
[fd
].send
= send
;
47 crypt_data_array
[fd
].private_data
= private_data
;
48 PARA_INFO_LOG("rc4 encryption activated for fd %d\n", fd
);
51 void disable_crypt(int fd
)
53 if (cda_size
< fd
+ 1)
55 crypt_data_array
[fd
].recv
= NULL
;
56 crypt_data_array
[fd
].send
= NULL
;
57 crypt_data_array
[fd
].private_data
= NULL
;
62 * initialize a struct sockaddr_in
63 * @param addr A pointer to the struct to be initialized
64 * @param port The port number to use
65 * @param he The address to use
67 * If \a he is null (server mode), \a addr->sin_addr is initialized with \p INADDR_ANY.
68 * Otherwise, the address given by \a he is copied to addr.
70 void init_sockaddr(struct sockaddr_in
*addr
, int port
, const struct hostent
*he
)
73 addr
->sin_family
= AF_INET
;
74 /* short, network byte order */
75 addr
->sin_port
= htons(port
);
77 addr
->sin_addr
= *((struct in_addr
*)he
->h_addr
);
79 addr
->sin_addr
.s_addr
= INADDR_ANY
;
80 /* zero the rest of the struct */
81 memset(&addr
->sin_zero
, '\0', 8);
85 * send out a buffer, resend on short writes
86 * @param fd the file descriptor
87 * @param buf The buffer to be sent
88 * @len The length of \a buf
90 * Due to circumstances beyond your control, the kernel might not send all the
91 * data out in one chunk, and now, my friend, it's up to us to get the data out
92 * there (Beej's Guide to Network Programming).
94 static int sendall(int fd
, const char *buf
, size_t *len
)
96 int total
= 0; /* how many bytes we've sent */
97 int bytesleft
= *len
; /* how many we have left to send */
100 while (total
< *len
) {
101 n
= send(fd
, buf
+ total
, bytesleft
, 0);
107 PARA_DEBUG_LOG("short write (%zd byte(s) left)",
110 *len
= total
; /* return number actually sent here */
111 return n
== -1? -E_SEND
: 1; /* return 1 on success */
115 * encrypt and send buffer
116 * @param fd: the file descriptor
117 * @param buf the buffer to be encrypted and sent
118 * @param len the length of \a buf
120 * Check if encrytion is available. If yes, encrypt the given buffer. Send out
121 * the buffer, encrypted or not, and try to resend the remaing part in case of
124 * @return Positive on success, \p -E_SEND on errors.
126 int send_bin_buffer(int fd
, const char *buf
, size_t len
)
129 crypt_function
*cf
= NULL
;
132 PARA_CRIT_LOG("%s", "len == 0\n");
133 if (fd
+ 1 <= cda_size
)
134 cf
= crypt_data_array
[fd
].send
;
136 void *private = crypt_data_array
[fd
].private_data
;
137 unsigned char *outbuf
= para_malloc(len
);
138 (*cf
)(len
, (unsigned char *)buf
, outbuf
, private);
139 ret
= sendall(fd
, (char *)outbuf
, &len
);
142 ret
= sendall(fd
, buf
, &len
);
147 * encrypt and send null terminated buffer.
148 * @param fd the file descriptor
149 * @param buf the null-terminated buffer to be send
151 * This is equivalent to send_bin_buffer(fd, buf, strlen(buf)).
153 * @return Positive on success, \p -E_SEND on errors.
155 int send_buffer(int fd
, const char *buf
)
157 return send_bin_buffer(fd
, buf
, strlen(buf
));
162 * send and encrypt a buffer given by a format string
163 * @param fd the file descriptor
164 * @param fmt a format string
166 * @return Positive on success, \p -E_SEND on errors.
168 __printf_2_3
int send_va_buffer(int fd
, const char *fmt
, ...)
173 PARA_VSPRINTF(fmt
, msg
);
174 ret
= send_buffer(fd
, msg
);
180 * receive and decrypt.
182 * @param fd the file descriptor
183 * @param buf the buffer to write the decrypted data to
184 * @param size the size of @param buf
186 * Receive at most \a size bytes from filedescriptor fd. If encrytion is
187 * available, decrypt the received buffer.
189 * @return the number of bytes received on success. On receive errors, -E_RECV
190 * is returned. On crypt errors, the corresponding crypt error number is
194 __must_check
int recv_bin_buffer(int fd
, char *buf
, ssize_t size
)
197 crypt_function
*cf
= NULL
;
199 if (fd
+ 1 <= cda_size
)
200 cf
= crypt_data_array
[fd
].recv
;
202 unsigned char *tmp
= para_malloc(size
);
203 void *private = crypt_data_array
[fd
].private_data
;
204 n
= recv(fd
, tmp
, size
, 0);
206 (*cf
)(n
, tmp
, (unsigned char *)buf
, private);
209 n
= recv(fd
, buf
, size
, 0);
216 * receive, decrypt and write terminating NULL byte
218 * @param fd the file descriptor
219 * @param buf the buffer to write the decrypted data to
220 * @param size the size of \a buf
222 * Read and decrypt at most size - 1 bytes from file descriptor \a fd and write
223 * a NULL byte at the end of the received data.
226 int recv_buffer(int fd
, char *buf
, ssize_t size
)
230 n
= recv_bin_buffer(fd
, buf
, size
- 1);
239 * wrapper around gethostbyname
241 * \param host hostname or IPv4 address
242 * \param ret the hostent structure is returned here
244 * \return positive on success, negative on errors. On success, \a ret
245 * contains the return value of the underlying gethostbyname() call.
247 * \sa gethostbyname(2)
249 int get_host_info(char *host
, struct hostent
**ret
)
251 PARA_INFO_LOG("getting host info of %s\n", host
);
252 /* FIXME: gethostbyname() is obsolete */
253 *ret
= gethostbyname(host
);
254 return *ret
? 1 : -E_HOST_INFO
;
258 * a wrapper around socket(2)
260 * Create an IPv4 socket for sequenced, reliable, two-way, connection-based
263 * @return The socket fd on success, -E_SOCKET on errors.
270 if ((socket_fd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1)
276 * a wrapper around connect(2)
278 * @param fd the file descriptor
279 * @param their_addr the address to connect
281 * @return \p -E_CONNECT on errors, 1 on success
284 int para_connect(int fd
, struct sockaddr_in
*their_addr
)
288 if ((ret
= connect(fd
, (struct sockaddr
*)their_addr
,
289 sizeof(struct sockaddr
))) == -1)
295 * paraslash's wrapper around the accept system call
297 * @param fd the listening socket
298 * @param addr structure which is filled in with the address of the peer socket
299 * @param size should contain the size of the structure pointed to by \a addr
303 int para_accept(int fd
, void *addr
, socklen_t size
)
307 new_fd
= accept(fd
, (struct sockaddr
*) addr
, &size
);
308 return new_fd
== -1? -E_ACCEPT
: new_fd
;
311 static int setserversockopts(int socket_fd
)
315 if (setsockopt(socket_fd
, SOL_SOCKET
, SO_REUSEADDR
, &yes
,
317 return -E_SETSOCKOPT
;
322 * prepare a structure for \p AF_UNIX socket addresses
324 * \param u pointer to the struct to be prepared
325 * \param name the socket pathname
327 * This just copies \a name to the sun_path component of \a u.
329 * \return Positive on success, \p -E_NAME_TOO_LONG if \a name is longer
330 * than \p UNIX_PATH_MAX.
332 int init_unix_addr(struct sockaddr_un
*u
, const char *name
)
334 if (strlen(name
) >= UNIX_PATH_MAX
)
335 return -E_NAME_TOO_LONG
;
336 memset(u
->sun_path
, 0, UNIX_PATH_MAX
);
337 u
->sun_family
= PF_UNIX
;
338 strcpy(u
->sun_path
, name
);
343 * prepare, create, and bind and socket for local communication
345 * \param name the socket pathname
346 * \param unix_addr pointer to the \p AF_UNIX socket structure
347 * \param mode the desired mode of the socket
349 * This functions creates a local socket for sequenced, reliable,
350 * two-way, connection-based byte streams.
355 int create_pf_socket(const char *name
, struct sockaddr_un
*unix_addr
, int mode
)
359 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
363 ret
= init_unix_addr(unix_addr
, name
);
366 if (bind(fd
, (struct sockaddr
*) unix_addr
, UNIX_PATH_MAX
) < 0)
368 if (chmod(name
, mode
) < 0)
377 ssize_t
send_cred_buffer(int sock
, char *buf
)
379 return send_buffer(sock
, buf
);
381 int recv_cred_buffer(int fd
, char *buf
, size_t size
)
383 return recv_buffer(fd
, buf
, size
) > 0? 1 : -E_RECVMSG
;
385 #else /* HAVE_UCRED */
387 * send NULL terminated buffer and Unix credentials of the current process
389 * \param sock the socket file descriptor
390 * \param buf the buffer to be sent
392 * \return On success, this call returns the number of characters sent. On
393 * error, \p -E_SENDMSG ist returned.
394 * \sa okir's Black Hats Manual
397 ssize_t
send_cred_buffer(int sock
, char *buf
)
399 char control
[sizeof(struct cmsghdr
) + 10];
401 struct cmsghdr
*cmsg
;
402 static struct iovec iov
;
408 iov
.iov_len
= strlen(buf
);
412 /* compose the message */
413 memset(&msg
, 0, sizeof(msg
));
416 msg
.msg_control
= control
;
417 msg
.msg_controllen
= sizeof(control
);
418 /* attach the ucred struct */
419 cmsg
= CMSG_FIRSTHDR(&msg
);
420 cmsg
->cmsg_level
= SOL_SOCKET
;
421 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
422 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
423 *(struct ucred
*)CMSG_DATA(cmsg
) = c
;
424 msg
.msg_controllen
= cmsg
->cmsg_len
;
425 ret
= sendmsg(sock
, &msg
, 0);
431 static void dispose_fds(int *fds
, int num
)
435 for (i
= 0; i
< num
; i
++)
440 * receive a buffer and the Unix credentials of the sending process
442 * \param fd the socket file descriptor
443 * \param buf the buffer to store the message
444 * \param size the size of \a buffer
446 * \return negative on errors, the user id on success.
448 * \sa okir's Black Hats Manual
451 int recv_cred_buffer(int fd
, char *buf
, size_t size
)
455 struct cmsghdr
*cmsg
;
461 setsockopt(fd
, SOL_SOCKET
, SO_PASSCRED
, &yes
, sizeof(int));
462 memset(&msg
, 0, sizeof(msg
));
463 memset(buf
, 0, size
);
468 msg
.msg_control
= control
;
469 msg
.msg_controllen
= sizeof(control
);
470 if (recvmsg(fd
, &msg
, 0) < 0)
472 result
= -E_SCM_CREDENTIALS
;
473 cmsg
= CMSG_FIRSTHDR(&msg
);
475 if (cmsg
->cmsg_level
== SOL_SOCKET
&& cmsg
->cmsg_type
476 == SCM_CREDENTIALS
) {
477 memcpy(&cred
, CMSG_DATA(cmsg
), sizeof(struct ucred
));
480 if (cmsg
->cmsg_level
== SOL_SOCKET
481 && cmsg
->cmsg_type
== SCM_RIGHTS
) {
482 dispose_fds((int *) CMSG_DATA(cmsg
),
483 (cmsg
->cmsg_len
- CMSG_LEN(0))
486 cmsg
= CMSG_NXTHDR(&msg
, cmsg
);
490 #endif /* HAVE_UCRED */
492 /** how many pending connections queue will hold */
496 * create a socket, bind it and listen
497 * \param port the tcp port to listen on
499 * \return The file descriptor of the created socket, negative
507 int init_tcp_socket(int port
)
509 struct sockaddr_in my_addr
;
510 int fd
, ret
= get_socket();
515 ret
= setserversockopts(fd
);
518 init_sockaddr(&my_addr
, port
, NULL
);
520 if (bind(fd
, (struct sockaddr
*)&my_addr
,
521 sizeof(struct sockaddr
)) == -1) {
522 PARA_CRIT_LOG("bind error: %s\n", strerror(errno
));
526 if (listen(fd
, BACKLOG
) == -1)
528 PARA_INFO_LOG("listening on port %d, fd %d\n", port
, fd
);
536 * receive a buffer and check for a pattern
538 * \param fd the file descriptor to receive from
539 * \param pattern the expected pattern
540 * \param bufsize the size of the internal buffer
542 * \return Positive if \a pattern was received, negative otherwise.
544 * This function creates a buffer of size \a bufsize and tries
545 * to receive at most \a bufsize bytes from file descriptor \a fd.
546 * If at least \p strlen(\a pattern) bytes were received, the beginning of
547 * the received buffer is compared with \a pattern, ignoring case.
551 int recv_pattern(int fd
, const char *pattern
, size_t bufsize
)
553 size_t len
= strlen(pattern
);
554 char *buf
= para_malloc(bufsize
+ 1);
555 int ret
= -E_RECV_PATTERN
, n
= recv_buffer(fd
, buf
, bufsize
);
559 if (strncasecmp(buf
, pattern
, len
))
564 PARA_NOTICE_LOG("did not receive pattern '%s'\n", pattern
);