+Sun Feb 17 15:52:30 EET 2002 Pekka Riikonen <priikone@silcnet.org>
+
+ * Added `user_count' to the SilcChannelEntry which now tells the
+ number of users on the channel. The user count is now saved
+ in normal server of global channels as well. Affected files
+ silcd/server.c, idlist.h, packet_receive.c and command.c.
+
+ * Splitted lib/silcutil/silcutil.[ch] into silcfileutil.[ch] to
+ include file utility functions.
+
+ * Fixed the lib/silcsftp/sftp_fs_memory.c to use silcutil routines
+ instead of calling directly OS routines.
+
+ * Fixed NICK change printing in Irssi SILC Client. Fixed
+ KICKED notify printing in Irssi SILC Client. Affected file
+ irssi/src/silc/core/client_ops.c.
+
+ * Fixed a NICK change bug in client library, to not recreate the
+ client_entry->channels hash table everytime nick is changed.
+ Affected file lib/silcclient/client.c.
+
Sun Feb 17 10:10:14 EET 2002 Pekka Riikonen <priikone@silcnet.org>
* ROBOdoc documented the lib/silcske/silcske.h, and improved
no founder on channel at all), the router will accept the server's
founder mode change, even though it perhaps should not do that.
- o Make the normal server save user counts with LIST command reply.
-
o The router should check for validity of received notify packets from
servers (after all buggy servers may send notify that is actually
something that should have not been sent).
o Rewrite the lib/silcsim/silcsim.h. The SilcSimContext should be
private and silc_sim_alloc should take necessary arguments.
- o lib/silcsftp/sftp_fs_memory.c use directly open(), close() etc.
- routines. Change to use silc_file_* routines.
-
o SILC RNG does not implement random seed files, and they should be
implemented.
client_entry = va_arg(va, SilcClientEntry);
client_entry2 = va_arg(va, SilcClientEntry);
- nicklist_rename_unique(SERVER(server),
- client_entry, client_entry->nickname,
- client_entry2, client_entry2->nickname);
-
memset(userhost, 0, sizeof(userhost));
snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
client_entry2->username, client_entry2->hostname);
+ nicklist_rename_unique(SERVER(server),
+ client_entry, client_entry->nickname,
+ client_entry2, client_entry2->nickname);
signal_emit("message nick", 4, server, client_entry2->nickname,
- client_entry2->nickname, userhost);
+ client_entry->nickname, userhost);
break;
case SILC_NOTIFY_TYPE_CMODE_CHANGE:
if (client_entry == conn->local_entry) {
printformat_module("fe-common/silc", server, channel->channel_name,
MSGLEVEL_CRAP, SILCTXT_CHANNEL_KICKED_YOU,
- client_entry2->nickname,
- channel->channel_name, tmp ? tmp : "");
+ channel->channel_name, client_entry2->nickname,
+ tmp ? tmp : "");
if (chanrec) {
chanrec->kicked = TRUE;
channel_destroy((CHANNEL_REC *)chanrec);
} else {
printformat_module("fe-common/silc", server, channel->channel_name,
MSGLEVEL_CRAP, SILCTXT_CHANNEL_KICKED,
- client_entry->nickname,
- client_entry2->nickname,
- channel->channel_name, tmp ? tmp : "");
+ client_entry->nickname, channel->channel_name,
+ client_entry2->nickname, tmp ? tmp : "");
if (chanrec) {
SILC_NICK_REC *nickrec = silc_nicklist_find(chanrec, client_entry);
nicklist_rename_unique(SERVER(server),
server->conn->local_entry, server->nick,
client, client->nickname);
-
signal_emit("message own_nick", 4, server, server->nick, old, "");
g_free(old);
break;
memset(usercount, 0, sizeof(usercount));
} else {
topic = entry->topic;
- users = silc_hash_table_count(entry->user_list);
+ users = entry->user_count;
SILC_PUT32_MSB(users, usercount);
}
chl->channel = channel;
silc_hash_table_add(channel->user_list, client, chl);
silc_hash_table_add(client->channels, channel, chl);
+ channel->user_count++;
/* Get users on the channel */
silc_server_get_users_on_channel(server, channel, &user_list, &mode_list,
cache->expire = time(NULL) + 60;
}
+ channel->user_count = usercount;
+
if (topic) {
silc_free(channel->topic);
channel->topic = strdup(topic);
/* All users on this channel */
SilcHashTable user_list;
+ uint32 user_count;
/* Pointer to the router */
SilcServerEntry router;
silc_hash_table_add(channel->user_list, client, chl);
silc_hash_table_add(client->channels, channel, chl);
silc_free(client_id);
+ channel->user_count++;
break;
port = server->sockets[server->sock]->port; /* Listenning port */
/* Check whether this connection is denied to connect to us. */
- deny = silc_server_config_find_denied(server, sock->ip, port);
+ deny = silc_server_config_find_denied(server, sock->ip);
if (!deny)
- deny = silc_server_config_find_denied(server, sock->hostname, port);
+ deny = silc_server_config_find_denied(server, sock->hostname);
if (deny) {
/* The connection is denied */
SILC_LOG_INFO(("Connection %s (%s) is denied",
/* Remove client from channel's client list */
silc_hash_table_del(channel->user_list, chl->client);
+ channel->user_count--;
/* If there is no global users on the channel anymore mark the channel
as local channel. Do not check if the removed client is local client. */
while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
silc_hash_table_del(chl2->client->channels, channel);
silc_hash_table_del(channel->user_list, chl2->client);
+ channel->user_count--;
silc_free(chl2);
}
silc_hash_table_list_reset(&htl2);
/* Remove client from channel's client list */
silc_hash_table_del(channel->user_list, chl->client);
+ channel->user_count--;
/* If there is no global users on the channel anymore mark the channel
as local channel. Do not check if the client is local client. */
while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
silc_hash_table_del(chl2->client->channels, channel);
silc_hash_table_del(channel->user_list, chl2->client);
+ channel->user_count--;
silc_free(chl2);
}
silc_hash_table_list_reset(&htl2);
chl->channel = channel;
silc_hash_table_add(channel->user_list, chl->client, chl);
silc_hash_table_add(client->channels, chl->channel, chl);
+ channel->user_count++;
}
}
}
/* Remove client from channel's client list */
silc_hash_table_del(channel->user_list, chl->client);
+ channel->user_count--;
/* If there is no global users on the channel anymore mark the channel
as local channel. Do not check if the removed client is local client. */
while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
silc_hash_table_del(chl2->client->channels, channel);
silc_hash_table_del(channel->user_list, chl2->client);
+ channel->user_count--;
silc_free(chl2);
}
silc_hash_table_list_reset(&htl2);
CONFIG_IS_DOUBLE(tmp->host);
tmp->host = (*(char *)val ? strdup((char *) val) : strdup("*"));
}
- else if (!strcmp(name, "port")) {
- int port = *(int *)val;
- if ((port <= 0) || (port > 65535)) {
- fprintf(stderr, "Invalid port number!\n");
- got_errno = SILC_CONFIG_ESILENT;
- goto got_err;
- }
- tmp->port = (uint16) port;
- }
else if (!strcmp(name, "reason")) {
CONFIG_IS_DOUBLE(tmp->reason);
tmp->reason = strdup((char *) val);
tmp->backup_replace_ip = (*(char *)val ? strdup((char *) val) :
strdup("*"));
}
+ else if (!strcmp(name, "backupport")) {
+ int port = *(int *)val;
+ if ((port <= 0) || (port > 65535)) {
+ fprintf(stderr, "Invalid port number!\n");
+ return SILC_CONFIG_ESILENT;
+ }
+ tmp->backup_replace_port = (uint16) port;
+ }
+ else if (!strcmp(name, "backuplocal")) {
+ tmp->backup_local = *(bool *)val;
+ }
else
return SILC_CONFIG_EINTERNAL;
static const SilcConfigTable table_deny[] = {
{ "host", SILC_CONFIG_ARG_STRE, fetch_deny, NULL },
- { "port", SILC_CONFIG_ARG_INT, fetch_deny, NULL },
{ "reason", SILC_CONFIG_ARG_STR, fetch_deny, NULL },
{ 0, 0, 0, 0 }
};
{ "initiator", SILC_CONFIG_ARG_TOGGLE, fetch_router, NULL },
{ "backuphost", SILC_CONFIG_ARG_STRE, fetch_router, NULL },
{ "backupport", SILC_CONFIG_ARG_INT, fetch_router, NULL },
- { "localbackup", SILC_CONFIG_ARG_TOGGLE, fetch_router, NULL },
+ { "backuplocal", SILC_CONFIG_ARG_TOGGLE, fetch_router, NULL },
{ 0, 0, 0, 0 }
};
return admin;
}
-/* Returns the denied connection configuration entry by host and port. */
+/* Returns the denied connection configuration entry by host. */
SilcServerConfigDeny *
-silc_server_config_find_denied(SilcServer server, char *host, uint16 port)
+silc_server_config_find_denied(SilcServer server, char *host)
{
SilcServerConfig config = server->config;
SilcServerConfigDeny *deny;
/* make sure we have a value for the matching parameters */
- if (!config || !port) {
- SILC_LOG_WARNING(("Bogus: config_find_denied(config=0x%08x, "
- "host=0x%08x \"%s\", port=%hu)",
- (uint32) config, (uint32) host, host, port));
- return NULL;
- }
- if (!host)
+ if (!config || !host)
return NULL;
for (deny = config->denied; deny; deny = deny->next) {
/* Holds all configured denied connections from config file */
typedef struct SilcServerConfigDenyStruct {
char *host;
- uint16 port;
char *reason;
struct SilcServerConfigDenyStruct *next;
} SilcServerConfigDeny;
silc_server_config_find_admin(SilcServer server, char *host, char *user,
char *nick);
SilcServerConfigDeny *
-silc_server_config_find_denied(SilcServer server, char *host, uint16 port);
+silc_server_config_find_denied(SilcServer server, char *host);
SilcServerConfigServer *
silc_server_config_find_server_conn(SilcServer server, char *host);
SilcServerConfigRouter *
# configuration possibilities and may not actually give any sensible
# configuration. For real life example see the examples/ directory.
#
+# Most of the settings in this file are optional. If some setting is
+# mandatory it is mentioned separately. If some setting is omitted it means
+# that its builtin default value will be used. Boolean values, that is
+# setting something on or off, is done by setting either "true" or "false"
+# value, respectively.
+#
+# The ServerInfo section is mandatory section. Other sections are optional.
+# However, if General section is defined it must be defined before the
+# ConnectionParams sections. On the other hand, the ConnectionParams section
+# must be defined before Client, ServerConnection or RouterConnection
+# sections. Other sections can be in free order.
+#
#
# Include global algorithms from the "silcalgs.conf" file. This file
#require_reverse_lookup = true;
# Maximum number of incoming connections allowed to this server.
- # If more attempt to connet they will be refused.
+ # If more attempt to connect they will be refused.
connections_max = 1000;
# Maximum number of incoming connections allowed per single host.
#conn_auth_timeout = 60;
# Channel key rekey interval (seconds). How often channel key is
- # regenerated. Note that channel key regenerated also always when
+ # regenerated. Note that channel key is regenerated also always when
# someone joins or leaves the channel.
#channel_rekey_secs = 3600;
# required.
#
# Next example connection will match to all incoming client connections,
-# and no authentication is required
+# and no authentication is required.
#
Client {
#Host = "10.1.*";
# option to the IP address of the router that the backup router will
# replace if it becomes unavailable. Set also the router's port to the
# "BackupPort" option. For normal connection leave both commented. If this
-# backup router is in our cell then set the "LocalBackup" option to true.
+# backup router is in our cell then set the "BackupLocal" option to true.
# If the backup router is in other cell then set it to false.
#
RouterConnection {
Initiator = true;
#BackupHost = "10.2.1.6";
#BackupPort = 706;
- #LocalBackup = true;
+ #BackupLocal = true;
};
#
#
# These connections are denied to connect to our server.
#
-# The "Reason" field is mandatory, while the "Host" and "Port" fields can be
-# omitted to match everything.
+# The "Reason" field is mandatory, while the "Host" field can be omitted to
+# match everything.
#
#Deny {
# Host = "10.2.1.99";
-# Port = 706;
# Reason = "Go away spammer";
#};
#Deny {
#include "silcbufutil.h"
#include "silcbuffmt.h"
#include "silcnet.h"
+#include "silcfileutil.h"
#include "silcutil.h"
#include "silcconfig.h"
#include "silcprotocol.h"
conn->local_entry->username = strdup(client->username);
if (!conn->local_entry->hostname)
conn->local_entry->hostname = strdup(client->hostname);
- conn->local_entry->server = strdup(conn->remote_host);
+ if (!conn->local_entry->server)
+ conn->local_entry->server = strdup(conn->remote_host);
conn->local_entry->id = conn->local_id;
conn->local_entry->valid = TRUE;
- conn->local_entry->channels = silc_hash_table_alloc(1, silc_hash_ptr,
- NULL, NULL,
- NULL, NULL, NULL, TRUE);
+ if (!conn->local_entry->channels)
+ conn->local_entry->channels = silc_hash_table_alloc(1, silc_hash_ptr,
+ NULL, NULL,
+ NULL, NULL, NULL,
+ TRUE);
/* Put it to the ID cache */
silc_idcache_add(conn->client_cache, strdup(conn->nickname), conn->local_id,
silc_hash_table_del(chu->channel->user_list, chu->client);
silc_free(chu);
}
+
silc_hash_table_list_reset(&htl);
}
{
SilcChannelUser chu = (SilcChannelUser)context;
+ SILC_LOG_DEBUG(("Start"));
+
/* Remove the context from the client's channel hash table as that
table and channel's user_list hash table share this same context. */
silc_hash_table_del(chu->client->channels, chu->channel);
{
bool ret = silc_idcache_del_by_context(conn->channel_cache, channel);
+ SILC_LOG_DEBUG(("Start"));
+
/* Free all client entrys from the users list. The silc_hash_table_free
will free all the entries so they are not freed at the foreach
callback. */
if (fs->handles[handle->handle] == handle) {
fs->handles[handle->handle] = NULL;
if (handle->fd != -1)
- close(handle->fd);
+ silc_file_close(handle->fd);
silc_free(handle);
return TRUE;
}
flags |= O_APPEND;
/* Attempt to open the file for real. */
- fd = open(entry->data + 7, flags,
- (attrs->flags & SILC_SFTP_ATTR_PERMISSIONS ?
- attrs->permissions : 0600));
+ fd = silc_file_open_mode(entry->data + 7, flags,
+ (attrs->flags & SILC_SFTP_ATTR_PERMISSIONS ?
+ attrs->permissions : 0600));
if (fd == -1) {
(*callback)(sftp, silc_sftp_map_errno(errno), NULL, callback_context);
return;
int ret;
if (h->fd != -1) {
- ret = close(h->fd);
+ ret = silc_file_close(h->fd);
if (ret == -1) {
(*callback)(sftp, silc_sftp_map_errno(errno), NULL, NULL,
callback_context);
lseek(h->fd, (off_t)offset, SEEK_SET);
/* Attempt to read */
- ret = read(h->fd, data, len);
+ ret = silc_file_read(h->fd, data, len);
if (ret <= 0) {
if (!ret)
(*callback)(sftp, SILC_SFTP_STATUS_EOF, NULL, 0, callback_context);
lseek(h->fd, (off_t)offset, SEEK_SET);
/* Attempt to write */
- ret = write(h->fd, data, data_len);
+ ret = silc_file_write(h->fd, data, data_len);
if (ret <= 0) {
(*callback)(sftp, silc_sftp_map_errno(errno), NULL, NULL,
callback_context);
SilcSFTPAttributes attrs;
int i;
char long_name[256];
- unsigned long filesize = 0;
+ uint64 filesize = 0;
char *date;
struct stat stats;
*strrchr(date, ':') = '\0';
if (!entry->directory)
-#ifndef SILC_WIN32
- if (!lstat(entry->data + 7, &stats))
-#else
- if (!stat(entry->data + 7, &stats))
-#endif
- filesize = stats.st_size;
+ filesize = silc_file_size(entry->data + 7);
/* Long name format is:
drwx------ 1 324210 Apr 8 08:40 mail/
1234567890 123 12345678 123456789012 */
snprintf(long_name, sizeof(long_name) - 1,
- "%c%c%c%c------ %3d %8lu %12s %s%s",
+ "%c%c%c%c------ %3d %8llu %12s %s%s",
(entry->directory ? 'd' : '-'),
((entry->perm & SILC_SFTP_FS_PERM_READ) ? 'r' : '-'),
((entry->perm & SILC_SFTP_FS_PERM_WRITE) ? 'w' : '-'),
struct stat stats;
if (!path || !strlen(path))
- path = (const char *)strdup("/");
+ path = (const char *)DIR_SEPARATOR;
/* Find such directory */
entry = mem_find_entry_path(fs->root, path);
struct stat stats;
if (!path || !strlen(path))
- path = (const char *)strdup("/");
+ path = (const char *)DIR_SEPARATOR;
/* Find such directory */
entry = mem_find_entry_path(fs->root, path);
SilcSFTPName name;
if (!path || !strlen(path))
- path = (const char *)strdup("/");
+ path = (const char *)DIR_SEPARATOR;
realpath = mem_expand_path(fs->root, path);
if (!realpath) {
@LINK=silcschedule.html:SILC Schedule API
@LINK=silcsockconn.html:SILC Socket Connection API
@LINK=silcprotocol.html:SILC Protocol API
+@LINK=silcfileutil.html:SILC File Util API
@LINK=silcutil.html:SILC Util API
@LINK=silczip.html:SILC Zip API
@LINK=silclist.html:SILC List API
silcmemory.c \
silcnet.c \
silcschedule.c \
+ silcfileutil.c \
silcutil.c \
silchashtable.c \
silcsockconn.c \
silcthread.h \
silclist.h \
silcdlist.h \
+ silcfileutil.h \
silcutil.h
endif
--- /dev/null
+/*
+
+ silcfileutil.c
+
+ Author: Pekka Riikonen <priikone@silcnet.org>
+
+ Copyright (C) 1997 - 2002 Pekka Riikonen
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+*/
+/* $Id$ */
+
+#include "silcincludes.h"
+
+/* Opens a file indicated by the filename `filename' with flags indicated
+ by the `flags'. */
+
+int silc_file_open(const char *filename, int flags)
+{
+ int fd = open(filename, flags, 0600);
+ return fd;
+}
+
+/* Opens a file indicated by the filename `filename' with flags indicated
+ by the `flags', and with the specified `mode'. */
+
+int silc_file_open_mode(const char *filename, int flags, int mode)
+{
+ int fd = open(filename, flags, mode);
+ return fd;
+}
+
+/* Reads data from file descriptor `fd' to `buf'. */
+
+int silc_file_read(int fd, unsigned char *buf, uint32 buf_len)
+{
+ return read(fd, (void *)buf, buf_len);
+}
+
+/* Writes `buffer' of length of `len' to file descriptor `fd. */
+
+int silc_file_write(int fd, const char *buffer, uint32 len)
+{
+ return write(fd, (const void *)buffer, len);
+}
+
+/* Closes file descriptor */
+
+int silc_file_close(int fd)
+{
+ return close(fd);
+}
+
+/* Writes a buffer to the file. */
+
+int silc_file_writefile(const char *filename, const char *buffer, uint32 len)
+{
+ int fd;
+
+ if ((fd = creat(filename, 0644)) == -1) {
+ SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
+ strerror(errno)));
+ return -1;
+ }
+
+ if ((silc_file_write(fd, buffer, len)) == -1) {
+ SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
+ silc_file_close(fd);
+ return -1;
+ }
+
+ silc_file_close(fd);
+
+ return 0;
+}
+
+/* Writes a buffer to the file. If the file is created specific mode is
+ set to the file. */
+
+int silc_file_writefile_mode(const char *filename, const char *buffer,
+ uint32 len, int mode)
+{
+ int fd;
+
+ if ((fd = creat(filename, mode)) == -1) {
+ SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
+ strerror(errno)));
+ return -1;
+ }
+
+ if ((silc_file_write(fd, buffer, len)) == -1) {
+ SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
+ silc_file_close(fd);
+ return -1;
+ }
+
+ silc_file_close(fd);
+
+ return 0;
+}
+
+/* Reads a file to a buffer. The allocated buffer is returned. Length of
+ the file read is returned to the return_len argument. */
+
+char *silc_file_readfile(const char *filename, uint32 *return_len)
+{
+ int fd;
+ char *buffer;
+ int filelen;
+
+ fd = silc_file_open(filename, O_RDONLY);
+ if (fd < 0) {
+ if (errno == ENOENT)
+ return NULL;
+ SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
+ return NULL;
+ }
+
+ filelen = lseek(fd, (off_t)0L, SEEK_END);
+ if (filelen < 0) {
+ silc_file_close(fd);
+ return NULL;
+ }
+ if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
+ silc_file_close(fd);
+ return NULL;
+ }
+
+ if (filelen < 0) {
+ SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
+ silc_file_close(fd);
+ return NULL;
+ }
+
+ buffer = silc_calloc(filelen + 1, sizeof(char));
+
+ if ((silc_file_read(fd, buffer, filelen)) == -1) {
+ memset(buffer, 0, sizeof(buffer));
+ silc_file_close(fd);
+ SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
+ strerror(errno)));
+ return NULL;
+ }
+
+ silc_file_close(fd);
+ buffer[filelen] = EOF;
+
+ if (return_len)
+ *return_len = filelen;
+
+ return buffer;
+}
+
+/* Returns files size. Returns 0 on error. */
+
+uint64 silc_file_size(const char *filename)
+{
+ int ret;
+ struct stat stats;
+
+#ifndef SILC_WIN32
+ ret = lstat(filename, &stats);
+#else
+ ret = stat(filename, &stats);
+#endif
+ if (ret < 0)
+ return 0;
+
+ return (uint64)stats.st_size;
+}
--- /dev/null
+/*
+
+ silcfileutil.h
+
+ Author: Pekka Riikonen <priikone@silcnet.org>
+
+ Copyright (C) 1997 - 2002 Pekka Riikonen
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+*/
+
+/****h* silcutil/SilcFileUtilAPI
+ *
+ * DESCRIPTION
+ *
+ *
+ ***/
+
+#ifndef SILCFILEUTIL_H
+#define SILCFILEUTIL_H
+
+/* Prototypes */
+
+int silc_file_open(const char *filename, int flags);
+int silc_file_open_mode(const char *filename, int flags, int mode);
+int silc_file_read(int fd, unsigned char *buf, uint32 buf_len);
+int silc_file_write(int fd, const char *buffer, uint32 len);
+int silc_file_close(int fd);
+char *silc_file_readfile(const char *filename, uint32 *return_len);
+int silc_file_writefile(const char *filename, const char *buffer, uint32 len);
+int silc_file_writefile_mode(const char *filename, const char *buffer,
+ uint32 len, int mode);
+uint64 silc_file_size(const char *filename);
+
+#endif /* SILCFILEUTIL_H */
#include "silcincludes.h"
-/* Opens a file indicated by the filename `filename' with flags indicated
- by the `flags'. */
-
-int silc_file_open(const char *filename, int flags)
-{
- int fd;
-
- fd = open(filename, flags, 0600);
-
- return fd;
-}
-
-/* Reads data from file descriptor `fd' to `buf'. */
-
-int silc_file_read(int fd, unsigned char *buf, uint32 buf_len)
-{
- return read(fd, (void *)buf, buf_len);
-}
-
-/* Writes `buffer' of length of `len' to file descriptor `fd. */
-
-int silc_file_write(int fd, const char *buffer, uint32 len)
-{
- return write(fd, (const void *)buffer, len);
-}
-
-/* Closes file descriptor */
-
-int silc_file_close(int fd)
-{
- return close(fd);
-}
-
-/* Writes a buffer to the file. */
-
-int silc_file_writefile(const char *filename, const char *buffer, uint32 len)
-{
- int fd;
-
- if ((fd = creat(filename, 0644)) == -1) {
- SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
- strerror(errno)));
- return -1;
- }
-
- if ((write(fd, buffer, len)) == -1) {
- SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
- close(fd);
- return -1;
- }
-
- close(fd);
-
- return 0;
-}
-
-/* Writes a buffer to the file. If the file is created specific mode is
- set to the file. */
-
-int silc_file_writefile_mode(const char *filename, const char *buffer,
- uint32 len, int mode)
-{
- int fd;
-
- if ((fd = creat(filename, mode)) == -1) {
- SILC_LOG_ERROR(("Cannot open file %s for writing: %s", filename,
- strerror(errno)));
- return -1;
- }
-
- if ((write(fd, buffer, len)) == -1) {
- SILC_LOG_ERROR(("Cannot write to file %s: %s", filename, strerror(errno)));
- close(fd);
- return -1;
- }
-
- close(fd);
-
- return 0;
-}
-
-/* Reads a file to a buffer. The allocated buffer is returned. Length of
- the file read is returned to the return_len argument. */
-
-char *silc_file_readfile(const char *filename, uint32 *return_len)
-{
- int fd;
- char *buffer;
- int filelen;
-
- fd = silc_file_open(filename, O_RDONLY);
- if (fd < 0) {
- if (errno == ENOENT)
- return NULL;
- SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
- return NULL;
- }
-
- filelen = lseek(fd, (off_t)0L, SEEK_END);
- if (filelen < 0) {
- close(fd);
- return NULL;
- }
- if (lseek(fd, (off_t)0L, SEEK_SET) < 0) {
- close(fd);
- return NULL;
- }
-
- if (filelen < 0) {
- SILC_LOG_ERROR(("Cannot open file %s: %s", filename, strerror(errno)));
- close(fd);
- return NULL;
- }
-
- buffer = silc_calloc(filelen + 1, sizeof(char));
-
- if ((read(fd, buffer, filelen)) == -1) {
- memset(buffer, 0, sizeof(buffer));
- close(fd);
- SILC_LOG_ERROR(("Cannot read from file %s: %s", filename,
- strerror(errno)));
- return NULL;
- }
-
- close(fd);
- buffer[filelen] = EOF;
-
- if (return_len)
- *return_len = filelen;
-
- return buffer;
-}
-
-/* Returns files size. Returns 0 on error. */
-
-uint64 silc_file_size(const char *filename)
-{
- int ret;
- struct stat stats;
-
-#ifndef SILC_WIN32
- ret = lstat(filename, &stats);
-#else
- ret = stat(filename, &stats);
-#endif
- if (ret < 0)
- return 0;
-
- return (uint64)stats.st_size;
-}
-
/* Gets line from a buffer. Stops reading when a newline or EOF occurs.
This doesn't remove the newline sign from the destination buffer. The
argument begin is returned and should be passed again for the function. */
#define SILCUTIL_H
/* Prototypes */
-int silc_file_open(const char *filename, int flags);
-int silc_file_read(int fd, unsigned char *buf, uint32 buf_len);
-int silc_file_write(int fd, const char *buffer, uint32 len);
-int silc_file_close(int fd);
-char *silc_file_readfile(const char *filename, uint32 *return_len);
-int silc_file_writefile(const char *filename, const char *buffer, uint32 len);
-int silc_file_writefile_mode(const char *filename, const char *buffer,
- uint32 len, int mode);
-uint64 silc_file_size(const char *filename);
int silc_gets(char *dest, int destlen, const char *src, int srclen, int begin);
int silc_check_line(char *buf);
char *silc_get_time();