GetPrivateProfileString failed when I use gflags to enable page heap with flags(full unaligned traces) - pageheap

My code is like this:
#include <Windows.h>
#include <tchar.h>
int main () {
TCHAR conffigPath[MAX_PATH] = {0};
GetCurrentDirectory(MAX_PATH, conffigPath);
_tcscat_s(conffigPath, MAX_PATH, _T("\\config.ini"));
TCHAR szValue[MAX_PATH] = {0};
GetPrivateProfileString(_T("361"), _T("DLL"), NULL, szValue, MAX_PATH, conffigPath);
return 0;
}
The context of config.ini is following:
[361]
Dll=daemon.dll
But when GetPrivateProfileString was called, the context of szValue is empty! And when I disable the pageheap, the context of szValue is "daemon.dll".
And when I enable pageheap with flags(full traces),the context of szVaule is "daemon.dll".
Is there a bug of the API GetPrivateProfileString?

Related

Error fixing up map structure, incompatible struct bpf_elf_map used?

I am trying to learn ebpf map. Compiling the code with libbpf but getting the error like "Error fixing up map structure, incompatible struct bpf_elf_map used?". Here is my ebpf code:
#include <linux/bpf.h>
#include <linux/pkt_cls.h>
#include <stdint.h>
#include <iproute2/bpf_elf.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#ifndef __section
# define __section(NAME) \
__attribute__((section(NAME), used))
#endif
#ifndef __inline
# define __inline \
inline __attribute__((always_inline))
#endif
struct bpf_map_def SEC("maps") port_h = {
.type = BPF_MAP_TYPE_HASH,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 1,
};
static __always_inline int do_inline_hash_lookup(void *inner_map, int port)
{
int *result;
if (inner_map != &port_h)
return 99;
result = bpf_map_lookup_elem(&port_h, &port);
return result ? *result : 99;
}
__section("ingress")
int tc_ingress(struct __sk_buff *skb)
{
void *outer_map, *inner_map;
do_inline_hash_lookup(inner_map, 100);
return TC_ACT_OK;
}
__section("egress")
int tc_egress(struct __sk_buff *skb)
{
return TC_ACT_OK;
}
char LICENSE[] SEC("license") = "GPL";
Here is how I am loading the code into kernel:
tc filter del dev lo ingress
tc filter del dev lo egress
tc filter show dev lo ingress
# use right device like eth0 or lo
# Add qdisc to a particular interface like eth0 or lo
tc qdisc add dev lo clsact
# Load the object file into kernel
tc filter add dev lo ingress bpf da obj ./.output/map.bpf.o sec ingress
tc filter add dev lo egress bpf da obj ./.output/map.bpf.o sec egress
Any idea, what I am doing wrong? It will be a great help.
I think you need to declare your map according to the libbpf style:
#define __uint(name, val) int(*(name))[val]
#define __type(name, val) typeof(val) *(name)
struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, int);
__type(value, int);
__uint(max_entries, 1);
} port_h __section(".maps");

aes_encrypt() function hooking

I want to know the pid of the process which is performing the AES encryption. I have written the following function hooking code:
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <openssl/aes.h>
void AES_encrypt(const unsigned char *in_var, unsigned char *out_var,
const AES_KEY *key_var)
{
void (*new_aes_encrypt)(const unsigned char *in_var, unsigned char *out_var,
const AES_KEY *key_var);
new_aes_encrypt = dlsym(RTLD_NEXT, "AES_encrypt");
FILE *logfile = fopen("logfile", "a+");
fprintf(logfile, "Process %d:nn%snnn", getpid(), (char *)in_var);
fclose(logfile);
new_aes_encrypt(in_var, out_var, key_var);
}
Then at terminal I did the followings:
#gcc aes_hook.c -o aes_hook.so -fPIC -shared -lssl -D_GNU_SOURCE
#export LD_PRELOAD="/<directory location>/aes_hook.so"
However, when I started the AES encryption (by a dummy process), I could not get it's pid in logfile. Why is this hooking not working?
*P.S.: Following is the declaration of AES_encrypt (in aes.h of OpenSSL) function that is called to perform the AES encryption.
void AES_encrypt(const unsigned char *in, unsigned char *out,
const AES_KEY *key)

