I am currently implementing openssl into my application. My problem arose when I had to set the hostname, IP - address, and port of the BIO. I have always known ip and hostname to be the same thing. Could someone please explain the difference.
A host name is a combination of the name of your machine and a domain name (e.g. machinename.domain.com). The purpose of a host name is readability - it's much easier to remember than an IP address. All hostnames resolve to IP addresses, so in many instances they are talked about like they are interchangeable.
A host name can have multiple IP addresses, but not the other way around. If you check out
https://beej.us/guide/bgnet/html/multi/gethostbynameman.html
you'll see that gethostbyname() returns a list of addresses for a particular host. To prove it, here's a small program:
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char** argv)
{
if (argc < 2)
{
printf("usage: %s hostname\n", argv[0]);
return 0;
}
struct in_addr addr;
struct hostent* he = gethostbyname(argv[1]);
if (!he)
{
perror("gethostbyname");
return 1;
}
printf("IP addresses for %s:\n\n", he->h_name);
for (int i = 0; he->h_addr_list[i]; i++)
{
memcpy(&addr, he->h_addr_list[i], sizeof(struct in_addr));
printf("%s\n", inet_ntoa(addr));
}
return 0;
}
Entering www.yahoo.com, I get the following:
98.137.246.8
98.137.246.7
98.138.219.232
98.138.219.231
Related
I have a shared library linked to an executable for which I would like to have code coverage instrumentation using custom _sanitizer_cov_trace_pc* functions.
library.cc
#include <stdio.h>
void so_function() {
printf("SO function.");
}
callbacks.cc
#include <stdint.h>
#include <stdio.h>
#include <sanitizer/coverage_interface.h>
extern "C" void __sanitizer_cov_trace_pc_guard_init(uint32_t *start,
uint32_t *stop) {
static uint64_t N;
if (start == stop || *start) return;
printf("INIT: %p %p\n", start, stop);
for (uint32_t *x = start; x < stop; x++)
*x = ++N;
}
extern "C" void __sanitizer_cov_trace_pc_guard(uint32_t *guard) {
if (!*guard) return;
void *PC = __builtin_return_address(0);
char PcDescr[1024];
__sanitizer_symbolize_pc(PC, "%p %F %L", PcDescr, sizeof(PcDescr));
printf("guard: %p %x PC %s\n", guard, *guard, PcDescr);
}
main.cc
#include <stdio.h>
void so_function();
int main(int argc, char **argv) {
so_function();
}
I compiled the library using clang's -fsanitize-coverage=trace-pc-guard into position-independent code (-fPIC) and then I created the shared library using both the resulted object file and callbacks.cc using -fsanitize=address.
I compiled main.cc and linked it with the shared library but it seems like these 2 custom __sanitizer_cov_trace_pc_guard* functions don't get called.
I would like have code coverage instrumentation using these 2 functions only for the shared library, and not for the main executable.
I've seen quite a numerous amount of examples that go over creating functions passes (e.g. Brandon Holt and Adrian Sampson), but I am curious as to the difficulty in creating a module pass to do these very similar problems. I've tried to implement a module pass to display the global variable names using this example and llvm source code to understand how you have to iterate through members.
I am using a source compiled version of LLVM, and using the example from the above links to add the pass, and then running:
$ clang -Xclang -load -Xclang build/Skeleton/libSkeletonPass.so something.c
Which then returns this gibberish. However, if I implement a functionPass and just use Auto to determine the type to be initialized it's very straight forward and works. Am I just going about printing the global variables the wrong way?
This is a pastebin of the error output from the terminal. link
Skeleton.cpp
#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/IR/LLVMContext.h"
using namespace llvm;
namespace {
// Helper method for converting the name of a LLVM type to a string
static std::string LLVMTypeAsString(const Type *T) {
std::string TypeName;
raw_string_ostream N(TypeName);
T->print(N);
return N.str();
}
struct SkeletonPass : public ModulePass {
static char ID;
SkeletonPass() : ModulePass(ID) {}
virtual bool runOnModule(Module &M) {
for (Module::const_global_iterator GI = M.global_begin(),
GE = M.global_end(); GI != GE; ++GI) {
errs() << "Found global named: " << GI->getName()
<< "\tType: " << LLVMTypeAsString(GI->getType()) << "!\n";
}
return false;
}
};
}
char SkeletonPass::ID = 0;
// Automatically enable the pass.
// http://adriansampson.net/blog/clangpass.html
static void registerSkeletonPass(const PassManagerBuilder &,
legacy::PassManagerBase &PM) {
PM.add(new SkeletonPass());
}
static RegisterStandardPasses
RegisterMyPass(PassManagerBuilder::EP_EarlyAsPossible,
registerSkeletonPass);
something.c
int value0 = 5;
int main(int argc, char const *argv[])
{
int value = 4;
value += 1;
return 0;
}
I was able to figure this out after some extensive github searching. Here is the answer from which I was following a tutorial to help others who may be curious how to implement a Module Pass.
According to erl_driver documentation for driver_async_port_key function,
Before OTP-R16, the actual port id could be used as a key with proper casting, but after the rewrite of the port subsystem, this is no longer the case. With this function, you can achieve the same distribution based on port id's as before OTP-R16.
What is this proper casting?
The ErlDrvPort type is a typedef of a pointer to a struct. To obtain an unsigned int async key type in older driver applications, you need to convert this pointer type to unsigned int. One way to achieve this is to cast it through the C99 uintptr_t type, which is guaranteed to be large enough to hold a pointer value:
#include <stdint.h>
#include "erl_driver.h"
unsigned int my_port_key(ErlDrvPort port)
{
return (unsigned int) (uintptr_t) port;
}
You can write a portable function to return an async key using driver API versioning information available in erl_driver.h. The driver_async_port_key function was introduced in driver API version 2.2, so we can call driver_async_port_key when using version 2.2 or newer, or fall back to the casting approach for older versions:
#include <stdint.h>
#include "erl_driver.h"
unsigned int my_port_key(ErlDrvPort port)
{
#if ERL_DRV_EXTENDED_MAJOR_VERSION > 2 || \
(ERL_DRV_EXTENDED_MAJOR_VERSION == 2 && ERL_DRV_EXTENDED_MINOR_VERSION >= 2)
return driver_async_port_key(port);
#else
return (unsigned int) (uintptr_t) port;
#endif
}
I want to write to a physical address to change the voltage of a pin using an ARM board- but in order to write to a physical address, I need to take a virtual address, and map it to the physical address using mmap.
So I did that, in this way:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int main(void) {
int fd;
int *map_base_c,*map_base_d, *map_base_p, *virt_addr;
off_t target,control,data,pullup;
control=0x56000050;
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
map_base_d = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd,data & ~MAP_MASK);
printf("Memory mapped at address %p.\n", map_base_d);
virt_addr = map_base_d; //+ (data & MAP_MASK)
*virt_addr = 0x00; //This is where it goes off. find out why!!!
printf("Value at address 0x%X (%p): 0x%X\n", data, virt_addr,(*virt_addr));
close(fd);
return 0;
}
But, The pin didn't get a high voltage as I'd expected. Is there something wrong with the way I'm changing the address?
Also, is there a way to see the physical address which was mapped to the virtual address?
Thanks!
In your call to mmap, the offset argument should be the lowest physical address which you want access to. In your code, you pass data & ~MAP_MASK, and data hasn't been initialized (or has been default-initialized to 0).
I believe you want something similar to the following:
uintptr_t control = 0x56000050;
uintptr_t base = control & ~MAP_MASK;
int fd;
void *map_base_d;
int *virt_addr;
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
map_base_d = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, base);
//now *map_base_d corresponds to the physical address of 0x56000000
virt_addr = map_base_d + (control - base);
*virt_addr = 0x00; //Make sure virt_addr is a pointer of the right width (int*, char*, etc), so that you don't accidentally write a dword when you really only want to write a single word.
I decided to add scripting with Lua. I've downloaded and compiled interpreter. It works fine, but when I want to use any functions from os.* or string.* libs, it says, that "attemt to index global 'os' (a nil value)"
Here is my code and should work, but it does not:
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
extern "C" {
#include "..\liblua\lua.h"
#include "..\liblua\lualib.h"
#include "..\liblua\lauxlib.h"
}
int main(int argc, TCHAR* argv[])
{
lua_State *LuaVM = luaL_newstate();
lua_pushcfunction(LuaVM,luaopen_base);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_math);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_string);
lua_call(LuaVM,0,0);
lua_pushcfunction(LuaVM,luaopen_table);
lua_call(LuaVM,0,0);
int error;
lua_pushstring(LuaVM,"Ver 0.525.5");
lua_setglobal(LuaVM,"Version");
while (true)
{
string strCode;
getline(cin,strCode);
error = luaL_loadbuffer(LuaVM,strCode.c_str(),strCode.length(),"") ||
lua_pcall(LuaVM,0,0,0);
if (error)
{
cout<< lua_tostring(LuaVM,-1)<<endl;
lua_pop(LuaVM,1);
}
}
lua_close(LuaVM);
return 0;
}
What's wrong with it?
In Lua 5.2 the standard luaopen_* functions do not set the corresponding global variables.
Why not copy and adapt the code in linit.c or just call luaL_openlibs?
Otherwise, do what they do: call luaL_requiref for each luaopen_* function.
See http://www.lua.org/source/5.2/linit.c.html#luaL_openlibs.