libs3 S3StatusConnectionFailed - c

I'm working on doing file uploads using the libs3 library found here: http://libs3.ischo.com/dox/index.html
I'm getting back an error of S3StatusConnectionFailed though. Can someone point me to how this situation could arise? This is my function for uploading files into S3.
int putFileIntoS3 (char *fileName, char *s3ObjName) {
S3Status status;
char *key;
struct stat statBuf;
uint64_t fileSize;
FILE *fd;
char *accessKeyId;
char *secretAccessKey;
put_object_callback_data data;
accessKeyId = S3_ACCESS_KEY;
secretAccessKey = S3_SECRET_ACCESS_KEY;
key = (char*) strchr(s3ObjName, '/');
if (key == NULL) {
printf("S3 Key not defined!!!!");
return (-1);
}
*key = '\0';
key++;
if (stat(fileName, &statBuf) == -1) {
printf("Unknown input file");
return(-1);
}
fileSize = statBuf.st_size;
fd = fopen(fileName, "r");
if (fd == NULL) {
printf("Unable to open input file");
return(-1);
}
data.infile = fd;
S3BucketContext bucketContext =
{s3ObjName, S3ProtocolHTTP, S3UriStylePath, accessKeyId, secretAccessKey}
S3PutObjectHandler putObjectHandler = {
{ &responsePropertiesCallback, &responseCompleteCallback },
&putObjectDataCallback
};
if ((status = S3_initialize("s3", S3_INIT_ALL)) != S3StatusOK) {
printf("Failed to initialize libs3: %s\n",S3_get_status_name(status));
return(-1);
}
S3_put_object(&bucketContext, key, fileSize, NULL, 0, &putObjectHandler, &data);
if (statusG != S3StatusOK) {
printf("Put failed: %i\n", statusG);
S3_deinitialize();
return(-1);
}
S3_deinitialize();
fclose(fd);
return(0);
}
I get back "Put failed: 46", which I'm pretty sure means that it's an S3StatusConnectionFailed error.
Any help would be great, or even pointers to a boto-like library that I can use instead of the drudgery that is doing this in C++.
Thanks!

Ok, i tried putting non-null value but i was getting same error. i found out the reason for this : you must set the contentLength of "data" (of type put_object_callback_data) as below
fileSize = statBuf.st_size;
//viren+
data.contentLength = fileSize;
//viren-
//..
data.infile = fd;
Oh, and also make sure you have correct permissions set on your bucket.

use NULL for first param in S3_initialize. S3StatusConnectionFailed is indicative that "s3" url maybe wrong. i assume you are providing correct S3_ACCESS_KEY and S3_SECRET_ACCESS_KEY.

Related

PE FILE reading in c

