88 lines
1.8 KiB
C
88 lines
1.8 KiB
C
#include <sys/types.h>
|
|
#include <sys/ipc.h>
|
|
#include <sys/shm.h>
|
|
#include <stdio.h>
|
|
#include <jni.h>
|
|
#include "it_valueteam_securityutility_KeyJNIBridge.h"
|
|
|
|
#define SHMSZ 27
|
|
//#define KEY 5678
|
|
#define KEY 5650
|
|
#define PERM 0600
|
|
|
|
JNIEXPORT jbyteArray JNICALL Java_it_valueteam_securityutility_KeyJNIBridge_readKey
|
|
(JNIEnv * env, jobject obj, jint indirizzo){
|
|
|
|
int shmid;
|
|
int i = 0;
|
|
key_t key;
|
|
char *shm, *s;
|
|
jbyteArray result;
|
|
char buffer[24] = "12345123451234512345123";
|
|
|
|
//key = KEY;
|
|
key = (key_t)indirizzo;
|
|
|
|
result = (*env)->NewByteArray(env,24);
|
|
if(result==NULL) {
|
|
printf("Allocazione fallita.\n");
|
|
}
|
|
|
|
if ((shmid = shmget(key, SHMSZ, PERM)) < 0) {
|
|
perror("shmget");
|
|
return NULL;
|
|
}
|
|
|
|
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1) {
|
|
perror("shmat");
|
|
return NULL;
|
|
}
|
|
|
|
for(s = shm; i<24; s++) {
|
|
buffer[i++] = *s;
|
|
}
|
|
|
|
(*env)->SetByteArrayRegion(env,result,0,24,buffer);
|
|
return result;
|
|
}
|
|
|
|
JNIEXPORT void JNICALL Java_it_valueteam_securityutility_KeyJNIBridge_writeKey
|
|
(JNIEnv * env, jobject obj, jint indirizzo, jbyteArray chiave) {
|
|
|
|
int shmid;
|
|
key_t key;
|
|
char * shm, *s;
|
|
int i;
|
|
jbyte *start;
|
|
|
|
//key = KEY;
|
|
key = (key_t)indirizzo;
|
|
|
|
printf("Inizio scrittura.\n");
|
|
|
|
/*
|
|
* Create the segment.
|
|
*/
|
|
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | PERM)) < 0) {
|
|
perror("shmget");
|
|
exit(1);
|
|
}
|
|
|
|
/*
|
|
* Now we attach the segment to our data space.
|
|
*/
|
|
if ((shm = shmat(shmid, NULL, 0)) == (char * ) - 1) {
|
|
perror("shmat");
|
|
exit(1);
|
|
}
|
|
|
|
start = (*env)->GetByteArrayElements(env,chiave,NULL);
|
|
s = shm;
|
|
for(i=0;i<24;i++) {
|
|
* s++ = start[i];
|
|
}
|
|
(*env)->ReleaseByteArrayElements(env, chiave, start, 0);
|
|
|
|
}
|
|
|