Linux基础

undefined

  • Linux相关说明

undefined13.1 基础操作

undefined设置文本编译器

  1. alias nano = "nano -w -x -E -T 3 -I -S"

undefined16进制展示与xxd

xxd 可用来查看生字节的16进制展示.

以下是个示例.

每行16个字节. 每4个字母数字为2个字节.

  1. $ xxd 1.block
  2. 0000000: 196f d33e b38e 61d5 4579 d98e 16d1 ac4e .o.>..a.Ey.....N
  3. 0000010: 279e 065c 07d7 5461 42b7 fe60 d226 72bc '..\..TaB..`.&r.
  4. 0000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
  5. 0000030: 0000 0000 0000 0000 0000 0000 0000 0000 ................
  6. 0000040: 0000 0000 0000 0000 0000 0000 0000 0000 ................
  7. 0000050: 0000 0000 0000 0000 0000 0000 0000 0000 ................

字节的计算过程:

e8 : 232

e803 = 232 + 256 *3 = 1000

图中所示, 前2个字节表示整数1000

  1. 00000e0: e803 0000 0000 0000 0000 0000 0000 0000 ................

undefinedlinux终端共享使用说明

作共享屏幕:

  1. tmux -S $SOCKET new -t $SESSION

接入共享屏幕

  1. tmux -S $SOCKET attach -t $SESSION -r

-r 参数表示只读, 去掉可以一起写.

示例:

  1. tmux -S /tmp/330 attach -t 0 -r
  2. or
  3. sh /tmp/a

undefinedlinux 传统安装

linux来源于unix的传统, 安装时会将不同的文件如设置,执行,变量会放在不同的路径

undefined13.2 编译执行相关

undefined程序运行过程

head |… |… |… |… |… |

section

_start()

initialize libc.so

run main()

libc.so and system call are heavily dependedt up global variables(全局变量)

argc argv envp

对于复杂的命令行参数,需要parser,analyzer来分析. 简单起见,也可以把顺序等写死.

./bitcoind —help

conciseCoin

./ccn -k yuanxun.private -s /tmp/blocks

argv[1] argv[2] argv[3]

![image_1buksdg4vk6r1ddd1eo266n1aek9.png-96.5kB][44]

命令行参数argc 与argv 在内存中的图示:

![image_1bukrl5tp1gbi108018po15cc1vbs9.png-104.2kB][45]

undefined编译执行源代码:

  1. tcc -c program.c -o program.o
  2. tcc -o program.elf program.o libtfm.a libtomcrypt.a
  3. ./program.elf

注意: 编译时文件名的顺序不要变, 否则无法编译通过.

undefined查看库

  • 查看 .a 的命令 : readedf -S libc.a
  • 查看有哪些函数 :readelf -S libtfm.a |grep File
  1. 查看库的函数名
  2. $ readelf -S libtomcrypt.a | grep File
  3. File: libtomcrypt.a(aes.o)
  4. File: libtomcrypt.a(aes_enc.o)
  5. File: libtomcrypt.a(anubis.o)
  6. File: libtomcrypt.a(blowfish.o)
  7. File: libtomcrypt.a(camellia.o)
  8. File: libtomcrypt.a(cast5.o)
  9. File: libtomcrypt.a(des.o)

undefinednc

  • 一端使用nc -l 127.0.0.1 6000启动监听程序,另一端使用nc 127.0.0.1 6000 启动连接程序,则两端能互相看到对方的输入。
  • ctrl+c可以退出监听。
  • 一端使用nc -l 127.0.0.1 6000启动监听程序,另一端运行自己的程序,则可以被监听到。
  • 监听测试 nc -l 127.0.0.1 6000
  • 运行命令 tcc -run program.c 127.0.0.1 6000

undefined多进程多线程

  • multiprocess 多进程
  • multithread 多线程
  • poll/select

symmetric asymmetric

如果一个应用程序去处理多个设备,例如应用程序读取网路数据,按键,串口,一般能想到的有三种方法:

方法1:

串行+阻塞的方式读取:

while(1) {

read(标准输入);

read(网络);

}

缺点:每当阻塞读取标准输入时,如果用户不进行标准输入的操作,而此时客户端给服务器发送数据,导致服务器无法读取客户端发送来的数据!

方法2:

采用多线程或者多进程机制来实现读取:

开辟多个线程,每一个线程处理一个设备,不会导致的数据的无法读取,但是系统的开销相比方法1要大!

方案3:采用linux系统提供的高级IO的处理机制

select/poll:两者一样,主进程能够利用select或者poll能够对多个设备进行监听!

undefined14. 程序开发语言相关说明

undefined14.1 C语言