I need to check if file is PE file or not. I need to check first two byte is MZ or not and I did this.
This is my task: When verifying the PE format, not only according to the MZ expression, but also using the conditions that the IMAGE_NT_HEADERS structure is read and the Signature field is verified by reading the IMAGE_FILE_HEADER field and the Machine field is equal to the Th value IMAGE_FILE_MACHINE_I386 or IMAGE_FILE_MACHINE_AMD64.
I cannot figure how can do the rest of them. I hope you can help me.
int checkPE(char *file){
int fd=open(file,READ_FLAGS,0777);
char buffer[TWOBYTE+1] = {'\0'};
size_t bytes_read;
char ch;
if(fd==-1){ //if file cannot be opened give a error message.
perror("The file cannot be opened.\n");
return -1;
}
bytes_read = read(fd,buffer,TWOBYTE);
if(bytes_read==-1){
perror("Error while reading file\n");
return -1;
}
if(strcmp(buffer,MZ)!=0){
return -1;
}
int closeFlag = close(fd);
if(closeFlag==-1){
perror("The file cannot be closed.\n");
return -1;
}
}
There is nothing more than just parsing some structures. You have already the algorithm. I assume that you just need the implementation. Consider the following example utility.
PS: For further details, just comment it below.
#include <stdio.h>
#include <windows.h>
BOOL CheckValidity(BYTE* baseAddress);
int main(int argc, char* argv[]) {
if (argc != 2)
{
printf("You didn't specified a PE file.\n");
printf("Usage: CheckPEImage.exe <Full path of PE File>\n");
return -1;
}
HANDLE hFile = CreateFileA(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return -1;
HANDLE hMemoryMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (!hMemoryMap)
return -2;
PBYTE baseAddress = (PBYTE)MapViewOfFile(hMemoryMap, FILE_MAP_READ, 0, 0, 0);
if (!baseAddress)
return -3;
printf("PE Image is %s.\n", CheckValidity(baseAddress) ? "valid" : "invalid");
getchar();
return 0;
}
BOOL CheckValidity(BYTE* baseAddress)
{
PIMAGE_DOS_HEADER lpDosHeader;
PIMAGE_FILE_HEADER lpFileHeader;
PIMAGE_NT_HEADERS lpNtHeaders;
PIMAGE_OPTIONAL_HEADER lpOptionalHeader;
lpDosHeader = (PIMAGE_DOS_HEADER)baseAddress;
lpNtHeaders = (PIMAGE_NT_HEADERS)(baseAddress + lpDosHeader->e_lfanew);
if (lpDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
return FALSE;
if (lpNtHeaders->Signature != IMAGE_NT_SIGNATURE)
return FALSE;
if (lpNtHeaders->FileHeader.Machine != IMAGE_FILE_MACHINE_I386 && lpNtHeaders->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64)
return FALSE;
return TRUE;
}

Copying a file with libssh

I am trying to write a simple program (toy example) that copies a file from a remote host to the local machine.
It works when I try to copy a txt file, but not for files like mp4.
Here is my code, which is basically parts stitched together from the tutorial: https://pastebin.com/0FPrmeDx
This is where the error happens:
int scp_receive(ssh_session session, ssh_scp scp)
{
int rc;
int size, mode;
char *filename, *buffer;
rc = ssh_scp_pull_request(scp);
if (rc != SSH_SCP_REQUEST_NEWFILE)
{
fprintf(stderr, "Error receiving information about file: %s\n",
ssh_get_error(session));
return SSH_ERROR;
}
size = ssh_scp_request_get_size(scp);
filename = strdup(ssh_scp_request_get_filename(scp));
mode = ssh_scp_request_get_permissions(scp);
printf("Receiving file %s, size %d, permisssions 0%o\n",
filename, size, mode);
free(filename);
buffer = malloc(size);
if (buffer == NULL)
{
fprintf(stderr, "Memory allocation error\n");
return SSH_ERROR;
}
ssh_scp_accept_request(scp);
rc = ssh_scp_read(scp, buffer, size);
if (rc == SSH_ERROR)
{
fprintf(stderr, "Error receiving file data: %s\n",
ssh_get_error(session));
free(buffer);
return rc;
}
printf("Done!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
int filedesc = open("/home/user/video.mp4", O_WRONLY | O_CREAT);
if (filedesc < 0) {
return -1;
}
write(filedesc, buffer, size);
free(buffer);
close(filedesc);
rc = ssh_scp_pull_request(scp);
if (rc != SSH_SCP_REQUEST_EOF)
{
fprintf(stderr, "Unexpected request: %s\n",
ssh_get_error(session));
return SSH_ERROR;
}
return SSH_OK;
}
Error is fired with the code:
rc = ssh_scp_pull_request(scp);
if (rc != SSH_SCP_REQUEST_EOF)
{
fprintf(stderr, "Unexpected request: %s\n",
ssh_get_error(session));
return SSH_ERROR;
}
This is the error that I get:
Unexpected request: ssh_scp_pull_request called under invalid state
I tried to figure it out, but I couldn't make any progress on it.
Update 1:
The size of the copied file is exactly the same as of the source file both for txt and mp4 files. However, the copied file seems to be largely empty...
When copied, the permissions are changed from -rwxr-xr-x to --wxr-----.
Update 2:
It seems that the file size plays a major role here. Very small files (10-15kb) are copied without problems. Bigger files are not copied and produce the above mentioned error...
You cannot expect, that ssh_scp_read() reads the whole data in a single call to it. You have to iterate, until no more data is left to read:
int r = 0;
while (r < size) {
int st = ssh_scp_read(scp, buffer+r, size-r);
r += st;
}
Now, a subsequent call to ssh_scp_pull_request(scp) should succeed.

Check XML file against DTD file using expat in C

I need to build an application the should parse an XML file using EXPAT and also check it against a DTD file. The problem is how do I do this, I checked the documentation, and couldn't find my way around.
My code so far:
int main(int argc, char *argv[])
{
XML_Parser p = XML_ParserCreate(NULL);
if (!p)
{
fprintf(stderr, "Couldn't allocate memory for parser\n");
exit(-1);
}
FILE* xmlFile;
xmlFile = fopen("test.xml", "r");
XML_SetElementHandler(p, start, end);
XML_SetParamEntityParsing(p, XML_PARAM_ENTITY_PARSING_ALWAYS);
for (;;)
{
int done;
int len;
len = (int) fread(Buff, 1, BUFFSIZE, xmlFile);
if (ferror(xmlFile))
{
fprintf(stderr, "Read error\n");
exit(-1);
}
done = feof(xmlFile);
if (XML_Parse(p, Buff, len, done) == XML_STATUS_ERROR)
{
fprintf(stderr, "Parse error at line %" XML_FMT_INT_MOD "u:\n%s\n",
XML_GetCurrentLineNumber(p),
XML_ErrorString(XML_GetErrorCode(p)));
exit(-1);
}
if (done)
break;
}
XML_ParserFree(p);
return 0;
}
I guess that the should be a call to the XML_SetExternalEntityRefHandler function, but I fail to understand how to use it.
Thank you in advance.

Checking Linux kernel config at runtime

Is there any C function that can check whether a Linux kernel configuration option (CONFIG_AAAA option in /boot/config- file) is set?
Or I have to develop it myself.
After a long search with no result, I developed a function myself. Here is the code:
static int is_kconfig_set(const char *config) {
int ret = 0;
struct utsname utsname;
char pattern[BUFSIZ], buf[BUFSIZ];
FILE *fp = NULL;
if (uname(&utsname) == -1)
return 0;
memset(pattern, 0, sizeof(pattern));
memset(buf, 0, sizeof(buf));
sprintf(pattern, "%s=y", config);
sprintf(buf, "/boot/config-%s", utsname.release);
fp = fopen(buf, "r");
if (fp == NULL)
return 0;
while(fgets(buf, sizeof(buf), fp) != NULL) {
if (strncmp(buf, pattern, strlen(pattern)) == 0) {
ret = 1;
break;
}
}
fclose(fp);
return ret;
}
To check whether CONFIG_CPU_FREQ is set:
if (is_kconfig_set("CONFIG_CPU_FREQ"))
return 1;
return 0;
The only way I can think is to create your own following the approach in kernel/configs.c (this is the code that creates /proc/config.gz).

Read the content of a file to memory, result different from 32bit os and 64bit os

I wrote a function below to read the content of a file to memory.
It works well on my local machine(Ubuntu 32bit), but it produces wrong result on server(CentOS 64bit).
Wrong case:
With a 40 byte file, the content is below, on the 64bit os, it gave me wrong result.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
The code:
char* file_get_contents(const char *filename) {
FILE *stream = NULL;
char *content = NULL;
size_t ret;
struct stat st;
if ((stream = fopen(filename,"r")) == NULL) {
fprintf(stderr, "Failed to open file %s\n", filename);
exit(1002);
}
if(stat(filename, &st) < 0) {
fprintf(stderr, "Failed to stat file %s\n", filename);
exit(1002);
}
content = malloc(st.st_size);
ret = fread(content, 1, st.st_size, stream);
if (ret != st.st_size) {
fprintf(stderr, "Failed to read file %s\n", filename);
exit(1002);
}
fclose(stream);
return content;
}
Your file_get_contents cannot be correctly used by its caller. It returns a char * but not its lenght, nor does it return a string (i.e. it isn't null terminated.).
As long as you're reading text, do e.g.
content = malloc(st.st_size + 1); // + 1 here for the nul terminator
ret = fread(content, 1, st.st_size, stream);
if (ret != st.st_size) {
fprintf(stderr, "Failed to read file %s\n", filename);
exit(1002);
}
content[st.st_size] = 0; //nul terminate

Resources