Error while consuming REST service in VC++

I have used the below code to use as an example to consume the REST services in VC++. I am getting an error stating "A Connection with the Server Could Not be Established". IIs version of my system is IIS 6.0. Please let me know what could be the problem. Thanks in advance.
#include "stdafx.h"
#include "http_client.h"
#include "filestream.h"
#include "iostream"
#include "sstream"
#include "conio.h"
#include "Windows.h"
using namespace utility;
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace concurrency::streams;
int _tmain(int argc, _TCHAR* argv[])
{
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("http://www.bing.com/"));
uri_builder builder(U("/search"));
builder.append_query(U("q"), U("Casablanca CodePlex"));
return client.request(methods::GET, builder.to_string());
// Build request URI and start the request.
})
// Handle response headers arriving.
.then([=](http_response response)
{
printf("Received response status code:%u\n", response.status_code());
// Write response body into the file.
return response.body().read_to_end(fileStream->streambuf());
})
// Close the file stream.
.then([=](size_t)
{
return fileStream->close();
});
// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}
Sleep(5000);
return 0;
}

How to find Address already in use?

This is my code which can run CentOS and Windows just fixing some headers.
#define _WIN32_WINNT 0x0501
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
/*
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
*/
int main()
{
int sock;
int ret = 0;
int port= 12345;
struct sockaddr_in addr;
char buf[1024];
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock<0){
printf("socket() ret = %d : %s\n",ret,strerror(errno));
return ret;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY;
ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
if(ret<0){
printf("bind() ret = %d errno =%d : %s\n",ret,errno,strerror(errno));
return ret;
}
printf("############# Binding port %d type Enter to stop \t",port);
fgets(buf,sizeof(buf),stdin);
return 0;
}
When I tried to bind same port by this program with runing tow process, there must be the messages that Address already in use like below.
[The first proc#centOS ]
$ ./udp
############# Binding port 12345 type Enter to stop
[The second proc#centOS]
$ ./udp
bind() ret = -1 errno =98 : Address already in use
$
However when I do same thing with same code on windows, message is different.
[The first proc#windows]
C:\ >udp
############# Binding port 12345 type Enter to stop
[The second proc#windows]
C:\ >udp
bind() ret = -1 errno =34 : Result too large
C:\ >
How can I get Address already in use on Windows?
I don't think you should use errno on windows for sockets code. You could try to use WSAGetLastError which returns WSAEADDRINUSE.
The MSDN page for errno suggests EADDRINUSE is not supported for errno.
I think you should devise a scheme where you have a my_errno function that depending on the platform uses errno or WSAGetLastError.
printf("socket() ret = %d : %s\n",ret,strerror(errno));
There may be a subtle issue with this call. The order of argument evaluation is unspecified and strerror itself can change errno, which means it has side-effects. You should print errno separately, before doing anything else.
Like cnicular said, you have to use WSAGetLastError() on Windows, not errno. To get a text message for a socket error code, you can use FormatMessage().
To answer your question, if you want to find out who is using the port, you can use the command-line netstat tool, or programmably using GetTcpTable2().

Does anyone know if ios4 supports unix domain sockets?

The following works without error on OSX 10.6, but fails in the iphone simulator using SDK 4.1
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/tcp.h>
#include <sys/un.h>
#include <string.h>
int main(void) {
int sock = socket(AF_UNIX, SOCK_DGRAM, 0);
struct sockaddr_un sock_addr;
memset(&sock_addr, 0, sizeof(struct sockaddr_un));
sock_addr.sun_family = AF_UNIX;
strcpy(sock_addr.sun_path, "/tmp/sock");
int err = bind(sock, (struct sockaddr*)&sock_addr, sizeof(struct sockaddr_un));
if(err == -1) {
perror("bind: ");
}
}
Error is "Address family not supported by protocol family"
Any ideas?
You really need to check sock already - most likely, the socket creation is what failed already.
My guess is that AF_UNIX/SOCK_DGRAM is not supported; try SOCK_STREAM instead.