undefined14.1.1 C语言基础

  • system call:

    open, close, read, write

  • gnu function:

    fopen, fclose, fread, fwrite

  1. #include "stdio.h" 包含输入和输出相关的函数 printf() fopne() fread() fwrite() fclose()
  2. #include "stdlib.h" 包含内存管理的函数 malloc() free()
  3. #include "string.h" 包含字符串处理的函数 strlen() strtok() strcpy()
  4. #include "sys/socket.h" 包含套接字的函数
  5. #include "arpa/inet.h" 互联网套接字函数

undefined14.1.2 注意点

undefined构造8字节, 64位的数字处理

32位的机器, 要想创造64位的整数,

C语言里long long int 会分配一个64位的整数.

用特定的函数来支持. 编译器支持. 虽然机器本身不支持.

  1. unsigned char *y;
  2. unsiged int z;
  3. y = malloc(8); // 分配8个字节
  4. z = 1000; // 4个字节的变量z
  5. memcpy(y, &z, 4); // 把 4个字节的z 复制到 y上.
  6. memset(y + 4, 0, 4); // 后4个字节补充0.

undefinedmemset与填充0

buffer 在创造时( malloc() ), 不会自动地填充成0. 所以可以用memset.

  1. buffer = malloc(1000);
  2. memset(buffer,0,1000);

undefinedvoid 指针

课上bug已解,原因是C语言不能从void类型的指针中取值,需要进行两次强转,形式如下:

  1. while ((int)list != 0 ) {
  2. data = (unsigned char *) *((int *) list);
  3. printf("value: %d%c", (int)data, 10);
  4. list = (void *)*((int *) (list + 4));
  5. printf("list: %d%c", (int)list, 10);
  6. }

undefined文件结束符

当公钥存在一个文件里.

文件里可能会增加一个跳行. 引起base64的函数转变的崩溃. 因为base64没有跳行字符.

最好是拿之前的函数来读文件, 判断最后一个字符是不是10, 若是10要去掉. 倒数第二个是10也要删掉.

或者把 钥匙 放在代码变量里.

undefinedsigned char 与 unsigned char区别

signed char取值范围是 -128 到 127(有符号位)

unsigned char 取值范围是 0 到 255

char 到底是signed char还是unsigned char?这得看编译器.

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. signed char a = -1;
  5. unsigned char b = -1;
  6. printf("%%d:\n");
  7. printf("signed: %d\n", a);
  8. printf("unsiged:%d\n", b);
  9. printf("%%u:\n");
  10. printf("signed: %u\n", a);
  11. printf("unsigned: %u\n", b);
  12. }
  1. %d:
  2. signed: -1
  3. unsiged:255
  4. %u:
  5. signed: 4294967295
  6. unsigned: 255

undefineduint8_t\uint_16_t\uint32_t\uint64_t

一般来说整形对应的*_t类型为:

uint8_t为1字节

uint16_t为2字节

uint32_t为4字节

uint64_t为8字节

  • 这些类型的来源:这些数据类型中都带有_t, _t 表示这些数据类型是通过typedef定义的,而不是新的数据类型。
  • 在涉及到跨平台时,不同的平台会有不同的字长,所以利用预编译和typedef可以方便的维护代码。
  • 在C99标准中定义了这些数据类型,具体定义在:/usr/include/stdint.h ISO C99: 7.18 Integer types

undefined14.2 php语言

undefined函数说明

undefinedord()

把字母转成字节

undefinedhash

string hash ( string $algo , string $data [, bool $raw_output = false ] )

  1. 参数
  2. algo
  3. 要使用的哈希算法,例如:"md5""sha256""haval160,4" 等。
  4. data
  5. 要进行哈希运算的消息。
  6. raw_output
  7. 设置为 TRUE 输出原始二进制数据, 设置为 FALSE 输出小写 16 进制字符串。
  8. 返回值
  9. 如果 raw_output 设置为 TRUE 则返回原始二进制数据表示的信息摘要, 否则返回 16 进制小写字符串格式表示的信息摘要。

http://php.net/manual/zh/function.hash.php

undefinedarray_push()

将一个或多个单元压入数组的末尾(入栈)

  1. $a=array("Dog","Cat");
  2. array_push($a,"Horse","Bird");//内容加到数组中
  3. print_r($a);

输出:

  1. Array
  2. (
  3. [0] => Dog
  4. [1] => Cat
  5. [2] => Horse
  6. [3] => Bird
  7. )

undefinedarray_shift()

将数组开头的单元移出数组

另外, array_unshift()在数组开头插入一个或多个单元

  1. $stack = array("Java", "Php", "C++", "C#", "Ruby");
  2. array_shift($stack);
  3. print_r($stack);
  1. Array
  2. (
  3. [0] => Php
  4. [1] => C++
  5. [2] => C#
  6. [3] => Ruby
  7. )